Chapter 8-Structures and Unions

Example-student1.cpp, Page no-260

In [1]:
from ctypes import Structure
class Student(Structure): #structure
    roll_no =int
    name =str
    branch =str
    marks=int
s1=Student() #object of struct student
print "Enter data for student..."
s1.roll_no=int(raw_input("Roll Number ? "))
s1.name=raw_input("Name ? ")
s1.branch=raw_input("Branch ? ")
s1.marks=int(raw_input("Total Marks <max-325> ? "))
print "Student Report"
print "--------------"
print "Roll Number:", s1.roll_no
print "Name:", s1.name
print "Branch:", s1.branch
print "Percentage:%f" %(s1.marks*(100.0/325))
Enter data for student...
Roll Number ? 5
Name ? Mangala
Branch ? Computer
Total Marks <max-325> ? 290
Student Report
--------------
Roll Number: 5
Name: Mangala
Branch: Computer
Percentage:89.230769

Example-days.cpp, Page no-262

In [1]:
from ctypes import Structure, c_int
class date(Structure):
    _fields_=[("day", c_int),("month", c_int),("year",c_int)]
d1=date(14, 4, 1971)
d2=date(3, 7, 1996)
print "Birth date:",
print "%s-%s-%s" %(d1.day, d1.month, d1.year)
print "Today date:",
print "%s-%s-%s" %(d2.day, d2.month, d2.year)
Birth date: 14-4-1971
Today date: 3-7-1996

Example-student2.cpp, Page no-264

In [1]:
from ctypes import Structure, c_int, c_char
class date(Structure):
    _fields_=[("day", c_int),("month", c_int),("year",c_int)]
class Student(Structure):
    _fields_=[("roll_no", c_int), ("name", c_char*25), ("birthday", date), ("branch", c_char*15),("marks", c_int)]#nested structure
s1=Student()
print "Enter data for student..."
s1.roll_no=int(raw_input("Roll Number ? "))
s1.name=raw_input("Name ? ")
birthday=date(0, 0, 0)
print "Enter date of birth <day month year>: ",
d, m, y=[int(x) for x in raw_input().split()]
birthday.day=d
birthday.month=m
birthday.year=y
s1.birthday=birthday
s1.branch=raw_input("Branch ? ")
s1.marks=int(raw_input("Total Marks <max-325> ? "))
print "Student Report"
print "--------------"
print "Roll Number:", s1.roll_no
print "Name:", s1.name
print "%s-%s-%s" %(s1.birthday.day, s1.birthday.month, s1.birthday.year)
print "Branch:", s1.branch
print "Percentage:%f" %(s1.marks*(100.0/325))
Enter data for student...
Roll Number ? 9
Name ? Savithri
Enter date of birth <day month year>: 2 2 1972
Branch ? Electrical
Total Marks <max-325> ? 295
 Student Report
--------------
Roll Number: 9
Name: Savithri
2-2-1972
Branch: Electrical
Percentage:90.769231

Example-student3.cpp, Page no-267

In [1]:
from ctypes import Structure, c_int, c_char
class Student(Structure):
    _fields_=[("roll_no", c_int), ("name", c_char*25), ("branch", c_char*15),("marks", c_int)]
s=[]
n=int(raw_input("How many students to be processed <max-10>: "))
for i in range(n):
    print "Enter data for student", i+1, "..."
    r=int(raw_input("Roll Number ? "))
    name=raw_input("Name ? ")
    b=raw_input("Branch ? ")
    m=int(raw_input("Total marks <max-325> ? "))
    s.append(Student(r, name, b, m)) #array of structure objects
print "Students Report"
print "--------------"
for i in range(n):
    print "Roll Number:", s[i].roll_no
    print "Name:", s[i].name
    print "Branch:", s[i].branch
    print "Percentage: %f" %(s[i].marks*(100.0/325))
How many students to be processed <max-10>: 2
Enter data for student 1 ...
Roll Number ? 5
Name ? Mangala
Branch ? Computer
Total marks <max-325> ? 290
Enter data for student 2 ...
Roll Number ? 9
Name ? Shivakumar
Branch ? Electronics
Total marks <max-325> ? 250
Students Report
--------------
Roll Number: 5
Name: Mangala
Branch: Computer
Percentage: 89.230769
Roll Number: 9
Name: Shivakumar
Branch: Electronics
Percentage: 76.923077

Example-student4.cpp, Page no-269

In [1]:
from ctypes import Structure, c_int, c_char
class Student(Structure):
    _fields_=[("roll_no", c_int), ("name", c_char*25), ("branch", c_char*15),("marks", c_int)]
