c - C 语言的多功能数学计算器

标签 c

我需要编写代码来使用 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/

相关文章:

c - 通过 TCP 连接使用 Berkeley 套接字传输可变结构大小

c - 为什么 gcc4 发出警告以及如何避免它

有人可以向我解释一下这个按位函数中的 putchar 行吗?

c - 无法解决错误 : control reaches end of non-void function

c - 从不兼容的指针类型 : forcing the type? 赋值

c - C中的链表——搜索方法

c - 共享内存和 unix domain socket 同步,不重复 shm 内容

c - 开关/外壳问题

有力量的食人者和传教士

c++ - 如何处理 .dump/.dump 文件?