-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathio.c
More file actions
145 lines (111 loc) · 2.21 KB
/
io.c
File metadata and controls
145 lines (111 loc) · 2.21 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include "io.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define STRING_SIZE 50
enum EStatus get_op(enum EOperation* op)
{
enum EOperation temp;
if (op == NULL)
{
return EStatus_NullReferenceError;
}
puts("Which operation would you like to take?\n");
for (int i = (int)EOperation_FirstOperation; i < EOperation_NumOfOperations; i++)
{
printf("%i. %s\n", i, EOperation_ToString[i]);
}
char str[STRING_SIZE];
memset(str, '\0', sizeof(str));
char *s = fgets(str, STRING_SIZE-1, stdin);
if (s == NULL)
{
return EStatus_IOError;
}
if (!isinteger(str, EBase_Decimal))
{
return EStatus_BadOpInput;
}
temp = atoi(str);
if (!(EOperation_FirstOperation <= temp && temp < EOperation_NumOfOperations))
{
return EStatus_BadOpInput;
}
*op = temp;
return EStatus_Success;
}
enum EStatus get_number(int* x, enum EBase base)
{
if (x == NULL)
{
return EStatus_NullReferenceError;
}
char str[STRING_SIZE];
memset(str, '\0', sizeof(str));
char *s = fgets(str, STRING_SIZE-1, stdin);
if (s == NULL)
{
return EStatus_IOError;
}
if (!isinteger(str, base))
{
return EStatus_BadInput;
}
*x = strtol(str, NULL, base);
return EStatus_Success;
}
enum EStatus get_input(int* a, int* b, enum EOperation op, enum EBase base)
{
if (a == NULL || b == NULL)
{
return EStatus_NullReferenceError;
}
enum EStatus err;
if(operationUsesTwoOperands(op))
{
printf("Enter the first number: ");
err = get_number(a, base);
if (err != EStatus_Success)
{
return err;
}
printf("Enter the second number: ");
err = get_number(b, base);
if (err != EStatus_Success)
{
return err;
}
}
else if (op == EOperation_ChangeBase)
{
puts("Which base do you want to switch to?\n");
puts("1. Binary");
puts("2. Octal");
puts("3. Decimal");
puts("4. Hexadecimal");
err = get_number(a, EBase_Decimal);
if (err != EStatus_Success)
{
return err;
}
if (a < 1 && 4 < a)
{
return EStatus_BadBaseInput;
}
}
else if (operationUsesOneOperand(op))
{
printf("Enter a number: ");
err = get_number(a, base);
if (err != EStatus_Success)
{
return err;
}
}
else
{
return EStatus_OperationNotValid;
}
return EStatus_Success;
}
#undef STRING_SIZE