C-源代码-计算简单表达式
2007-10-20 00:32
423 查看
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <string.h>
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<strlen(corch))break;
}
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();
}
}
#include <stdio.h>
#include <conio.h>
#include <string.h>
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<strlen(corch))break;
}
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();
}
}
相关文章推荐
- JAVA实现一个简单的代数运算语言编译器(四)-- 表达式计算
- 极限测试之Matlab和Forcal编译含简单计算的长表达式
- JavaScript计算简单后缀表达式
- Java 简单表达式计算
- C++实现简单源代码计算
- 简单算法—计算表达式
- 简单字符串表达式计算方法
- Clojure: 实现简单的数学表达式计算
- 简单的用堆栈实现的表达式计算
- 极限测试之Matlab与Forcal编译计算含自变量的简单长表达式
- Java实现简单的表达式计算
- 简单的表达式计算
- 含括号和四则运算符的简单表达式的计算【c++ 50 lines】
- Openjudge-计算概论(A)-简单算术表达式求值
- 用正则表达式完成简单公式的替换计算
- Qt实现的简单计算器-计算简单表达式
- 简单的JAVA计算表达式,支持加减乘除括号
- 使用栈计算多项表达式的简单实现--java
- 简单的用堆栈实现的表达式计算
- Java中使用Socket完成简单的远程计算(含粗糙界面和正则表达式判断数字类型)