-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathexercise_05_04.c
70 lines (59 loc) · 1.28 KB
/
exercise_05_04.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/*
* exercise_05_04.c
*
* Program that acts as a calculator.
*
* Author: Henrik Samuelsson, henrk.samuelsson(at)gmail.com
*/
#include <stdio.h>
#include <stdbool.h>
int main (void)
{
double accumulator;
double number;
char operator;
bool isCalculating;
// The accumulator shall be 0 at startup
accumulator = 0.0;
// Set flag indicating that calculations are ongoing
isCalculating = true;
printf ("Begin Calculations\n");
// Run calculations until user indicates exit
while (isCalculating)
{
// Get input from the user. Note the blank in the format string that
// tells scanf to skip leading whitespace, and the first non-whitespace
// character will be read with the %c conversion specifier.
scanf ("%lf %c", &number, &operator);
switch (operator)
{
case '+':
accumulator += number;
break;
case '-':
accumulator -= number;
break;
case '*':
accumulator *= number;
break;
case '/':
if (number == 0)
printf ("ERROR: Division by 0 is not allowed!");
else
accumulator /= number;
break;
case 'S':
accumulator = number;
break;
case 'E':
isCalculating = false;
break;
default:
printf ("ERROR: Unknown operator!\n");
break;
}
printf ("= %f\n", accumulator);
}
printf ("End of Calculations");
return 0;
}