~cpp #include <fstream> #include "const.h" #include "student.h" class Calculate { private: Student a; public: void input();//파일입력, 학점으로 전환 void Calculate_grade(); //void output();//파일출력 };
~cpp #ifndef STUDENT_H_ #define STUDENT_H_ #include "const.h" class Student { private: char name[STUDENT_NUM][10]; double credit_average[STUDENT_NUM]; //장학생 명단을 찍어내기 위한 변수 //char sort_grade_name[STUDENT_NUM][10]; //double sort_grade[STUDENT_NUM]; public: Student(); void name_find(); // 이름을 파일에서 받아서 저장. void average(); // 평균을 계산하여 저장. void sort(); // 학점평균이 높은 순으로 저장. void scholarship();//장학금, 학고 학생 출력, double grade[STUDENT_NUM][SUBJECT_NUM]; }; #endif
~cpp #define SUBJECT_NUM 4 #define STUDENT_NUM 120 #define SCHOLARSHIP_NUM 12
~cpp #include <iostream> using namespace std; #include <fstream> #include "calculate.h" #include "student.h" void Calculate::input() { char temp[20]; char grade_input[SUBJECT_NUM][3]; double credit[9] = {4.5, 4.0, 3.5, 3.0, 2.5, 2.0, 1.5, 1.0, 0}; //학점 char grade_data[9][3] = {"A+", "A", "B+", "B", "C+", "C", "D+", "D", "F"}; fstream fin("input.txt"); for(int student_num = 0; student_num < 120; student_num++) { fin.getline(temp, 20, ':'); for(int i = 0; i < SUBJECT_NUM; i++) { fin.get(); fin >> grade_input[i]; for(int j = 0; j < 9; j++) { if(strcmp(grade_input[i], grade_data[j]) == 0) a.grade[student_num][i] = credit[j]; } } } } /* void Calculate::output() { fstream fout("output.txt"); } */ void Calculate::Calculate_grade() { a.name_find(); a.average(); a.sort(); a.scholarship(); }
~cpp #include <iostream> using namespace std; #include <fstream> #include "student.h" #include "calculate.h" #include "const.h" Student::Student() { for(int j = 0; j < STUDENT_NUM; j++) { credit_average[j] = 0.0; for(int i = 0; i < SUBJECT_NUM; i++) grade[j][i] = 0; } } void Student::name_find() { char temp[20]; fstream fin("input.txt"); for(int i = 0; i < STUDENT_NUM; i++) { fin.getline(name[i], 10, ':'); fin.getline(temp, 20, 'n'); } } void Student::average() { double sum = 0; for (int j = 0; j < STUDENT_NUM; j++) { for(int i = 0; i < SUBJECT_NUM; i++) sum += grade[j][i]; credit_average[j] = (double)(sum/SUBJECT_NUM); sum = 0; } } void Student::scholarship() { //fstream fout("output.txt"); cout << "학고 명단" << endl; for(int i = 0; i < STUDENT_NUM; i++) { if(credit_average[i] <= 1.5) cout << name[i] << endl; } cout << "장학생 명단" << endl; for(i = 0; i < SCHOLARSHIP_NUM; i++) { cout << name[i]; cout << endl; } } void Student::sort() { char temp[10]; double temp_grade; for(int i = 0; i < STUDENT_NUM; i++){ for(int j = i; j < STUDENT_NUM; j++){ if(credit_average[i] <= credit_average[j]) { temp_grade = credit_average[i]; credit_average[i] = credit_average[j]; credit_average[j] = temp_grade; strcpy(temp, name[j]); strcpy(name[j], name[i]); strcpy(name[i], temp); } } } }
~cpp #include <iostream> #include <fstream> #include "calculate.h" #include "student.h" using namespace std; void main() { Calculate a; a.input(); a.Calculate_grade(); }