Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Программа вычисления выражения
- * использующая операции + или -
- *
- * Автор: Краснов Д.О.
- * Дисклеймер:
- *
- * Конечно, я люблю char*, но
- * у меня мало времени на это
- * не очень много времени :(
- *
- * */
- #include <iostream>
- #include <stack>
- using namespace std;
- int main() {
- string a;
- cin>>a;
- stack<char > op; // хранит операции
- stack<double > num;// хранит числа
- for(int i=0; i < (int)a.size(); i++) {
- string az;
- while(a[i] != '+' && a[i] != '-' && i<(int)a.size()){
- az+=a[i];
- i++;
- }
- if(i < (int)a.size() - 1){
- op.push(a[i]);
- }
- int t = atoi(az.c_str());
- num.push(t);
- }
- while(!op.empty()) {
- int a = num.top();
- num.pop();
- int b = num.top();
- num.pop();
- int temp(0);
- if (op.top() == '+')
- temp = (b + a);
- else if (op.top() == '-')
- temp = (b - a);
- op.pop();
- int sum (temp);
- num.push(sum);
- }
- cout<<num.top()<<endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement