I was doing an exercise of the book Programming in C 4th edition, This is the exercise
/*
* 4. 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.
*
* Make certain that the program detects division by zero and also checks for
* unknown operators.
*
* By Faisal Saadatmand
*/
And for some reason when i try to exit typing E, neither my code or the code of a solution that i found on the internet worked, it always end in an infinite loop.
This is the solution i found on the internet:
#include <stdio.h>
int main(void)
{
float number, accumulator;
char operator;
printf("Begin Calculations\n\n");
scanf("%f %c", &number, &operator);
accumulator = 0;
while (operator != 'E') {
switch (operator) {
case 'S':
printf("= %f\n\n", accumulator = number);
break;
case '+':
printf("= %f\n\n", accumulator += number);
break;
case '-':
printf("= %f\n\n", accumulator -= number);
break;
case '*':
printf("= %f\n\n", accumulator *= number);
break;
case '/':
if (number == 0)
printf("Division by zero\n\n");
else
printf("= %f\n\n", accumulator /= number);
break;
default:
printf("Uknown operator\n\n");
break;
}
scanf("%f %c", &number, &operator);
}
printf("= %f\n\n", accumulator);
printf("End of Calculations\n\n");
return 0;
}
This is my solution:
#include <stdio.h>
int main(void){
float accumulator = 0, number;
char operator;
printf("Begin Calculations\n");
scanf("%f %c", &number, &operator);
while (operator != 'E'){
switch (operator){
case 'S':
if (accumulator != 0)
accumulator = number;
else
accumulator += number;
scanf("%f %c", &number, &operator);
break;
case '*':
accumulator *= number;
printf("= %f\n", accumulator);
scanf("%f %c", &number, &operator);
break;
case '/':
if (number == 0)
printf("Cannot divide by 0!");
else
accumulator /= number;
printf("= %f\n", accumulator);
scanf("%f %c", &number, &operator);
break;
case '+':
accumulator += number;
printf("= %f\n", accumulator);
scanf("%f %c", &number, &operator);
break;
case '-':
accumulator -= number;
printf("= %f\n", accumulator);
scanf("%f %c", &number, &operator);
break;
case 'E':
break;
default:
printf("Unkown operator\n");
scanf("%f %c", &number, &operator);
break;
}
}
printf("= %f\n", accumulator);
printf("End of Calculations\n");
return 0;
}