-
Notifications
You must be signed in to change notification settings - Fork 0
/
converter.c
170 lines (153 loc) Β· 4.28 KB
/
converter.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
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#include "base.h"
/**
* code_base - converter function
* Return: (0) on success or (1) for otherwise
*/
/**
* clear_screen - Clear the terminal screen
*/
void clear_screen(void)
{
#ifdef _WIN32
system("cls");
#else
system("clear");
#endif
}
/**
* loading_indicator - Display a simple loading indicator
*/
void loading_indicator(void)
{
printf("π Processing");
for (int i = 0; i < 3; i++)
{
printf(".");
fflush(stdout);
sleep(1);
}
printf("\n");
}
/**
* display_help - Show help menu
*/
void display_help(void)
{
printf("π HELP MENU π\n");
printf("===================================\n");
printf("π Convert numbers between Binary, Decimal,\n");
printf(" and Hexadecimal formats.\n\n");
printf("π Instructions:\n");
printf("1οΈβ£ Choose the format you are converting from.\n");
printf("2οΈβ£ Enter the number.\n");
printf("3οΈβ£ Choose the format you want to convert to.\n");
printf("4οΈβ£ View the result!\n");
printf("===================================\n");
}
int code_base(void)
{
int convert_from, convert_to;
char binary[250], hexadecimal[250];
int decimal;
char end_loop[5] = "yes";
clear_screen();
printf("################# BASE CONVERTER ##########\n");
printf("============================================\n\n");
printf("π Welcome to the Base Converter! π\n");
// Display help menu at the start or upon request
display_help();
while (true)
{
printf("\nπ Choose what you are converting from:\n");
printf("1οΈβ£ Binary\n");
printf("2οΈβ£ Decimal\n");
printf("3οΈβ£ Hexadecimal\n");
printf("0οΈβ£ Help\n\n");
scanf("%d", &convert_from);
if (convert_from == 0)
{
display_help();
continue;
}
if (convert_from < 1 || convert_from > 3)
{
printf("β Invalid choice. Please enter 1, 2, 3, or 0 for help.\n");
continue;
}
printf("\nπ Choose what you are converting to:\n");
printf("1οΈβ£ Binary\n");
printf("2οΈβ£ Decimal\n");
printf("3οΈβ£ Hexadecimal\n");
scanf("%d", &convert_to);
if (convert_to < 1 || convert_to > 3)
{
printf("β Invalid choice. Please enter 1, 2, or 3.\n");
continue;
}
// Input based on conversion type
if (convert_from == 1)
{
printf("π’ Input the Binary number: ");
scanf("%s", binary);
}
else if (convert_from == 2)
{
printf("π’ Input the Decimal number: ");
scanf("%d", &decimal);
}
else if (convert_from == 3)
{
printf("π’ Input the Hexadecimal number: ");
scanf("%s", hexadecimal);
}
// Perform the conversion
if (convert_from == 1 && convert_to == 2)
{
loading_indicator();
bin_to_dec(binary);
}
else if (convert_from == 2 && convert_to == 1)
{
loading_indicator();
dec_to_bin(decimal);
}
else if (convert_from == 1 && convert_to == 3)
{
loading_indicator();
bin_to_hex(binary);
}
else if (convert_from == 2 && convert_to == 3)
{
loading_indicator();
dec_to_hex(decimal);
}
else if (convert_from == 3 && convert_to == 1)
{
loading_indicator();
hex_to_bin(hexadecimal);
}
else if (convert_from == 3 && convert_to == 2)
{
loading_indicator();
hex_to_dec(hexadecimal);
}
else if (convert_from == convert_to)
{
printf("β οΈ Error: You cannot perform the same operation.\n");
}
else
{
printf("β Error occurred. Provide accurate instructions.\n");
return (-1);
}
printf("\nπ Type 'yes' to try again. Type 'no' to end program\n");
scanf("%s", end_loop);
if (strcasecmp(end_loop, "no") == 0)
{
printf("\nπ Goodbye! Have a great day! π\n");
break;
}
clear_screen();
}
return (0);
}