Chapter 9: Structures & Unions

Example 9.1, page no. 338

In [2]:
class date:
    dat = 0
    month = 0
    year = 0
class person:
    name = ''
    lastnam = ''
    birthday = date()
    salary = 0.0
emprec = person()
birth = emprec.birthday
emprec.name = "Tejaswi.V."
emprec.lastnam = "Rajuk"
emprec.birthday.day = 24
emprec.birthday.month = 7
emprec.birthday.year = 90
x = 6500.00
emprec.salary = x
print "Employee Details: "
print "Name: ", emprec.name, " ", emprec.lastnam
print "Birthdate: ", emprec.birthday.day, ":", emprec.birthday.month, ":", emprec.birthday.year
print "Salary: ", emprec.salary
Employee Details: 
Name:  Tejaswi.V.   Rajuk
Birthdate:  24 : 7 : 90
Salary:  6500.0

Example 9.2, page no. 340

In [3]:
CURRENT_YEAR = 96
def increment(sal, year, inc):
    if CURRENT_YEAR - year > 30:
        sal += inc
        return sal
class date:
    day = 0
    month = 0
    year = 0
class person:
    name = ''
    birthday = date()
    salary = 0.0
n = 500
emprec = person()
emprec.name = 'Arun R.'
emprec.birthday.day = 10
emprec.birthday.month = 8
emprec.birthday.year = 64
emprec.salary = 4000.00
print "Employee Details: "
print "Name: ", emprec.name
print "Birthdate: ", emprec.birthday.day, ":", emprec.birthday.month, ":", emprec.birthday.year
print "Salary: ", emprec.salary
emprec.salary = increment(emprec.salary, emprec.birthday.year, n)
print "Employee Details: "
print "Name: ", emprec.name
print "Birthdate: ", emprec.birthday.day, ":", emprec.birthday.month, ":", emprec.birthday.year
print "Salary: ", emprec.salary
Employee Details: 
Name:  Arun R.
Birthdate:  10 : 8 : 64
Salary:  4000.0
Employee Details: 
Name:  Arun R.
Birthdate:  10 : 8 : 64
Salary:  4500.0

Example 9.3, page no. 342

In [6]:
CURRENT_YEAR = 96
def increment(emprec):
    if CURRENT_YEAR - emprec.birthday.year > 30:
        emprec.salary += 500
        return emprec
class date:
    day = 0
    month = 0
    year = 0
class person:
    name = ''
    birthday = date()
    salary = 0.0
emprec = person()
emprec.name = 'Arun R.'
emprec.birthday.day = 10
emprec.birthday.month = 8
emprec.birthday.year = 64
emprec.salary = 4000.00
print "Employee Details: "
print "Name: ", emprec.name
print "Birthdate: ", emprec.birthday.day, ":", emprec.birthday.month, ":", emprec.birthday.year
print "Salary: ", emprec.salary
emprec = increment(emprec)
print "Employee Details: "
print "Name: ", emprec.name
print "Birthdate: ", emprec.birthday.day, ":", emprec.birthday.month, ":", emprec.birthday.year
print "Salary: ", emprec.salary
Employee Details: 
Name:  Arun R.
Birthdate:  10 : 8 : 64
Salary:  4000.0
Employee Details: 
Name:  Arun R.
Birthdate:  10 : 8 : 64
Salary:  4500.0

Example 9.4, page no. 343

In [1]:
class date:
    day = 0
    month = 0
    year = 0

class person:
    name = ''
    birthday = date()
    salary = 0.0

def printout(per):
    print "Employee Details: "
    print "Name: ", per.name
    print "Birthdate: %3d:%3d:%3d" %(per.birthday.day, per.birthday.month, per.birthday.year)
    print "Salary: %6.2f" %(per.salary)

def readin(record):
    record.name = raw_input("Enter the name: ")
    print "Enter Birthdate"
    record.birthday.day = int(raw_input("Day: "))
    record.birthday.month = int(raw_input("Month: "))
    record.birthday.year = int(raw_input("Year: "))
    record.salary = float(raw_input("Enter the salary: "))

test = person()
test.name = "Arun"
test.birthday.day = 10
test.birthday.month = 8
test.birthday.year = 75
test.salary = 4500.00
temp = person()
readin(temp)
printout(test)
Enter the name: Balaji
Enter Birthdate
Day: 24
Month: 2
Year: 74
Enter the salary: 6500.00
Employee Details: 
Name:  Arun
Birthdate:  24:  2: 74
Salary: 4500.00

