C# 이것저것/초보자를 위한 C#200제

[C#] Winform 콤보박스를 이용한 학점계산기 만들기

agingcurve 2024. 4. 9. 15:27
반응형

 

 

학점계산기를 만들어보았다.

 

구현 사항

1) 교과목, 학점, 성적을 표시하는 컨트롤 배열

2) Form1_Load() 메서드는 6개 교과목을 입력, 학생마다 신청과목이 다를 수 있으므로, 실행 화면에서 수정

3) 학점, 성적, 교과목을 표시하는 콤보박스와 텍스트박스의 배열 초기화

4) 학점, 성적 콤보박스에 표시될 Item을 배열로 만듬

5) 학점 콤보박스 배열 crds의 각 요소에 대해 arrCredit의 각 요소를 Items로 등록 후, 최초 3을 SelectedItem으로 지정

6) 성적 콤보박스 배열 grds의 각 요소에 대해 lstGrade 리스트의 각 요소를 Items로 등록

7) "평균평점" 버튼을 클릭시, 실행 되는 메서드 제작

8) totalSocre는 각 과목의 학점수와 성적을 곱한 값의 합이며, totalCredits는 학점의 합

9) crds의 요소 수만큼 반복하며, 교과목이 입력되어 있다면, 학점을 정수로 변환 후, crd에 할당 후, totalCredits에 더함

10) 평균평점은 totalSocre/totalCreidts를 소수점 둘째자리까지 표시

11) A+에서 F까지 학점에 해당하는 점수를 리턴함

12) 성적을 하나라도 미입력 시, 평점계산이 불가하도록 함

 

 

코드

using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace A143_Gradecalc2
{
    public partial class Form1 : Form
    {
        TextBox[] titles; // 교과목 TextBox 배열
        ComboBox[] crds; // 학점 ComboBox 배열
        ComboBox[] grds; // 성적 ComboBox 배열
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            txt1.Text = "인체구조";
            txt2.Text = "수학1";
            txt3.Text = "실험";
            txt4.Text = "자료구조";
            txt5.Text = "vba";
            txt6.Text = "기업가정신";
            txt7.Text = "컴퓨터과학";

            crds = new ComboBox[] { crd1, crd2, crd3, crd4, crd5, crd6, crd7 };
            grds = new ComboBox[] { grd1, grd2, grd3, grd4, grd5, grd6, grd7 };
            titles = new TextBox[] { txt1, txt2, txt3, txt4, txt5, txt6, txt7 };
            int[] arrCredit = { 1, 2, 3, 4, 5 };
            List<String> lstGrade = new List<string> { "A+", "A", "B+", "B", "C+", "C", "D+", "D", "E", "F" };

            foreach (var combo in crds)
            {
                foreach(var i in arrCredit)
                {
                    combo.Items.Add(i);
                }
                combo.SelectedItem = 3;
            }

            foreach (var cb in grds)
            {
                foreach(var gr in lstGrade)
                {
                    cb.Items.Add(gr);
                }
            }

        }

        private void button1_Click(object sender, EventArgs e)
        {
            double totalScore = 0;
            int totalCredits = 0;
            bool isclac = true;
            for(int i =0; i < crds.Length; i++)
            {
                if (titles[i].Text != "")
                {
                    int crd = int.Parse(crds[i].SelectedItem.ToString());
                    totalCredits += crd;
                    if (grds[i].SelectedItem != null)
                    {
                        totalScore += crd * GetGrade(grds[i].SelectedItem.ToString());
                    }
                    else
                    {
                        MessageBox.Show($"{titles[i]} 과목의 학점이 선택되지 않았습니다.");
                        isclac = false; 
                    }

                }
            }
            if (isclac) { txtGrade.Text = (totalScore / totalCredits).ToString("0.00");}
        }

        private double GetGrade(string text)
        {
            double grade = 0;
            if (text == "A+") grade = 4.5;
            else if (text == "A") grade = 4.0;
            else if (text == "B+") grade = 3.5;
            else if (text == "B") grade = 3.0;
            else if (text == "C+") grade = 2.5;
            else if (text == "D+") grade = 2.0;
            else if (text == "D") grade = 1.5;
            else if (text == "E") grade = 1.0;
            else grade = 0;
            return grade;
        }
    }
}

 

'C# 이것저것 > 초보자를 위한 C#200제' 카테고리의 다른 글

[C#] LINQ 기초  (0) 2024.02.02
[C#] Predicate<T> 델리게이트  (1) 2024.02.02
[C#] 람다식(Lambda Expression)  (0) 2024.02.01
[C#] 델리게이트의 기본  (0) 2024.02.01