2009. 3. 23. 14:07

꼬꼬마때 만든 성적처리 C++


 컴퓨터를 뒤지다보니 아주 예전에 만들었던 성적 처리 C++ 소스를 찾았는데 지금 보면 쬐끔 엉성한것 같기도...
~(ㅡ``ㅡ)~

문제 : 3명의 학번,이름,국어,수학,영어 점수를 입력받아 평균을 처리하고 양식에 맞게 화면 및 파일로 출력하시오.

내가 맹근 소스


#include <iostream>
#include <fstream>
#include <time.h>

using namespace std;

class score {
private:
	char *id_num;     // 학번
	char *name;       // 이름
protected:
	double kor;       // 국어 점수
	double math;      // 수학 점수
	double eng;       // 영어 점수
	double ave;       // 세 과목의 평균
public:
	score();
	score(char *_id_num, char *_name, double _kor, double _math, double _eng); // 생성자
	~score();
	void initStuData(char *_id_num, char *_name, double _kor, double _math, double _eng); // 객체 초기화 메소드
	double getScoreByKor();
	double getScoreByMath();
	double getScoreByEng();
	double getScoreByAve();
	void printScore();
	void writeScore(char *filename);
};

class tot_score : public score {    // 계승
private:
	double tot_ave;		// 모든 과목의 전체 평균 
						// 각 과목의 평균은 계승받은
						// 멤버 데이터의 kor, math, eng 사용
public:
	tot_score();
	~tot_score() {}
	void getStuData(score &obj);
	void calcStuData(int stuNum);
	void printStuAvg();
	void writeStuAvg(char *filename);
};

score::score() { // 객체 초기화시 기본 정보로 초기화
	id_num = new char[2];
	name = new char[9];

	strcpy(id_num,"0");
	strcpy(name,"정보없음");

	kor = 0.0f;
	math = 0.0f;
	eng = 0.0f;
	ave = 0.0f;
}

score::score(char *_id_num, char *_name, double _kor, double _math, double _eng) { // 생성자 에서 각각 데이타로 초기화
	id_num = new char[strlen(_id_num)+1];
	name = new char[strlen(_name)+1];

	strcpy(id_num,_id_num);
	strcpy(name,_name);

	kor = _kor;
	math = _math;
	eng = _eng;
	ave = (_kor + _math + _eng) / 3 ;
}

score::~score() { // 사용한 독적 배열을 해제
	delete [] id_num;
	delete [] name;
}

void score::initStuData(char *_id_num, char *_name, double _kor, double _math, double _eng) { // 별도의 초기화 메소드
	id_num = new char[strlen(_id_num)+1];
	name = new char[strlen(_name)+1];

	strcpy(id_num,_id_num);
	strcpy(name,_name);

	kor = _kor;
	math = _math;
	eng = _eng;
	ave = (_kor + _math + _eng) / 3 ;
}

double score::getScoreByKor() {  // 국어 점수 리턴
	return kor;
}

double score::getScoreByMath() { // 수학 점수 리턴
	return math;
}

double score::getScoreByEng() { // 영어점수 리턴
	return eng;
}

double score::getScoreByAve() { // 평균 리턴
	return ave;
}

void score::printScore() { // 학생의 성적 정보를 화면에 출력
	cout << "학생성적정보" << endl;
	cout << "아이디 : " << id_num << endl;
	cout << "이름 : " << name << endl;
	cout << "국어 : " << kor << endl;
	cout << "수학 : " << math << endl;
	cout << "영어 : " << eng << endl;
	cout << "평균 : " << ave << endl << endl;
}

void score::writeScore(char *filename = "stuTotAvg.dat") { // 학생의 성적 정보를 파일로 저장
	ofstream fp;
	fp.open(filename,ios_base::out | ios_base::app); // "추가" 모드로 파일을 오픈
	fp << "학생성적정보" << endl;
	fp << "아이디 : " << id_num << endl;
	fp << "이름 : " << name << endl;
	fp << "국어 : " << kor << endl;
	fp << "수학 : " << math << endl;
	fp << "영어 : " << eng << endl;
	fp << "평균 : " << ave << endl << endl;
	fp.close();
}

