0% found this document useful (0 votes)
6 views

Binary_Stack_Menu_Driven_Programs

The document contains two menu-driven programs: one for performing various operations on a binary file using Python's pickle module, including writing, reading, searching, deleting, appending, and updating records; and another for managing a stack using a list, allowing push, pop, display, and peek operations. Each program includes functions that handle specific tasks and a main menu for user interaction. The binary file operations program stores student data, while the stack program manages integer data.

Uploaded by

riona.banerjee07
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Binary_Stack_Menu_Driven_Programs

The document contains two menu-driven programs: one for performing various operations on a binary file using Python's pickle module, including writing, reading, searching, deleting, appending, and updating records; and another for managing a stack using a list, allowing push, pop, display, and peek operations. Each program includes functions that handle specific tasks and a main menu for user interaction. The binary file operations program stores student data, while the stack program manages integer data.

Uploaded by

riona.banerjee07
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

PRACTICE 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:

name=input('Enter name: ')

roll_no=int(input('Enter roll no: '))

marks=int(input('Enter marks: '))

SL.append([name,roll_no,marks])

ch=input('Want to enter more, press y/Y.To stop press n/N: ')

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)

roll=int(input('Enter roll no. you want to search: '))

flag=0

for d in data:

if d[1]==roll:

print(d)

flag=1

break

if flag==0:

print('No such data')

def delete():

fob=open(r"C:\Users\ankit\OneDrive\Desktop\python_files\binary_test.dat","rb")

data = pk.load(fob)

fob.close()

roll=int(input('Enter roll no. you want to delete: '))

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)

print('Enter data to append in binary file...')

while True:

name=input('Enter name: ')

roll_no=int(input('Enter roll no: '))

marks=int(input('Enter marks: '))

data.append([name,roll_no,marks])

ch=input('Want to enter more, press y/Y.To stop press n/N: ')

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)

roll=int(input('Enter student roll no to update: '))

for i in data:

if i[1]==roll:

c=input("do you want to change name? press y or n")

if c=='y':

i[0]=input("Enter new name: ")

c=input("do you want to change marks? press y or n")

if c=='y':

i[2]=input("Enter new marks: ")

fob.seek(0)

pk.dump(data,fob)

print('File updated...')

def menu():

while True:

print("Binary File Operations Menu")

print("~~~~~~~~~~~~~~~~~~~~~~~~~~~")

print("1. Create & write in binary file")

print("2. Read from binary file")

print("3. Search from binary file")

print("4. Delete from binary file")

print("5. Append in binary file")

print("6. Update binary file")


print("E. Exit")

ch=input("Enter your choice: ")

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():

print("Stack Underflow/Stack is Empty")

else:

d=stk.pop()

print(d," is deleted...")

def display():

print(stk[::-1])

def peek():

var=stk[-1]

print(f"TOP- - -> {var}")

stk=[]

while True:

print("Enter 1 for Push operation")

print("Enter 2 for Pop operation")

print("Enter 3 for Display Stack")

print("Enter 4 Peek Stack")

print("Enter 5 Exit")

ch=int(input())

if ch==1:

while True:

item=int(input("Enter data to insert: "))

PUSH(item)

m=input("Do you want to enter more? Press y or n: ")

if m in 'nN':

break
elif ch==2:

POP()

elif ch==3:

display()

elif ch==4:

peek()

else:

print("THE END...")

break

You might also like