Binary_Stack_Menu_Driven_Programs
Binary_Stack_Menu_Driven_Programs
CLASS XII
1. Write a menu driven program to show all the operations performed on a
Binary file.
2. Write a menu driven program to show all the operations performed on a
Stack using List.
SOLUTIONS
Q1.
#BINARY FILE OPERATIONS IN ONE PROGRAM
import pickle as pk
def write():
SL=[]
with
open(r"C:\Users\ankit\OneDrive\Desktop\python_files\binary_test.dat","wb") as
fob:
while True:
SL.append([name,roll_no,marks])
if ch not in 'yY':
break
pk.dump(SL,fob)
def read():
with
open(r"C:\Users\ankit\OneDrive\Desktop\python_files\binary_test.dat","rb") as
fob:
data=pk.load(fob)
for d in data:
print(d)
#fob.close()
def search():
with
open(r"C:\Users\ankit\OneDrive\Desktop\python_files\binary_test.dat","rb") as
fob:
data=pk.load(fob)
flag=0
for d in data:
if d[1]==roll:
print(d)
flag=1
break
if flag==0:
def delete():
fob=open(r"C:\Users\ankit\OneDrive\Desktop\python_files\binary_test.dat","rb")
data = pk.load(fob)
fob.close()
nwLst=[]
for d in data:
if d[1]==roll:
print("Data deleted...")
continue
nwLst.append(d)
fob=open(r"C:\Users\ankit\OneDrive\Desktop\python_files\binary_test.dat","wb")
pk.dump(nwLst,fob)
fob.close()
def append():
with
open(r"C:\Users\ankit\OneDrive\Desktop\python_files\binary_test.dat","rb+") as
fob:
data=pk.load(fob)
while True:
data.append([name,roll_no,marks])
if ch not in 'yY':
break
fob.seek(0)
pk.dump(data,fob)
print('File appended...')
def update():
with
open(r"C:\Users\ankit\OneDrive\Desktop\python_files\binary_test.dat","rb+") as
fob:
data=pk.load(fob)
for i in data:
if i[1]==roll:
if c=='y':
if c=='y':
fob.seek(0)
pk.dump(data,fob)
print('File updated...')
def menu():
while True:
print("~~~~~~~~~~~~~~~~~~~~~~~~~~~")
if ch == '1':
write()
if ch== '2':
read()
if ch== '3':
search()
if ch== '4':
delete()
if ch== '5':
append()
if ch== '6':
update()
if ch in 'eE':
break
menu()
Q2.
#STACK OPERATIONS USING LIST IN ONE PROGRAM
def isEmpty():
if stk == []:
return True
else:
return False
def PUSH(x):
stk.append(x)
def POP():
if isEmpty():
else:
d=stk.pop()
print(d," is deleted...")
def display():
print(stk[::-1])
def peek():
var=stk[-1]
stk=[]
while True:
print("Enter 5 Exit")
ch=int(input())
if ch==1:
while True:
PUSH(item)
if m in 'nN':
break
elif ch==2:
POP()
elif ch==3:
display()
elif ch==4:
peek()
else:
print("THE END...")
break