fork download
  1. #include<bits/stdc++.h>
  2. #define file "gtbt"
  3.  
  4. using namespace std;
  5.  
  6. #define int long long
  7. #define ld long double
  8. #define pii pair<int,int>
  9. #define pb push_back
  10. #define mp make_pair
  11. #define X first
  12. #define Y second
  13. #define lb lower_bound
  14. #define ub upper_bound
  15. #define all(x) x.begin(), x.end()
  16. #define sz(x) x.size()
  17.  
  18. const int N=1e6+5;
  19. const int NN=1e3+9;
  20. const int MOD=1e9+7;
  21. const int dx[4]={0, -1, 0, 1};
  22. const int dy[4]={-1, 0, 1, 0};
  23. const int INF=1e18;
  24. const ld eps=1e-6;
  25. const ld pi=acos(-1.0); // 48 digits
  26.  
  27. string s;
  28.  
  29. int weight(char kt) {
  30. if (kt=='^') return 3;
  31. if (kt=='*' || kt=='/') return 2;
  32. if (kt=='+' || kt=='-') return 1;
  33. return 0;
  34. }
  35.  
  36. bool isOp(char kt) {
  37. return kt=='^' || kt=='*' || kt=='/' ||
  38. kt=='+' || kt=='-';
  39. }
  40.  
  41. // Chuyen trung to sang hau to
  42. vector<string> solve(string sth) {
  43. sth='('+sth+')';
  44. stack<char> st;
  45. vector<string> res;
  46. string num="";
  47. for (int i=0;i<sz(sth);i++) {
  48. char c=sth[i];
  49. if (c==' ') continue;
  50. // cout<<c<<endl;
  51. if ('0'<=c && c<='9')
  52. num+=c;
  53. else {
  54. if (!sz(num)==0) {
  55. res.pb(num);
  56. num="";
  57. }
  58. if (c=='(')
  59. st.push(c);
  60. else if (c==')') {
  61. while (!st.empty() && st.top()!='(') {
  62. res.pb(string(1,st.top()));
  63. st.pop();
  64. }
  65. if (!st.empty())
  66. st.pop();
  67. }
  68. else if (isOp(c)) {
  69. if (c=='-' && sth[i-1]=='(' && st.top()=='(')
  70. num+='-';
  71. else {
  72. while (!st.empty() && isOp(st.top()) && // check stack ko rong hoac st.top != '('
  73. (weight(st.top())>weight(c) ||
  74. (weight(st.top())==weight(c) && c!='^'))) {// a^b^c => a^(b^c) chu khong phai (a^b)^c
  75. res.pb(string(1,st.top()));
  76. st.pop();
  77. }
  78. st.push(c);
  79. }
  80. }
  81. }
  82. }
  83. return res;
  84. }
  85.  
  86. int to_num(string s) {
  87. int res=0;
  88. int start;
  89. start=(s[0]=='-' ? 1 : 0);
  90. for (int i=start;i<sz(s);i++)
  91. res=res*10+(s[i]-'0');
  92. return (start ? -res : res);
  93. }
  94.  
  95. double calc(vector<string> v) {
  96. if (sz(v)==1) return to_num(v[0]);
  97. vector<double> st;
  98. for (int i=0;i<sz(v);i++) {
  99. string t=v[i];
  100. if (isOp(t[0]) && sz(t)==1) {
  101. double a, b;
  102. // cout<<sz(st)<<endl;
  103. // for (int x : st)
  104. // cout<<x<<" ";
  105. // cout<<endl;
  106. b=st.back();
  107. st.pop_back();
  108. a=st.back();
  109. st.pop_back();
  110. if (t[0]=='+') st.pb(a+b);
  111. if (t[0]=='-') st.pb(a-b);
  112. if (t[0]=='*') st.pb(a*b);
  113. if (t[0]=='/') st.pb(a/b);
  114. if (t[0]=='^') st.pb(pow(a,b));
  115. }
  116. else {
  117. int num=to_num(t);
  118. st.pb(num);
  119. }
  120. }
  121. return st.back();
  122. }
  123.  
  124. void process() {
  125. // Code here :V
  126. cout<<setprecision(6)<<fixed;
  127. getline(cin,s);
  128. int i=0;
  129. while (i<sz(s)) {
  130. if (s[i]==' ') s.erase(i,1);
  131. else i++;
  132. }
  133. vector<string> t=solve(s);
  134. // for (int i=0;i<sz(t);i++)
  135. // cout<<t[i]<<" ";
  136. // cout<<endl;
  137. cout<<calc(t);
  138. }
  139.  
  140. signed main() {
  141. cin.tie(0)->sync_with_stdio(0);
  142. // freopen(file".inp","r",stdin);
  143. // freopen(file".out","w",stdout);
  144. int t=1;
  145. // cin>>t;
  146. while (t--) process();
  147. return 0;
  148. }
  149.  
Success #stdin #stdout 0.01s 5284KB
stdin
(-123 * 45 - 6789) / (23 + 54) ^ 2 + 1024 / (-5 + 3)
stdout
-514.078597