1、. .C语言编写四那么运算#include/计算器#include#defineSTACK_SIZE100/max size of the stack#defineSTACK_INCREMENT10/realloc sizetypedefstructFStack/stack of intfloat* base;float* top;intstacksize;FStack;voidInit(FStack* s)s-base = (float*)malloc(STACK_SIZE * sizeof(FStack);if (!s-base)printf(overflow!n);return;s-to
2、p = s-base;s-stacksize = STACK_SIZE;boolisEmpty(FStack* s)if (s-top = s-base)returntrue;elsereturnfalse;voidPush(FStack* s, floate)if (s-top - s-base = s-stacksize)printf(stack is full!nrealloc %dn, STACK_INCREMENT);s-base = (float*)realloc(s-base, (s-stacksize + sizeof(FStack);if (!s-base)printf(ov
3、erflow!n);return;s-top = s-base + s-stacksize;s-stacksize += STACK_INCREMENT;*(s-top) = e;(s-top)+;floatGetTop(FStack* s)if (s-top = s-base)printf(stack is empty!n);return 0;floate = *(s-top - 1);returne;voidPop(FStack* s)if (s-top = s-base)printf(stack is empty!n);return;s-top-;typedefstructCStack/
4、stack of charchar* base;char* top;intstacksize;CStack;voidInit(CStack* s)s-base = (char*)malloc(STACK_SIZE * sizeof(CStack);if (!s-base)printf(overflow!n);return;s-top = s-base;s-stacksize = STACK_SIZE;boolisEmpty(CStack* s)if (s-top = s-base)returntrue;elsereturnfalse;voidPush(CStack* s, inte)if (s
5、-top - s-base = s-stacksize)printf(stack is full!nrealloc %dn, STACK_INCREMENT);s-base = (char*)realloc(s-base, (s-stacksize + sizeof(CStack);if (!s-base)printf(overflow!n);return;s-top = s-base + s-stacksize;s-stacksize += STACK_INCREMENT;*(s-top) = e;(s-top)+;charGetTop(CStack* s)if (s-top = s-bas
6、e)printf(stack is empty!n);return 0;chare = *(s-top - 1);returne;voidPop(CStack* s)if (s-top = s-base)printf(stack is empty!n);return;s-top-;boolisOper(charch)if (ch = + | ch = - | ch = * | ch = / | ch = %)returntrue;elsereturnfalse;intPriority(charch)intp;switch(ch)case(:p = 0;break;case+:case-:p =
7、 1;break;case*:case/:case%:p = 2;break;returnp;floatCalculate(floatf1, floatf2, charoper)floatf3;switch(oper)case+:f3 = f1 + f2;break;case-:f3 = f1 - f2;break;case*:f3 = f1 * f2;break;case%:f3 = (float)(int)f1 % (int)f2);break;case/:if (f2 = 0)printf(nDevided by zero!);exit(1);elsef3 = f1 / f2;break
8、;returnf3;floatStrtoFloat(char* str, int* pos)floatfRes;inti = *pos;intk;charn50;for (k = 0; stri = 0 & stri = 9 | stri = .; i+, k+)nk = stri;nk = 0;*pos = i;fRes = atof(n);returnfRes;boolCheck(char* str)inti = 0;while (stri != 0)if (stri != + & stri != - & stri != * & stri != / & stri != % & stri !
9、= . & stri != ( & stri != ) & (stri 9)returnfalse;i+;returntrue;voidmain()charexp100;inti;floatf, f1, f2;charoper;FStackfstack;CStackcstack;Init(&fstack);Init(&cstack);printf(The expression is:);gets(exp);if (!Check(exp)printf(input error! exit now!n);exit(1);for (i = 0; expi != 0 & expi != -52; i+)
10、if (!isOper(expi)f = StrtoFloat(exp, &i);Push(&fstack, f);if (isOper(expi)if (!isEmpty(&cstack)while (!isEmpty(&cstack) & Priority(expi) = Priority(GetTop(&cstack)oper = GetTop(&cstack);Pop(&cstack);f2 = GetTop(&fstack);Pop(&fstack);f1 = GetTop(&fstack);Pop(&fstack);f = Calculate(f1, f2, oper);Push(
11、&fstack, f);Push(&cstack, expi);elsePush(&cstack, expi);elseif (expi = ()Push(&cstack, expi);elseif (expi = )while (GetTop(&cstack) != ( & !isEmpty(&cstack)oper = GetTop(&cstack);Pop(&cstack);f2 = GetTop(&fstack);Pop(&fstack);f1 = GetTop(&fstack);Pop(&fstack);f = Calculate(f1, f2, oper);Push(&fstack, f);Pop(&cstack);while (!isEmpty(&cstack)oper = GetTop(&cstack);Pop(&cstack);f2 = GetTop(&fstack);Pop(&fstack);f1 = GetTop(&fstack);Pop(&fstack);f = Calculate(f1, f2, oper);Push(&fstack, f);printf(nThe result is:%fn, GetTop(&fstack);Pop(&fstack);. .word.