세 번째 작고 소듕한 과제(Class Roster)입니다.
다음은 main.cs의 코드입니다.
using System;
public class Program
{
public static void Main()
{
// set capacity
const int Capacity = 40;
// define arrays
string[] LastName = new string[Capacity];
string[] FirstName = new string[Capacity];
string[] MiddleName = new string[Capacity];
string[] PhoneNumber = new string[Capacity];
string[] Email = new string[Capacity];
string[] GPA = new string[Capacity];
// define variables
string strLastName ="";
string strFirstName ="";
string strMiddleName ="";
string strPhone = "";
string Phone10digits = "";
string strphone1;
string strphone2;
string strphone3;
string strEmail;
string strGPA;
double dblGPA;
bool HasNumericDigits;
bool IsValidPhoneNumber;
// get user's lastname
int size = 0;
do
{
do
{
Console.WriteLine ("Last Name = ");
strLastName = Console.ReadLine();
}
while (Validate.HasNumericDigits(strLastName)&&strLastName!="");
if (strLastName=="") continue;
LastName[size] = strLastName;
// get user's firstname
for (int y = 0; y < Capacity; y++)
{
Console.WriteLine ("First Name = ");
strFirstName = Console.ReadLine();
int firstLength = strFirstName.Length;
HasNumericDigits = Validate.HasNumericDigits(strFirstName);
if (HasNumericDigits == true || firstLength == 0)
{
continue;
}
else
{
FirstName[size] = strFirstName;
}
break;
}
// get user's middlename
do
{
Console.WriteLine ("Middle Name = ");
strMiddleName = Console.ReadLine();
}
while (Validate.HasNumericDigits(strMiddleName));
MiddleName[size] = strMiddleName;
// get user's PhoneNumber
for (int a = 0; a < Capacity; a++)
{
Console.WriteLine ("Phone Number = ");
strPhone = Console.ReadLine();
IsValidPhoneNumber = Validate.IsValidPhoneNumber(strPhone, out Phone10digits);
if (strPhone == "")
{
PhoneNumber[size] = strPhone;
}
else if (IsValidPhoneNumber == false)
{
continue;
}
else
{
strphone1 = Phone10digits.Insert(0, "(");
strphone2 = strphone1.Insert(4, ") ");
strphone3 = strphone2.Insert(9, "-");
PhoneNumber[size] = strphone3;
}
break;
}
// get user's email
do
{
Console.WriteLine ("Email = ");
strEmail = Console.ReadLine();
if (strEmail == "")
{
Email[size] = strEmail;
}
else if (!Validate.IsValidEmail(strEmail) == true)
{
continue;
}
else
{
Email[size] = strEmail;
}break;
}
while (!Validate.IsValidEmail(strEmail));
Email[size] = strEmail;
// get user's GPA
do
{
Console.WriteLine("GPA = ");
strGPA = Console.ReadLine();
dblGPA = Convert.ToDouble(strGPA);
}
while (0>dblGPA && dblGPA>4);
if(strGPA=="")continue;
string strGpa = Convert.ToString(dblGPA);
GPA[size] = strGPA;
//big do while loop
size++;
}
while (size< Capacity && strLastName !="");
//Print student table
int[] field = new int[] {20,18,25,4};
string Header = "Name".PadRight(field[0]) + "Phone".PadRight(field[1]) + "Email".PadRight(field[2]) + "GPA".PadRight(field[3]);
string Separator = " ".PadLeft(field[0], '=') + " ".PadLeft(field[1], '=') + " ".PadLeft(field[2], '=') +" ".PadLeft(field[3], '=');
Console.WriteLine(Header);
Console.WriteLine(Separator);
for (int i =0; i < size; i++)
{
Console.WriteLine((LastName[i] + ", " + FirstName[i] + " " + MiddleName[i]).PadRight(field[0]) + PhoneNumber[i].PadRight(field[1])+ Email[i].PadRight(field[2]) + GPA[i].PadRight(field[3]));
}
string Separator2 = " ".PadLeft((68), '=');
Console.WriteLine(Separator2);
//get avg gpa
int z = 0;
double sum=0;
for (z = 0; z < size; z++)
{
sum+=double.Parse(GPA[z]);
}
Console.Write("There are " + z + " student(s) with an average GPA of {0}",Math.Round(sum/ (z),2));
}
}
다음은 validation.cs의 코드입니다. 해당 코드들은 위의 main.cs에서 이름, 전화번호, 이메일을 validate하는데 사용됩니다. 위에 Validate.()을 통해서 불러와 validation을 하게 되는 구조입니다.
using System;
public class Validate {
public static bool IsNumericDigit(char c) {
return '0' <= c && c <= '9';
}
public static bool HasNumericDigits(string s) {
foreach (char c in s)
if (IsNumericDigit(c)) return true;
// Same loop written with for
// for (int i = 0; i < s.Length; i++)
// if (IsNumericDigit(s[i])) return true;
return false;
}
public static bool IsValidPhoneNumber(string s, out string strPhoneNumber) {
strPhoneNumber = "";
for (int i=0; i < s.Length; i++) {
if (IsNumericDigit(s[i]))
strPhoneNumber += s[i];
else switch (s[i]) {
case '-' :
case '.' :
case ' ' :
case '(' :
case ')' :
break;
default:
return false;
}
}
return strPhoneNumber.Length == 10;
}
public static bool IsValidEmail(string s) {
string[] word;
// Check the presence of exactly one @ character and non-empty user name
word = s.Split('@');
if (word.Length != 2 || word[0] == "")
return false;
// Check the presence of exactly one dot character and non-empty domain name
string domain = word[1];
word = domain.Split('.');
if (word.Length != 2 || word[0].Length == 0)
return false;
// Check the domain suffix
string suffix = word[1];
switch (suffix) {
case "com":
case "net":
case "org":
case "me":
case "edu":
break;
default:
return false;
}
return true;
}
}
위 코드들을 통해 다음과 아웃풋을 도출하였습니다. 유저가 입력을 마치면 table에 모든 정보를 저장하고 학생들의 수와 학점 평균을 도출합니다.
여기서부터는 donet 으로 실행하네?