Advertisement
EstEsca

adf

Dec 3rd, 2015
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.05 KB | None | 0 0
  1. using namespace std;
  2.  
  3. #include <iostream>
  4. #include <stdio.h>
  5. #include <math.h>
  6. #include <fstream>
  7. #define SIZE 3
  8.  
  9. struct student_{
  10.   double kor;
  11.   double eng;
  12.   double math;
  13. };
  14.  
  15. struct student_ calc_average(struct student_* s, int n){
  16.   struct student_ average;
  17.   average.kor = 0;
  18.   average.math = 0;
  19.   average.eng = 0;
  20.  
  21.   for(int i=0;i<n;i++){
  22.     average.kor = average.kor+s[i].kor;
  23.     average.eng = average.eng+s[i].eng;
  24.     average.math = average.math+s[i].math;
  25.   }
  26.  
  27.   average.kor = average.kor/(float)n;
  28.   average.eng = average.eng/(float)n;
  29.   average.math = average.math/(float)n;
  30.  
  31.   return average;
  32. }
  33.  
  34. struct student_ calc_std(struct student_* s, int n){
  35.   struct student_ std;
  36.   std.kor = 0;
  37.   std.math = 0;
  38.   std.eng = 0;
  39.  
  40.   struct student_ average;
  41.   average = calc_average(s, n);
  42.  
  43.   for(int i=0;i<n;i++){
  44.     std.kor = std.kor+(s[i].kor-average.kor)*(s[i].kor-average.kor);
  45.     std.eng = std.eng+(s[i].eng-average.eng)*(s[i].eng-average.eng);
  46.     std.math = std.math+(s[i].math-average.math)*(s[i].math-average.math);
  47.   }
  48.  
  49.   std.kor = sqrt(std.kor);
  50.   std.eng = sqrt(std.eng);
  51.   std.math = sqrt(std.math);
  52.  
  53.   return std;
  54. }
  55.  
  56. int main(void){
  57.   char buff[1024];
  58.   size_t n_size;
  59.  
  60.   struct student_ s[SIZE];
  61.   char Number[4]="No.";
  62.   char Korean[3]="K";
  63.   char English[3]="E";
  64.   char Math[3]="M";
  65.  
  66.   int i=0;
  67.   int t=0;
  68.  
  69.   std::ifstream fin("student.txt");
  70.   if(fin.fail()){
  71.     //DO NOTHING
  72.   }
  73.   else{
  74.     double tKor;
  75.     double tEng;
  76.     double tMath;
  77.     while(fin>>tKor>>tEng>>tMath){
  78.  
  79.       s[i].kor = tKor;
  80.       s[i].eng = tEng;
  81.       s[i].math = tMath;
  82.  
  83.       i++;
  84.  
  85.       if(i>=SIZE){
  86.         break;
  87.       }
  88.     }
  89.   }
  90.  
  91.   while(i<SIZE){
  92.     int error=0;
  93.     printf("Input Kor Eng Math score of student %d: ",i+1);
  94.     scanf("%lf %lf %lf",&s[i].kor, &s[i].eng, &s[i].math);
  95.     if(s[i].kor<0 || s[i].kor>100){
  96.       error=1;
  97.     }
  98.     if(s[i].eng<0 || s[i].eng>100){
  99.       error=1;
  100.     }
  101.     if(s[i].math<0 || s[i].math>100){
  102.       error=1;
  103.     }
  104.     if(error>1){
  105.       printf("ERROR OCCURED\n");
  106.       continue;
  107.     }
  108.     i++;
  109.   }
  110.  
  111.   printf("%-8s %8s %8s %8s\n",Number, Korean, English, Math);
  112.   while(t<SIZE){
  113.     printf("%d %2.0lf %2.0lf %2.0lf\n", t+1, s[t].kor, s[t].eng, s[t].math);
  114.     t++;
  115.   }
  116.  
  117.   struct student_ average;
  118.   average = calc_average(s, SIZE);
  119.   printf("AVERAGE_KOR = %f\n",average.kor);
  120.   printf("AVERAGE_ENG = %f\n",average.eng);
  121.   printf("AVERAGE_MATH = %f\n",average.math);
  122.  
  123.   struct student_ std;
  124.   std = calc_std(s, SIZE);
  125.   printf("STD_KOR = %f\n",std.kor);
  126.   printf("STD_ENG = %f\n",std.eng);
  127.   printf("STD_MATH = %f\n",std.math);
  128.  
  129.  
  130.   t=0;
  131.   ofstream fout("output.txt");
  132.  
  133.   while(t<SIZE){
  134.     fout << s[t].kor << " " << s[t].eng << " " << s[t].math << endl;
  135.     t++;
  136.   }
  137.   fout << "AVE_KOR AVE_ENG AVE_MATH"<<endl;
  138.   fout << average.kor << " " << average.eng << " " << average.math << endl;
  139.   fout << "STD_KOR STD_ENG STD_MATH"<<endl;
  140.   fout << std.kor << " " << std.eng << " " << std.math << endl;
  141.  
  142.   fout.close();
  143.  
  144.   return 0;
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement