-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalculator.cpp
More file actions
122 lines (115 loc) · 1.93 KB
/
calculator.cpp
File metadata and controls
122 lines (115 loc) · 1.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include <iostream>
#include <stack>
#include <math.h>
#include <map>
using namespace std;
// this program calcute the expression with non-negative intergers and +,-,/,*,^
int findNum(string s,int i){
int j;
for (j=i;j<s.size();j++){
if (s[j]<'0' || s[j]>'9')
break;
}
return j-1;
}
int cal(int a,int b, char c){
switch (c){
case '+': return a+b;
case '-': return a-b;
case '*': return a*b;
case '/': return a/b;
case '^': return pow(a,b);
}
}
int toNum(string s){
int res=0;
int n=s.size();
for (int i=0;i<n;i++){
res+=((s[i]-48)*pow(10,n-i-1));
}
return res;
}
int order(char c){
if (c=='+'||c=='-')
return 1;
if (c=='*'||c=='/')
return 2;
if (c=='^')
return 3;
return 0;
}
int cal(string s){
int i=0,k;
stack<char> st;
stack<int> st1;
string res="";
while (i<s.size()){
if (s[i]>='0' && s[i]<='9'){
k=findNum(s,i);
res+=(s.substr(i,k-i+1)+" ");
i=k+1;
}
else{
if(s[i]!='('&& s[i]!=')'){
if (st.empty()){
st.push(s[i]);
}
else{
while (!st.empty()&&((order(s[i])<order(st.top()))||(order(s[i])==order(st.top())&&s[i]!='^'))){
res+=st.top();
st.pop();
}
st.push(s[i]);
}
}
else{
if (s[i]=='(')
st.push(s[i]);
else{
while(st.top()!='('){
res+=st.top();
st.pop();
}
st.pop();
}
}
i++;
}
}
while(!st.empty()){
res+=st.top();
st.pop();
}
//cout<<res<<"\n"; // convert to postfix notation
i=0;
while(i<res.size()){
if (res[i]>='0' && res[i]<='9'){
k=findNum(res,i);
st1.push(toNum(res.substr(i,k-i+1)));
i=k+2;
}
else{
int b=st1.top();
st1.pop();
int a=st1.top();
st1.pop();
st1.push(cal(a,b,res[i]));
i++;
}
}
return st1.top();
}
int main(){
int i=0,k;
stack<char> st;
stack<int> st1;
string s,res;
res="";
cout<<"CALCULATOR CONSOLE PROGRAM"<<endl;
while(1){
cout<<"Insert the expression: ";
getline(cin,s);
cout<<cal(s)<<endl;
}
return 0;
}