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 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
| #include <stdio.h> #include <stdlib.h> #include <string.h> #define stack_size 100 #define stackzengsize 10 char str[1000]; typedef struct {
int *base; int *top; int stacksize; }sqstack; char yunsuanfu[7] = {'+','-','*','/','(',')','#'}; unsigned char prior[7][7] = { {'>','>','<','<','<','>','>'}, {'>','>','<','<','<','>','>'}, {'>','>','>','>','<','>','>'}, {'>','>','>','>','<','>','>'}, {'<','<','<','<','<','=',' '}, {'<','<','<','<','<',' ','>'}, {'<','<','<','<','<',' ','='}}; int chushihua(sqstack *s) { s->base = (int *)malloc(sizeof(int) * stack_size); if( !s->base) //分配失败 { exit(0); } s->top = s->base; s->stacksize = stack_size; return 1; } int push(sqstack *s,int x) //入栈 { //如果栈已满,在申请·额外·空间 if((s->top - s->base) >= s->stacksize) { s->base = (int *)realloc(s->base,(s->stacksize + stackzengsize)*sizeof(int)); if(!s->base) { exit(0); } s->stacksize = s->stacksize + stackzengsize; } *(s->top)++ = x;
return 1; } int gettop(sqstack *s) //获取栈顶元素 { if(s->base == s->top) { return 0; } int x = *(s->top - 1); return x; } int yunsuan_fu(char ch,char str[]) { int i; for(i = 0; ch != str[i] ; i++) { } if(i < 7) { return 1; }
return 0; } void lianjie(char *s1,char *s2) //把字符串 2 连接到 1 的后面 { int i = 0; int j = 0; while(s1[i] != '\0') { i++; } while(s2[j] != '\0') { s1[i++] = s2[j++]; }
s1[i] = '\0'; } int AToi(char *str) { int i = 0; int x = 0; while(str[i] != '\0') { x = x * 10 + str[i] -'0'; i++; }
return x; } int youxianji(char x,char y) { int i = 0; int j = 0; while(x != yunsuanfu[i]) { i++; } while(y != yunsuanfu[j]) { j++; } return prior[i][j]; } int pop(sqstack *s) //脱括号 { if(s->base == s->top) { return 0; } int x = *--(s->top); return x; } int yunsuan(int x,int y,int z) //运输 { switch(y) { case '+': return x + z; case '-': return x - z; case '*': return x * z; case '/': return x / z; } } int Biaodashi(char *biaodashi) { sqstack fuzhan; //运算符栈 sqstack shuzhan; //运算数栈
char TD[20]; int data; int a; int b; char *ch; char D[2]; char e; int fu;
chushihua(&fuzhan); push(&fuzhan,'#'); chushihua(&shuzhan);
ch = biaodashi; TD[0] = '\0'; while(*ch != '#' gettop(&fuzhan) != '#') {
if(!yunsuan_fu(*ch,yunsuanfu))//不是运算符则进栈 { D[0] = *ch; D[1] = '\0'; lianjie(TD,D); ch++; if(yunsuan_fu(*ch,yunsuanfu))//是运算符时 { data = AToi(TD); push(&shuzhan,data); TD[0] = '\0'; } } else { switch(youxianji(gettop(&fuzhan),*ch)) { case '<': push(&fuzhan,*ch); ch++; break; case '=': pop(&fuzhan);
ch++; break; case '>': a = pop(&shuzhan); b = pop(&shuzhan); fu = pop(&fuzhan); push(&shuzhan,yunsuan(b,fu,a)); break; } } } return gettop(&shuzhan); } int main() { while(scanf("%s",str)!=EOF) { printf("%d\n",Biaodashi(str)); } return 0;
}
|