-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathscript.py
180 lines (150 loc) · 5.12 KB
/
script.py
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
from tkinter import *
def SetStatusError():
""" Sets Status bar label to error message """
Status["text"] = "Wronge Input(s)... :\ "
Status["fg"] = "red"
def Con_Base_X_to_Dec(num, base_x):
# Converting the integeral part
# Extract integerals in reverse order
integeral_part = str(int(num))[::-1]
i = 0
res = 0
for number in integeral_part:
# Convert each number to decimal
element = int(number) * (base_x**i)
# Add element to result
res += element
i += 1
# Converting the decimal part
decimal_part = str(num)
# Extract decimal part using string manipulation
decimal_part = decimal_part[decimal_part.index(".")+1:]
i = -1
for decimal in decimal_part:
# Convert each number to decimal
element = int(decimal) * (base_x**i)
# Add element to result
res += element
i += -1
# Return total result
return res
def Con_Dec_to_Base_Y(num, base_y):
# Converting the integeral part
integeral_part = int(num)
res_int = []
while int(integeral_part) != 0:
integeral_part = integeral_part / base_y # Divide number by base
element = (integeral_part - int(integeral_part)) * \
base_y # Get the remainder
# Append element
res_int.append(str(int(element)))
# Numbers are organised from LCM to HCM
res_int = res_int[::-1]
# Converting the decimal part
decimal_part = num - int(num)
res_dec = []
while (decimal_part != 0):
decimal_part = (decimal_part - int(decimal_part)) * \
base_y # Multiply decimal part by base
# Check if not duplicated, for no infinite loops
if str(int(decimal_part)) in res_dec:
break
# Append element
res_dec.append(str(int(decimal_part)))
# Organize result
if len(res_dec) == 0:
# If result has decimal numbers
res = res_int
else:
res = res_int + ["."] + res_dec # If not
# Return grouped result
return " ".join(res)
def Main():
""" Function to validate entry fields """
# <----- Validation ----->
# Validate Number
num = Num_Val.get()
try:
num = float(num)
except:
Num.focus()
SetStatusError()
return
# Validate Base X
base_x = Base_X_Val.get()
try:
base_x = int(base_x)
except:
Base_X.focus()
SetStatusError()
return
# Validate Base X
base_y = Base_Y_Val.get()
try:
base_y = int(base_y)
except:
Base_Y.focus()
SetStatusError()
return
# If same bases are entered
if base_x == base_y or base_x < 2 or base_y < 2:
Status["text"] = "Huh?! -_- "
Status["fg"] = "orange"
return
# <----- check base x value ----->
if base_x == 10:
Result = Con_Dec_to_Base_Y(num, base_y)
if base_y == 10:
Result = Con_Base_X_to_Dec(num, base_x)
else:
Result = Con_Base_X_to_Dec(num, base_x)
Result = Con_Dec_to_Base_Y(Result, base_y)
Status["text"] = "Successfull Conversion! :0 "
Status["fg"] = "green"
Result_Entry_Val.set(Result)
# <----- GUI Code Beginning ----->
main_window = Tk()
main_window.title("Base-N Calculator")
Icon = PhotoImage(file="./Base-N_Calc/data/GSSOC.png")
main_window.iconphoto(False, Icon)
main_window.geometry("420x250")
# <----- Elements for number that is going to be converted ----->
Num_Label = Label(main_window, text="Enter Number :",
anchor=E, font=("Calibri", 9))
Num_Label.place(x=30, y=30)
Num_Val = StringVar()
Num = Entry(main_window, textvariable=Num_Val, font=("Calibri", 9))
Num.place(x=120, y=32)
# <----- Elements for Base-X ----->
Base_X_Label = Label(main_window, text="Base-X :",
anchor=E, font=("Calibri", 9))
Base_X_Label.place(x=250, y=30)
Base_X_Val = StringVar()
Base_X = Entry(main_window, textvariable=Base_X_Val, font=("Calibri", 9))
Base_X.place(x=305, y=32, width=30)
# <----- Elements for Base-Y ----->
Base_Y_Label = Label(main_window, text="Base-Y :",
anchor=E, font=("Calibri", 9))
Base_Y_Label.place(x=250, y=50)
Base_Y_Val = StringVar()
Base_Y = Entry(main_window, textvariable=Base_Y_Val, font=("Calibri", 9))
Base_Y.place(x=305, y=52, width=30)
# <----- Elements for calculate button ----->
Calculate_Button = Button(main_window, text="Convert",
font=("Calibri", 9), command=Main)
Calculate_Button.place(x=180, y=75, width=80)
# <----- Elements for Result ----->
Result_Label = Label(main_window, text="Result :",
anchor=E, font=("Calibri", 9))
Result_Label.place(x=100, y=130)
Result_Entry_Val = StringVar()
Result_Entry = Entry(
main_window, textvariable=Result_Entry_Val, font=("Calibri", 9))
Result_Entry.configure(state='readonly')
Result_Entry.place(x=150, y=130)
# <----- Elements for Status Bar ----->
Status = Label(main_window, text="Hello!! :D", fg="green", font=(
"Calibri", 9), bd=1, relief=SUNKEN, anchor=W, padx=3)
Status.pack(side=BOTTOM, fill=X)
# <----- Load Main Window ----->
main_window.mainloop()