今天去面试,出了一道用c代码写“包括加减乘除的计算器”的题目?
结果没写出来,回来学习下,那位帮忙贴下代码,和注释?谢谢
[解决办法]
#include <stdio.h>
#include <stdlib.h>
#define MAXOP 100
#define NUMBER '0'
int getop (char []);
void push (double f);
double pop(void);
main()
{
int type;
double op2;
char s[MAXOP];
while((type =getop(s)) != EOF){
switch (type){
case NUMBER:
push(atof(s));
break;
case '+':
push (pop() +pop());
break;
case '-':
op2=pop();
push (pop() -op2);
break;
case '*':
push(pop() * pop());
break;
case '/':
op2=pop();
if(op2 !=0.0)
push(pop()/op2);
else
printf("error:zero divisor\n");
break;
case '\n':
printf("\t%.8g\n",pop());
break;
default:
printf("error: unknown command %s \n",s);
break;
}
}
return 0;
}
#define MAXVAL 100
int sp=0;
double val[MAXVAL];
void push (double f)
{
if(sp<MAXVAL)
val[sp++]=f;
else
printf("error:stackfull,can't push %g\n",f);
}
double pop(void)
{
if(sp>0)
return val [--sp];
else{
printf("error :stack empty\n");
return 0.0;
}
}
#include <ctype.h>
int getch(void);
void ungetch(int );
int getop(char s[])
{
int i,c;
while((s[0] =c =getch())== ' '||c=='\t')
;
s[1]='\0';
if(!isdigit(c)&&c !='.')
return c;
i=0;
if(isdigit(c))
while(isdigit(s[++i] =c =getch()))
;
if(c=='.')
while(isdigit(s[++i]=c=getch()))
;
s[i]='\0';
if(c!=EOF)
ungetch(c);
return NUMBER;
}
#define BUFSIZE 100
char buf[BUFSIZE];
int bufp=0;
int getch(void)
{
return (bufp>0?buf[--bufp]: getchar());
}
void ungetch (int c)
{
if(bufp>=BUFSIZE)
printf("ungetch:toomany characters\n");
else
buf[bufp++] =c;
}
[解决办法]
- C/C++ code
//表达式求值#include <stdio.h>#include <malloc.h>#include <string.h>/**功能:根据运算符计算*参数:a, b参与运算的数, ch运算符*返回值:计算结果,操作符错误则返回0*/int cal(int a, char ch, int b){ switch(ch) { case '+': return a+b; break; case '-': return a-b; break; case '*': return a*b; break; case '/': return a/b; break; } return 0;}/**功能:计算表达式的值(用数组模拟栈)*参数:表达式字符串*返回值:计算结果*/int evaluateExpression(char *str){ int i = 0, result, numSub = 0, operSub = 0; int tmp, len = strlen(str); int *operand = (int*)malloc(sizeof(int)*len); char *operat = (char*)malloc(sizeof(char)*len); while(str[i] != '\0') { switch(str[i]) { case '+': while(operSub > 0 && operat[operSub-1] != '(') { printf("%d %c %d = ", operand[numSub-2], operat[operSub-1], operand[numSub-1]); operand[numSub-2] = cal(operand[numSub-2], operat[operSub-1], operand[numSub-1]); printf("%d\n", operand[numSub-2]); --numSub; --operSub; } operat[operSub++] = '+'; break; case '-': while(operSub > 0 && operat[operSub-1] != '(') { printf("%d %c %d = ", operand[numSub-2], operat[operSub-1], operand[numSub-1]); operand[numSub-2] = cal(operand[numSub-2], operat[operSub-1], operand[numSub-1]); printf("%d\n", operand[numSub-2]); --numSub; --operSub; } operat[operSub++] = '-'; break; case '*': if(str[i+1] >= '0' && str[i+1] <= '9') { tmp = 0; while(str[i+1] >= '0' && str[i+1] <= '9') { tmp = tmp * 10 + str[i+1] - '0'; ++i; } --i; printf("%d * %d = ", operand[numSub-1], tmp); operand[numSub-1] = cal(operand[numSub-1], '*', tmp); printf("%d\n", operand[numSub-1]); ++i; } else operat[operSub++] = '*'; break; case '/': if(str[i+1] >= '0' && str[i+1] <= '9') { tmp = 0; while(str[i+1] >= '0' && str[i+1] <= '9') { tmp = tmp * 10 + str[i+1] - '0'; ++i; } --i; printf("%d / %d = ", operand[numSub-1], tmp); operand[numSub-1] = cal(operand[numSub-1], '/', tmp); printf("%d\n", operand[numSub-1]); ++i; } else operat[operSub++] = '/'; break; case '(': operat[operSub++] = '('; break; case ')': while(operat[operSub-1] != '(') { printf("%d %c %d = ", operand[numSub-2], operat[operSub-1], operand[numSub-1]); operand[numSub-2] = cal(operand[numSub-2], operat[operSub-1], operand[numSub-1]); printf("%d\n", operand[numSub-2]); --numSub; --operSub; } --operSub; break; default: tmp = 0; while(str[i] >= '0' && str[i] <= '9') { tmp = tmp * 10 + str[i] - '0'; ++i; } --i; operand[numSub++] = tmp; break; } ++i; } while(numSub > 1 && operSub >= 1) { printf("%d %c %d = ", operand[numSub-2], operat[operSub-1], operand[numSub-1]); operand[numSub-2] = cal(operand[numSub-2], operat[operSub-1], operand[numSub-1]); printf("%d\n", operand[numSub-2]); --numSub; --operSub; } result = operand[numSub-1]; free(operand); free(operat); return result;}int main(){ char *str = "225/15-20+(4-3)*2"; int result; printf("计算过程:\n"); result = evaluateExpression(str); printf("计算结果:result = %d\n", result); return 0;}计算过程:225 / 15 = 1515 - 20 = -54 - 3 = 11 * 2 = 2-5 + 2 = -3计算结果:result = -3
[解决办法]