-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata 11.cpp
More file actions
130 lines (122 loc) · 2.13 KB
/
data 11.cpp
File metadata and controls
130 lines (122 loc) · 2.13 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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct stackop{
char a[2900];
int top;
}stackop;
int initial(stackop *sta){
for(int i =0 ;i<2000;i++) sta->a[i] = 0;
sta->top = 1;
return 0;
}
int pops(stackop *sta){
if(sta->top == 0){
return 0;
}
else{
sta->top--;
return 1;
}
}
int pushs(stackop *sta , char x){
if(sta->top == 1999){
return 0;
}
else{
sta->a[sta->top] = x;
sta->top++;
return 1;
}
}
int comp(char a, char b){
if(a == '#') a = 0;
else if(a == '(') a = 1;
else if(a == '^') a = 6;
else if(a == '*' || a == '/' || a == '%') a = 5;
else if(a == '+' || a == '-') a = 3;
else if(a == ')') a = 8;
if(b == '#') b = 0;
else if(b == '(') b = 8;
else if(b == '^') b = 7;
else if(b == '*' || b == '/' || b == '%') b = 4;
else if(b == '+' || b == '-') b = 2;
else if(b == ')') b = 1;
return a-b;
}
int main(void){
int n=0;
scanf("%d",&n);
getchar();
while(n--){
char tmp[2000]={0};
int temp=0;
scanf("%s",tmp);
for(int i=0;i<strlen(tmp);i++){
if(i == 0 && tmp[0] == '-') tmp[0]=-1;
if(tmp[i]=='-' && tmp[i-1] == '(') tmp[i] = -1;
}
char str[2000] = {0};
int len = 0;
stackop *sta = (stackop*)malloc(sizeof(stackop));
initial(sta);
pushs(sta, '#');
char c;
int count = 0;
while(1){
c=tmp[temp];
temp++;
if(c == '#'){
while(1){
if(sta->top == 0) break;
if(sta->a[sta->top - 1] != '#'){
str[len] = sta->a[sta->top - 1];
len++;
str[len]='\0';
}
sta->top--;
}
printf("%s\n",str);
break;
}
else if(c == -1){
printf("-");
continue;
}
else if(c >= '0' && c <= '9'){
str[len]=c;
len++;
continue;
}
else if(c >= 'a' && c <= 'z'){
str[len]=c;
len++;
continue;
}
else if(c >= 'A' && c <= 'Z'){
str[len]=c;
len++;
continue;
}
else if(c == ' '){
continue;
}
else{
g: ;
if(comp(sta->a[sta->top - 1] , c) > 0){
str[len] = sta->a[sta->top - 1];
len++;
sta->top--;
goto g;
}
else if(comp(sta->a[sta->top - 1] , c) < 0){
pushs(sta,c);
}
else{
sta->top--;
}
continue;
}
}
}
}