-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChain_calculator.c
More file actions
71 lines (62 loc) · 1.54 KB
/
Copy pathChain_calculator.c
File metadata and controls
71 lines (62 loc) · 1.54 KB
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
#include <stdio.h>
int calculate(int a, int b, char op){
switch (op){
case '+':
return a + b;
case '-':
return a - b;
case '*':
return a * b;
case '/':
if (b != 0){
return a / b;
}
return 0;
default:
return 0;
}
}
int processOperation(int a, int b, char op, int *result){
if (op == '/' && b == 0){
printf("Error: Division by zero is not allowed.\n");
return 0;
}else if (op != '+' && op != '-' && op != '*' && op != '/'){
printf("Invalid operator.\n");
return 0;
}else{
*result = calculate(a, b, op);
return 1;
}
}
int main(){
char op;
int a, b;
int result = 0;
char uc = 'y';
printf("Enter 1st no.: ");
scanf("%d", &a);
printf("Enter 2nd no.: ");
scanf("%d", &b);
printf("Enter choice (+,-,*,/): ");
scanf(" %c", &op);
if (processOperation(a, b, op, &result)){
printf("Current Result: %d\n", result);
}else{
result = 0;
printf("Starting with Result: %d\n", result);
}
while (uc == 'y' || uc == 'Y'){
printf("Do you want to continue? (Y/N): ");
scanf(" %c", &uc);
if (uc == 'y' || uc == 'Y'){
printf("Enter next operator (+,-,*,/): ");
scanf(" %c", &op);
printf("Enter next no.: ");
scanf("%d", &b);
if (processOperation(result, b, op, &result)){
printf("Current Result: %d\n", result);
}
}
}
return 0;
}