본문 바로가기
C++/뇌를 자극하는 C++

[C++] 챕터 8 - 성적표 프로젝트 Ver 1

by Minkyu Lee 2023. 4. 25.

// 8-1 : 성적표 Ver 1.0

#include <iostream>
using namespace std;

int main()
{
    while (1) // 계속해서 메뉴 보여주기
    {
        // 메뉴
        cout << "\n------ 메뉴 ------\n";
        cout << "1. 학생 성적 추가\n";
        cout << "2. 전체 성적 보기\n";
        cout << "Q. 프로그램 종료\n";
        cout << "-------------------\n\n";
        cout << "원하는 작업의 번호를 입력하세요 : ";

        // 입력 받기
        char select;
        cin >> select;

        // 처리
        switch (select)
        {
            case '1':
                cout << "\n학생 성적 추가가 선택되었습니다.\n";
                break;
            case '2':
                cout << "\n전체 성적 보기가 선택되었습니다.\n";
                break;
            case 'Q':
            case 'q':
                cout << "\n프로그램을 종료합니다.\n";
                return 0; // 왜 여기에 return 0이 오는가? main 함수 종료를 위해
            default:
                cout << "\n올바른 값을 입력해주세요.\n";
                break;
        }
        return 0; // main 함수 종료를 위해
    }
}

/*
입력을 받을 때는 cin 객체를 사용한다.
*/

 

// 8-2 : 성적표 Ver 1.1 - 학생 정보 입력하기

#include <iostream>
using namespace std;

int main()
{
    int S1_No, S2_No, S3_No; // 학번
    int S1_Kor, S2_Kor, S3_Kor; // 국어 점수
    int S1_Math, S2_Math, S3_Math; // 수학
    int S1_Eng, S2_Eng, S3_Eng; // 영어
    float S1_Ave, S2_Ave, S3_Ave; // 개인 평균
    float TotalAve = 0.0f; // 전체 평균
    int NumberOfStudent = 0; // 학생수

    while (1)
    {
        // 메뉴
        cout << "\n------ 메뉴 ------\n";
        cout << "1. 학생 성적 추가\n";
        cout << "2. 전체 성적 보기\n";
        cout << "Q. 프로그램 종료\n";
        cout << "-------------------\n\n";
        cout << "원하는 작업의 번호를 입력하세요 : ";
       
        // 입력 받기
        char select;
        cin >> select;

        switch (select)
        {
            case '1' :
            {
                if (3 == NumberOfStudent)
                {
                    cout << "\n더 이상 입력할 수 없습니다.\n";
                    break;
                }

                // 점수 입력받기
                int Kor, Eng, Math;
                cout << "국어, 영어, 수학 점수를 입력하세요 : ";
                cin >> Kor >> Eng >> Math;

                // 평균 계산
                float Ave = float(Kor + Eng + Math) / 3.0f;
               
                // 현재 입력된 학생수에 따라 변수에 값 넣기
                if (0 == NumberOfStudent)
                {
                    // 개인 정보
                    S1_No = NumberOfStudent + 1;
                    S1_Kor = Kor;
                    S1_Eng = Eng;
                    S1_Math = Math;
                    S1_Ave = Ave;

                    // 전체 평균
                    TotalAve = S1_Ave;
                }
                else if (1 == NumberOfStudent)
                {
                    // 개인 정보
                    S2_No = NumberOfStudent + 1;
                    S2_Kor = Kor;
                    S2_Eng = Eng;
                    S2_Math = Math;
                    S2_Ave = Ave;

                    // 전체 평균
                    TotalAve = (S1_Ave + S2_Ave) / 2;
                }
                else
                {
                    S3_No = NumberOfStudent + 1;
                    S3_Kor = Kor;
                    S3_Eng = Eng;
                    S3_Math = Math;
                    S3_Ave = Ave;

                    // 전체 평균
                    TotalAve = (S1_Ave + S2_Ave + S3_Ave) / 3;
                }
                NumberOfStudent++;

                cout << "\n학생 성적이 올바르게 입력되었습니다.\n";
                break;
            }
            case '2' :
            {
                cout << "\n 전체 성적 보기 \n";
                cout << "학번 국어 영어 수학 평균\n";

                // 입력된 학생수만큼 반복한다.
                for (int i = 0; i < NumberOfStudent; ++i)
                {
                    // 입력된 학생수에 맞는 변수 출력 (S1, S2, S3 중에 하나)
                    if (0 == i)
                    {
                        cout << S1_No << " " << S1_Kor << " " << S1_Eng;
                        cout << " " << S1_Math << " " << S1_Ave << "\n";
                    }
                    else if (1 == i)
                    {
                        cout << S2_No << " " << S2_Kor << " " << S2_Eng;
                        cout << " " << S2_Math << " " << S2_Ave << "\n";
                    }
                    else
                    {
                        cout << S3_No << " " << S3_Kor << " " << S3_Eng;
                        cout << " " << S3_Math << " " << S3_Ave << "\n";
                    }
                }
                cout << "\n전체 평균 = " << TotalAve << "\n";
                break;
            }
            case 'q' :
            case 'Q' :
                cout << "\n프로그램을 종료합니다.\n";
                return 0;
            default :
                cout << "\n올바른 값을 입력해주세요.\n";
                break;
        }
    }
    return 0;
}

