GradeCalc.cs (학점계산기)

Seungbin Yang / 양승빈·2024년 3월 25일

비주얼프로그래밍

목록 보기
8/21

<디자인>

학점, 성적 Label 밑에는 comboBox로 배치한다.

<코드>


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace _014_GradeCalc
{
    public partial class Form1 : Form
    {
        TextBox[] titles;
        ComboBox[] crds;
        ComboBox[] grds;
        public Form1()
        {
            InitializeComponent();
        }
        //form1이 load 될때(프로그램이 시작될 때)
        private void button1_Click(object sender, EventArgs e)
        {
            double totalScore = 0;
            double totalCredits = 0;
            for (int i = 0; i < crds.Length; i++)
            {
                if (titles[i].Text != "")
                {
                    int crd = int.Parse(crds[i].SelectedItem.ToString());
                    totalCredits += crd;
                    totalScore += crd * GetGrade(grds[i].SelectedItem.ToString());
                }
            }
            txtGrade.Text = (totalScore / totalCredits).ToString("0.00");
        }
        private double GetGrade(string v)
        {
            double grade = 0;
            if (v == "A+") grade = 4.5;
            else if (v == "A0") grade = 4.0;
            else if (v == "B+") grade = 3.5;
            else if (v == "B0") grade = 3.0;
            else if (v == "C+") grade = 2.5;
            else if (v == "C0") grade = 2.0;
            else if (v == "D+") grade = 1.5;
            else if (v == "D0") grade = 1.0;
            else grade = 0;
            return grade;
        }
        private void Form1_Load_1(object sender, EventArgs e)
        {
            txt1.Text = "인체의 구조와 기능";
            txt2.Text = "비주얼프로그래밍";
            txt3.Text = "설계및 프로젝트 기본";
            txt4.Text = "외국어";
            txt5.Text = "일반수학";
            txt6.Text = "전기전자 실험";
            txt7.Text = "데이터사이언스";
            txt8.Text = "교양";
            titles = new TextBox[] { txt1, txt2, txt3, txt4, txt5, txt6, txt7, txt8 };
            crds = new ComboBox[] { crd1, crd2, crd3, crd4, crd5, crd6, crd7, crd8 };
            grds = new ComboBox[] { grd1, grd2, grd3, grd4, grd5, grd6, grd7, grd8 };
            int[] arrCredit = { 1, 2, 3, 4, 5 };
            List<string> lstGrade = new List<string> { "A+", "A0", "B+", "B0", "C+", "C0", "D+", "D0", "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);
            }
        }
    }

}

titles, crds, grds 등 배열을 생성해준다.
foreach 문으로 crds, grds 값을 처리해준다.
조건문(if, else if)으로 학점을 계산하여 txtGrade에 출력한다.

<결과>

0개의 댓글