简体   繁体   English

如何通过一次而不是两次输入q字符来退出程序?

[英]How can I exit the program by entering the q character once, not twice?

How can I exit the program by entering the q character once, not twice? 如何通过一次而不是两次输入q字符来退出程序?

Why do I need to enter "q" for once more? 为什么我需要再次输入“ q”?

I think the expression || scanf("%c", &operation) 我认为表达|| scanf("%c", &operation) || scanf("%c", &operation) did not work. || scanf("%c", &operation)无法正常工作。

/*
 * Name: Calculator.
 * Description: Calculator for simple math operations.
 *
 * Compiler: Apple LLVM version 10.0.0 (clang-1000.11.45.5).
 * Coding style: Google.
 */

#include <locale.h>
#include <math.h>
#include <stdio.h>

#define TRUE 1
#define EXIT 'q'

double add(double x, double y);
double subtract(double x, double y);
double multiply(double x, double y);
double divide(double x, double y);
double degree(double x, int y);
double sqrt(double x);          
double mod(double x, double y);
double div(double x, double y);

int main(void) {
  char *locale = setlocale(LC_ALL, "");

  printf("Examples:\n\n");

  printf("1 + 2\n");
  printf("1 - 2\n");
  printf("1 * 2\n");
  printf("1 / 2\n");
  printf("1 ^ 2\n");
  printf("(sqrt): s 2\n");
  printf("(mod): 1 m 2\n");
  printf("(div): 1 d 2\n\n");

  printf("Input for exit: \"q\"\n\n");

  while (TRUE) {
    double x, y;
    char operation;

    scanf("%lf %c %lf", &x, &operation, &y) ||
        scanf("%c %lf", &operation, &x) || scanf("%c", &operation);

    switch (operation) {
      case ' ':
        break;

      case '\n':
        break;

      case '+':
        printf("Result = %.2lf\n", add(x, y));
        break;

      case '-':
        printf("Result = %.2lf\n", subtract(x, y));
        break;

      case '*':
        printf("Result = %.2lf\n", multiply(x, y));
        break;

      case '/':
        if (y != 0) {
          printf("Result = %.2lf\n", divide(x, y));
        } else {
          printf("\nError!.\n");
        }
        break;

      case '^':
        printf("Result = %.2lf\n", degree(x, y));
        break;

      case 's':
        printf("Result = %.2lf\n", sqrt(x));
        break;

      case 'm':
        printf("Result = %.2lf\n", divide(x, y));
        break;

      case 'd':
        printf("Result = %.2lf\n", divide(x, y));
        break;

      case EXIT:
        printf("Input symbol \"%c\"\nExit...\n", EXIT);
        return 0;
    }
  }

  return 0;
}

double add(double x, double y) { return (x + y); }

double subtract(double x, double y) { return (x - y); }

double multiply(double x, double y) { return (x * y); }

double divide(double x, double y) { return (x / y); }

double degree(double x, int y) {
  int response = 1;
  while (y) {
    if (y & 1) response *= x;
    x *= x;
    y >>= 1;
  }
  return response;
}

Your scanf calls will [probably] interfere with one another. 您的scanf呼叫将[可能]相互干扰。

If the first one fails, it will [probably] disrupt the others (ie the first one has already pulled the data from stdin , so the others won't see anything). 如果第一个失败,它将(可能)破坏其他(即第一个已经从stdin提取数据,因此其他将看不到任何东西)。

A surer way is to use fgets and sscanf on the resulting buffer: 一种更可靠的方法是在结果缓冲区上使用fgetssscanf

 do {
    char buf[1000];

    fgets(buf,sizeof(buf),stdin);

    if (sscanf(buf,"%lf %c %lf", &x, &operation, &y) == 3)
        break;

    if (sscanf(buf,"%c %lf", &operation, &x) == 2)
        break;

    if (sscanf(buf,"%c", &operation) == 1)
        break;

    // error ...
} while (0);

I think running the code snippet below will answer your question. 我认为运行下面的代码片段将回答您的问题。

#include<stdio.h>


void main(){

double x,y;
char operation='n';
int i;

printf("Enter the variables\n");

i=scanf("%lf %c %lf",&y, &operation, &x);
printf("This is operation, %c and this is long float %lf, this is y %lf  and i %d\n",operation,x,y, i);

i=scanf("%c %lf", &operation, &x);
printf("This is operation, %c and this is long float %lf, this is y %lf  and i %d\n",operation,x,y, i);

i=scanf("%c", &operation);
printf("This is operation, %c and this is long float %lf, this is y %lf  and i %d\n",operation,x,y, i);

}

This is the output I got 这是我得到的输出

Enter the variables
x
This is operation, n and this is long float 0.000000, this is y 0.000000  and i 0
x
This is operation, x and this is long float 0.000000, this is y 0.000000  and i 1
This is operation, x and this is long float 0.000000, this is y 0.000000  and i 1

What is happening is that the first scanf statement reads your input 'q' and discards the buffer as it is not formatted properly and return the 0. Which causes the second scanf to run. 发生的情况是,第一个scanf语句读取您的输入'q'并丢弃缓冲区,因为该缓冲区未正确格式化并返回0。这将导致第二个scanf运行。

The second scanf reads your 'q' adds it to the operation object, and returns a '1' causing the third scand to not run. 第二个scanf读取您的“ q”,将其添加到操作对象,并返回“ 1”,导致第三个扫描不运行。

I would suggest the best way to solve this issue, it to keep a standard form of input. 我建议解决此问题的最佳方法,即保持标准的输入形式。 Always take characters first, and the doubles later. 总是先取字符,然后再取双。

Also monitor what scanf returns. 还监视scanf返回什么。 It returns the number of identifiers properly parsed. 它返回正确解析的标识符的数量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM