-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinfix_to_postfix.cpp
More file actions
90 lines (84 loc) · 2.11 KB
/
infix_to_postfix.cpp
File metadata and controls
90 lines (84 loc) · 2.11 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
#include <iostream>
#include <stack>
using namespace std;
int pr_onstack(char kr) //it returns relative precedence
//of operators which is on stack
{
if (kr == '^')
return 5;
else if (kr == '*' || kr == '/')
return 4;
else if (kr == '-' || kr == '+')
return 1;
else
return 0;
}
int pr_onstirng(char kr) //it returns relative precedence
//of operators which is on string(equation)
{
if (kr == '^')
return 6;
else if (kr == '*' || kr == '/')
return 3;
else if (kr == '-' || kr == '+')
return 2;
else
return 0;
}
string infix_to_postfix(string s) //function for convert infix equation to postfix equation
//string s for take input (infix equation)
{
string ans; //ans string for store and return postfix equation
stack<char> sta; //sta stack store operators for while
int k = s.length();
for (int i = 0; i < k; i++)
{
char ccc = s[i];
if (ccc == '^' || ccc == '*' || ccc == '/' || ccc == '+' || ccc == '-')
{
if ((sta.empty() != 1) && (pr_onstack(sta.top())) > (pr_onstirng(ccc)))
{
ans += sta.top();
ans +=',';
sta.pop();
}
sta.push(ccc);
}
else if (ccc == '(')
{
sta.push(ccc);
}
else if (ccc == ')')
{
while (sta.top() != '(')
{
ans += sta.top();
ans +=',';
sta.pop();
}
sta.pop();
}
else if(ccc=='.')
{
ans +='.';
}
else
{
ans += ccc;
if (!(s[i+1] - 48 <= 9 && s[i+1] - 48 >= 0)&& s[i+1]!='.')
ans +=',';// , for saperate each operand
}
}
while (!sta.empty())
{
ans += sta.top();
ans +=','; // , for saperate each operator
sta.pop();
}
return ans; //ans=> postfix equation
}
// int main()
// {
// string s=infix_to_postfix("0.001^0.5");
// cout<<"postix conversion is"<<s<<endl;
// }