tot_score::tot_score() { // 객체 생성시 초기화
	kor = 0.0f;
	math = 0.0f;
	eng = 0.0f;
	tot_ave = 0.0f;
}

void tot_score::getStuData(score &obj){ // score 객체를 받아 총점을 누적
	kor += obj.getScoreByKor();
	math += obj.getScoreByMath();
	eng += obj.getScoreByEng();
	tot_ave += obj.getScoreByAve();
}

void tot_score::calcStuData(int stuNum) { // 총점에서 평균을 구함
	kor = kor / stuNum;
	math = math / stuNum;
	eng = eng / stuNum;
	tot_ave = tot_ave / stuNum;
}

void tot_score::printStuAvg() { // 과목평균,총평균을 화면에 출력
	cout << "과목 평균" << endl;
	cout << "국어 : " << kor << endl;
	cout << "수학 : " << math << endl;
	cout << "영어 : " << eng << endl;
	cout << "총평균 : " << tot_ave << endl;
}

void tot_score::writeStuAvg(char *filename = "stuTotAvg.dat") { // 과목균,총평균을 파일로 저장
	ofstream fp;
	fp.open(filename,ios_base::out | ios_base::app);
	fp << "과목 평균" << endl;
	fp << "국어 : " << kor << endl;
	fp << "수학 : " << math << endl;
	fp << "영어 : " << eng << endl;
	fp << "총평균 : " << tot_ave << endl;
	fp.close();
}

int main() {
	char id_name[10]; // 입력받은 데이타를 저장할 임시 변수들
	char name[10];
	double kor = 0.0f;
	double math = 0.0f;
	double eng = 0.0f;
	int stuNum = 0; // 평균을 처리할 학생수 저장

	char filename[100]; // 평균을 저장할 파일명
	time_t now;
	struct tm *now_day_time;

	time(&now);
	now_day_time = localtime(&now);
	strftime(filename,100,"score%Y%m%d%H%M%S.dat",now_day_time); // 파일명을 "score년월일시분초.dat"로 설정

	printf("3명의 학생정보를 입력해주세요.\n학번 이름 국어 수학 영어\n");

	score stu1,stu2,stu3; // try문 내에서 객체 생성시 블럭 밖에서는 객제가 존재하지 않아 try문 밖에서 생성

	try {
		cin >> id_name >> name;  // 정보를 입력 받으며 예외 처리를 함
		cin >> kor; if(cin.fail()) throw 1;
		cin >> math; if(cin.fail()) throw 2;
		cin >> eng; if(cin.fail()) throw 3;
		stu1.initStuData(id_name,name,kor,math,eng); // 입력 받은 정보를 이용해 객체 초기화

		cin >> id_name >> name;
		cin >> kor; if(cin.fail()) throw 1;
		cin >> math; if(cin.fail()) throw 2;
		cin >> eng; if(cin.fail()) throw 3;
		stu2.initStuData(id_name,name,kor,math,eng);

		cin >> id_name >> name;
		cin >> kor; if(cin.fail()) throw 1;
		cin >> math; if(cin.fail()) throw 2;
		cin >> eng; if(cin.fail()) throw 3;
		stu3.initStuData(id_name,name,kor,math,eng);
	}
	catch(int e) { 
		switch(e) {
		case 1 :
			printf("kor 점수 입력이 잘못 되었습니다.\n");
			break;
		case 2 :
			printf("math 점수 입력이 잘못 되었습니다.\n");
			break;
		case 3 :
			printf("eng 점수 입력이 잘못 되었습니다.\n");
			break;
		}
		return 0;
	}

	tot_score stuTot; // 각 점수 객체들을 tot_score 로 넘겨 점수를 누적
	stuTot.getStuData(stu1); stuNum++;
	stuTot.getStuData(stu2); stuNum++;
	stuTot.getStuData(stu3); stuNum++;
	stuTot.calcStuData(stuNum); // 평균을 구함

	stu1.printScore(); // 정보를 화면에 출력
	stu2.printScore();
	stu3.printScore();
	stuTot.printStuAvg();

	stu1.writeScore(filename); // 정보를 파일로 출력
	stu2.writeScore(filename);
	stu3.writeScore(filename);
	stuTot.writeStuAvg(filename);

	system("pause");

	return 0;
}