Advertisement
Guest User

Untitled

a guest
Nov 17th, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.26 KB | None | 0 0
  1. #include <iostream>
  2. #include <sstream>
  3. #include <vector>
  4. #include <algorithm>
  5.  
  6. struct Equation
  7. {
  8. std::string equation;
  9. int result = 0;
  10. Equation(std::string equation = "") :equation(equation)
  11. {
  12. int op1, op2;
  13. char oper;
  14. std::istringstream istr(equation);
  15. istr >> op1;
  16. istr >> oper;
  17. istr >> op2;
  18. switch (oper)
  19. {
  20. case '+': result = op1 + op2; break;
  21. case '-': result = op1 - op2; break;
  22. case '*': result = op1 * op2; break;
  23. case '/': result = op1 / op2; break;
  24. case '%': result = op1 % op2; break;
  25. default: break;
  26. }
  27. }
  28. bool operator<(const Equation& other) const
  29. {
  30. return this->result > other.result; // descending
  31. }
  32. };
  33.  
  34. void fillEquations(std::vector<Equation>& equations, int N)
  35. {
  36. int i;
  37. std::string line;
  38. for (i = 0; i < N; i++)
  39. {
  40. std::getline(std::cin, line);
  41. equations.push_back(Equation(line));
  42. }
  43. }
  44.  
  45. void displayEquations(const std::vector<Equation>& equations)
  46. {
  47. for (const auto& eq : equations)
  48. {
  49. std::cout << eq.equation << '\n';
  50. }
  51. }
  52.  
  53. int main()
  54. {
  55. int N;
  56. std::cin >> N;
  57. std::cin.ignore();
  58.  
  59. std::vector<Equation> equations;
  60. equations.reserve(N);
  61.  
  62. fillEquations(equations, N);
  63.  
  64. sort(equations.begin(), equations.end());
  65.  
  66. displayEquations(equations);
  67.  
  68. return 0;
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement