#program using union.
class union_name:
a=0
b=[0,1]
c=union_name()
c.a=256
print "c.a value is %d\n "%c.a
print "c.b[0] value is %d\n"%c.b[0]
print "c.b[1] value is %d\n"%c.b[1]
#program to find size of union and number of bytes
import sys
class union_result:
marks=0
grade=''
class struct_res:
name=[15]
age=0
sex=''
address=''
pincode=0
perf=union_result()
data=struct_res()
#Result
print "Size of Union: %d\n"%sys.getsizeof(data.perf)
print "Size of Structure: %d\n"%sys.getsizeof(data)
#Pointer to Unions cannot convert as no pointer in python. Page no. 5.88
#Program that includes of initial values to structure variable
import sys
class union_id:
color='20'
size=0
class struct_clothes:
manufacturer='30'
cost=0.0
description=union_id()
shirt=struct_clothes()
shirt.manufacturer='Indian'
shirt.cost=35.00
shirt.description.color='Black'
print "%d\n"%(sys.getsizeof(union_id))
print "%s %f "%(shirt.manufacturer,shirt.cost),"%s %d\n "%(shirt.description.color, shirt.description.size)
shirt.description.size =12
print "%s %f "%(shirt.manufacturer, shirt.cost),"%s %d\n "%(shirt.description.color, shirt.description.size)
#Program to read and write employee and their date of joining using nested structure.
class date:
day=0
month=""
year=0
class employee:
code=0
name=''
salary=0.0
doj=date()
emp1=employee()
emp1.code=input("Enter Employee Code: ")
emp1.name=raw_input("Enter the Employee Name: ")
emp1.salary=input("Enter the Employee Salary: ")
print "\nEnter Date of joining in order\n"
emp1.doj.day=input("Enter day: ")
emp1.doj.month=raw_input("Enter month: ")
emp1.doj.year=input("Enter year: ")
print "\nThe Employee Code is: %d"%emp1.code
print "\nThe Employee Name is: %s"%emp1.name
print "\nThe Employee Name is: %f"%emp1.salary
print "\nThe Employee DOJ is: %d %s %d"%(emp1.doj.day,emp1.doj.month,emp1.doj.year)
#Program to store 3 records in one structure
class book:
name=''
price=0
pages=0
b=[book() for a in range(0,3)]
for i in range(0,3):
b[i].name=raw_input("Enter the Book Name: ")
b[i].price=input("Enter the Book Price: ")
b[i].pages=input("Enter the Book Pages: ")
print '\n'
for i in range(0,3):
print "%s\t"%(b[i].name),"%d\t"%(b[i].price),"%d\n"%(b[i].pages)
#Program to copy of entire structure to a function
class std:
no=0
avg=0.0
a=std()
a.no=15
a.avg=90.75
def func(Struct_p):
print "Number is.... %d\n"%a.no
print "Average is... %f"%a.avg
func(a)
#Programs shows how the structure can be accessed by a pointer variable.
class student:
roll_no=0
name=''
marks=0.0
stud1=student()
print "Enter the student details: \n"
stud1.roll_no=input()
stud1.name=raw_input()
stud1.marks=input()
print "\nDisplay of structure using structure variable\n"
print "Roll No\t Name\t Marks"
print "\n-------\t ----- -------\n"
print "%d\t %s\t %f"%(stud1.roll_no,stud1.name,stud1.marks)
pt=stud1
print "\nDisplay of structure using pointer variable\n"
print "Roll No\t Name\t Marks"
print "\n-------\t ----- -------\n"
print "%d\t %s\t %f"%(pt.roll_no,pt.name,pt.marks)
#Program to transfer a structure to a function by passing the structure address to the function
class struct:
name=''
no=0
age=0
record=struct()
std=record
std.name,std.no,std.age=["LAK",15,25]
print "%s\t%d\t%d"%(std.name,std.no,std.age)
def fun(std):
pt.name="Muni"
pt.no=16
pt.age=26
std.name=pt.name
std.no=pt.no
std.age=pt.age
print "%s\t%d\t%d"%(std.name,std.no,std.age)
#Program to pass an array of structure to a function, and return a pointer to a structure.
import sys
N=3
NULL=0
class struct:
sno=0
name=''
age=0
record=struct()
std=record
print "Student Number Locater"
print "To Exit the program enter '0' for student number"
stdno=input("Enter the Student Number: ")
print "\n"
if stdno ==15:
std.sno,std.name,std.age=[15,"MUNI",28]
print "Number .... %d \n"%(std.sno)
print "Name ...... %s\n"%(std.name)
print "Age ....... %d\n"%(std.age)
if stdno == 16:
std.sno,std.name,std.age=[16,"LAK",27]
print "Number .... %d \n"%(std.sno)
print "Name ...... %s\n"%(std.name)
print "Age ....... %d\n"%(std.age)
if stdno == 17:
std.sno,std.name,std.age=[17,"RAJA",31]
print "Number .... %d \n"%(std.sno)
print "Name ...... %s\n"%(std.name)
print "Age ....... %d\n"%(std.age)
if stdno==0:
exit
#Program to print student name and mark using structure
class student:
name=''
marks=0.00
s1=''
f=0.00
student1=student()
student2=student()
student1.name=raw_input("Enter Name: ")
f=input("Enter Mark: ")
student2.marks=f
print "\nName is %s \n"%student1.name
print "Marks are %f \n"%student2.marks
#Program to print marks, grade and their address using union
class union_marks:
perc=0.00
grade=''
student1=union_marks()
student1.perc=98.5
print "Marks are %f \t address is %8lu\n"%(student1.perc, id(student1.perc))
student1.grade='A'
print "Grade is %c \t address is %8lu\n"%(student1.grade,id(student1.grade))