Example 9.5 page no. 344

In [3]:
class date:
    day = 0
    month = 0
    year = 0

class person:
    name = ''
    birthday = date()
    salary = 0.0

def printout(per):
    print "Employee Details: "
    print "Name: ", per.name
    print "Birthdate: %3d:%3d:%3d" %(per.birthday.day, per.birthday.month, per.birthday.year)
    print "Salary: %6.2f" %(per.salary)

def readin(record):
    record.name = raw_input("Enter the name: ")
    print "Enter Birthdate"
    record.birthday.day = int(raw_input("Day: "))
    record.birthday.month = int(raw_input("Month: "))
    record.birthday.year = int(raw_input("Year: "))
    record.salary = float(raw_input("Enter the salary: "))

test = person()
test.name = "Arun"
test.birthday.day = 10
test.birthday.month = 8
test.birthday.year = 75
test.salary = 4500.00
temp = person()
readin(temp)
printout(temp)
Enter the name: Balaji
Enter Birthdate
Day: 24
Month: 2
Year: 74
Enter the salary: 6500.00
Employee Details: 
Name:  Balaji
Birthdate:  24:  2: 74
Salary: 6500.00

Example 9.6, page no. 346

In [5]:
class date:
    day = 0
    month = 0
    year = 0

class person:
    name = ''
    birthday = date()
    salary = 0.0
    
    def __init__(self, n, dt, sal):
        self.name = n
        self.birthday.day = dt[0]
        self.birthday.month = dt[1]
        self.birthday.year = dt[2]
        self.salary = sal

def printout(per):
    print "Employee Details: "
    print "Name: ", per.name
    print "Birthdate: %3d:%3d:%3d" %(per.birthday.day, per.birthday.month, per.birthday.year)
    print "Salary: %6.2f" %(per.salary)

def highest(sals):
    return max(sals)
    
record = [["Arun R.", 10, 8, 75, 4000.00],
          ["vinod S.", 3, 10, 74, 3000.00],
          ["Tanuj M.", 21, 1, 73, 5500.00]]

emp1 = person(record[0][0], (record[0][1], record[0][2], record[0][3]), record[0][4])
emp2 = person(record[1][0], (record[1][1], record[1][2], record[1][3]), record[1][4])
emp3 = person(record[2][0], (record[2][1], record[2][2], record[2][3]), record[2][4])

sals = [emp1.salary, emp2.salary, emp3.salary]

highest = highest(sals)
print "The highest salary being paid is %6.2f" %highest
printout(emp3)
The highest salary being paid is 5500.00
Employee Details: 
Name:  Tanuj M.
Birthdate:  21:  1: 73
Salary: 5500.00

Example 9.7, page no. 347

In [13]:
class date:
    day = 0
    month = 0
    year = 0

class person:
    name = ''
    birthday = date()
    salary = 0.0

def readin(rec, list1):
    rec.name = list1[0]
    rec.birthday.day = list1[1]
    rec.birthday.month = list1[2]
    rec.birthday.year = list1[3]
    rec.salary = list1[4]

    
def printout(per):
    print "Employee Details: "
    print "Name: ", per.name
    print "Birthdate: %3d:%3d:%3d" %(per.birthday.day, per.birthday.month, per.birthday.year)
    print "Salary: %6.2f" %(per.salary)

def highest(sals):
    return max(sals)
    
record = [["Tejaswi", 24, 7, 70, 7000.00],
          ["vinod S.", 3, 10, 74, 3000.00],
          ["Tanuj M.", 21, 1, 73, 5500.00]]

emp1 = person()
readin(emp1, record[0])
print emp1.birthday.day
emp2 = person()
readin(emp2, record[1])
emp3 = person()
readin(emp3, record[2])

highest = highest([emp1.salary, emp2.salary, emp3.salary])
temp = emp1
print "Highest salary being paid is ", highest
printout(temp)
24
Highest salary being paid is  7000.0
Employee Details: 
Name:  Tejaswi
Birthdate:  21:  1: 73
Salary: 7000.00

Example 9.8, page no. 349

In [17]:
class date:
    day = 0
    month = 0
    year = 0

