C#
(C#) 구조체 struct 예제: 성적 처리
코딩ABC
2023. 5. 31. 19:48
반응형
C#에서 구조체의 간단한 예제를 만들었습니다.
이름, 국어점수, 영어점수, 수학점수를 구조체로 묶어서 처리합니다.
석차를 구하기 위해서 총점까지 구조체로 묶었습니다.
using System;
//using System.Collections.Generic;
//using System.Linq;
//using System.Text;
//using System.Threading.Tasks;
namespace 구조체_CS_콘솔_성적
{
struct Score
{
public string name;
public int kor, mat, eng;
public int tot;
public Score(string name, int kor, int mat, int eng)
{
this.name = name;
this.kor = kor;
this.mat = mat;
this.eng = eng;
this.tot = 0;
}
}
internal class Program
{
static void Main(string[] args)
{
Score[] score =
{
new Score("이순신", 77,81,99),
new Score("홍길동", 90,90,90),
new Score("정재욱", 100,100,100),
new Score("정경환", 90,90,90),
new Score("이영삼", 71,85,95)
};
// 총점
for (int i = 0; i < score.Length; i++)
score[i].tot = score[i].kor + score[i].eng + score[i].mat;
for (int i=0; i<score.Length; i++)
{
int rank = 1;
for(int j=0; j< score.Length; j++)
{
if (score[i].tot < score[j].tot) rank++;
}
Console.WriteLine("{0} {1,3} {2,3} {3,3} {4,3} {5}",
score[i].name, score[i].kor, score[i].eng, score[i].mat,
score[i].tot, rank);
}
}
}
}
반응형