-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
429 lines (366 loc) · 14.4 KB
/
Copy pathapp.py
File metadata and controls
429 lines (366 loc) · 14.4 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
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
from flask import Flask, render_template, request, redirect, url_for, jsonify
app = Flask(__name__)
# --- Helper Functions (Define ALL functions first) ---
# Binary to other number system convertions.
def binary_to_decimal(binary_input):
if binary_input is None:
return "Binary input missing!"
binary_input = str(binary_input).strip()
if binary_input == "":
return "Binary input missing!"
result = 0
if '.' in binary_input:
try:
# Check for multiple decimal points
if binary_input.count('.') > 1:
return "Invalid binary input!"
integer_part, fractional_part = binary_input.split('.')
# Handle empty parts like ".101" or "101."
if not integer_part:
integer_part = "0"
if not fractional_part:
fractional_part = "0"
for digit in integer_part + fractional_part:
if digit not in ['0', '1']:
return "Binary inputs can only be 0s and 1s"
# Convert integer part
for i in range(len(integer_part)):
digit = int(integer_part[i])
power = len(integer_part) - i - 1
result = result + digit * (2 ** power)
# Convert fractional part
for j in range(len(fractional_part)):
digit = int(fractional_part[j])
result = result + digit * (2 ** -(j + 1))
return result
except ValueError:
return "Something went wrong!"
else:
integer_part = binary_input
for digit in integer_part:
if digit not in ['0', '1']:
return "Binary inputs can only be 0s and 1s"
for i in range(len(integer_part)):
digit = int(integer_part[i])
power = len(integer_part) - i - 1
result = result + digit * (2 ** power)
return result
def binary_to_hexadecimal(binary_input):
if binary_input is None:
return "Binary input missing!"
binary_input = str(binary_input).strip()
hexadecimal_values = {
'0000': '0', '0001': '1', '0010': '2', '0011': '3',
'0100': '4', '0101': '5', '0110': '6', '0111': '7',
'1000': '8', '1001': '9', '1010': 'A', '1011': 'B',
'1100': 'C', '1101': 'D', '1110': 'E', '1111': 'F'
}
if '.' in binary_input:
if binary_input.count('.') > 1:
return "Invalid binary input!"
integer_part, fractional_part = binary_input.split('.')
if not integer_part:
integer_part = "0"
if not fractional_part:
fractional_part = "0"
else:
integer_part = binary_input
fractional_part = ''
for ch in integer_part + fractional_part:
if ch not in ('0', '1', ''):
return "Binary inputs can only be 0s and 1s"
# Pad integer part
while len(integer_part) % 4 != 0:
integer_part = '0' + integer_part
if not integer_part: # Handle case of just ".101"
integer_part = "0000"
hex_integer = ''
for i in range(0, len(integer_part), 4):
four_bit_group = integer_part[i:i+4]
hex_integer += hexadecimal_values.get(four_bit_group, '?')
# Remove leading zeros unless it's the only digit
if len(hex_integer) > 1 and hex_integer.startswith('0'):
hex_integer = hex_integer.lstrip('0')
hex_fraction = ''
if fractional_part:
# Pad fractional part
while len(fractional_part) % 4 != 0:
fractional_part = fractional_part + '0'
for i in range(0, len(fractional_part), 4):
four_bit_group = fractional_part[i:i+4]
hex_fraction += hexadecimal_values.get(four_bit_group, '?')
# Remove trailing zeros from fraction
hex_fraction = hex_fraction.rstrip('0')
if hex_fraction:
return hex_integer + '.' + hex_fraction
else:
return hex_integer
def decimal_to_binary(decimal_input):
if decimal_input is None:
return "Decimal input missing!"
decimal_input = str(decimal_input).strip()
try:
decimal_number = float(decimal_input)
except ValueError:
return "Invalid decimal input!"
integer_part = int(decimal_number)
fractional_part = decimal_number - integer_part
remainders = []
if integer_part == 0:
binary_number = "0"
else:
while integer_part > 0:
remainder = integer_part % 2
remainders.append(str(remainder))
integer_part //= 2
binary_number = ''.join(reversed(remainders))
if fractional_part > 0:
binary_fraction = []
count = 0
# Limit precision to avoid infinite loops
while fractional_part > 0 and count < 10:
fractional_part = fractional_part * 2
binary_bit = int(fractional_part)
binary_fraction.append(str(binary_bit))
fractional_part -= binary_bit
count += 1
binary_number = binary_number + '.' + ''.join(binary_fraction)
return binary_number
def decimal_to_octal(decimal_input):
try:
decimal_number = float(decimal_input)
except ValueError:
return "Invalid decimal input!"
integer_part = int(decimal_number)
fractional_part = decimal_number - integer_part
remainders = []
if integer_part == 0:
octal_number = '0'
else:
while integer_part > 0:
remainder = integer_part % 8
remainders.append(str(remainder))
integer_part //= 8
octal_number = ''.join(reversed(remainders))
if fractional_part > 0:
octal_fraction = []
count = 0
while fractional_part > 0 and count < 10: # Limit precision
fractional_part *= 8
digit = int(fractional_part)
octal_fraction.append(str(digit))
fractional_part -= digit
count += 1
octal_number = octal_number + '.' + ''.join(octal_fraction)
return octal_number
def decimal_to_hexadecimal(decimal_input):
try:
decimal_number = float(decimal_input)
except ValueError:
return "Invalid decimal input!"
integer_part = int(decimal_number)
fractional_part = decimal_number - integer_part
hex_digits = '0123456789ABCDEF'
remainders = []
if integer_part == 0:
hex_number = '0'
else:
while integer_part > 0:
remainder = integer_part % 16
remainders.append(hex_digits[remainder])
integer_part //= 16
hex_number = ''.join(reversed(remainders))
if fractional_part > 0:
hex_fraction = []
count = 0
while fractional_part > 0 and count < 10: # Limit precision
fractional_part *= 16
digit = int(fractional_part)
hex_fraction.append(hex_digits[digit])
fractional_part -= digit
count += 1
hex_number = hex_number + '.' + ''.join(hex_fraction)
return hex_number
def octal_to_decimal(octal_input):
if octal_input is None:
return "Invalid octal input!"
octal_input = str(octal_input).strip()
try:
if octal_input.count('.') > 1:
return "Invalid octal input!"
integer_part = octal_input
fractional_part = ""
if '.' in octal_input:
integer_part, fractional_part = octal_input.split('.')
if not integer_part:
integer_part = "0"
if not fractional_part:
fractional_part = "0"
for ch in integer_part + fractional_part:
if ch not in '01234567':
return "Invalid octal input!"
result = 0
# Convert integer part
for i in range(len(integer_part)):
result += int(integer_part[i]) * (8 ** (len(integer_part) - i - 1))
# Convert fractional part
for j in range(len(fractional_part)):
result += int(fractional_part[j]) * (8 ** -(j + 1))
return result
except Exception:
return "Conversion error!"
def hexadecimal_to_decimal(hex_input):
if hex_input is None:
return "Invalid hexadecimal input!"
hex_input = str(hex_input).upper().strip()
try:
hex_digits = '0123456789ABCDEF'
integer_part = hex_input
fractional_part = ""
if '.' in hex_input:
if hex_input.count('.') > 1:
return "Invalid hexadecimal input!"
integer_part, fractional_part = hex_input.split('.')
if not integer_part:
integer_part = "0"
if not fractional_part:
fractional_part = "0"
for ch in integer_part + fractional_part:
if ch not in hex_digits:
return "Invalid hexadecimal input!"
result = 0
# Convert integer part
for i in range(len(integer_part)):
result += hex_digits.index(integer_part[i]) * (16 ** (len(integer_part) - i - 1))
# Convert fractional part
for j in range(len(fractional_part)):
result += hex_digits.index(fractional_part[j]) * (16 ** -(j + 1))
return result
except Exception:
return "Invalid hexadecimal input!"
# --- REFACTORED/DRY Functions ---
# These functions reuse the ones above
def binary_to_octal(binary_input):
"""
Refactored to be DRY (Don't Repeat Yourself).
Converts Binary -> Decimal -> Octal.
"""
if binary_input is None:
return "Binary input missing!"
binary_input = str(binary_input).strip()
# 1. First, convert binary to decimal
decimal_value = binary_to_decimal(binary_input)
if isinstance(decimal_value, str):
# This means binary_to_decimal returned an error message
return decimal_value
# 2. Now, reuse your existing function to convert decimal to octal
return decimal_to_octal(str(decimal_value))
def octal_to_binary(octal_input):
"""
Converts Octal -> Decimal -> Binary.
"""
if octal_input is None:
return "Octal input missing!"
octal_input = str(octal_input).strip()
try:
decimal_value = octal_to_decimal(octal_input)
if isinstance(decimal_value, str):
return decimal_value
return decimal_to_binary(str(decimal_value))
except Exception:
return "Conversion error!"
def octal_to_hexadecimal(octal_input):
"""
Converts Octal -> Decimal -> Hexadecimal.
"""
if octal_input is None:
return "Invalid Octal Input!"
octal_input = str(octal_input).strip()
try:
decimal_value = octal_to_decimal(octal_input)
if isinstance(decimal_value, str):
return decimal_value
return decimal_to_hexadecimal(str(decimal_value))
except Exception:
return "Conversion error!"
def hexadecimal_to_binary(hex_input):
"""
Converts Hexadecimal -> Decimal -> Binary.
(Note: Direct conversion is often faster, but this reuses code)
"""
if hex_input is None:
return "Invalid hexadecimal input!"
hex_input = str(hex_input).strip()
try:
decimal_value = hexadecimal_to_decimal(hex_input)
if isinstance(decimal_value, str):
return decimal_value
return decimal_to_binary(str(decimal_value))
except Exception:
return "Conversion error!"
def hexadecimal_to_octal(hex_input):
"""
Converts Hexadecimal -> Decimal -> Octal.
(FIXED TYPO: hex_input)
"""
if hex_input is None: # Fixed typo here
return "Invalid hexadecimal input!"
hex_input = str(hex_input).strip()
try:
decimal_value = hexadecimal_to_decimal(hex_input)
if isinstance(decimal_value, str):
return decimal_value
return decimal_to_octal(str(decimal_value))
except Exception:
return "Conversion error!"
# --- CONVERSION_MAP (Must be defined AFTER functions) ---
CONVERSION_MAP = {
('binary', 'decimal'): binary_to_decimal,
('binary', 'octal'): binary_to_octal,
('binary', 'hexadecimal'): binary_to_hexadecimal,
('decimal', 'binary'): decimal_to_binary,
('decimal', 'octal'): decimal_to_octal,
('decimal', 'hexadecimal'): decimal_to_hexadecimal,
('octal', 'binary'): octal_to_binary,
('octal', 'decimal'): octal_to_decimal,
('octal', 'hexadecimal'): octal_to_hexadecimal,
('hexadecimal', 'binary'): hexadecimal_to_binary,
('hexadecimal', 'decimal'): hexadecimal_to_decimal,
('hexadecimal', 'octal'): hexadecimal_to_octal,
}
# --- Flask Routes ---
@app.route('/')
def home():
# This route still renders the full page, which is correct
return render_template('index.html', from_option='binary', to_option='decimal')
@app.route('/calculate', methods=['POST'])
def check_convertion():
from_option = request.form.get('from-option')
to_option = request.form.get('to-option')
input_number = request.form.get('input-number')
result = "" # Default result
# Basic guards
if input_number is None or str(input_number).strip() == "":
result = "Please enter a number to convert."
# *** CHANGED ***
# Instead of rendering a template, return JSON
return jsonify({'result': result})
elif from_option == to_option:
result = "You chose the same convertion! Try different one."
return jsonify({'result': result})
else:
# Use the dispatch map
conversion_key = (from_option, to_option)
if conversion_key in CONVERSION_MAP:
# Look up the correct function from the map
conversion_function = CONVERSION_MAP[conversion_key]
# Call it
input_val = str(input_number).strip()
result = conversion_function(input_val)
else:
# This should ideally not be reachable
result = "Error: Conversion type not supported."
# Return the final result as JSON
return jsonify({'result': result})
if __name__ == '__main__':
app.run(debug=True)