STUDENTS_COUNT=5
s=[]
s.append(Student(2, 'Tejaswi', 'CS', 285))#initialization of array of structures
s.append(Student(3, 'Laxmi', 'IT', 215))
s.append(Student(5, 'Bhavani', 'Electronics', 250))
s.append(Student(7, 'Anil', 'Civil', 215))
s.append(Student(9, 'Savithri', 'Electrical', 290))
print "Students Report"
print "--------------"
for i in range(STUDENTS_COUNT):
    print "Roll Number:", s[i].roll_no
    print "Name:", s[i].name
    print "Branch:", s[i].branch
    print "Percentage: %0.4f" %(s[i].marks*(100.0/325))
Students Report
--------------
Roll Number: 2
Name: Tejaswi
Branch: CS
Percentage: 87.6923
Roll Number: 3
Name: Laxmi
Branch: IT
Percentage: 66.1538
Roll Number: 5
Name: Bhavani
Branch: Electronics
Percentage: 76.9231
Roll Number: 7
Name: Anil
Branch: Civil
Percentage: 66.1538
Roll Number: 9
Name: Savithri
Branch: Electrical
Percentage: 89.2308

Example-student5.cpp, Page no-271

In [1]:
from ctypes import Structure, c_int, c_char
class Student(Structure):
    _fields_=[("roll_no", c_int), ("name", c_char*25), ("branch", c_char*15),("marks", c_int)]
def read():
    dull=Student()
    dull.roll_no=int(raw_input("Roll Number ? "))
    dull.name=raw_input("Name ? ")
    dull.branch=raw_input("Branch ? ")
    dull.marks=int(raw_input("Total marks <max-325> ? "))
    return dull #returning structure object
def show(genius): #passing object of structure
    print "Roll Number:", genius.roll_no
    print "Name:", genius.name
    print "Branch:", genius.branch
    print "Percentage: %0.4f" %(genius.marks*(100.0/325))
s=[]
n=int(raw_input("How many students to be processed <max-10>: "))
for i in range(n):
    print "Enter data for student", i+1, "..."
    s.append(read())
print "Students Report"
print "--------------"
for i in range(n):
    show(s[i])
How many students to be processed <max-10>: 2
Enter data for student 1 ...
Roll Number ? 3
Name ? Smrithi
Branch ? Genetics
Total marks <max-325> ? 295
Enter data for student 2 ...
Roll Number ? 10
Name ? Bindhu
Branch ? MCA
Total marks <max-325> ? 300
Students Report
--------------
Roll Number: 3
Name: Smrithi
Branch: Genetics
Percentage: 90.7692
Roll Number: 10
Name: Bindhu
Branch: MCA
Percentage: 92.3077

Example-student6.cpp, Page no-273

In [1]:
from ctypes import Structure, c_int, c_char
class Student(Structure):
    _fields_=[("roll_no", c_int), ("name", c_char*25), ("branch", c_char*15),("marks", c_int)]
def HighestMarks(s, count): #passing array of structures
    big=s[0].marks
    for i in range(1, count):
        if s[i].marks>big:
            big=s[i].marks
            index=i
    return index
def read():
    dull=Student()
    dull.roll_no=int(raw_input("Roll Number ? "))
    dull.name=raw_input("Name ? ")
    dull.branch=raw_input("Branch ? ")
    dull.marks=int(raw_input("Total marks <max-325> ? "))
    return dull
def show(genius):
    print "Roll Number:", genius.roll_no
    print "Name:", genius.name
    print "Branch:", genius.branch
    print "Percentage: %0.4f" %(genius.marks*(100.0/325))
s=[]
n=int(raw_input("How many students to be processed <max-10>: "))
for i in range(n):
    print "Enter data for student", i+1, "..."
    s.append(read())
print "Students Report"
print "--------------"
Id=HighestMarks(s, n)
print "Details of student scoring higest marks..."
show(s[Id])
How many students to be processed <max-10>: 3
Enter data for student 1 ...
Roll Number ? 3
Name ? Smrithi
Branch ? Genetics
Total marks <max-325> ? 295
Enter data for student 2 ...
Roll Number ? 15
Name ? Rajkumar
Branch ? Computer
Total marks <max-325> ? 315
Enter data for student 3 ...
Roll Number ? 7
Name ? Laxmi
Branch ? Electronics
Total marks <max-325> ? 255
Students Report
--------------
Details of student scoring higest marks...
Roll Number: 15
Name: Rajkumar
Branch: Computer
Percentage: 96.9231

Example-complex.cpp, Page no-278

In [1]:
from ctypes import Structure
import sys
import math
class Complex(Structure):
    x=int
    y=int
    def read(self):
        self.x=int(raw_input("Real part? "))
        self.y=int(raw_input("Imaginary part? "))
    def show(self, msg):
        print msg, self.x,
        if self.y<0:
            sys.stdout.write('-i')
        else:
            sys.stdout.write('+i')
        print math.fabs(self.y)
    def add(self, c2):
        self.x+=c2.x
        self.y+=c2.y
