-
Notifications
You must be signed in to change notification settings - Fork 0
/
expression.cpp
94 lines (86 loc) · 1.74 KB
/
expression.cpp
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
#include <iostream>
#include <cstdlib>
#include <stack>
#include <cstring>
using namespace std;
stack<char> s;
char pop2()
{
if (s.empty())
{
return 0;
}
char a = s.top();
s.pop();
return a;
}
void reverse(char* str) {
int n = strlen(str);
for (int i = 0; i < n / 2; i++) {
char temp = str[i];
str[i] = str[n - i - 1];
str[n - i - 1] = temp;
}
}
void inf_post(char *inf, char *post)
{
int i = 0, j = 0;
char token, x;
for (i = 0; inf[i] != 0; i++)
{
token = inf[i];
if (token == '(')
{
s.push(token);
}
else if (isalnum(token))
{
post[j++] = token;
}
else if (token == ')')
{
while ((x = pop2()) != '\0')
{
if (x != ')' && x != '(')
post[j++] = x;
}
}
else
{
s.push(token);
}
}
}
void inf_pref(char *inf, char *pref)
{
int len = strlen(inf);
char revInf[20];
strcpy(revInf, inf);
reverse(revInf);
for (int i = 0; i < len; i++)
{
if (revInf[i] == ')')
revInf[i] = '(';
else if (revInf[i] == '(')
revInf[i] = ')';
}
char post[20] = {};
inf_post(revInf, post);
int postLen = strlen(post);
for (int i = 0; i < postLen; i++)
{
pref[i] = post[postLen - i - 1];
}
pref[postLen] = '\0';
}
int main()
{
char inf[20] = {"(a+b)^c/d*(e-f)"};
char pref[20] = {};
char post[20] = {};
inf_pref(inf, pref);
for (char x : pref)
{
cout << x << " ";
}
}