#include
#include
#include
void ErrorInfo()//出错提示
{
printf("The operator Must be \"+,-,*,/\"\n");
}
int judgeNum(float num)//判断是否为整数
{
if(num-(int)num==0)
return 1;
else
return 0;
}
int IsOperator(char ch)//判断运算符是否正确
{
if(ch=='+'||ch=='-'||ch=='*'||ch=='/')
return 1;
else
return 0;
}
float AddNum(float a,float b)
{
return a+b;
}
float SubNum(float a,float b)
{
return a-b;
}
float MultiplyNum(float a,float b)
{
return a*b;
}
float DivideNum(float a,float b)
{
return a/b;
}
void main()
{
float a,b,Result;
char ch;
char key;
while(1)
{
printf("Please Input the number model As \"a+b\":");
scanf("%f%c%f",&a,&ch,&b);
if(!IsOperator(ch))
{
ErrorInfo();
}
else
{
switch(ch)
{
case '+':
Result=AddNum(a,b);
break;
case '-':
Result=SubNum(a,b);
break;
case '*':
Result=MultiplyNum(a,b);
break;
case '/':
Result=DivideNum(a,b);
}
if(judgeNum(a)&&judgeNum(b)&&ch!='/')
{
printf("%.0f %c %.0f = %.0f",a,ch,b,Result);
}
else
{
printf("%.2f %c %.2f= %.2f",a,ch,b,Result);
}
printf("\nCompute success ! ");
}
printf("Press [C] to clear the screen,\nPress [X] to exit,and Press other key to continue!\n");
key=getch();
fflush(stdin);
if(key=='c'||key=='C')
{
system("cls");
}
else if(key=='x'||key=='X')
{
break;
}
}
}