c1=Complex()
c2=Complex()
c3=Complex()
print "Enter complex number c1..."
c1.read()
print "Enter complex number c2..."
c2.read()
c1.show('c1 =')
c2.show('c2 =')
c3=c1
c3.add(c2)
c3.show('c3 = c1 + c2 =')
Enter complex number c1...
Real part? 1
Imaginary part? 2
Enter complex number c2...
Real part? 3
Imaginary part? 4
c1 = 1+i 2.0
c2 = 3+i 4.0
c3 = c1 + c2 = 4+i 6.0

Example-emp.cpp, Page no-279

In [1]:
from ctypes import Structure
class employee(Structure): #structure member functions
    name=str
    ID=long
    dept=str
    salary=float
    def read(self):
        self.name=raw_input("Employee Name: ")
        self.ID=long(raw_input("Employee ID: "))
        self.dept=raw_input("Department: ")
        self.salary=float(raw_input("Salary: "))
    def show(self):
        print "Employee Name:", self.name
        print "Employee ID:", self.ID
        print "Department:", self.dept
        print "Salary:", self.salary
emp=employee()
print "Enter employee data:"
emp.read()
print "\n****Employee Record****"
emp.show()
Enter employee data:
Employee Name: Vishwanathan
Employee ID: 953
Department: Finance
Salary: 18500

****Employee Record****
Employee Name: Vishwanathan
Employee ID: 953
Department: Finance
Salary: 18500.0

Example-union.cpp, Page no-283

In [1]:
from ctypes import Union, c_char, sizeof
#Creates a union
class Strings(Union):
    _fields_ = [("filename",c_char*200),
            ("output", c_char*400)]
s=Strings()
s.filename="/cdacb/usrl/raj/oops/mcrokernel/pserver.cpp"
print "filename:", s.filename
s.output="OOPs is a most complex entity ever created by humans"
print "output:", s.output
print "Size of union Strings =",  sizeof(Strings)
filename: /cdacb/usrl/raj/oops/mcrokernel/pserver.cpp
output: OOPs is a most complex entity ever created by humans
Size of union Strings = 400

Example-sudiff.cpp, Page no-284

In [1]:
from collections import namedtuple
from ctypes import c_char, c_int, c_float, sizeof, Structure, Union
class struct(Structure):
    _fields_ = [("name",c_char*25), ("idno", c_int), ("salary", c_float)]
emp=struct()
class union(Union):
    _fields_ = [("name",c_char*25), ("idno", c_int), ("salary", c_float)]
desc=union()
print "The size of the structure is", sizeof(emp)
print "The size of the union is", sizeof(desc)
The size of the structure is 36
The size of the union is 28

Example-uaccess.cpp, Page no-285

In [1]:
from ctypes import Union, c_int, c_char, c_float
class emp(Union):
    _fields_=[("name",c_char*25), ("idno", c_int), ("salary", c_float)]
def show(e):
    print "Employee Details..."
    print "The name is %s" %e.name
    print "The idno is %d" %e.idno
    print "The salary is %g" %e.salary
e=emp()
e.name="Rajkumar"
show(e)
e.idno=10
show(e)
e.salary=9000
show(e)
Employee Details...
The name is Rajkumar
The idno is 1802133842
The salary is 2.83348e+26
Employee Details...
The name is 

The idno is 10
The salary is 1.4013e-44
Employee Details...
The name is 
The idno is 1175232512
The salary is 9000

Example-uscope.cpp, Page no-286

In [1]:
from ctypes import Union, c_int, c_char, c_float
#Creates a union
class union(Union):
    _fields_ = [("i", c_int), 
              ("c", c_char), 
              ("f", c_float)]
u=union()
u.i=10
u.c='9'
u.f=4.5
print "The value of i is", u.i
print "The value of c is", u.c
print "The value of f is", u.f
The value of i is 1083179008
The value of c is 
The value of f is 4.5

Example-share.cpp, Page no-289

In [1]:
from collections import namedtuple
from ctypes import Structure, c_uint, c_int, Union
import sys
class with_bits(Structure):
    _fields_ = [("first", c_uint), ("second", c_uint)]
class union(Union):
    _fields_=[("b", with_bits), ("i", c_int)]
i=0
u=union()
print "On i=0: b.first =",u.b.first,"b.second =", u.b.second
u.b.first=9
print "b.first =9:", 
print "b.first =", u.b.first, "b.second =", u.b.second
On i=0: b.first = 0 b.second = 0
b.first =9: b.first = 9 b.second = 0