Advertisement
Guest User

Addition And Subtraction

a guest
Apr 8th, 2020
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.69 KB | None | 0 0
  1.     std::string Line;
  2.     std::string tmp = "";
  3.     std::vector<int> numerics;
  4.     std::vector<int> ops;
  5.  
  6.     const int OperatorAddition = 5;
  7.     const int OperatorSubtraction = 10;
  8.    
  9.     int opsCount = 0;
  10.     int opsPos = 0;
  11.     int result = 0;
  12.    
  13.     bool isNum = false;
  14.  
  15.  
  16.     getline(cin, Line);
  17.  
  18.     for(int i = 0;i<Line.length();i++)
  19.     {
  20.         if(Line[i] == 43)//ascii +
  21.         {
  22.             ops.push_back(OperatorAddition);
  23.             //Code reused, consider a function instead.
  24.             if(isNum == true)
  25.             {
  26.                 int r = atoi(tmp.c_str());
  27.                 numerics.push_back(r);
  28.                 tmp = "";
  29.                 isNum = false;
  30.             }
  31.         }
  32.         if(Line[i] == 45)//ascii -
  33.         {
  34.             ops.push_back(OperatorSubtraction);
  35.             //Code reused, consider a function instead.
  36.             if(isNum == true)
  37.             {
  38.                 int r = atoi(tmp.c_str());
  39.                 numerics.push_back(r);
  40.                 tmp = "";
  41.                 isNum = false;
  42.             }
  43.         }
  44.         else if(Line[i]>=48&&Line[i]<=57)
  45.         {
  46.             isNum = true;
  47.             tmp += Line[i];
  48.         }
  49.         else
  50.         {
  51.             if(isNum == true)
  52.             {
  53.                 int r = atoi(tmp.c_str());
  54.                 numerics.push_back(r);
  55.                 tmp = "";
  56.                 isNum = false;
  57.             }
  58.         }
  59.     }
  60.    
  61.     if(isNum == true)
  62.     {
  63.         int r = atoi(tmp.c_str());
  64.         numerics.push_back(r);
  65.     }
  66.    
  67.     std::cout << "\n\n";
  68.    
  69.     for(int i =0; i < numerics.size(); i++)
  70.     {
  71.         std::cout << "Variable " << i << " = " << numerics[i] << "\n";
  72.     }
  73.  
  74.     for(int i =0; i < ops.size(); i++)
  75.     {
  76.         std::cout << "operator " << i << " = ";
  77.        
  78.         if(ops[i] == OperatorAddition)
  79.         {
  80.             std::cout << "+\n";
  81.         }
  82.         else if(ops[i] == OperatorSubtraction)
  83.         {
  84.             std::cout << "-\n";
  85.         }
  86.     }
  87.     std::cout << "\n\n";
  88.    
  89.     opsCount = ops.size();
  90.     result = numerics[0];
  91.    
  92.     for(int i =1; i < numerics.size(); i++)
  93.     {
  94.         if(opsCount !=0)
  95.         {
  96.             if(ops[opsPos] == OperatorAddition)
  97.             {
  98.                 result+= numerics[i];
  99.             }
  100.             else if(ops[opsPos] == OperatorSubtraction)
  101.             {
  102.                 result-= numerics[i];
  103.             }
  104.             else
  105.             {
  106.                 cout << "Unrecognized op..";
  107.             }
  108.             opsCount--;
  109.             opsPos++;
  110.         }
  111.         else cout << "Handle this condition..";
  112.     }
  113.     cout << "Answer = " << result;
  114.     cin.get();
  115.     return 0;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement