简体   繁体   中英

Exercise #5 Chapter 6 of Programming in C

Write a program that acts as a simple "printing" calculator. The program should allow the user to type in expressions of the form: number operator. The following operators should be recognized by the program: +, -, *, /, S. E

  • The S operator tells the program to set the "accumulator" to the typed-in number.
  • The E operator tells the program that execution is to end.The arithmetic operations are performed on the contents of the accumulator with the number that was keyed in acting as the second operand.

The following is a "sample run" showing how the program should operate:

 Begin Calculations 10 S Set Accumulator to 10 = 10.000000 Contents of Accumulator 2 / Divide by 2 = 5.000000 Contents of Accumulator 55 - Subtract 55 -50.000000 100.25 S Set Accumulator to 100.25 = 100.250000 4 * Multiply by 4 = 401.000000 0 E End of program = 401.000000 End of Calculations. 

Here's my code

#include <stdio.h>

int main(void)
{
char op;
float acc = 0, num;

printf("Begin Calculations\n");
while (op != 'E') {
    scanf("%f %c", &num, &op);
    switch (op) {
        case '+':
            acc += num;
            printf("= %f\n", acc);
            break;
        case '-':
            acc -= num;
            printf("= %f\n", acc);
            break;
        case '*':
            acc *= num;
            printf("= %f\n", acc);
            break;
        case '/':
            if (num == 0) {
                printf("Division by zero\n");
            }
            else {
                acc /= num;
                printf("= %f\n", acc);
            }
            break;
        case 'S':
            acc = num;
            printf("= %f\n", acc);
            break;
        case 'E':
            printf("= %f\n", acc);
            break;
        default:
            printf("Unknown operator\n");
            break;
    }
}
printf("End of Calculations");
}

My code work perfectly with the question input. However, when I input the following (and the output) (same input as question but without space between "number" and "operator"):

Begin Calculations
    10S            
    = 10.000000    
    2/            
    = 5.000000      
    55-            
    -50.000000
    100.25S        
    = 100.250000
    4*            
    = 401.000000
    0E   

The program stuck when I input 0E. I have tried to change scanf("%f %c", &num, &op); to scanf("%f%c", &num, &op); but it didn't seem to solve my problem. Could you point out the problem in my code? Please give me a detail explaination because I'm relative new to C. Thank you in advance for any help you can provide.

You are reading a float value. So if scanf sees 0E, it awaits more digits as it thinks you are inputting an exponential as eg 0E5 (=0*10^5). That's why your program gets stuck at this point. I would suggest to use a different character to end to avoid this ambiguity.

If, on the other hand, scanf sees 0S, it will output 0.0 as float value and 'S' as character, as 'S' is never a valid part of a float value.

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