Advertisement
AdamTheGreat

Untitled

Sep 2nd, 2022
31
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.23 KB | None | 0 0
  1. #include <iostream>
  2. #include <stack>
  3. #include <string>
  4. #include <cmath>
  5. #include <iomanip>
  6. using namespace std;
  7.  
  8. double doit(double a, double b, char o) {
  9.     if (o == '+') {
  10.         return a+b;
  11.     } else if (o == '-') {
  12.         return a-b;
  13.     } else if (o == '*') {
  14.         return a*b;
  15.     } else if (o == '/') {
  16.         return a/float(b);
  17.     } else if (o == '^') {
  18.         double result = 1;
  19.         for (int i = 0; i < b; i++) {
  20.             result*=a;
  21.         }
  22.         return result;
  23.     }
  24.     return -1;
  25. }
  26.  
  27. double calculator(string expression) {
  28.     stack<char> operationStack;
  29.     stack<double> numberStack;
  30.     for (int i = 0; i < expression.length(); i++) {
  31.         char current = expression[i];
  32.         if (current >= '0' && current <= '9') {
  33.             int n = 0;
  34.             int nlength = 0;
  35.             while (i < expression.length() && expression[i] >= '0' && expression[i] <= '9') {
  36.                 n*=10;
  37.                 n+=expression[i+nlength]-'0';
  38.                 i++;
  39.             }
  40.             i--;
  41.             numberStack.push(n);
  42.             continue;
  43.         }
  44.         else {
  45.             if (current == ')') {
  46.                 int index = i-1;
  47.                 int paras = 1;
  48.                 while (expression[index] != '(' && paras != 0) {
  49.                     if (expression[index] == ')') {
  50.                         paras++;
  51.                     } else if (expression[index] == '(') {
  52.                         paras--;
  53.                     }
  54.                     index--;
  55.                     cout << index << endl;
  56.                 }
  57.                 operationStack.pop();
  58.                 while (operationStack.top() != '(') {
  59.                     numberStack.pop();
  60.                     operationStack.pop();
  61.                 }
  62.                 numberStack.pop();
  63.                 operationStack.pop();
  64.                 string subexpression = expression.substr(index+1, i-1-index);
  65.                 cout << subexpression << endl;
  66.                 double answer = calculator(subexpression);
  67.                 cout << answer << endl;
  68.                 numberStack.push(answer);
  69.             } else if (current == '(') {
  70.                 operationStack.push(current);
  71.             } else if (current == '^') {
  72.                 if (operationStack.empty()) {
  73.                     operationStack.push(current);
  74.                 } else if (operationStack.top() == '+' || operationStack.top() == '-' || operationStack.top() == '*' || operationStack.top() == '/' || operationStack.top() == '^' || operationStack.top() == '(') {
  75.                     operationStack.push(current);
  76.                 } else if (operationStack.top() == ')') {
  77.                     double a = numberStack.top();
  78.                     numberStack.pop();
  79.                     double b = numberStack.top();
  80.                     numberStack.pop();
  81.                     double result = doit(b, a, operationStack.top());
  82.                     numberStack.push(result);
  83.                     operationStack.pop();
  84.                 }
  85.             } else if (current == '*' || current == '/') {
  86.                 if (operationStack.empty()) {
  87.                     operationStack.push(current);
  88.                 } else if (operationStack.top() == '+' || operationStack.top() == '-' || operationStack.top() == '*' || operationStack.top() == '/' || operationStack.top() == '^' || operationStack.top() == '(') {
  89.                     operationStack.push(current);
  90.                 } else if (operationStack.top() == '^' || operationStack.top() == ')') {
  91.                     double a = numberStack.top();
  92.                     numberStack.pop();
  93.                     double b = numberStack.top();
  94.                     numberStack.pop();
  95.                     double result = doit(b, a, operationStack.top());
  96.                     numberStack.push(result);
  97.                     operationStack.pop();
  98.                     operationStack.push(current);
  99.                 }
  100.             } else if (current == '+' || current == '-') {
  101.                 if (operationStack.empty()) {
  102.                     operationStack.push(current);
  103.                 } else if (operationStack.top() == '+' || operationStack.top() == '-' || operationStack.top() == '(') {
  104.                     operationStack.push(current);
  105.                 } else if (operationStack.top() == '*' || operationStack.top() == '/' || operationStack.top() == '^' || operationStack.top() == ')') {
  106.                     double a = numberStack.top(); numberStack.pop();
  107.                     double b = numberStack.top(); numberStack.pop();
  108.                     char o = operationStack.top(); operationStack.pop();
  109.                     numberStack.push(doit(b, a, o));
  110.                     operationStack.push(current);
  111.                 }
  112.             }
  113.         }
  114.     }
  115.     while (!operationStack.empty()) {
  116.         double a = numberStack.top();
  117.         numberStack.pop();
  118.         double b = numberStack.top();
  119.         numberStack.pop();
  120.         numberStack.push(doit(b, a, operationStack.top()));
  121.         operationStack.pop();
  122.     }
  123.     return numberStack.top();
  124. }
  125.  
  126. int main(void) {
  127.     string expression;
  128.     getline(cin, expression);
  129.     double final;
  130.     final = calculator(expression);
  131.     cout << fixed << setprecision(2) << final << endl;
  132.     return final;
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement