【问题标题】:(C) Integer Calculator capable of Cancel and Quit [closed](C)能够取消和退出的整数计算器[关闭]
【发布时间】:2014-10-27 12:50:28
【问题描述】:

我对编码很陌生,应该编写一个整数计算器。到目前为止,我已经完成了所有设置,但需要能够随时通过键入以“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;

【问题讨论】:

  • 您可以将 while(running == 1) 修改为 while(ok == 'c')while(ok != 'q') 或(即使这样做没有意义)while(ok != q &amp;&amp; ok == c) ,然后阅读 ok打印结果。要能够随时开始新的计算或退出,那就是另一回事了。
  • 顺便说一句 include &lt;stdio.h&gt; -> #include &lt;stdio.h&gt;
  • 你必须学习linux编程至少基础知识..
  • 似乎需要切换到将输入间接解析为字符串的方法,而不是将直接数字输入为scanf("%d",&amp;x);
  • 2 + 3 + 7 怎么样?

标签: c integer calculator


【解决方案1】:

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

#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;
}

【讨论】:

  • 谢谢,你救了我的命!
【解决方案2】:

为简单起见,我将使用 gets() 或 **getChar() 函数读取变量并通过执行所需值的条件检查来处理。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-06-29
    • 2018-06-27
    • 2016-09-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-21
    • 1970-01-01
    相关资源
    最近更新 更多