-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalculator.c
99 lines (72 loc) · 1.85 KB
/
calculator.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*
Filename: calculator.c
partof: assignment 2
createdby: Jared
LastModifiedon: 10/12/14
*/
#include <stdio.h>
#include <string.h>
// below is a simple calculator function
void Calculator()
{
char operator;
double acc,number;
int b;
b = 0;
// creating my own boolean
printf("Begin Calculations \n");
printf("Initialize your Accumulator with data of the form \"number\" \"S\" which sets the Accumulator to the value of your number. \n");
// if b is 0 then it will run through the calculator in a loop
// when the operator E is entered after a number b becomes 1 and the loop ends.
// there are cases for +,/,*,-, and S for saving a value to the accumulator
// if something besides these are entered then it says unknown operator.
while(b == 0){
scanf("%lf %s", &number, &operator);
switch(operator){
case 'S':
acc = number;
printf("Value in the Accumulator = %lf\n", acc);
break;
case '+':
acc = acc + number;
printf("Value in the Accumulator = %lf\n", acc);
break;
case '*':
acc = acc * number;
printf("Value in the Accumulator = %lf\n", acc);
break;
case '-':
acc = acc - number;
printf("Value in the Accumulator = %lf\n", acc);
break;
case '/':
if(number == 0){
printf("Can not divide by 0.\n");
printf("Value in the Accumulator = %lf\n", acc);
break;
}
else {
acc = acc / number;
printf("Value in the Accumulator = %lf\n", acc);
break;
}
case 'E':
printf("Value in the Accumulator = %lf\n", acc);
printf("End of Calculations. \n");
b = 1;
break;
default:
printf("Unknown operator. \n");
break;
}
}
}
int main(void)
{
printf(" \n");
printf("Below is a calculator function. \n");
printf(" \n");
printf(" \n");
Calculator();
return(0);
}