Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include<iostream>
- #include<string>
- #include<iomanip>
- #include<stdio.h>
- #include<cmath>
- using namespace std;
- #include "list.cpp"
- #include "stack.h"
- double sum(double rhs, double lhs);
- double subtract(double rhs, double lhs);
- double mult(double rhs, double lhs);
- double quotient(double rhs, double lhs);
- double power(double rhs, double lhs);
- int main(){
- Stack<double> astack;
- double d, rhs, lhs, res;
- char ch;
- while(cin.peek() != EOF) {
- cin >> ch;
- if(isspace(ch))
- cin.ignore();
- if(isdigit(ch) || ch == '.') {
- d = ch;
- astack.push(d);
- }
- switch(ch) {
- case '+':
- rhs = astack.pop();
- lhs = astack.pop();
- astack.push(sum(rhs, lhs));
- break;
- case '-':
- rhs = astack.pop();
- lhs = astack.pop();
- astack.push(subtract(rhs, lhs));
- break;
- case '*':
- rhs = astack.pop();
- lhs = astack.pop();
- astack.push(mult(rhs, lhs));
- break;
- case '/':
- rhs = astack.pop();
- lhs = astack.pop();
- astack.push(quotient(rhs, lhs));
- break;
- case '^':
- rhs = astack.pop();
- lhs = astack.pop();
- astack.push(power(rhs, lhs));
- break;
- default:
- cerr << "Error. Invalid expression" << endl;
- }
- }
- res = astack.pop();
- cout << fixed << showpoint << setprecision(6) << res << endl;
- return 0;
- }//main
- double sum(double rhs, double lhs) {
- return lhs + rhs;
- }
- double subtract(double rhs, double lhs) {
- return lhs - rhs;
- }
- double mult(double rhs, double lhs) {
- return lhs * rhs;
- }
- double quotient(double rhs, double lhs) {
- return lhs / rhs;
- }
- double power(double rhs, double lhs) {
- return pow(lhs, rhs);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement