프로그래밍/C#

단계별 프로젝트 - 학생 성적 계산 및 프린트

김꿀꿀이 2023. 3. 28. 11:09
반응형

 

오늘 학습 내용 링크

http://learn.microsoft.com/ko-kr/training/modules/guided-project-calculate-print-student-grades/?WT.mc_id=dotnet-35129-website&ns-enrollment-type=learningpath&ns-enrollment-id=learn.wwl.get-started-c-sharp-part-1 

 

Guided project - Calculate and print student grades - Training

Guided project - Calculate and print student grades

learn.microsoft.com

 

C#을 사용하여 첫 번째 코드 작성(C#로 시작, 파트 1)

- 단계별 프로젝트 - 학생 성적 계산 및 프린트 -

 

* \t : tab 공백 넣기
 - 탭의 간격은 문자열 길이 4칸이 기본으로 설정.
 -  space로 4번 치는 간격이 아니고 앞에 입력된 문자열과 포함해서 4칸 단위로 공백생성

Code >>

Console.WriteLine("1\t1\t1"); 
Console.WriteLine("22\t22\t22");
Console.WriteLine("333\t333\t333");
Console.WriteLine("4444\t4444\t4444");
Console.WriteLine("55555\t55555\t55555");

Output >>

 > 1 포함 3 공백(1+3=4) 뒤에 두 번째 1이 출력되고
    두 번째 1 포함 3 공백 뒤에 세 번째 1 출력
 > 22 포함 2 공백(2+2=4) 뒤에 3번째 2가 출력되고
    3,4번째 22 포함 2 공백 뒤에 5번째 2 출력
 > 333 포함 1 공백(3+1=4) 뒤에 4번째 3이 출력되고
    4,5,6번째 333 포함 1 공백 뒤에 7번째 3 출력
 > 4444로 4칸이 채워지므로 4 공백 후 5번째 4가 출력되고
    5,6,7,8번째의 4로 4칸이 채워지므로 4 공백 뒤에 9번째 4 출력
 > 55555로 5칸 채워지고 5 다음의 4의 배수인 8까지
    3칸 모자라므로 3 공백(5+3=8) 뒤에 6번째 5 출력
    6,7,8,9,10번째 5까지 출력하면 총 문자열 길이는 공백포함 13이므로 
    다음 4의 배수 16까지 3 공백 뒤에 11번째 5 출력

(글로 쓰니 말이 길어져서 이해가 어려울 수 있어서
위의 예제 코드와 출력결과를 보고 이해하는 것이 빠를 것 같다.)
  
이해가 어렵다면 그냥 출력해 보고 열이 안 맞는 곳은 \t을 한번 더 쓰면 된다.

Code >>

Console.WriteLine("1\t\t1\t\t1");
Console.WriteLine("22\t\t22\t\t22");
Console.WriteLine("333\t\t333\t\t333");
Console.WriteLine("4444\t4444\t4444");
Console.WriteLine("55555\t55555\t55555");

Output >>

 

이번 학습은 하나의 예제를 완성하기 위해 여러 단계를 거치며 코드를 작성한 코드로
결과를 보면 각 학생들의 시험 점수 총점의 평균으로 학점을 출력하는 프로그램이다.

사이트를 보고 한 단계씩 차근차근 코드입력 후 출력해 보면 되므로
이때까지 차근차근 공부했다면 어렵진 않을 것 같다.

int currentAssignments = 5;

int sophia1 = 93;
int sophia2 = 87;
int sophia3 = 98;
int sophia4 = 95;
int sophia5 = 100;

int nicolas1 = 80;
int nicolas2 = 83;
int nicolas3 = 82;
int nicolas4 = 88;
int nicolas5 = 85;

int zahirah1 = 84;
int zahirah2 = 96;
int zahirah3 = 73;
int zahirah4 = 85;
int zahirah5 = 79;

int jeong1 = 90;
int jeong2 = 92;
int jeong3 = 98;
int jeong4 = 100;
int jeong5 = 97;

int sophiaSum = sophia1 + sophia2 + sophia3 + sophia4 + sophia5;
int nicolasSum = nicolas1 + nicolas2 + nicolas3 + nicolas4 + nicolas5;
int zahirahSum = zahirah1 + zahirah2 + zahirah3 + zahirah4 + zahirah5;
int jeongSum = jeong1 + jeong2 + jeong3 + jeong4 + jeong5;

decimal sophiaScore = (decimal)sophiaSum / currentAssignments;
decimal nicolasScore = (decimal)nicolasSum / currentAssignments;
decimal zahirahScore = (decimal)zahirahSum / currentAssignments;
decimal jeongScore = (decimal)jeongSum /currentAssignments;

Console.WriteLine("Student\t\tGrade\n");
Console.WriteLine("Sophia:\t\t" + sophiaScore + "\tA");
Console.WriteLine("Nicolas:\t" + nicolasScore + "\tB");
Console.WriteLine("Zahirah:\t" + zahirahScore + "\tB");
Console.WriteLine("Jeong:\t\t" + jeongScore + "\tA");

 

코드가 아주 길다.
배열을 쓰면 줄일 수 있는데
아직 여기 과정에선 배열을 배우지 않아서 이게 최선인 것 같다.

 

기억을 더듬어 for문과 배열을 이용한 코드도 작성해 보았다.
Code >>

int currentAssignments = 5;
int[] sophia = {93,87,98,95,100};
int[] nicolas = {80,83,82,88,85};
int[] zahirah = {84,96,73,85,79};
int[] jeong = {90,92,98,100,97};

int sophiaSum = 0;
int nicolasSum = 0;
int zahirahSum = 0;
int jeongSum = 0;

for(int a=0;a<currentAssignments;a++)
{
sophiaSum += sophia[a];
nicolasSum += nicolas[a];
zahirahSum += zahirah[a];
jeongSum += jeong[a];
}

decimal sophiaScore = (decimal)sophiaSum / currentAssignments;
decimal nicolasScore = (decimal)nicolasSum / currentAssignments;
decimal zahirahScore = (decimal)zahirahSum / currentAssignments;
decimal jeongScore = (decimal)jeongSum /currentAssignments;

Console.WriteLine("Student\t\tGrade\n");
Console.WriteLine("Sophia:\t\t" + sophiaScore + "\tA");
Console.WriteLine("Nicolas:\t" + nicolasScore + "\tB");
Console.WriteLine("Zahirah:\t" + zahirahScore + "\tB");
Console.WriteLine("Jeong:\t\t" + jeongScore + "\tA");

출력 결과는 같으므로 생략..
코드가 썩 마음에 들지 않는데
프로그래밍을 너무 오래 쉬어서 지금은 이 정도가 최선...

드디어 다음은 첫 단원의 마지막 프로젝트다.

반응형