用C语言编一个简单的计算器小程序
5个回答
展开全部
你说的是 vc 还是 tc 啊???
其他的运算:
#include <stdio.h>
int add(int x,int y) {return x+y;}
int sub(int x,int y) {return x-y;}
int mul(int x,int y) {return x*y;}
int div(int x,int y) {return x/y;}
int (*func[])()={add,sub,mul,div};
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(i<strlen(corch)) break;
}
return curch;
}
int getid() {
int i;
if(curch>='0'&&curch<='9') {
for(num=0;curch>='0'&&curch<='9';getach()) num=10*num+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<=4) {
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>
int add(int x,int y) {return x+y;}
int sub(int x,int y) {return x-y;}
int mul(int x,int y) {return x*y;}
int div(int x,int y) {return x/y;}
int (*func[])()={add,sub,mul,div};
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(i<strlen(corch)) break;
}
return curch;
}
int getid() {
int i;
if(curch>='0'&&curch<='9') {
for(num=0;curch>='0'&&curch<='9';getach()) num=10*num+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<=4) {
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();
}
}
只能 + - * /
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
没有悬赏,人们都没有动力啊!
呵呵
呵呵
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你说的是
vc
还是
tc
啊???
其他的运算:
#include
<stdio.h>
int
add(int
x,int
y)
{return
x+y;}
int
sub(int
x,int
y)
{return
x-y;}
int
mul(int
x,int
y)
{return
x*y;}
int
div(int
x,int
y)
{return
x/y;}
int
(*func[])()={add,sub,mul,div};
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(i<strlen(corch))
break;
}
return
curch;
}
int
getid()
{
int
i;
if(curch>='0'&&curch<='9')
{
for(num=0;curch>='0'&&curch<='9';getach())
num=10*num+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<=4)
{
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();
}
}
只能
+
-
*
/
vc
还是
tc
啊???
其他的运算:
#include
<stdio.h>
int
add(int
x,int
y)
{return
x+y;}
int
sub(int
x,int
y)
{return
x-y;}
int
mul(int
x,int
y)
{return
x*y;}
int
div(int
x,int
y)
{return
x/y;}
int
(*func[])()={add,sub,mul,div};
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(i<strlen(corch))
break;
}
return
curch;
}
int
getid()
{
int
i;
if(curch>='0'&&curch<='9')
{
for(num=0;curch>='0'&&curch<='9';getach())
num=10*num+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<=4)
{
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();
}
}
只能
+
-
*
/
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你说的是
vc
还是
tc
啊???
其他的运算:
#include
<stdio.h>
int
add(int
x,int
y)
{return
x+y;}
int
sub(int
x,int
y)
{return
x-y;}
int
mul(int
x,int
y)
{return
x*y;}
int
div(int
x,int
y)
{return
x/y;}
int
(*func[])()={add,sub,mul,div};
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(i<strlen(corch))
break;
}
return
curch;
}
int
getid()
{
int
i;
if(curch>='0'&&curch<='9')
{
for(num=0;curch>='0'&&curch<='9';getach())
num=10*num+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<=4)
{
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();
}
}
只能
+
-
*
/
vc
还是
tc
啊???
其他的运算:
#include
<stdio.h>
int
add(int
x,int
y)
{return
x+y;}
int
sub(int
x,int
y)
{return
x-y;}
int
mul(int
x,int
y)
{return
x*y;}
int
div(int
x,int
y)
{return
x/y;}
int
(*func[])()={add,sub,mul,div};
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(i<strlen(corch))
break;
}
return
curch;
}
int
getid()
{
int
i;
if(curch>='0'&&curch<='9')
{
for(num=0;curch>='0'&&curch<='9';getach())
num=10*num+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<=4)
{
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>
float power(char, float, float);
main()
{
float a,b,c;
char ch;
printf("put");
scanf("%f",&a);
for(;(ch=getchar())!='\n';)
{
scanf("%f",&b);
if(ch=='/'&&b==0)
{
c=0;break;
}
else
{
c=1;
a=power(ch,a,b);
}
}
if(c)
printf("=%f",a);
else
printf("error!");
}
float power(char ch, float x, float y)
{
switch(ch)
{case'*':x=x*y;break;
case'/':x=x/y;break;
case'+':x=x+y;break;
case'-':x=x-y;break;
}
return(x);
#include<stdio.h>
float power(char, float, float);
main()
{
float a,b,c;
char ch;
printf("put");
scanf("%f",&a);
for(;(ch=getchar())!='\n';)
{
scanf("%f",&b);
if(ch=='/'&&b==0)
{
c=0;break;
}
else
{
c=1;
a=power(ch,a,b);
}
}
if(c)
printf("=%f",a);
else
printf("error!");
}
float power(char ch, float x, float y)
{
switch(ch)
{case'*':x=x*y;break;
case'/':x=x/y;break;
case'+':x=x+y;break;
case'-':x=x-y;break;
}
return(x);
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询