클래스와 구조체
헤더파일 include 해서 완성하기
2명의 사람과 6개의 과목으로 설정
StudentScore.h
#pragma once
#include <iostream>
#include <string>
#include <algorithm>
class StudentScore
{
public:
StudentScore();
~StudentScore();
void SetStudentName(std::string);
int SetSubjectScore(std::string sub1, int a);
int DoCalc();
private:
int en_i = 0;
int en_j = 0;
int GetSum();
int GetAvg();
int GetMin();
int GetMax();
std::string name;
struct score
{
int score;
std::string subject;
};
struct save
{
std::string _names;
int _sum;
float _avg;
score _item_[6];
};
struct StudentInfo
{
std::string _name;
save _items[2];
};
StudentInfo stStudentInfo;
};
StudentScore.cpp
#include "StudentScore.h"
StudentScore::StudentScore()
:name("")
{
std::cout << "StudentScore::Ctor" << std::endl;
}
StudentScore::~StudentScore()
{
std::cout << "StudentScore::Dtor" << std::endl;
}
void StudentScore::SetStudentName(std::string A)
{
stStudentInfo._name = A;
}
int StudentScore::SetSubjectScore(std::string sub1, int a)
{
if (en_j == 6)
{
en_i++;
en_j = 0;
}
stStudentInfo._items[en_i]._item_[en_j].subject = sub1;
stStudentInfo._items[en_i]._item_[en_j].score = a;
en_j++;
return 0;
}
int StudentScore::DoCalc()
{
int i = 0;
std::cout<<"=================================================================================================================" << std::endl;
std::cout << stStudentInfo._name << " 의 점수 :\t" << stStudentInfo._items[i]._item_[0].subject << ": " << stStudentInfo._items[i]._item_[0].score << "\t" << stStudentInfo._items[i]._item_[1].subject << ": " << stStudentInfo._items[i]._item_[1].score << "\t" << stStudentInfo._items[i]._item_[2].subject << ": " << stStudentInfo._items[i]._item_[2].score << "\t" << stStudentInfo._items[i]._item_[3].subject << ": " << stStudentInfo._items[i]._item_[3].score;
std::cout << "\t" << stStudentInfo._items[i]._item_[4].subject << ": " << stStudentInfo._items[i]._item_[4].score << "\t " << stStudentInfo._items[i]._item_[5].subject << ": " << stStudentInfo._items[i]._item_[5].score << std::endl;
i++;
GetSum();
GetAvg();
GetMin();
GetMax();
std::cout << "=================================================================================================================" << std::endl;
return 0;
}
int StudentScore::GetSum()
{
int i = 0;
stStudentInfo._items[i]._sum = stStudentInfo._items[i]._item_[0].score + stStudentInfo._items[i]._item_[1].score + stStudentInfo._items[i]._item_[2].score
+ stStudentInfo._items[i]._item_[3].score + stStudentInfo._items[i]._item_[4].score + stStudentInfo._items[i]._item_[5].score;
std::cout << stStudentInfo._name << " 의 총합 : " << stStudentInfo._items[i]._sum;
i++;
return 0;
}
int StudentScore::GetAvg()
{
int j = 0;
int i = 0;
stStudentInfo._items[i]._avg = static_cast<float>(stStudentInfo._items[i]._sum) / 6;
std::cout << " \t평균 : " << stStudentInfo._items[i]._avg;
i++;
j++;
return 0;
}
int StudentScore::GetMin()
{
int i = 0;
int min = std::min({ stStudentInfo._items[i]._item_[0].score, stStudentInfo._items[i]._item_[1].score , stStudentInfo._items[i]._item_[2].score , stStudentInfo._items[i]._item_[3].score
, stStudentInfo._items[i]._item_[4].score , stStudentInfo._items[i]._item_[5].score });
std::cout << " \t과목 최소: " << min;
i++;
return 0;
}
int StudentScore::GetMax()
{
int i = 0;
int max = std::max({ stStudentInfo._items[i]._item_[0].score, stStudentInfo._items[i]._item_[1].score , stStudentInfo._items[i]._item_[2].score , stStudentInfo._items[i]._item_[3].score
, stStudentInfo._items[i]._item_[4].score , stStudentInfo._items[i]._item_[5].score });
std::cout << " \t과목 최대: " << max << std::endl;
i++;
return 0;
}
실행부분
#pragma once
#include "StudentScore.h" // 현재 디렉토리 파일 include
#include <iostream> //시스템이 등록된 디렉토리에서 파일 inclue
#include <string>
#include <algorithm>
int num1;
std::string sub1;
std::string name;
int main()
{
{
StudentScore SC;
for (int i = 0; i < 2; i++)
{
std::cout << "이름 입력" << std::endl;
std::cin >> name;
SC.SetStudentName(name);
std::cout << "과목과 성적 입력" << std::endl;
for (int j = 0; j < 6; j++)
{
std::cout << "과목 " << j+1 <<std::endl;
std::cin >> sub1;
std::cin >> num1;
SC.SetSubjectScore(sub1, num1);
}
SC.DoCalc();
}
}
return 0;
}