class person:
    name = ''
    birthday = date()
    salary = 0.0

def readin():
    record = []
    for i in range(3):
        print "Employee no. ", i+1
        temp = person()
        temp.name = raw_input("Enter the name: ")
        print "Enter Birthdate"
        temp.birthday.day = int(raw_input("Day: "))
        temp.birthday.month = int(raw_input("Month: "))
        temp.birthday.year = int(raw_input("Year: "))
        temp.salary = float(raw_input("Enter the salary: "))
        record.append(temp)
    return record

def printout(per):
    print "Employee Details: "
    print "Name: ", per.name
    print "Birthdate: %3d:%3d:%3d" %(per.birthday.day, per.birthday.month, per.birthday.year)
    print "Salary: %6.2f" %(per.salary)

record = readin()
for i in range(3):
    printout(record[i])
    print "hit enter to continue..."
    raw_input()
    
#there is a printing mistake in the book
Employee no.  1
Enter the name: Balaji
Enter Birthdate
Day: 24
Month: 2
Year: 74
Enter the salary: 6500.50
Employee no.  2
Enter the name: Tarun
Enter Birthdate
Day: 30
Month: 9
Year: 69
Enter the salary: 6890.00
Employee no.  3
Enter the name: Vinod
Enter Birthdate
Day: 7
Month: 6
Year: 70
Enter the salary: 6700.00
Employee Details: 
Name:  Balaji
Birthdate:   7:  6: 70
Salary: 6500.50
hit enter to continue...

Employee Details: 
Name:  Tarun
Birthdate:   7:  6: 70
Salary: 6890.00
hit enter to continue...

Employee Details: 
Name:  Vinod
Birthdate:   7:  6: 70
Salary: 6700.00
hit enter to continue...

Example 9.9, page no. 351

In [23]:
class date:
    day = 0
    month = 0
    year = 0

class person:
    name = ''
    birthday = date()
    salary = 0.0

def readin(n):
    record = []
    for i in range(n):
        print "Employee no. ", i+1
        temp = person()
        temp.name = raw_input("Enter the name: ")
        print "Enter Birthdate"
        temp.birthday.day = int(raw_input("Day: "))
        temp.birthday.month = int(raw_input("Month: "))
        temp.birthday.year = int(raw_input("Year: "))
        temp.salary = float(raw_input("Enter the salary: "))
        record.append(temp)
    return record

def printout(record):
    for i in range(n):
        print "Employee Details: "
        print "Name: ", record[i].name
        print "Birthdate: %3d:%3d:%3d" %(record[i].birthday.day, record[i].birthday.month, record[i].birthday.year)
        print "Salary: %6.2f" %(record[i].salary)

def sortalpha(rec):
    for i in range(n-1):
        for j in range(n-i-1):
            if rec[j].name > rec[j+1].name:
                rec[j], rec[j+1] = rec[j+1], rec[j]
    return rec

def sortage(rec):
    for i in range(n-1):
        for j in range(n-i-1):
            if (rec[j].birthday.year > rec[j+1].birthday.year or rec[j].birthday.year == rec[j+1].birthday.year and
                rec[j].birthday.month > rec[j+1].birthday.month or rec[j].birthday.month == rec[j+1].birthday.month and
                rec[j].birthday.day > rec[j+1].birthday.day or rec[j].birthday.day == rec[j+1].birthday.day):
                rec[j], rec[j+1] = rec[j+1], rec[j]
    return rec

def sortsal(rec):
    for i in range(n-1):
        for j in range(n-i-1):
            if rec[j].salary > rec[j+1].salary:
                rec[j], rec[j+1] = rec[j+1], rec[j]
    return rec

n = int(raw_input("Enter the number of records: "))
record = readin(n)

flag = True
while(flag):
    print "1: SORT BY NAME"
    print "2: SORT BY AGE"
    print "3: SORT BY SALARY"
    print "4: QUIT "
    i = int(raw_input("Enter your choice: "))
    if i == 1:
        sorted_rec = sortalpha(record)
        printout(sorted_rec)
    elif i == 2:
        sorted_rec = sortage(record)
        printout(sorted_rec)
    elif i == 3:
        sorted_rec = sortsal(record)
        printout(sorted_rec)
    elif i == 4:
        flag = False
        break
    else:
        print "invalid choice..."
