-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalculator.c
More file actions
78 lines (61 loc) · 1.38 KB
/
calculator.c
File metadata and controls
78 lines (61 loc) · 1.38 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
#include <stdio.h>
#include <stdlib.h>
#include "calculation.h"
#include "io.h"
#include "utilities.h"
int main(int argc, char *argv[])
{
int a; // first number to do calculation on
int b; // second number to do calculation on
int res; // the result of the calculation
char* res_str = NULL;
enum EStatus err; // errror value
enum EOperation operation; // the operation to do on variables a and b
enum EBase base = EBase_Decimal; // which base the input and output will be
while(1)
{
err = get_op(&operation);
if(err != EStatus_Success)
{
handle_error(err);
goto EndOfCalculation;
}
if(operation == EOperation_ExitProgram)
{
break;
}
err = get_input(&a, &b, operation, base);
if(err != EStatus_Success)
{
handle_error(err);
goto EndOfCalculation;
}
if (operation == EOperation_ChangeBase)
{
err = change_base(a, &base);
if(err != EStatus_Success)
{
handle_error(err);
}
goto EndOfCalculation;
}
err = calculate_result(a, b, operation, &res);
if (err == EStatus_Overflow)
{
printf("An overflow happend during the calculation\n");
}
else if(err != EStatus_Success)
{
handle_error(err);
goto EndOfCalculation;
}
res_str = int_to_base_string(res, base);
printf("the result of this calculation is: %s\n", res_str);
if (res_str != NULL)
{
free(res_str);
}
EndOfCalculation:;
}
return 0;
}