Advertisement
Guest User

Untitled

a guest
Oct 21st, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.66 KB | None | 0 0
  1. #include<iostream>
  2. #include<string>
  3. #include<sstream>
  4. #include<iomanip>
  5. #include<vector>
  6. #include<algorithm>
  7. #include<numeric>
  8. #include<cmath>
  9. using namespace std;
  10. class StrictError{};
  11. class IntOnlyErr{};
  12. class EmptyInput{};
  13. int check_flags(string& ref_input,bool& ref_strict,bool& ref_int_only,bool& ref_both_flags)
  14. {
  15. if(ref_input=="strict")ref_strict=true;
  16. else if(ref_input=="integers-only")ref_int_only=true;
  17. else if(ref_input=="strict:integers-only")ref_both_flags=true;
  18.  
  19. if(ref_strict==true || ref_int_only==true || ref_both_flags==true)return 1;
  20.  
  21. return 0;
  22. }
  23. int main()
  24. {
  25. bool check_strict=false,check_integers_only=false,check_both=false,error_strict=false,empty_input=false,both_err=false;
  26. string input;
  27. vector<double> v;
  28. double max=0,min,sum=0,value;
  29. int i=0,res=0;
  30. while(true)
  31. {
  32. getline(cin,input);
  33. try{
  34. if(input=="END" && i==0)
  35. {
  36. throw EmptyInput();
  37. }
  38. }
  39. catch(EmptyInput only_end)
  40. {
  41. empty_input=true;
  42. break;
  43. }
  44. if(input=="END" && i>0)break;
  45. if(i==0)
  46. {
  47. res=check_flags(input,check_strict,check_integers_only,check_both);
  48. if(res==1)getline(cin,input);
  49. }
  50. istringstream in_stream(input);
  51. try{
  52. while(in_stream.good())
  53. {
  54. in_stream>>value;
  55. if(!in_stream.fail())
  56. {
  57. if(check_integers_only==true)
  58. {
  59. if(value!=(int)value)value=(int)value;
  60. }
  61. else if(check_both==true && value!=(int)value)throw IntOnlyErr();
  62. v.push_back(value);
  63. }
  64. else
  65. {
  66. if(check_strict==true)throw StrictError();
  67. else if(check_both==true)throw StrictError();
  68. else
  69. {
  70. in_stream.clear();
  71. in_stream.ignore();
  72. }
  73. }
  74. }
  75. }
  76. catch(StrictError strict_err)
  77. {
  78. error_strict=true;
  79. }
  80. catch(IntOnlyErr errs)
  81. {
  82. both_err=true;
  83. v.push_back(value);
  84. }
  85. i++;
  86. }
  87. try{
  88. if(v.size()==0)throw EmptyInput();
  89. }
  90. catch(EmptyInput no_numbers)
  91. {
  92. empty_input=true;
  93. }
  94. if(empty_input==true)cout<<"ERR: PROVIDE AT LEAST ONE NUMBER"<<endl;
  95. else if(error_strict==true)cout<<"ERR: PROVIDE ONLY NUMBERS"<<endl;
  96. else if(both_err==true)cout<<"ERR: PROVIDE ONLY INTEGERS"<<endl;
  97. else {
  98. if(check_integers_only==true)
  99. {
  100. cout<<trunc(*min_element(v.begin(),v.end()))<<" "
  101. <<trunc(*max_element(v.begin(),v.end()))<<" ";
  102. cout<<trunc(accumulate(v.begin(),v.end(),0.0)/v.size());
  103. }
  104. else
  105. {
  106. cout<<*min_element(v.begin(),v.end())<<" "
  107. <<*max_element(v.begin(),v.end())<<" "
  108. <<setprecision(4)<<accumulate(v.begin(),v.end(),0.0)/v.size()<<endl;
  109. }
  110. }
  111. return 0;
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement