Skip to content

Commit 618cc23

Browse files
authored
Merge pull request #174 from adiXcodr/master
A calculator that converts Infix to Prefix, Postfix and also calculat…
2 parents 837691d + c6f42c9 commit 618cc23

File tree

1 file changed

+134
-0
lines changed

1 file changed

+134
-0
lines changed
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
2+
#include <bits/stdc++.h>
3+
#include<string>
4+
#include<stack>
5+
using namespace std;
6+
7+
8+
string removeSpaces(string s)
9+
{
10+
s.erase(remove(s.begin(), s.end(), ' '), s.end());
11+
return s;
12+
}
13+
14+
bool isOperator(char c)
15+
{
16+
return (!isalpha(c) && !isdigit(c));
17+
}
18+
19+
int getPriority(char C)
20+
{
21+
if (C == '-' || C == '+')
22+
return 1;
23+
else if (C == '*' || C == '/')
24+
return 2;
25+
else if (C == '^')
26+
return 3;
27+
return 0;
28+
}
29+
30+
string infixToPostfix(string infix)
31+
{
32+
infix = '(' + infix + ')';
33+
int l = infix.size();
34+
stack<char> char_stack;
35+
string output;
36+
37+
for (int i = 0; i < l; i++) {
38+
if (isalpha(infix[i]) || isdigit(infix[i]))
39+
output += infix[i];
40+
else if (infix[i] == '(')
41+
char_stack.push('(');
42+
else if (infix[i] == ')') {
43+
while (char_stack.top() != '(') {
44+
output += char_stack.top();
45+
char_stack.pop();
46+
}
47+
char_stack.pop();
48+
}
49+
else {
50+
51+
if (isOperator(char_stack.top())) {
52+
while (getPriority(infix[i])
53+
<= getPriority(char_stack.top())) {
54+
output += char_stack.top();
55+
char_stack.pop();
56+
}
57+
char_stack.push(infix[i]);
58+
}
59+
}
60+
}
61+
return output;
62+
}
63+
64+
string infixToPrefix(string infix)
65+
{
66+
int l = infix.size();
67+
reverse(infix.begin(), infix.end());
68+
for (int i = 0; i < l; i++) {
69+
if (infix[i] == '(') {
70+
infix[i] = ')';
71+
i++;
72+
}
73+
else if (infix[i] == ')') {
74+
infix[i] = '(';
75+
i++;
76+
}
77+
}
78+
79+
string prefix = infixToPostfix(infix);
80+
reverse(prefix.begin(), prefix.end());
81+
return prefix;
82+
}
83+
84+
85+
int postfixEval(string exp) {
86+
stack <int> x;
87+
for (int i = 0; exp[i]; ++i)
88+
{
89+
if (isdigit(exp[i]))
90+
x.push(exp[i] - '0');
91+
92+
else
93+
{
94+
int val1 = x.top();
95+
x.pop();
96+
int val2 = x.top();
97+
x.pop();
98+
switch (exp[i])
99+
{
100+
case '+': x.push( val2 + val1); break;
101+
case '-': x.push( val2 - val1); break;
102+
case '*': x.push(val2 * val1); break;
103+
case '/': x.push( val2/val1); break;
104+
}
105+
}
106+
}
107+
return x.top();
108+
}
109+
110+
111+
112+
int main(int argc, char *argv[])
113+
{
114+
int value;
115+
string s;
116+
for (int i=1;i<argc;i++){
117+
s.append(argv[i]).append(" ");
118+
}
119+
if(argc=1){
120+
cout<<"No Arguments Passed\nEnter expression here\n";
121+
getline(cin,s);
122+
}
123+
if(s.size()>=1){
124+
s=removeSpaces(s);
125+
cout<<"Postfix : "<<infixToPostfix(s)<<endl;
126+
cout<<"Prefix : "<<infixToPrefix(s)<<endl;
127+
s=infixToPostfix(s);
128+
value=postfixEval(s);
129+
cout<<"Value : "<<value<<endl;
130+
}
131+
else
132+
cout<<"Value : 0"<<endl;
133+
return 0;
134+
}

0 commit comments

Comments
 (0)