Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.17 KB | None | 0 0
  1. /*
  2. * Программа вычисления выражения
  3. * использующая операции + или -
  4. *
  5. * Автор: Краснов Д.О.
  6. * Дисклеймер:
  7. *
  8. * Конечно, я люблю char*, но
  9. * у меня мало времени на это
  10. * не очень много времени :(
  11. *
  12. * */
  13.  
  14. #include <iostream>
  15. #include <stack>
  16.  
  17. using namespace std;
  18.  
  19. int main() {
  20. string a;
  21. cin>>a;
  22.  
  23. stack<char > op; // хранит операции
  24. stack<double > num;// хранит числа
  25.  
  26.  
  27. for(int i=0; i < (int)a.size(); i++) {
  28. string az;
  29. while(a[i] != '+' && a[i] != '-' && i<(int)a.size()){
  30. az+=a[i];
  31. i++;
  32. }
  33.  
  34.  
  35. if(i < (int)a.size() - 1){
  36. op.push(a[i]);
  37. }
  38. int t = atoi(az.c_str());
  39. num.push(t);
  40.  
  41. }
  42.  
  43.  
  44.  
  45.  
  46.  
  47. while(!op.empty()) {
  48.  
  49. int a = num.top();
  50. num.pop();
  51.  
  52. int b = num.top();
  53. num.pop();
  54.  
  55. int temp(0);
  56.  
  57.  
  58. if (op.top() == '+')
  59. temp = (b + a);
  60. else if (op.top() == '-')
  61. temp = (b - a);
  62.  
  63. op.pop();
  64. int sum (temp);
  65. num.push(sum);
  66.  
  67.  
  68. }
  69.  
  70.  
  71. cout<<num.top()<<endl;
  72.  
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement