Skip to content

Commit 127ea4c

Browse files
authored
Added Base-N-Calc Files (avinashkranjan#1002)
1 parent fe77510 commit 127ea4c

File tree

3 files changed

+184
-0
lines changed

3 files changed

+184
-0
lines changed

Base-N_Calc/Readme.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Base-N Calculator
2+
3+
It is a GUI based script that allows user to enter a number of any base and convert it to another. However, there is a small limitation for the conversion.
4+
5+
* The base should not be smaller than 2.
6+
* Alphabetic symbols are not used in conversions, you'll have to convert this part by yourself. To know why check this [link](https://www.mathsisfun.com/numbers/bases.html)
7+
8+
# Installation
9+
10+
Nothing to be installed, just run the script directly.
11+
12+
# Usage
13+
14+
1) Fill the required fields.
15+
2) the conversion result will be displayed in the Result Entry.
16+
17+
# Output
18+
19+
Converted number to base Y.
20+
21+
# Authors
22+
23+
Written by [XZANATOL](https://www.github.com/XZANATOL).
24+
25+
The project was built as a contribution during [GSSOC'21](https://gssoc.girlscript.tech/).

Base-N_Calc/data/GSSOC.png

47.7 KB
Loading

Base-N_Calc/script.py

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
from tkinter import *
2+
3+
def SetStatusError():
4+
""" Sets Status bar label to error message """
5+
Status["text"] = "Wronge Input(s)... :\ "
6+
Status["fg"] = "red"
7+
8+
9+
def Con_Base_X_to_Dec(num, base_x):
10+
# Converting the integeral part
11+
integeral_part = str(int(num))[::-1] # Extract integerals in reverse order
12+
i=0
13+
res = 0
14+
for number in integeral_part:
15+
element = int(number) * (base_x**i) # Convert each number to decimal
16+
res += element # Add element to result
17+
i+=1
18+
19+
# Converting the decimal part
20+
decimal_part = str(num)
21+
decimal_part = decimal_part[decimal_part.index(".")+1:] # Extract decimal part using string manipulation
22+
i = -1
23+
for decimal in decimal_part:
24+
element = int(decimal) * (base_x**i) # Convert each number to decimal
25+
res += element # Add element to result
26+
i += -1
27+
28+
# Return total result
29+
return res
30+
31+
32+
def Con_Dec_to_Base_Y(num, base_y):
33+
# Converting the integeral part
34+
integeral_part = int(num)
35+
res_int = []
36+
while int(integeral_part) != 0:
37+
integeral_part = integeral_part / base_y # Divide number by base
38+
element = (integeral_part - int(integeral_part)) * base_y # Get the remainder
39+
res_int.append(str(int(element))) # Append element
40+
res_int = res_int[::-1] # Numbers are organised from LCM to HCM
41+
42+
# Converting the decimal part
43+
decimal_part = num - int(num)
44+
res_dec = []
45+
while (decimal_part != 0):
46+
decimal_part = (decimal_part - int(decimal_part)) * base_y # Multiply decimal part by base
47+
if str(int(decimal_part)) in res_dec: # Check if not duplicated, for no infinite loops
48+
break
49+
res_dec.append(str(int(decimal_part))) # Append element
50+
51+
# Organize result
52+
if len(res_dec) == 0:
53+
res = res_int # If result has decimal numbers
54+
else:
55+
res = res_int + ["."] + res_dec # If not
56+
57+
# Return grouped result
58+
return " ".join(res)
59+
60+
61+
def Main():
62+
""" Function to validate entry fields """
63+
# <----- Validation ----->
64+
# Validate Number
65+
num = Num_Val.get()
66+
try:
67+
num = float(num)
68+
except:
69+
Num.focus()
70+
SetStatusError()
71+
return
72+
# Validate Base X
73+
base_x = Base_X_Val.get()
74+
try:
75+
base_x = int(base_x)
76+
except:
77+
Base_X.focus()
78+
SetStatusError()
79+
return
80+
# Validate Base X
81+
base_y = Base_Y_Val.get()
82+
try:
83+
base_y = int(base_y)
84+
except:
85+
Base_Y.focus()
86+
SetStatusError()
87+
return
88+
# If same bases are entered
89+
if base_x == base_y or base_x<2 or base_y<2:
90+
Status["text"] = "Huh?! -_- "
91+
Status["fg"] = "orange"
92+
return
93+
# <----- check base x value ----->
94+
if base_x == 10:
95+
Result = Con_Dec_to_Base_Y(num, base_y)
96+
if base_y == 10:
97+
Result = Con_Base_X_to_Dec(num, base_x)
98+
else:
99+
Result = Con_Base_X_to_Dec(num, base_x)
100+
Result = Con_Dec_to_Base_Y(Result, base_y)
101+
102+
Status["text"] = "Successfull Conversion! :0 "
103+
Status["fg"] = "green"
104+
Result_Entry_Val.set(Result)
105+
106+
107+
# <----- GUI Code Beginning ----->
108+
main_window = Tk()
109+
main_window.title("Base-N Calculator")
110+
Icon = PhotoImage(file="./Base-N_Calc/data/GSSOC.png")
111+
main_window.iconphoto(False, Icon)
112+
main_window.geometry("420x250")
113+
114+
115+
# <----- Elements for number that is going to be converted ----->
116+
Num_Label = Label(main_window, text="Enter Number :", anchor=E, font=("Calibri", 9))
117+
Num_Label.place(x=30,y=30)
118+
Num_Val = StringVar()
119+
Num = Entry(main_window, textvariable=Num_Val, font=("Calibri", 9))
120+
Num.place(x=120,y=32)
121+
122+
123+
# <----- Elements for Base-X ----->
124+
Base_X_Label = Label(main_window, text="Base-X :", anchor=E, font=("Calibri", 9))
125+
Base_X_Label.place(x=250,y=30)
126+
Base_X_Val = StringVar()
127+
Base_X = Entry(main_window, textvariable=Base_X_Val, font=("Calibri", 9))
128+
Base_X.place(x=305,y=32,width=30)
129+
130+
131+
# <----- Elements for Base-Y ----->
132+
Base_Y_Label = Label(main_window, text="Base-Y :", anchor=E, font=("Calibri", 9))
133+
Base_Y_Label.place(x=250,y=50)
134+
Base_Y_Val = StringVar()
135+
Base_Y = Entry(main_window, textvariable=Base_Y_Val, font=("Calibri", 9))
136+
Base_Y.place(x=305,y=52,width=30)
137+
138+
139+
# <----- Elements for calculate button ----->
140+
Calculate_Button = Button(main_window, text="Convert", font=("Calibri", 9), command=Main)
141+
Calculate_Button.place(x=180,y=75,width=80)
142+
143+
144+
# <----- Elements for Result ----->
145+
Result_Label = Label(main_window, text="Result :", anchor=E, font=("Calibri", 9))
146+
Result_Label.place(x=100,y=130)
147+
Result_Entry_Val = StringVar()
148+
Result_Entry = Entry(main_window, textvariable=Result_Entry_Val, font=("Calibri", 9))
149+
Result_Entry.configure(state='readonly')
150+
Result_Entry.place(x=150,y=130)
151+
152+
153+
# <----- Elements for Status Bar ----->
154+
Status = Label(main_window, text="Hello!! :D", fg="green", font=("Calibri", 9), bd=1, relief=SUNKEN, anchor=W, padx=3)
155+
Status.pack(side=BOTTOM, fill=X)
156+
157+
158+
# <----- Load Main Window ----->
159+
main_window.mainloop()

0 commit comments

Comments
 (0)