-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path8.cpp
85 lines (82 loc) · 1.72 KB
/
8.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
/*
逆波兰式
设置两个栈,一个符号栈,一个变量栈
*/
#include<iostream>
#define MAX_LEN 10001
using namespace std;
class stack{
private:
char st[MAX_LEN];
public:
int point;
bool push(char c);
bool pop(char *buf);
bool get(char *buf);
stack();
};
stack::stack(){
this->point = -1;
}
bool stack::pop(char *buf){
if(this->point < 0) return false;
*buf = this->st[this->point];
this->point--;
return true;
}
bool stack::push(char c){
if(this->point +1 >MAX_LEN) return false;
this->point++;
this->st[this->point] = c;
return true;
}
bool stack::get(char *buf){
if(this->point < 0) return false;
*buf = this->st[this->point];
return true;
}
void func(){
stack op;
//stack pra;
char c;
char buf;
//EOF noj能过,'\n'noj会显示超时
while((c = getchar()) != EOF && c != '\n'){
switch(c){
case '(':
case '*':
case '/':
op.push(c);
break;
case '+':
case '-':
while(op.get(&buf) && (buf == '*' || buf == '/')){
op.pop(&buf);
cout<<buf;
}
op.push(c);
break;
case ')':
while(op.get(&buf) && buf != '(' ){
op.pop(&buf);
cout<<buf;
}
op.pop(&buf);
break;
default: //变量
cout<<c;
break;
}
}
while(op.point != -1){
op.pop(&buf);
cout<<buf;
}
cout<<endl;
return;
}
int main()
{
func();
return 0;
}