楼主可能用的是TC6.0吧,一楼的程序很不错,不过需要在VC++里面运行,下面是我的可以在TC里面执行的代码,OPTION是只能用英文了呵呵!
注意!如果出现死循环,就用Ctrl+C来结束程序。
/*运算器程序*/
#include
#include
void getOption() /*显示菜单,读选项*/
{
printf("****OPTION****\n");
printf("1,ADD\n");
printf("2,MINUS\n");
printf("3,MULTIPLY\n");
printf("4,DIVIDE\n");
printf("5,EXIT\n");
printf("***************\n");
printf("Please input your option:\n");
}
/*读两个操作数,若为 除法,判别除数不为零 若除数为零,要求用户 重新输入*/
void getNumbers(float *a, float *b)
{
printf("input the first number:\n");
scanf("%f",a);
printf("input the second number:\n");
scanf("%f",b);
}
double calc(int c, float d, float e) /*完成运算,并返回结果*/
{
switch(c)
{
case 1:{return(d+e);}break;
case 2:{return(d-e);}break;
case 3:{return(d*e);}break;
case 4:{
if(e==0)
{
printf("The divisor is zero, please input again!\n\n\n");
return(0);
}
else
return(d/e);
} break;
case 5:{exit(0);}break;
default: return(0); break;
}
}
void printResult(int i, double f, float g, float h) /*输出表达式及结果*/
{
switch(i)
{
case 1:printf("RESULT: %g+%g=%g\n\n\n", g, h, f); break;
case 2:printf("RESULT: %g-%g=%g\n\n\n", g, h, f); break;
case 3:printf("RESULT: %g*%g=%g\n\n\n", g, h, f); break;
case 4:if(h!=0.)printf("RESULT: %g/%g=%g\n\n\n", g, h, f); break;
default: break;
}
}
int main()
{
int option;
float c1,c2;
double result;
do
{
getOption();
scanf("%d", &option);
if( option!=1&&
option!=2&&
option!=3&&
option!=4&&
option!=5){ printf("Wrong option!\n\n\n");continue;}
if(option!=5)
{
getNumbers(&c1,&c2);
result=calc(option,c1,c2);
printResult(option,result,c1,c2);
}
printf("Continue(1)? or exit(5)?:"); /*确认退出(5)还是继续计算(1)*/
scanf("%d",&option);
}
while(option!=5);
return(1);
}
#include
#include
void getOption()
{
cout<<"*****菜单栏****"<
void getNumbers(double *a,double *b)
{
cout<<"请输入第一个操作数:"<
cout<<"请输入第二个操作数:"<
}
double calc(int c,double d,double e)
{
switch(c)
{
case 1:{return(d+e);}break;
case 2:{return(d-e);}break;
case 3:{return(d*e);}break;
case 4:{
if(e==0)
{
cout<<"除数为零,请重新输入!"<
}
else
return(d/e);
}break;
case 5:{exit(0);}break;
}
}
void printResult(int i,double f,double g,double h)
{
if(i==1)
cout<
cout<
cout<
cout<
int main()
{
int option;
double c1,c2,result;
do
{
getOption();
cin>>option;
if(option!=5)
{
getNumbers(&c1,&c2);
result=calc(option,c1,c2);
printResult(option,result,c1,c2);
}
}
while(option!=5);
return(1);
}
//我亲自调试过来,是可行的,绝对正确而且简单。
#include
main()
{
float x,y,result;
char op;
printf("请输入计算式:");
scanf("%f%c%f",&x,&op,&y);
switch(op)
{
case'+':result=x+y;break;
case'-':result=x-y;break;
case'*':result=x*y;break;
case'/':result=x/y;break;
}
if (op='/'&&y==0)printf("计算结果为: 错误,分母不能为零\n");
else
printf("计算结果为: %.2f\n",result);
}