c - (C) 能够取消和退出的整数计算器

标签 c integer calculator

我对编码很陌生,应该编写一个整数计算器。到目前为止,我已经完成了所有设置,但需要能够随时通过键入以“q”开头的单词来退出程序。我还需要能够取消该程序,即通过输入以“c”开头的任何单词来开始新的计算。 代码是这样的

include <stdio.h>
include <stdlib.h>
int main()  {
    int running = 1;
    while (running==1) {
    int x = 0;
    int y = 0;
    int r = 0;
    char o;
    printf("*****INTEGER CALCULATOR*****\n\n\n");
    printf("enter x: ");
    scanf("%d",&x);
    printf("enter y: ");
    scanf("%d",&y);
    printf("%d %d\n",x,y);
    printf("+ - * / %% : ");
    scanf("%s",&o);
    if (o == '+') {
        r = x+y;
    }
    else if (o == '-')  {
        r = x-y;
    }
    else if(o == '*')   {
        r = x*y;
    }
    else if(o == '/')   {
        if (x==0&&y==0) {
            printf("UNDEFINED\n");
        }
        else if(y==0)   {
            printf("ERROR: DIVISION BY ZERO\n");
        }
        else    {
            r= x/y;
        }
    }
    else if(o == '%')   {
        r= x%y;
    }
    else    {
        printf("OPERATOR ERROR\n");
    }
    printf("Operation: %c\n",o);
    printf("RESULT: %d\n\n\n",r);
    }
    return 0;

最佳答案

这是根据您的需要编辑的程序。所有新内容都在代码的注释中进行了解释:

#include <stdio.h>
#include <stdlib.h>  // Don't forget # before include!
int main()  {
    char ch = NULL; //character for storing 'q' or 'c'
    do{
    int x = 0;
    int y = 0;
    int r = 0;
    char o;
    printf("*****INTEGER CALCULATOR*****\n\n\n");
    printf("enter x: ");
    scanf("%d",&x);
    printf("enter y: ");
    scanf("%d",&y);
    printf("%d %d\n",x,y);
    printf("+ - * / %% : ");
    scanf(" %c",&o);   //%c not %s as o is a char
    if (o == '+')
        r = x+y;
    else if (o == '-')
        r = x-y;
    else if(o == '*')
        r = x*y;
    else if(o == '/')
    {
        if (x==0&&y==0) {
            printf("UNDEFINED\n");
        }
        else if(y==0)   {
            printf("ERROR: DIVISION BY ZERO\n");
        }
        else    {
            r= x/y;
        }
    }
    else if(o == '%')
        r= x%y;
    else    {
        printf("OPERATOR ERROR! Try again:\n");
    }
    printf("Operation: %c\n",o);
    printf("RESULT: %d\n\n\n",r);

    while(1)  //infinite loop
    {
        printf("Enter 'c' for calculating once more and q to exit");
        scanf(" %c",&ch);
        if(ch=='c' || ch=='q') // if input is c or q
            break;             //break out of infinite loop
        else                   // if input is not c or q
            printf("Invalid option.Try again\n");//print this and continue the infinite loop
    }
    //ch is now sure to be either c or q when execution reaches here
    }while(ch!='q'); // loop until ch is not q
    return 0;
}

关于c - (C) 能够取消和退出的整数计算器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26587971/

相关文章:

c - 为什么在此 C 程序中将全局变量定义为静态变量?

excel - 将命名范围设置为整数

仅包含数字的输入字符串的 Java 数字格式异常

python - Tkinter 窗口格式化 (Python)

calculator - If Then 语句显示所有可能性

javascript - 根据输入类型日期 html 在 javascript 中计算 2 个日期之间的时间

更改 LinkedList 中的数组值

c - 从 C 中的命令行参数打印整数

java - 无法定位符号 "__android_log_write"- Android 原生日志记录

c++ - 缩小转换范围