Advertisement
Guest User

Untitled

a guest
Jan 29th, 2014
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.46 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <sstream>
  4. #include <vector>
  5. #include <stdlib.h>
  6. using namespace std;
  7.  
  8. int main(){
  9.  
  10.     enum readInStates {Normal, Name, Average};
  11.     readInStates state;
  12.     string s, name;
  13.     double qScore = 0, eScore = 0, totalExam = 0, numExam = 0, grade = 0, avg = 0, finalGrade = 0;
  14.     bool calculateAverage;
  15.  
  16.     while (cin >> s){
  17.         if (state == Normal){
  18.             if (s == "NAME") {state = Name;}
  19.             else if (s == "AVERAGE"){
  20.                 state = Average;
  21.             //new average, so initialize values to 0
  22.                 numExam = 0;
  23.                 totalExam = 0;
  24.             }
  25.             else if (s[0] >= '0' && s[0] <= '9'){
  26.                 qScore = atof(s.c_str());
  27.                 grade += qScore;
  28.             }
  29.  
  30.  
  31.         }
  32.  
  33.  
  34.     if (state == Name){
  35.         name = s;
  36.         state = Normal;
  37.     }    
  38.          
  39.     if (state == Average){
  40.         calculateAverage = false;
  41.         if (s == "NAME")
  42.         {
  43.             state = Name;
  44.             calculateAverage = true;
  45.         }
  46.         else if (s[0] >= '0' && s[0] <= '9'){
  47.             eScore = atof(s.c_str());
  48.             numExam++;
  49.             totalExam += eScore;
  50.         }
  51.    
  52.         if (calculateAverage)
  53.         {
  54.         if (numExam > 0)
  55.             {
  56.                 grade += (totalExam/numExam);
  57.             }
  58.         }
  59.     }
  60.     }
  61.  
  62.     cout << name << " " << grade << endl;
  63.  
  64.  
  65.     return 0;
  66. }//end main
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement