C语言作业,编写一个简单的计算器,实现加减乘除运算,谢谢大家

如题所述

#include<stdio.h>
#include<math.h>
#include<stdlib.h>

int cls()
{
system("cls");
                 return 0;
}
int pause()
{
system("pause");
                 return 0;
}

int switchs(double a,double b,char s)
{
     switch (s)
{
case '+':
{
cls();
printf("a+b\n");
            Sleep(500);
printf("=%0.3lf+%0.3lf\n",a,b);
Sleep(500);
printf("=%0.3lf\n\007",a+b);
Sleep(500);
}break;
case '-':
{
cls();
printf("a-b\n");
Sleep(500);
printf("=%0.3lf-%0.3lf\n",a,b);
Sleep(500);
printf("=%0.3lf\n\007",a-b);
Sleep(500);
}break;
case '*':
{
cls();
printf("a*b\n");
Sleep(500);
printf("=%0.3lf*%0.3lf\n",a,b);
Sleep(500);
printf("=%0.3lf\n\007",a*b);
Sleep(500);
}break;
case '/':
{
if(b==0)
{
cls();
printf("³ýÊý²»ÄÜΪ0£¡\n\007");
Sleep(500);
goto R;
}
else
{
cls();
printf("a/b\n");
Sleep(500);
printf("=%0.3lf/%0.3lf\n",a,b);
Sleep(500);
printf("=%0.5lf\n\007",a/b);
Sleep(500);
}
}break;
default: 
{
cls();
printf("error!\n\007");
Sleep(500);
pause();
cls();
goto R;
}
}
R:;
}

int main(void)
{
        char c;
double a,b;
char s;
R: printf("\007please input a formula:\n");
scanf("%lf%c%lf",&a,&s,&b);
if(a==0||b==0||s=='\0')printf("Error!");
else switchs(a,b,s);
pause();
while((c = getchar()) != '\n' && c != EOF);
a=0;b=0;s='\0';
cls();
goto R;
}

温馨提示:答案为网友推荐,仅供参考
相似回答