一行中的多个不同类型的输入

Multiple different type inputs in one line

我正在尝试制作一个二进制计算器,在输入 q 时终止程序。结果,我创建了一个名为 quit 的变量,作为输入标志 q 的行为。但是,当我为变量 q 创建 scanf 时,当未按下 q 时,它似乎将输入分开。例如,如果我输入 110 + 110 我得到:

110 + 110 = 1000

好像在做 10 + 110

输入必须全部在一行中。输入的格式始终为:

Operand operator Operand

这就是为什么我有:

scanf(" %s %c %s",Binary1, &op, Binary2);

在我的代码上。但是,如果用户输入只是 qQ,它会终止程序。

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

double convertDouble(char Bin []);
double convertBinary(double theAns);
double calculate(double num1,double num2,char op);
main(void) {
    char op;
    float num1,num2;
    char quit;
    double n1,n2,ans;
    double binAns;
    char Binary1[20];
    char Binary2[20];
    char Input[100];
    char myChar;
    int i = 0;
 // quit = 'k';
    while (quit != 'q'){

      printf("Enter an expression using binary numbers or Q to quit: "); 
   // scanf("%s", &quit);
    if(quit == 'q' ){
      exit(EXIT_SUCCESS);
    }else{

    }
    scanf(" %s %c %s",Binary1, &op, Binary2);
 /* while(Binary1[i] > 49 || Binary2[i] > 49){

         if(Binary1[i] >= 'q' || Binary1[i]<= 'Q' ){
             exit(0);
             quit = 'q';
         }else{
             printf("please enter binary: "); 
             scanf("%s %c %s\n",&Binary1, &op, &Binary2);
         }
         i++;
     } */
 // quit[0] = Binary1[0];
    printf("quit = %c\n", quit);
    printf("Binary1 =%s\n", Binary1);
    printf("op =%c\n", op);
    printf("Binary2 =%s\n", Binary2);
    n1 = convertDouble(Binary1);
    n2 = convertDouble(Binary2);
    ans = calculate(n1,n2,op);
    binAns = convertBinary(ans);

//  printf("value of n1 = %f\n", n1);
//  printf("value of n2 = %f\n", n2);
//  printf("value of binAns = %f\n", binAns);
//  printf("ans = %f", ans);

    printf(" = %f\n",binAns);
  } // end of while

    printf("quit = %c\n", quit);
    printf("Binary1 =%s\n", Binary1);
    printf("op =%c\n", op);
    printf("Binary2 =%s\n", Binary2);
    printf("quit");
    exit(EXIT_SUCCESS);
}

注:

关于我的程序的更多信息:我将输入二进制文件作为字符串的原因是因为它需要将它们转换为 double。它在一个名为 convertDouble 的不同函数中执行此操作,此处未粘贴。它计算函数计算中的答案(也未粘贴)。答案在convertBinary函数中转换为二进制,然后显示。

示例 运行

Enter an expression using binary numbers or Q to quit: 1101.001 * 101.1101
= 1001100.0100101
Enter an expression using binary numbers or Q to quit: 0.0000000001 * 0.0000000001
= 0.00000000000000000001
Enter an expression using binary numbers or Q to quit: 0.111 * 1000.0
= 111.0
Enter an expression using binary numbers or Q to quit: 11.0 * 11.0
= 1001.0
Enter an expression using binary numbers or Q to quit: 11.11 + 11.11
= 111.1
Enter an expression using binary numbers or Q to quit: 101.1 / 10.0
= 10.11
Enter an expression using binary numbers or Q to quit: 1001.11 - 11.01
= 110.1
Enter an expression using binary numbers or Q to quit: q

您可以读取文本行(函数 getlinegets_s)并解析它(函数 sscanf)。尝试这样的事情:

char buffer[128];
int nbParsedItems;
printf("Enter an expression using binary numbers or Q to quit: ");
gets_s(buffer, 127);
if (buffer[0] == 'q' || buffer[0] == 'Q')
  return;
nbParsedItems = sscanf(buffer, " %s %c %s", Binary1, &op, Binary2);
if (nbParsedItems == 3) /* in other cases you should ask user to enter request again */
{
  /* calculate */
}

Ctrl+C 是终止控制台程序的标准方法。但是如果你想使用 'q' 作为终止信号,你可以执行以下操作:

char line[128];
float a, b;
char op;

while(true) {
    printf("Enter an expression using binary numbers or q to quit: ");
    if( gets(line) ) {
        if( strcmp(line, "q") == 0 || strcmp(line, "Q") == 0 ) {
            printf("See you later");
            break;
        }
        if( sscanf(line, "%f %c %f", &a, &op, &b) != 3 ) {
            printf("Error: wrong expression. Please try again");
            continue;
        }
        ...
    }
}

保持代码尽可能简单,保持功能所需的变量和函数数量最少。

认真思考程序的每一个需求,把它们当成一个个的小问题...

像这样...

#include <stdio.h>
#include <stdlib.h>

#define BUFLEN 32

double convertToDouble(const char *chars) {
    return strtod(chars, NULL);
}

double calculateAnswer(double left, char operator, double right) {
    switch (operator) {
        case '<': return left < right; break;
        case '>': return left > right; break;
        case '=': return left == right; break;
        case '*': return left * right; break;
        case '/': return left / right; break;
    }

    /* do something */
    return -1;
}

int main(int argc, char *argv[]) {
    char buffer[BUFLEN*2];

    printf("Enter an expression using binary numbers or Q to quit: ");  

    while (fgets(buffer, BUFLEN, stdin)) {
        char left[BUFLEN] = {0},
              right[BUFLEN] = {0},
              operator = 0;

        if(sscanf(buffer, "%s %c %s\n", left, &operator, right)) {
            if (!operator) {
                if (left[0] == 'q' || left[0] == 'Q') {
                    printf("Bye!\n");
                    break;
                }
            }

            printf("%s %c %s = %f\n", 
                left, operator, right, 
                calculateAnswer(
                    convertToDouble(left), 
                    operator,
                    convertToDouble(right)));
        } else {
            /* do something */
        }

        printf("Enter an expression using binary numbers or Q to quit: ");
    }

    return 0;
}

请注意,此代码并不意味着正确,它只是您描述的要求的近似值,无需付出太多努力。

更重要的是,它很容易理解。