Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Sample code for ADA topic 1 Date: 2 0 - 1 0 - 2 0 2 2 Following should be inside ratNum.py

"""
Sample code for ADA topic 1
Date: 20-10-2022
Following should be inside "ratNum.py"
"""
# find greatest common divisor
# using the fact that gcd(a,b)= gcd(b,c) where c = a % b
# if b is zero, gcd(a,b)= gcd(a,0)= a
def gcd(a,b):
while b !=0:
r = a % b
a = b
b = r
return a
# find least common multiple using gcd() function
def lcm(a, b):
return a*b // gcd(a,b)
# ratNum representing rational numbers (a/b)
class ratNum:
# constructor for creating a rational number, default is zero
def __init__(self, a=0, b=1):
self.a = a
if b >0: self.b = b
else:
print("b shound be greater than zero!")
self.b =1
def input(self): # input a rational number from keyboard
while True:
a, b = input("input a/b (b>0): ").split("/")
a, b = int(a), int(b)
if b >0: break
print("b shound be greater than zero!")
self.a = a
self.b = b
def __str__(self): # convert to str for display
return f"{self.a}/{self.b}"
def __add__(self, y): # rational number addition
b = lcm(self.b, y.b)
a = self.a * b // self.b + y.a * b // y.b
return ratNum(a,b)
def __sub__(self, y): # rational number subtraction
b = lcm(self.b, y.b)
a = self.a * b // self.b - y.a * b // y.b
return ratNum(a,b)
def __mul__(self, y): # rational number multiplication
a = self.a * y.a
b = self.b * y.b
g = gcd(a, b);
a = a // g
b = b // g
return ratNum(a,b)
def __truediv__(self, y): # rational number division
if (y.a ==0): print("Division by zero!")
else:
a = self.a * y.b
b = self.b * y.a
g = gcd(a, b)
a = a // g
b = b // g
return ratNum(a,b)
def __eq__(self, y): # comparing two rational numbers
diff =(self - y).a
if diff >0: return 1
elif diff 0: return -1
return 0
# end of "ratNum.py"
# Following should be inside "main.py"
# from ratNum import *
z1= ratNum(); z1.input()
z2= ratNum(); z2.input()
print(z1,"+", z2,"=", z1+z2) # Task 0
print(z1,"-", z2,"=", z1-z2) # Task 1
print(z1,"x", z2,"=", z1*z2) # Task 2
print(z1,"/", z2,"=", z1/z2) # Task 3
print(z1,"==", z2,"=", z1==z2) # Task 4
"""
User login using dictionary
Date: 20-10-2022
"""
# user records using dictionary, name:password
Users ={ "John Chan": "999abc", "Mary": "888cde" }
# add user, name = st
def addUser(st, pas):
if st not in Users:
Users[st]= pas
print(st, "has been added")
else: print(st, "could not be added")
# delete user, name = st
def delUser(st):
if st in Users:
del Users[st]
print(st, "has been removed")
else: print(st,"is not a user")
# change st's password to pas
def changePass(st, pas):
Users[st]= pas
print("Password has been changed")
from getpass import getpass # getpass() function will hide user input
# return True if login successful, name = st
def login(st):
#pa = input("Password: ")
pa = getpass("Password: ")
for u in Users:
if u == st and pa == Users[u]:
return True
return False
# main program
# your may read password file here
# f = open("users.txt")
# Users={}; st=f.readline(); Users[st]=f.readline()
while True:
print("1. User login
"
"2. Change password
"
"3. Add user
"
"4. Delete user
"
"5. Quit
")
ch = input("Your choice =").strip()
if ch =="1":
st = input("User name =")
if login(st): print("Login successful!")
else: print("Incorrect user name or password")
elif ch =="2":
st = input("User name =")
if login(st):
#pa = input("New password =")
pa = getpass("New password =")
changePass(st, pa)
else: print("Incorrect user name or password")
elif ch =="3":
st = input("User name =")
#addUser(st, input("Password: "))
addUser(st, getpass())
elif ch =="4":
delUser( input("User name ="))
elif ch =="5": break
else: print("Illegal choice!")
# your may save password file here
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Linked Data A Geographic Perspective

Authors: Glen Hart, Catherine Dolbear

1st Edition

1000218910, 9781000218916

More Books

Students also viewed these Databases questions

Question

What is its position?

Answered: 1 week ago

Question

What are the organizations relationship goals on this issue?

Answered: 1 week ago