Enter the number of records: 5
Employee no.  1
Enter the name: Vinod
Enter Birthdate
Day: 23
Month: 8
Year: 74
Enter the salary: 6500
Employee no.  2
Enter the name: Arun
Enter Birthdate
Day: 10
Month: 5
Year: 70
Enter the salary: 7000
Employee no.  3
Enter the name: Puneet
Enter Birthdate
Day: 30
Month: 6
Year: 74
Enter the salary: 6900
Employee no.  4
Enter the name: Tanuj
Enter Birthdate
Day: 7
Month: 1
Year: 70
Enter the salary: 7700
Employee no.  5
Enter the name: Rohit
Enter Birthdate
Day: 17
Month: 12
Year: 73
Enter the salary: 6800
1: SORT BY NAME
2: SORT BY AGE
3: SORT BY SALARY
4: QUIT 
Enter your choice: 1
Employee Details: 
Name:  Arun
Birthdate:  17: 12: 73
Salary: 7000.00
Employee Details: 
Name:  Puneet
Birthdate:  17: 12: 73
Salary: 6900.00
Employee Details: 
Name:  Rohit
Birthdate:  17: 12: 73
Salary: 6800.00
Employee Details: 
Name:  Tanuj
Birthdate:  17: 12: 73
Salary: 7700.00
Employee Details: 
Name:  Vinod
Birthdate:  17: 12: 73
Salary: 6500.00
1: SORT BY NAME
2: SORT BY AGE
3: SORT BY SALARY
4: QUIT 
Enter your choice: 2
Employee Details: 
Name:  Vinod
Birthdate:  17: 12: 73
Salary: 6500.00
Employee Details: 
Name:  Tanuj
Birthdate:  17: 12: 73
Salary: 7700.00
Employee Details: 
Name:  Rohit
Birthdate:  17: 12: 73
Salary: 6800.00
Employee Details: 
Name:  Puneet
Birthdate:  17: 12: 73
Salary: 6900.00
Employee Details: 
Name:  Arun
Birthdate:  17: 12: 73
Salary: 7000.00
1: SORT BY NAME
2: SORT BY AGE
3: SORT BY SALARY
4: QUIT 
Enter your choice: 33
invalid choice...
1: SORT BY NAME
2: SORT BY AGE
3: SORT BY SALARY
4: QUIT 
Enter your choice: 3
Employee Details: 
Name:  Vinod
Birthdate:  17: 12: 73
Salary: 6500.00
Employee Details: 
Name:  Rohit
Birthdate:  17: 12: 73
Salary: 6800.00
Employee Details: 
Name:  Puneet
Birthdate:  17: 12: 73
Salary: 6900.00
Employee Details: 
Name:  Arun
Birthdate:  17: 12: 73
Salary: 7000.00
Employee Details: 
Name:  Tanuj
Birthdate:  17: 12: 73
Salary: 7700.00
1: SORT BY NAME
2: SORT BY AGE
3: SORT BY SALARY
4: QUIT 
Enter your choice: 4

Example mem.c, page no. 359

In [24]:
#Note: There is no concept of unions in Python. We will implment using classes

import sys

class emp:
    name = ''
    idno = 0
    salary = 0.0

class desc:
    name = ''
    idno = 0
    salary = 0.0

e = emp()
d = desc()
    
print "The size of the structure is ", sys.getsizeof(e)
print "The size of the union is ", sys.getsizeof(d)
The size of the structure is  72
The size of the union is  72

Example on page no. 359

In [2]:
class desc:
    name = ''
    idno = 0
    salary = 0.0

des = desc()
des.name = 'Vinod'
print "Employee Details: "
print "The name is ", des.name
print "The idno is ", des.idno
print "The salary is ", des.salary
des.idno = 10
print "Employee Details: "
print "The name is ", des.name
print "The idno is ", des.idno
print "The salary is ", des.salary
des.salary = 6500.00
print "Employee Details: "
print "The name is ", des.name
print "The idno is ", des.idno
print "The salary is ", des.salary
Employee Details: 
The name is  Vinod
The idno is  0
The salary is  0.0
Employee Details: 
The name is  Vinod
The idno is  10
The salary is  0.0
Employee Details: 
The name is  Vinod
The idno is  10
The salary is  6500.0