C语言作业,编写一个简单的计算器,实现加减乘除运算,谢谢大家
发布网友
发布时间:2022-04-30 19:35
我来回答
共1个回答
热心网友
时间:2022-06-30 05:30
#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;
}