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
| #include <stdio.h> #include <stdlib.h> #include <string.h> #include <math.h> #define MAX_LEN 30 #define MAX 100
typedef struct { int data[MAX_LEN]; int top; }Stack;
Stack *Createstack() { Stack *p; p = (Stack *)malloc(sizeof(*p)); p->top = -1; return p; }
int Push(Stack *p,int x) { if (p->top == MAX_LEN - 1) { return -1; } p->top++; p->data[p->top] = x; return 0; }
int Pop(Stack *L,int *x) { if (L->top == -1) { return -1; } *x = L->data[L->top]; L->top--; return 0; }
int TOP(Stack *L,int *x) { if (L->top == -1) { return -1; } *x = L->data[L->top]; return 0; }
int Empty(Stack *L) { return (L->top == -1); }
int Priority(int ope) { switch(ope) { case '(': return 0; case '+': case '-': return 1; case '*': case '%': case '/': return 2; case '^': return 3; default : return -1; } }
void Calculation(Stack *snum,int ope) { int n,n1,n2; Pop(snum,&n1); Pop(snum,&n2); switch(ope) { case '+': n = n1 + n2; break; case '-': n = n2 - n1; break; case '*': n = n1 * n2; break; case '/': n = n2 / n1; break; case '%': n = n2 % n1; break; case '^': n = pow(n2,n1);break; } Push(snum,n); }
void Deal_ope(Stack *snum,Stack *sope,int ope) { int old_ope; if (Empty(sope) || ope == '(') { Push(sope,ope); return ; } TOP(sope,&old_ope); if (Priority(ope) > Priority(old_ope)) { Push(sope,ope); return ; } while (Priority(ope) <= Priority(old_ope)) { Pop(sope,&old_ope); printf("%c ",old_ope); Calculation(snum,old_ope); if (Empty(sope)) { break; } TOP(sope,&old_ope); } Push(sope,ope); }
void Right(Stack *snum,Stack *sope) { int old_ope; TOP(sope,&old_ope); while (old_ope != '(') { Pop(sope,&old_ope); printf("%c ",old_ope); Calculation(snum,old_ope); TOP(sope,&old_ope); } Pop(sope,&old_ope); }
void Display(Stack *L) { int i; if (L->top == -1) { return; } for (i = 0 ; i <= L->top; i++) { printf("%d ",L->data[i]); } printf("\n"); }
void Displayope(Stack *L) { int i; if (L->top == -1) { return ; } for (i = 0 ; i <= L->top; i++) { printf("%c ",L->data[i]); } printf("\n"); }
int main() { char str[] = "10+(2*10+9)/5"; char dst[MAX]; printf("中缀表达式为:"); printf("%s\n",str); int i = 0,value = 0,flag = 0; int old_ope; Stack *numstack,*opestack; numstack = Createstack(); opestack = Createstack(); printf("后缀表达式为:");
while (str[i] != '\0') { if (str[i] >= '0' && str[i] <= '9') { value *= 10; value +=str[i]-'0'; flag = 1; } else { if (flag) { printf("%d ",value); Push (numstack, value); flag = 0; value = 0; } if(str[i] == ')') { Right(numstack,opestack); } else { Deal_ope(numstack,opestack,str[i]); } } i++; } if (flag) { printf("%d ",value); Push(numstack,value); } while (!Empty(opestack)) { Pop(opestack,&old_ope); printf("%c ",old_ope); Calculation(numstack,old_ope); } Pop(numstack,&value); printf("\n%s = %d\n",str,value); return 0; }
|