fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define ll long long
  4. bool operand(string x)
  5. {
  6. if(x=="+")
  7. {
  8. return false;
  9. }
  10. else if(x=="-")
  11. {
  12. return false;
  13. }
  14. else if(x=="*")
  15. {
  16. return false;
  17. }
  18. else if(x=="/")
  19. {
  20. return false;
  21. }
  22. else if(x=="^")
  23. {
  24. return false;
  25. }
  26. else
  27. {
  28. return true;
  29. }
  30. }
  31.  
  32. int main() {
  33. string b;
  34. cin>>b;
  35. int len=0;
  36. // cout<<b.length()<<endl;
  37. for(int i=0;i<b.length();i++)
  38. {
  39. if(b[i]==',')
  40. {
  41. continue;
  42. }
  43. else
  44. {
  45. len++;
  46. }
  47. }
  48. cout<<len<<endl;
  49. string a[len];
  50. ll j=0;
  51. ll cnt=0;
  52. ll store=0;
  53. for(int i=0;i<b.length();i++)
  54. {
  55. if(b[i]==',')
  56. { store=i;
  57. //cout<<store<<endl;
  58. for(int k=j;k<i;k++)
  59. {
  60. a[cnt]+=b[k];
  61. // cout<<a[cnt]<<" ";
  62. //cout<<b[k]<<endl;
  63. }
  64. cnt++;
  65. j=i+1;
  66. // cout<<j<<endl;
  67. }
  68. }
  69. // cout<<store<<endl;
  70. for(int f=store+1;f<b.length();f++);
  71. {
  72. a[cnt]+=b[j];
  73.  
  74. }
  75. //a[cnt+1]=b[]
  76. // cout<<a[0]<<endl;
  77. // cout<<cnt<<len<<endl;
  78. for(int i=0;i<=cnt;i++)
  79. {
  80. cout<<a[i]<<" ";
  81. }
  82. cout<<endl;
  83. stack<string>s;
  84. for(int i=0;i<=cnt;i++)
  85. {
  86. if(operand(a[i])==true)
  87. { string d=(1,a[i]);
  88. // cout<<d<<endl;
  89. s.push(d);
  90. // cout<<s.top()<<endl;
  91. }
  92. else
  93. {
  94. string op1=s.top();
  95. // cout<<s.top()<<endl;
  96. s.pop();
  97. // string op1=s.top();
  98. // cout<<s.top()<<endl;
  99. if(s.empty())
  100. { // cout<<"d"<<endl;
  101. //break;
  102. string e="("+op1+a[i]+")";
  103. s.push(e);
  104. }
  105. else
  106. {
  107. string op2=s.top();
  108. s.pop();
  109. string e="("+op2+a[i]+op1+")";
  110. // cout<<"("+op2+a[i]+op1+")"<<endl;
  111. s.push(e);
  112. }
  113.  
  114. // cout<<op2<<" "<<op1<<endl;
  115. // cout<<op2<<endl;
  116. }
  117. }
  118. cout<<s.top()<<endl;
  119. stack<double>s1;
  120. for(int i=0;i<=cnt;i++)
  121. {
  122. if(operand(a[i]))
  123. {
  124. s1.push(stoi(a[i]));
  125. }
  126. else
  127. {
  128. if(a[i]=="+")
  129. {
  130. int val1=s1.top();
  131. s1.pop();
  132. int val2=s1.top();
  133. s1.pop();
  134. s1.push(val2+val1);
  135. }
  136. else if(a[i]=="-")
  137. {
  138. int val1=s1.top();
  139. s1.pop();
  140. int val2=s1.top();
  141. s1.pop();
  142. s1.push(val2-val1);
  143. }
  144. else if(a[i]=="*")
  145. {
  146. int val1=s1.top();
  147. s1.pop();
  148. int val2=s1.top();
  149. s1.pop();
  150. s1.push(val2*val1);
  151. }
  152. else if(a[i]=="/")
  153. {
  154. double val1=s1.top();
  155. s1.pop();
  156. double val2=s1.top();
  157. s1.pop();
  158. cout<<setprecision(3);
  159. double val3=val2/val1;
  160. // cout<<val3<<endl;
  161. // double rounded = (int)(val3 * 100.0)/100.0;
  162. // cout<<rounded<<endl;
  163. s1.push(val3);
  164. }
  165. else if(a[i]=="^")
  166. {
  167. int val1=s1.top();
  168. s1.pop();
  169. s1.push(val1*val1);
  170. }
  171.  
  172. }
  173. }
  174. cout<<s1.top()<<endl;
  175.  
  176.  
  177. }
Success #stdin #stdout 0s 5492KB
stdin
 25,9,6,*,-,3,/
stdout
8
25 9 6 * - 3 / 
((25-(9*6))/3)
-9.67