Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<string>
- #include<sstream>
- #include<iomanip>
- #include<vector>
- #include<algorithm>
- #include<numeric>
- #include<cmath>
- using namespace std;
- class StrictError{};
- class IntOnlyErr{};
- class EmptyInput{};
- int check_flags(string& ref_input,bool& ref_strict,bool& ref_int_only,bool& ref_both_flags)
- {
- if(ref_input=="strict")ref_strict=true;
- else if(ref_input=="integers-only")ref_int_only=true;
- else if(ref_input=="strict:integers-only")ref_both_flags=true;
- if(ref_strict==true || ref_int_only==true || ref_both_flags==true)return 1;
- return 0;
- }
- int main()
- {
- bool check_strict=false,check_integers_only=false,check_both=false,error_strict=false,empty_input=false,both_err=false;
- string input;
- vector<double> v;
- double max=0,min,sum=0,value;
- int i=0,res=0;
- while(true)
- {
- getline(cin,input);
- try{
- if(input=="END" && i==0)
- {
- throw EmptyInput();
- }
- }
- catch(EmptyInput only_end)
- {
- empty_input=true;
- break;
- }
- if(input=="END" && i>0)break;
- if(i==0)
- {
- res=check_flags(input,check_strict,check_integers_only,check_both);
- if(res==1)getline(cin,input);
- }
- istringstream in_stream(input);
- try{
- while(in_stream.good())
- {
- in_stream>>value;
- if(!in_stream.fail())
- {
- if(check_integers_only==true)
- {
- if(value!=(int)value)value=(int)value;
- }
- else if(check_both==true && value!=(int)value)throw IntOnlyErr();
- v.push_back(value);
- }
- else
- {
- if(check_strict==true)throw StrictError();
- else if(check_both==true)throw StrictError();
- else
- {
- in_stream.clear();
- in_stream.ignore();
- }
- }
- }
- }
- catch(StrictError strict_err)
- {
- error_strict=true;
- }
- catch(IntOnlyErr errs)
- {
- both_err=true;
- v.push_back(value);
- }
- i++;
- }
- try{
- if(v.size()==0)throw EmptyInput();
- }
- catch(EmptyInput no_numbers)
- {
- empty_input=true;
- }
- if(empty_input==true)cout<<"ERR: PROVIDE AT LEAST ONE NUMBER"<<endl;
- else if(error_strict==true)cout<<"ERR: PROVIDE ONLY NUMBERS"<<endl;
- else if(both_err==true)cout<<"ERR: PROVIDE ONLY INTEGERS"<<endl;
- else {
- if(check_integers_only==true)
- {
- cout<<trunc(*min_element(v.begin(),v.end()))<<" "
- <<trunc(*max_element(v.begin(),v.end()))<<" ";
- cout<<trunc(accumulate(v.begin(),v.end(),0.0)/v.size());
- }
- else
- {
- cout<<*min_element(v.begin(),v.end())<<" "
- <<*max_element(v.begin(),v.end())<<" "
- <<setprecision(4)<<accumulate(v.begin(),v.end(),0.0)/v.size()<<endl;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement