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



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

为什么我需要再次输入"q"?

我认为|| 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:nn");
  printf("1 + 2n");
  printf("1 - 2n");
  printf("1 * 2n");
  printf("1 / 2n");
  printf("1 ^ 2n");
  printf("(sqrt): s 2n");
  printf("(mod): 1 m 2n");
  printf("(div): 1 d 2nn");
  printf("Input for exit: "q"nn");
  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 = %.2lfn", add(x, y));
        break;
      case '-':
        printf("Result = %.2lfn", subtract(x, y));
        break;
      case '*':
        printf("Result = %.2lfn", multiply(x, y));
        break;
      case '/':
        if (y != 0) {
          printf("Result = %.2lfn", divide(x, y));
        } else {
          printf("nError!.n");
        }
        break;
      case '^':
        printf("Result = %.2lfn", degree(x, y));
        break;
      case 's':
        printf("Result = %.2lfn", sqrt(x));
        break;
      case 'm':
        printf("Result = %.2lfn", divide(x, y));
        break;
      case 'd':
        printf("Result = %.2lfn", 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;
}

您的scanf调用[可能]会相互干扰。

如果第一个失败,它将[可能]破坏其他的(即第一个已经从stdin中提取了数据,所以其他人不会看到任何东西)。

一种更可靠的方法是在生成的缓冲区上使用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);

我认为运行下面的代码片段会回答你的问题。

#include<stdio.h>

void main(){
double x,y;
char operation='n';
int i;
printf("Enter the variablesn");
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 %dn",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 %dn",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 %dn",operation,x,y, i);
}

这是我得到的输出

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

正在发生的事情是,第一个scanf语句读取您的输入"q"并丢弃缓冲区,因为它的格式不正确并返回0。这会导致第二个扫描运行。

第二个扫描读取您的"q"将其添加到操作对象,并返回"1",导致第三个扫描不运行。

我建议解决此问题的最佳方法,即保持标准的输入形式。总是先拿角色,然后再拿双打。

还要监控 scanf 返回的内容。它返回正确解析的标识符数。

最新更新