Advertisement
Guest User

Untitled

a guest
Oct 20th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.07 KB | None | 0 0
  1. #include <iostream>
  2. #include <sstream>
  3. #include <iomanip>
  4. #include <math.h>
  5.  
  6. using namespace std;
  7.  
  8. int main(){
  9.   double max;
  10.   double min;
  11.   double average;
  12.   double sum = 0;
  13.   double temp;
  14.   double check;
  15.   int i = 0;
  16.   string define;
  17.   int strict_flag = 0;
  18.   int integers_only_flag = 0;
  19.  
  20.   cin >> define;
  21.   size_t strict = define.find("strict");
  22.   size_t integers_only = define.find("integers-only");
  23.  
  24.   if(define == "END"){
  25.       cout <<"ERR: PROVIDE AT LEAST ONE NUMBER" << endl;
  26.       return 0;
  27.   }
  28.  
  29.   if(strict != string::npos){
  30.     define.erase(define.begin(),define.end());
  31.     strict_flag = 1;
  32.   }
  33.   if(integers_only != string::npos){
  34.     define.erase(define.begin(),define.end());
  35.     integers_only_flag = 1;
  36.   }
  37.  
  38.   if(strict_flag == 0 && integers_only_flag == 0){
  39.     istringstream valid_elements(define);
  40.     valid_elements >> temp;
  41.     max = temp;
  42.     min = temp;
  43.     sum = temp;
  44.     i = 1;
  45.   }
  46.  
  47.   while(1){
  48.     cin >> define;
  49.     if(define == "END"){
  50.       break;
  51.     }
  52.     if(strict_flag == 1){
  53.       if((define < "0" || define > "9") && define != " " && define != "\t" && define != "\n" && define != "/"&& define != "." ){
  54.         cout << "ERR: PROVIDE ONLY NUMBERS" << endl;
  55.         return 0;
  56.       }
  57.     }
  58.  
  59.     istringstream valid_elements(define);
  60.     valid_elements >> temp;
  61.     if(strict_flag == 1 && integers_only_flag == 1){
  62.       if(modf(temp, &check) != 0){
  63.         cout << "ERR: PROVIDE ONLY INTEGERS" << endl;
  64.         return 0;
  65.       }
  66.     }
  67.     if(i == 0){
  68.       max = temp;
  69.       min = temp;
  70.       sum = temp;
  71.       i = 1;
  72.     }else{
  73.       if(temp > max){
  74.         max = temp;
  75.       }
  76.       if(temp < min){
  77.         min = temp;
  78.       }
  79.       sum += temp;
  80.       i++;
  81.     }
  82.   }
  83.   average = sum/i;
  84.   if(integers_only_flag == 1){
  85.     int imax = max;
  86.     int imin = min;
  87.     int iaverage = average;
  88.     cout << imin << " " << imax << " " << iaverage  << endl;
  89.   }else{
  90.     cout << setprecision(4 )<< min << " " << setprecision(4) << max << " " << setprecision(4) << average  << endl;
  91.   }
  92.   return 0;
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement