1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<ctype.h> 4 5 #define OK 1 6 #define ERROR 0 7 #define STACK_INIT_SIZE 20 8 #define STACK_INCREMENT
1 #include<stdio.h>
2 #include<stdlib.h>
3 #include<ctype.h>
4
5 #define OK 1
6 #define ERROR 0
7 #define STACK_INIT_SIZE 20
8 #define STACK_INCREMENT 10
9 #define DIGITBUFFER 10
10
11 typedef int Status;
12 typedef double Elemtype;
13 typedef struct StackNode{
14 Elemtype* base;
15 Elemtype* top;
16 int stackSize;
17 }StackNode;
18 typedef struct StackNode* Stack;
19
20 Status InitStack(Stack s){
21 s->base = (Elemtype*)malloc(sizeof(Elemtype) * STACK_INIT_SIZE);
22 if(!s->base)
23 return ERROR;
24 s->top = s->base;
25 s->stackSize = STACK_INIT_SIZE;
26 return OK;
27 }
28 Status Pop(Stack s,Elemtype* result){
29 if(s->base == s->top)
30 return ERROR;
31 *result = *(--s->top);
32 return ERROR;
33 }
34 Status Push(Stack s,Elemtype value){
35 if(s->top - s->base == s->stackSize){
36 s->base = (Elemtype*)realloc(s->base,sizeof(Elemtype) * (STACK_INIT_SIZE + STACK_INCREMENT));
37 if(!s->base)
38 return ERROR;
39 s->top = s->base + STACK_INIT_SIZE;
40 s->stackSize = STACK_INIT_SIZE + STACK_INCREMENT;
41 }
42 *(s->top) = value;
43 s->top++;
44 return OK;
45 }
46 int StackLenth(Stack s){
47 return s->top - s->base;
48 }
49 Status RPT(){ //reverse polish notation
50 char c;
51 double operater1,operater2;
52 double result;
53 int i = 0;
54 char bufferDigit[DIGITBUFFER];
55
56 Stack s;
57 InitStack(s);
58
59 printf(" Please Enter Reverse Polish Notation!(RPN)\n");
60 printf("------note: separated by space between -------\n");
61 printf("------ number or operator.end of '#'-------\n");
62
63 scanf("%c", &c);
64 while(c != '#'){
65 /* 處理輸入的數字:由於使用%c接受輸入,所以對於123這樣的多位數的
66 * 輸入%c是不能處理的。所以設置了char型的數組bufferDigit來緩存輸
67 * 入的多位數。在開始了多位數的輸入之後,必將以空格來結束該多位數
68 * 輸入,所以if(c == ' ')用來判斷多位數的結束。以便將緩存在char
69 * 型數組中的多位數轉化為double並且Push。
70 */
71 while( isdigit(c) || c == '.'){
72 if(i == 10){
73 printf("number is too lager\n");
74 return ERROR;
75 }
76 bufferDigit[i++] = c;
77 bufferDigit[i] = '\0';
78 scanf("%c", &c);
79 if(c == ' '){ //不是空格就一定還是數字
80 result = atof(bufferDigit);
81 Push(s,result);
82 i = 0;
83 }
84 }
85 /* 處理輸入的運算符
86 */
87 switch(c){
88 case '+':
89 Pop(s,&operater1);
90 Pop(s,&operater2);
91 Push(s,operater1 + operater2);
92 break;
93 case '-':
94 Pop(s,&operater1);
95 Pop(s,&operater2);
96 Push(s,operater2 - operater1);
97 break;
98 case '*':
99 Pop(s,&operater1);
100 Pop(s,&operater2);
101 Push(s,operater1 * operater2);
102 break;
103 case '/':
104 Pop(s,&operater1);
105 Pop(s,&operater2);
106 Push(s,operater2 / operater1);
107 break;
108 }
109 scanf("%c", &c);
110 }
111
112 Pop(s,&result);
113 printf("The result of RPN is %f\n",result);
114 }
115
116 //test;
117 Status ShowStack(Stack s){
118 while(s->base != s->top){
119 printf("%f ",*(--(s->top)));
120 }
121 printf("\n");
122 }
123 Status Test(){
124 Stack s1;
125 InitStack(s1);
126 Push(s1,1);
127 Push(s1,2);
128 Push(s1,3);
129 ShowStack(s1);
130 }
131 int main(){
132 RPT();
133
134 Stack s;
135 InitStack(s);
136 Push(s,1);
137 Push(s,2);
138 Push(s,3);
139 ShowStack(s);
140 return 0;
141 }
142 /* 該程式編寫的RPT部分功能可以完成,但是Text部分存在不解之處,在程式的124~129行的代碼與134到
143 * 139行的代碼完全相同。但是在main函數中的可以順利運行,而在Test函數中的則出現錯誤(無錯誤提示)
144 * 如果將124~129行的代碼改成如下則可以順利運行:
145 StackNode s1;
146 InitStack(&s1);
147 Push(&s1,1);
148 Push(&s1,2);
149 Push(&s1,3);
150 ShowStack(&s1);
151 不知為何期待大牛指導。
152 * */