/*
case 1,2는 중괄호가 있고 q,Q,default는 중괄호가 없음에 주의하라.
후자들은 break를 제외하고는 한문장 밖에 없어서 그런듯하다.
*/

 

// 8-5: 성적표 Ver 1.2 - 성적표 출력하기

#include <iostream>
#include <iomanip> // setw 사용하기 위해 필요.

using namespace std;

int main()
{
    int S1_No, S2_No, S3_No; // 학번
    int S1_Kor, S2_Kor, S3_Kor; // 국어 점수
    int S1_Math, S2_Math, S3_Math; // 수학
    int S1_Eng, S2_Eng, S3_Eng; // 영어
    float S1_Ave, S2_Ave, S3_Ave; // 개인 평균
    float TotalAve = 0.0f; // 전체 평균
    int NumberOfStudent = 0; // 학생수

    while (1)
    {
        // 메뉴
        cout << "\n------ 메뉴 ------\n";
        cout << "1. 학생 성적 추가\n";
        cout << "2. 전체 성적 보기\n";
        cout << "Q. 프로그램 종료\n";
        cout << "-------------------\n\n";
        cout << "원하는 작업의 번호를 입력하세요 : ";

        // 입력 받기
        char select;
        cin >> select;

        switch (select)
        {
        case '1':
        {
            if (3 == NumberOfStudent)
            {
                cout << "\n더 이상 입력할 수 없습니다.\n";
                break;
            }

            // 점수 입력받기
            int Kor, Eng, Math;
            cout << "국어, 영어, 수학 점수를 입력하세요 : ";
            cin >> Kor >> Eng >> Math;

            // 평균 계산
            float Ave = float(Kor + Eng + Math) / 3.0f;

            // 현재 입력된 학생수에 따라 변수에 값 넣기
            if (0 == NumberOfStudent)
            {
                // 개인 정보
                S1_No = NumberOfStudent + 1;
                S1_Kor = Kor;
                S1_Eng = Eng;
                S1_Math = Math;
                S1_Ave = Ave;

                // 전체 평균
                TotalAve = S1_Ave;
            }
            else if (1 == NumberOfStudent)
            {
                // 개인 정보
                S2_No = NumberOfStudent + 1;
                S2_Kor = Kor;
                S2_Eng = Eng;
                S2_Math = Math;
                S2_Ave = Ave;

                // 전체 평균
                TotalAve = (S1_Ave + S2_Ave) / 2;
            }
            else
            {
                S3_No = NumberOfStudent + 1;
                S3_Kor = Kor;
                S3_Eng = Eng;
                S3_Math = Math;
                S3_Ave = Ave;

                // 전체 평균
                TotalAve = (S1_Ave + S2_Ave + S3_Ave) / 3;
            }
            NumberOfStudent++;

            cout << "\n학생 성적이 올바르게 입력되었습니다.\n";
            break;
        }
        case '2':
        {
            // 소수점 이하 두 자리만 표시 (실수 출력시)
            cout.precision(2);
            cout << fixed;

            // 타이틀 출력
            cout << "\n 전체 성적 보기 \n";
            cout << "학번 국어 영어 수학 평균\n";

            // 입력된 학생수만큼 반복한다.
            for (int i = 0; i < NumberOfStudent; ++i)
            {
                // * 개선됨 * 입력된 학생수에 맞는 변수 출력 (S1, S2, S3 중에 하나)
                if (0 == i)
                {
                    cout << setw(3) << S1_No << setw(5) << S1_Kor << setw(5);
                    cout << S1_Eng << setw(5) << S1_Math << setw(7) << S1_Ave << "\n";
                }
                else if (1 == i)
                {
                    cout << S2_No << " " << S2_Kor << " " << S2_Eng;
                    cout << " " << S2_Math << " " << S2_Ave << "\n";
                }
                else
                {
                    cout << S3_No << " " << S3_Kor << " " << S3_Eng;
                    cout << " " << S3_Math << " " << S3_Ave << "\n";
                }
            }
            cout << "\n전체 평균 = " << TotalAve << "\n";
            break;
        }
        case 'q':
        case 'Q':
            cout << "\n프로그램을 종료합니다.\n";
            return 0;
        default:
            cout << "\n올바른 값을 입력해주세요.\n";
            break;
        }
    }
    return 0;
}

/*
--- 개선사항 ---
출력을 개선한다.
1. 소수점 이하 자리수 일정하게 만들기.
2. 변수값 일정한 간격 출력.

--- setw ---
값에 상관없이 일정한 칸 출력한다.
남은 공간은 공백이 된다.
<< set(7) << S1_NO와 같이 쓰면 S1_NO 출력시 7칸 사용하라는 뜻이다.
cout 객체에게 먼저 setw를 보낸다.라고 해석한다.

*/

댓글