文档详情

用堆栈实现表达式计算(Using stack to implement expression calculation).doc

发布:2017-07-22约3.34千字共8页下载文档
文本预览下载声明
用堆栈实现表达式计算(Using stack to implement expression calculation) Using stack to implement expression calculation -------------------------------------------------------------------------------- A stack to achieve the expression calculation example, the program introduced two stack, operation components stack and operator stack. When a program evaluates an expression, Scan numeric expressions from left to right. When you encounter numbers, count them into the component stack The top of the operator stack compares the precedence of the operator to the upper stack, whereas the stack handles the other. #include stdio.h #include malloc.h #include math.h #define MUL 1 #define ADD 2 #define SUB 3 #define END 4 Struct{ Char op; Int code; }opchTbl[]={{*, 1}, 2}, {+, {-, 3}, {#, 4}}; Typedef struct node{/*... / stack data... Double data; Struct node *link; }LNODE; Int osp[] = {3,1,1,0}; / *... * (x) + (plus) - (minus) # (end) * / priority... LNODE, *optop, *numtop; Void l_push (double x, LNODE * *toppt) function into the stack... Link storage stack * / / *... { LNODE *p = (LNODE *) malloc (sizeof (LNODE)); P-data = x; P-link = *toppt; *toppt = p; } Int l_pop (double *cp, LNODE * *toppt) / *... Link storage stack stack function *... { LNODE *p = *toppt; If (*toppt = = NULL) return 1; / *... * / empty stack... *cp = p-data; *toppt = p-link; Free (P); Return 0; } Double Eval (char, tag, double, left, double, right) { Switch (tag) {/*. Case MUL: return left*right; Case ADD: return left+right; Case SUB: return left-right; } } My_delay (int n) / *... * / delay function call... { Int i; For (i=0; in; i++) Delay (50000); } Void, synError () { } (main) { Int cts[]={40,5,6,7}, opTbl[]={3,2,1,4}; / *... CTS formula opTbl formula can match the data array changes *... Double, num, Op, operand1, operand2, res; Int, type, i=0, j=0; (clrscr); / *... * / clearscreen... Printf (Please wait for... N); Optop = numtop =NULL; While (1) {/*. Num = (double) cts[i++]; Printf (num=%fn, Num); / *... The sta
显示全部
相似文档