我需要编写代码来使用 C 语言创建一个简单的计算器。 我需要在这里放置一些基本功能。
#include <stdio.h>
void main ()
{
int a,b,sum;
printf("");
scanf("%d",&a);
printf("");
scanf("%d",&b);
printf("enter 1 to add,2 to sub,3 to divi,4 to mul");
scanf("%d",&sum);
int add = a+b, sub =a-b,divi=a/b,mul=a*b;
if(sum==1)
printf("add of two values= %d",add);
if(sum==2)
printf("sub of two values=%d",sub);
if(sum==3)
printf("divi of two values=%d",divi);
if(sum==4)
printf("mul of two values=%d",mul);
}
当我输入两个整数并给出选项时,此代码终止。我需要编写代码以在给出“=”时终止代码。
最佳答案
你的错误是你没有在 main() 的末尾暂停。
要修复它,您可以将其放在主命令系统的末尾(“暂停”);或者你可以这样做
char c; std::cin >> c;
return;
这样它将等待字符输入,然后如果您想在输入“=”时终止应用程序 你可以写:
if (c=='=') return;
简单:)
如果您希望计算器执行多项计算,直到插入“=”号。你可以这样做:
int main()
{
char c;
while(c!='N'){
//YOUR CODE GOES HERE AND IT WILL BE REPEATED UNTIL YOU INSERT N at the end
std::cout << "\n Do one more calculation ? (Y/N) : ";
std::cin >> c;
}
system("pause");
return 0;
}
C:
while(c!='N'){
//YOUR CODE GOES HERE AND IT WILL BE REPEATED UNTIL YOU INSERT N at the end
//Doing it this way, if you type N it will terminate, if you type any other character it will do the loop.
printf(" Do one more calculation ? (Y/N) : ");
scanf("%c", &c);
}
关于c - C 语言的多功能数学计算器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42873012/