简体   繁体   中英

Multiple different type inputs in one line

I'm trying to make a binary calculator that terminates the the program when q is entered. As a result I made a variable called quit to act as the flag q is entered. However when I made the scanf for the variable q it seems to take away split the inputs up when q is not pressed. for example, if I enter 110 + 110 I get:

110 + 110 = 1000

as if it's doing 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.

#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 . It does this in a different function called convertDouble which is not pasted here. 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.

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 ). 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. But if you want to use 'q' as terminate signal, you can do the following:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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