简体   繁体   English

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

[英]Multiple different type inputs in one line

I'm trying to make a binary calculator that terminates the the program when q is entered. 我正在尝试制作一个二进制计算器,在输入q时终止程序。 As a result I made a variable called quit to act as the flag q is entered. 结果,我创建了一个名为quit的变量,以便在输入标志q起作用。 However when I made the scanf for the variable q it seems to take away split the inputs up when q is not pressed. 然而,当我为变量q制作scanf ,似乎在没有按下q情况下将输入分开。 for example, if I enter 110 + 110 I get: 例如,如果我输入110 + 110我得到:

110 + 110 = 1000

as if it's doing 10 + 110 . 好像它正在做10 + 110

The input has to be all in one line. 输入必须全部在一行中。 The format of the input is always going to be: 输入的格式始终是:

Operand operator Operand 操作数操作符操作数

Which is why I have the: 这就是为什么我有:

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

on my code. 在我的代码上。 However if the user input is just q or Q it terminates the program. 但是,如果用户输入只是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);
}

Note: 注意:

More info about my program: the reason why I have the input binaries as strings was because it needs to convert them to a double . 有关我的程序的更多信息:我将输入二进制文件作为字符串的原因是因为它需要将它们转换为double It does this in a different function called convertDouble which is not pasted here. 它在一个名为convertDouble的不同函数中执行此操作,此函数未粘贴在此处。 It calculates the answer in the function calculate(also not pasted). 它计算函数计算中的答案(也未粘贴)。 The answer is converted to a binary in the convertBinary function and then is displayed. 答案在convertBinary函数中转换为二进制,然后显示。

Example of How it's supposed to run 它应该如何运行的示例

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

Keep code as simple as possible, keep the minimum number of variables and functions required for functionality. 保持代码尽可能简单,保留功能所需的最小数量的变量和函数。

Think carefully about each requirement of the program, treat them as individual small problems ... 仔细考虑该计划的每个要求,将它们视为个别小问题......

Like this ... 像这样 ...

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

Note that, this code is not meant to be correct, it's an approximation of the requirements you describe without putting in too much effort. 请注意,此代码并不正确,它是您描述的要求的近似值,而不需要付出太多努力。

Much more importantly, it's easy to understand. 更重要的是,它很容易理解。

You can read line of text (function getline or gets_s ) and parse it (function sscanf ). 您可以读取文本行(函数getlinegets_s )并解析它(函数sscanf )。 Try something like this: 尝试这样的事情:

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 is standard way to terminate console program. Ctrl + C是终止控制台程序的标准方法。 But if you want to use 'q' as terminate signal, you can do the following: 但是如果你想使用'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;
        }
        ...
    }
}

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

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