-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalc.HC
More file actions
98 lines (84 loc) · 2.02 KB
/
Copy pathcalc.HC
File metadata and controls
98 lines (84 loc) · 2.02 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
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
/*
* Simple HolyC Calculator
* Designed for TempleOS Environment
* I'm just learning HolyC, so this is a basic implementation to demonstrate classes, functions, and exception handling.
*/
class Calc
{
public:
F64 Add(F64 a, F64 b)
{
return a + b;
}
F64 Sub(F64 a, F64 b)
{
return a - b;
}
F64 Mul(F64 a, F64 b)
{
return a * b;
}
F64 Div(F64 a, F64 b)
{
if (b == 0)
throw(1); // Throw exception for divide by zero
return a / b;
}
};
U0 Main()
{
Calc c;
F64 num1, num2, result;
U8 op;
I64 running = 1;
Cls;
Print("=== HolyC Calculator ===\n");
Print("Press 'q' in operator field to quit.\n");
while (running) {
Print("\n------------------------\n");
// Input First Number
Print("Enter first number: ");
Scanf("%f", &num1);
// Input Operator
// Note: The space before %c skips whitespace/newlines
Print("Enter operator (+ - * / q): ");
Scanf(" %c", &op);
if (op == 'q' || op == 'Q') {
running = 0;
continue;
}
// Input Second Number
Print("Enter second number: ");
Scanf("%f", &num2);
// Process Calculation with Exception Handling
try {
switch (op) {
case '+':
result = c.Add(num1, num2);
Print("Result: %f\n", result);
break;
case '-':
result = c.Sub(num1, num2);
Print("Result: %f\n", result);
break;
case '*':
result = c.Mul(num1, num2);
Print("Result: %f\n", result);
break;
case '/':
result = c.Div(num1, num2);
Print("Result: %f\n", result);
break;
default:
Print("Invalid Operator!\n");
}
} catch (I64 err) {
// Catch the throw(1) from Div function
Print("Math Error: Division by Zero!\n");
}
Print("\nPress Enter to continue...");
GetChar(); // Wait for user input
Cls; // Clear screen for next operation
}
Print("Goodbye.\n");
}