标签:
结对编程对象:张忠玉
代码共计:326行
1 #include <stdio.h> //计算器 2 #include <malloc.h> 3 #include <stdlib.h> 4 5 #define STACK_SIZE 100 //max size of the stack 6 #define STACK_INCREMENT 10 //realloc size 7 8 typedef struct FStack //stack of int 9 { 10 float* base; 11 float* top; 12 int stacksize; 13 }FStack; 14 15 void Init(FStack* s) 16 { 17 s->base = (float*)malloc(STACK_SIZE * sizeof(FStack)); 18 if (!s->base) 19 { 20 printf("overflow!\n"); 21 return; 22 } 23 s->top = s->base; 24 s->stacksize = STACK_SIZE; 25 } 26 27 bool isEmpty(FStack* s) 28 { 29 if (s->top == s->base) 30 { 31 return true; 32 } 33 else 34 { 35 return false; 36 } 37 } 38 39 void Push(FStack* s, float e) 40 { 41 if (s->top - s->base >= s->stacksize) 42 { 43 printf("stack is full!\nrealloc %d\n", STACK_INCREMENT); 44 s->base = (float*)realloc(s->base, (s->stacksize + sizeof(FStack))); 45 if (!s->base) 46 { 47 printf("overflow!\n"); 48 return; 49 } 50 s->top = s->base + s->stacksize; 51 s->stacksize += STACK_INCREMENT; 52 } 53 *(s->top) = e; 54 (s->top)++; 55 } 56 57 float GetTop(FStack* s) 58 { 59 if (s->top == s->base) 60 { 61 printf("stack is empty!\n"); 62 return 0; 63 } 64 float e = *(s->top - 1); 65 return e; 66 } 67 68 void Pop(FStack* s) 69 { 70 if (s->top == s->base) 71 { 72 printf("stack is empty!\n"); 73 return; 74 } 75 s->top--; 76 } 77 78 typedef struct CStack //stack of char 79 { 80 char* base; 81 char* top; 82 int stacksize; 83 }CStack; 84 85 void Init(CStack* s) 86 { 87 s->base = (char*)malloc(STACK_SIZE * sizeof(CStack)); 88 if (!s->base) 89 { 90 printf("overflow!\n"); 91 return; 92 } 93 s->top = s->base; 94 s->stacksize = STACK_SIZE; 95 } 96 97 bool isEmpty(CStack* s) 98 { 99 if (s->top == s->base) 100 { 101 return true; 102 } 103 else 104 { 105 return false; 106 } 107 } 108 109 void Push(CStack* s, int e) 110 { 111 if (s->top - s->base >= s->stacksize) 112 { 113 printf("stack is full!\nrealloc %d\n", STACK_INCREMENT); 114 s->base = (char*)realloc(s->base, (s->stacksize + sizeof(CStack))); 115 if (!s->base) 116 { 117 printf("overflow!\n"); 118 return; 119 } 120 s->top = s->base + s->stacksize; 121 s->stacksize += STACK_INCREMENT; 122 } 123 *(s->top) = e; 124 (s->top)++; 125 } 126 127 char GetTop(CStack* s) 128 { 129 if (s->top == s->base) 130 { 131 printf("stack is empty!\n"); 132 return 0; 133 } 134 char e = *(s->top - 1); 135 return e; 136 } 137 138 void Pop(CStack* s) 139 { 140 if (s->top == s->base) 141 { 142 printf("stack is empty!\n"); 143 return; 144 } 145 s->top--; 146 } 147 148 bool isOper(char ch) 149 { 150 if (ch == ‘+‘ || ch == ‘-‘ || ch == ‘*‘ || ch == ‘/‘ || ch == ‘%‘) 151 { 152 return true; 153 } 154 else 155 { 156 return false; 157 } 158 } 159 160 int Priority(char ch) 161 { 162 int p; 163 switch (ch) 164 { 165 case ‘(‘: 166 p = 0; 167 break; 168 case ‘+‘: 169 case ‘-‘: 170 p = 1; 171 break; 172 case ‘*‘: 173 case ‘/‘: 174 case ‘%‘: 175 p = 2; 176 break; 177 } 178 return p; 179 } 180 181 float Calculate(float f1, float f2, char oper) 182 { 183 float f3; 184 switch (oper) 185 { 186 case ‘+‘: 187 f3 = f1 + f2; 188 break; 189 case ‘-‘: 190 f3 = f1 - f2; 191 break; 192 case ‘*‘: 193 f3 = f1 * f2; 194 break; 195 case ‘%‘: 196 f3 = (float)((int)f1 % (int)f2); 197 break; 198 case ‘/‘: 199 if (f2 == 0) 200 { 201 printf("\nDevided by zero!"); 202 exit(1); 203 } 204 else 205 { 206 f3 = f1 / f2; 207 } 208 break; 209 } 210 return f3; 211 } 212 213 float StrtoFloat(char* str, int* pos) 214 { 215 float fRes; 216 int i = *pos; 217 int k; 218 char n[50]; 219 for (k = 0; str[i] >= ‘0‘ && str[i] <= ‘9‘ || str[i] == ‘.‘; i++, k++) 220 { 221 n[k] = str[i]; 222 } 223 n[k] = ‘\0‘; 224 *pos = i; 225 fRes = atof(n); 226 return fRes; 227 } 228 229 bool Check(char* str) 230 { 231 int i = 0; 232 while (str[i] != ‘\0‘) 233 { 234 if (str[i] != ‘+‘ && str[i] != ‘-‘ && str[i] != ‘*‘ && str[i] != ‘/‘ && str[i] != ‘%‘ && str[i] != ‘.‘ && str[i] != ‘(‘ && str[i] != ‘)‘ && (str[i] < ‘0‘ || str[i] > ‘9‘)) 235 { 236 return false; 237 } 238 i++; 239 } 240 return true; 241 } 242 243 void main() 244 { 245 char exp[100]; 246 int i; 247 float f, f1, f2; 248 char oper; 249 FStack fstack; 250 CStack cstack; 251 Init(&fstack); 252 Init(&cstack); 253 printf("The expression is:"); 254 gets(exp); 255 if (!Check(exp)) 256 { 257 printf("input error! exit now!\n"); 258 exit(1); 259 } 260 for (i = 0; exp[i] != ‘\0‘ && exp[i] != -52; i++) 261 { 262 if (!isOper(exp[i])) 263 { 264 f = StrtoFloat(exp, &i); 265 Push(&fstack, f); 266 } 267 if (isOper(exp[i])) 268 { 269 if (!isEmpty(&cstack)) 270 { 271 while (!isEmpty(&cstack) && Priority(exp[i]) <= Priority(GetTop(&cstack))) 272 { 273 oper = GetTop(&cstack); 274 Pop(&cstack); 275 f2 = GetTop(&fstack); 276 Pop(&fstack); 277 f1 = GetTop(&fstack); 278 Pop(&fstack); 279 f = Calculate(f1, f2, oper); 280 Push(&fstack, f); 281 } 282 Push(&cstack, exp[i]); 283 } 284 else 285 { 286 Push(&cstack, exp[i]); 287 } 288 } 289 else if (exp[i] == ‘(‘) 290 { 291 Push(&cstack, exp[i]); 292 } 293 else if (exp[i] == ‘)‘) 294 { 295 while (GetTop(&cstack) != ‘(‘ && !isEmpty(&cstack)) 296 { 297 oper = GetTop(&cstack); 298 Pop(&cstack); 299 f2 = GetTop(&fstack); 300 Pop(&fstack); 301 f1 = GetTop(&fstack); 302 Pop(&fstack); 303 f = Calculate(f1, f2, oper); 304 Push(&fstack, f); 305 } 306 Pop(&cstack); 307 } 308 } 309 while (!isEmpty(&cstack)) 310 { 311 oper = GetTop(&cstack); 312 Pop(&cstack); 313 f2 = GetTop(&fstack); 314 Pop(&fstack); 315 f1 = GetTop(&fstack); 316 Pop(&fstack); 317 f = Calculate(f1, f2, oper); 318 Push(&fstack, f); 319 } 320 printf("\nThe result is:%f\n", GetTop(&fstack)); 321 Pop(&fstack); 322 323 getchar(); 324 getchar(); 325 getchar(); 326 }
总结:
通过这次项目的实践,我深切体会到结对编程带来的好处,其高效,其正确率都让我为之惊讶。
(By snowing)
标签:
原文地址:http://www.cnblogs.com/snowing0119/p/5373739.html