daily pastebin goal
28%
SHARE
TWEET

Untitled

a guest Mar 20th, 2019 70 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5.  
  6.  
  7. struct Stack {
  8.     char znak;
  9.     double  number;
  10.     Stack* next;
  11. };
  12.  
  13. Stack *top1 = NULL;
  14. Stack *top2 = NULL;
  15.  
  16. int priora (char a)
  17. {
  18.     if ( a == '*' || a == '/')
  19.     {
  20.         return 2;
  21.     }
  22.     if ( a == '+' || a == '-')
  23.     {
  24.         return 1;
  25.     }
  26.     else
  27.     {
  28.         return 0;
  29.     }
  30. }
  31.  
  32. Stack pop (Stack * &top)
  33. {
  34.     Stack p = * top;
  35.     top = (*top).next;
  36.     return p;
  37. }
  38.  
  39. void push (Stack * & top, double num, char zn)
  40. {
  41.     Stack * p = (Stack *) malloc (sizeof(Stack));
  42.     (*p).znak = zn;
  43.     (*p).number = num;
  44.     (*p).next = top;
  45.     top = p;
  46.     return;
  47. }
  48. void apply(char zn, Stack * & pom){
  49.  
  50.     double p1 = (pop(pom)).number;
  51.     double p2 = (pop(pom)).number;
  52.     if (zn == '+') push(pom, p1 + p2, '@');
  53.     if (zn == '*') push(pom, p1 * p2, '@');
  54.     if (zn == '-') push(pom, p2 - p1, '@');
  55.     if (zn == '/') push(pom, p2 / p1, '@');
  56. }
  57.  
  58. void calculate(string s, string s2) {
  59.     if (s2[0] == '-') {
  60.         s += '0';
  61.     }
  62.     for (int i = 0; i < s2.size(); i++)
  63.     {
  64.         if (i == 0 || s2[i - 1] != '(')
  65.     {
  66.         s += s2[i];
  67.     }
  68.     else
  69.     {
  70.         if (s2[i] == '-')
  71.         {
  72.             s += "0-";
  73.         }
  74.             else
  75.             {
  76.                 s += s2[i];
  77.             }
  78.     }
  79. }
  80.  
  81.     int k = s.size();
  82.     int  temp;
  83.     for (int i = 0; i < k; i++) {
  84.  
  85.         if (s[i] <= '9' && s[i] >= '0') {
  86.             temp = 0.0;
  87.             while (s[i] >= '0' && s[i] <= '9') {
  88.                 temp = temp * 10 + (s[i] - '0');
  89.                 i++;
  90.             }
  91.             push(top1, temp, '@');
  92.             temp = 0;
  93.         }
  94.  
  95.         if (s[i] == '(') {
  96.             push(top2, 0.0, s[i]);
  97.         }
  98.  
  99.         if (s[i] == ')') {
  100.             while ( (*top2).znak != '(')
  101.                 apply(pop(top2).znak,top1);
  102.             pop(top2);
  103.         }
  104.         if (s[i] == '+' || s[i] == '-' || s[i] == '*' || s[i] == '/' ) {
  105.             while ((top2 != NULL) && (priora(s[i]) <= priora((* top2).znak)))
  106.                 apply(pop(top2).znak,top1);
  107.             push(top2, 0, s[i]);
  108.         }
  109.  
  110.  
  111.     }
  112.     while (top2 != NULL) {
  113.         apply(pop(top2).znak,top1);
  114.     }
  115. }
  116. int main()
  117. {
  118.     freopen("input.txt","r",stdin);
  119.     freopen("output.txt","w",stdout);
  120.     string s, s2;
  121.     cin >> s2;
  122.     calculate(s, s2);
  123.     cout << fixed << setprecision(6) << pop(top1).number << endl;
  124.  
  125.  
  126. return 0;
  127.  
  128. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top