用c程编写一个程序 可以从键盘输入一个公式,回车后得到结果

来源:百度知道 编辑:UC知道 时间:2024/05/15 03:13:10
比如:
2+44*0-7/7回车得
1

#include<stdio.h>
#include<ctype.h>
#include<stdlib.h>
#include <conio.h>

char token[61];
int n=0;
void error(void)
{
printf("ERROR!\n");
exit(1);
}
void match(char expected)
{
if(token[n]==expected)
token[++n]=getchar();
else error();
}
double term(void);
double factor(void);
double exp(void)
{
double temp=term();
while((token[n]=='+')||(token[n]=='-'))
switch(token[n])
{
case'+':match('+');
temp+=term();
break;
case'-':match('-');
temp-=term();
break;
}
return temp;
}
double term(void)
{
double div;
double temp=factor();
while((token[n]=='*')||(token[n]=='/'))
switch(token[n])
{
case'*':match(&#