-
Notifications
You must be signed in to change notification settings - Fork 0
/
Cal-Plus-Exsp.c
59 lines (52 loc) · 1.58 KB
/
Cal-Plus-Exsp.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
#include<stdio.h>
#include<math.h> // As the name suggests, it's a math library
#include<setjmp.h> // This library is for custume exceptions
double add(double x, double y) {return x + y;};
double sub(double x, double y) {return x - y;};
double mul(double x, double y) {return x * y;};
double dIv(double x, double y) {return x / y;};
// Setting an exception message as 'buf'
jmp_buf buf;
// Function to raise an exception
void raise_an_exception() {
longjmp(buf, 1);
}
int main() {
char operation;
double x, y;
double result;
printf("Select the operation you want (+ - * /) : ");
scanf("%c", &operation);
printf("Type two real numbers :");
scanf("%lf %lf", &x, &y);
// Begin the calculations
switch(operation) {
case '+':
result = add(x, y);
break;
case '-':
result = sub(x, y);
break;
case '*':
result = mul(x, y);
break;
case '/':
result = dIv(x, y);
break;
default:
printf("Enter a valid operation symbol!");
raise_an_exception();
if(setjmp(buf)) {
printf("Oooops, you seems to have typed an invalid operation! :(");
return 1;
}
break;
}
// Checking if there is any decimal numbers behinb the decimal point!
// 'fmod' here is being used as '%' in Python, same functionality as module.
if (fmod(result, 1) == 0) {
printf("%0.0lf", result);
} else {
printf("%lf", result);
}
}