-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPostfixnotation.cpp
More file actions
145 lines (119 loc) · 2.65 KB
/
Postfixnotation.cpp
File metadata and controls
145 lines (119 loc) · 2.65 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include <iostream>
#include <fstream>
#include <string>
//후위수식
//전위수식 중간고사다.!
using namespace std;
class Stack{
private:
int top;
int stacksize;
char *stack;
char oper[2][6];
public:
Stack();
void push(char n);
char pop();
void display();
int empty();
int isfull();
int operator1(char n);
char getTop();
};
Stack::Stack(){
int top =-1;
int stacksize = 10;
stack = new char[stacksize];
oper[0][0] =')';
oper[1][0] ='3';
oper[0][1] ='*';
oper[1][1] ='2';
oper[0][2] = '/';
oper[1][2] ='2';
oper[0][3] = '+';
oper[1][3] = '1';
oper[0][4] ='-';
oper[1][4] = '1';
oper[0][5] ='(';
oper[1][5] = '0';
}
void Stack::push(char n){
stack[++top] = n;
}
char Stack::pop(){
return stack[top--];
}
void Stack::display(){
for (int i = 0; i<top; i++){
cout<<stack[i]<<endl;
}
}
int Stack::empty(){
if ( top == -1){
return 1;
}
else
{
return 0;
}
}
int Stack::isfull(){
if(top == stacksize){
return 0;
}
else
{
return 1;
}
}
int Stack::operator1(char exp){
for ( int i = 0; i<6; i++){
if (oper[0][i]==exp){
return oper[1][i] -'0';
}
}
return -1;
}
char Stack::getTop(){
return stack[top];
}
int main(){
int priority;
int topvalue;
ifstream infile;
infile.open("hw2.txt");
string str;
Stack s;
for (int i = 0; i<4; i++ ){
infile >> str;
for (int j = 0; j<str.size(); j++){
topvalue =s.getTop();
if (str[j] == '+' || str[j] == '-' || str[j] == '*' || str[j] == '(' || str[j] =='/' || str[j] ==')' ){
if (str[j] == '('){
s.push(str[j]);
}
else if (str[j] == ')'){
while(s.getTop() != '('){
cout << s.pop() << ' ';
}
s.pop();
}
else if(s.empty() || s.operator1(str[j]) > s.operator1(topvalue)){
s.push(str[j]);
}
else if(s.operator1(str[j]) <= s.operator1(topvalue)){
cout << s.pop() << ' ';
s.push(str[j]);
}
}
else if(s.operator1(str[j])== -1){
cout <<str[j];
}
}
while(!s.empty())
{
cout << s.pop()<< ' ';
}
cout << endl;
}
}