简单的c语言表达式,C语言计算简单表达式

这是一个简单的C语言计算机表达式,大家有时间可以研究一下!

#include

#include

#include

#include

int eadd(int x,int y){return x y;}

int esub(int x,int y){return x-y;}

int emul(int x,int y){return x*y;}

int ediv(int x,int y){return x/y;}

int (*func[])(int,int)={eadd,esub,emul,ediv};

int num,curch;

char chtbl[]=\" -*/()=\";

char corch[]=\" -*/()=0123456789\";

int getach()

{

int i;

while(1)

{

curch=getchar();

if(curch==EOF)return -1;

for(i=0;corch[i]&&curch!=corch[i];i );

if((unsigned int)i

}

return curch;

}

int getid()

{

int i;

if(curch>=’0’&&curch<=’9’)

{

for(num=0;curch>=’0’&&curch<=’9’;getach())

num=num*10 curch-’0’;

return -1;

}

else

{

for(i=0;chtbl[i];i )

if(chtbl[i]==curch)break;

if(i<=5)getach();

return i;

}

}

int cal()

{

int x1,x2,x3,op1,op2,i;

i=getid();

if(i==4)x1=cal();else x1=num;

op1=getid();

if(op1>=5)return x1;

i=getid();

if(i==4)x2=cal();else x2=num;

op2=getid();

while(op2<5)

{

i=getid();

if(i==4)x3=cal();else x3=num;

if((op1/2==0)&&(op2/2==1))

x2=(*func[op2])(x2,x3);

else

{

x1=(*func[op1])(x1,x2);

x2=x3;

op1=op2;

}

op2=getid();

}

return (*func[op1])(x1,x2);

}

void main(void)

{

int value;

printf(\"please input an expression:\\n\");

getach();

while(curch!=’=’)

{

value=cal();

printf(\"the result is:%d\\n\",value);

printf(\"please input an expression:\\n\");

getach();

}

}

本文来源:搜集于网络

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值