Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using namespace std;
- #include <iostream>
- #include <stdio.h>
- #include <math.h>
- #include <fstream>
- #define SIZE 3
- struct student_{
- double kor;
- double eng;
- double math;
- };
- struct student_ calc_average(struct student_* s, int n){
- struct student_ average;
- average.kor = 0;
- average.math = 0;
- average.eng = 0;
- for(int i=0;i<n;i++){
- average.kor = average.kor+s[i].kor;
- average.eng = average.eng+s[i].eng;
- average.math = average.math+s[i].math;
- }
- average.kor = average.kor/(float)n;
- average.eng = average.eng/(float)n;
- average.math = average.math/(float)n;
- return average;
- }
- struct student_ calc_std(struct student_* s, int n){
- struct student_ std;
- std.kor = 0;
- std.math = 0;
- std.eng = 0;
- struct student_ average;
- average = calc_average(s, n);
- for(int i=0;i<n;i++){
- std.kor = std.kor+(s[i].kor-average.kor)*(s[i].kor-average.kor);
- std.eng = std.eng+(s[i].eng-average.eng)*(s[i].eng-average.eng);
- std.math = std.math+(s[i].math-average.math)*(s[i].math-average.math);
- }
- std.kor = sqrt(std.kor);
- std.eng = sqrt(std.eng);
- std.math = sqrt(std.math);
- return std;
- }
- int main(void){
- char buff[1024];
- size_t n_size;
- struct student_ s[SIZE];
- char Number[4]="No.";
- char Korean[3]="K";
- char English[3]="E";
- char Math[3]="M";
- int i=0;
- int t=0;
- std::ifstream fin("student.txt");
- if(fin.fail()){
- //DO NOTHING
- }
- else{
- double tKor;
- double tEng;
- double tMath;
- while(fin>>tKor>>tEng>>tMath){
- s[i].kor = tKor;
- s[i].eng = tEng;
- s[i].math = tMath;
- i++;
- if(i>=SIZE){
- break;
- }
- }
- }
- while(i<SIZE){
- int error=0;
- printf("Input Kor Eng Math score of student %d: ",i+1);
- scanf("%lf %lf %lf",&s[i].kor, &s[i].eng, &s[i].math);
- if(s[i].kor<0 || s[i].kor>100){
- error=1;
- }
- if(s[i].eng<0 || s[i].eng>100){
- error=1;
- }
- if(s[i].math<0 || s[i].math>100){
- error=1;
- }
- if(error>1){
- printf("ERROR OCCURED\n");
- continue;
- }
- i++;
- }
- printf("%-8s %8s %8s %8s\n",Number, Korean, English, Math);
- while(t<SIZE){
- printf("%d %2.0lf %2.0lf %2.0lf\n", t+1, s[t].kor, s[t].eng, s[t].math);
- t++;
- }
- struct student_ average;
- average = calc_average(s, SIZE);
- printf("AVERAGE_KOR = %f\n",average.kor);
- printf("AVERAGE_ENG = %f\n",average.eng);
- printf("AVERAGE_MATH = %f\n",average.math);
- struct student_ std;
- std = calc_std(s, SIZE);
- printf("STD_KOR = %f\n",std.kor);
- printf("STD_ENG = %f\n",std.eng);
- printf("STD_MATH = %f\n",std.math);
- t=0;
- ofstream fout("output.txt");
- while(t<SIZE){
- fout << s[t].kor << " " << s[t].eng << " " << s[t].math << endl;
- t++;
- }
- fout << "AVE_KOR AVE_ENG AVE_MATH"<<endl;
- fout << average.kor << " " << average.eng << " " << average.math << endl;
- fout << "STD_KOR STD_ENG STD_MATH"<<endl;
- fout << std.kor << " " << std.eng << " " << std.math << endl;
- fout.close();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement