Advertisement
mitakvd

3. Algebra Helper

Nov 17th, 2019
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.15 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <sstream>
  5. #include <algorithm>  
  6.  
  7. using namespace std;
  8. bool sortInRev(const pair<int, string>& a, const pair<int, string>& b)
  9. {
  10.     return (a.first > b.first);
  11. }
  12.  
  13. int main()
  14. {
  15.     int numberOfEquations;
  16.     cin >> numberOfEquations;
  17.     cin.ignore();
  18.     vector<pair<double, string>> memory;
  19.  
  20.     for (int i = 0; i < numberOfEquations; i++)
  21.     {
  22.         string equation;
  23.         getline(cin, equation);
  24.  
  25.         double first = 0.0;
  26.         string operation;
  27.         double second = 0.0;
  28.         istringstream istr(equation);
  29.         istr >> first >> operation >> second;
  30.  
  31.         double result = 0.0;
  32.         if (operation == "+")
  33.         {
  34.             result = first + second;
  35.         }
  36.         else if (operation == "-")
  37.         {
  38.             result = first - second;
  39.         }
  40.         else if (operation == "*")
  41.         {
  42.             result = first * second;
  43.         }
  44.         else if (operation == "/")
  45.         {
  46.             result = first / second;
  47.         }
  48.         else
  49.         {
  50.             result = int(first) % int(second);
  51.         }
  52.  
  53.         memory.push_back(pair<double, string>(result, equation));
  54.     }
  55.  
  56.     sort(memory.begin(), memory.end(), sortInRev);
  57.  
  58.     for (int i = 0; i < memory.size(); i++)
  59.     {
  60.         cout << memory[i].second << endl;
  61.     }
  62.     return 0;
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement