# Chapter 10: Structures and Unions

### Example 10.1, Page Number: 223

In [11]:
class inv_type:
def __init__(self):
self.item=None
self.cost=0
self.retail=0
self.on_hand=0

#Variable declaration
size=100
invtry = []*size
i=5                    #User iput for menu selection

#Initialize the array
def init_list():
for t in range(size):
invtry.append(inv_type())

#get a menu selection
global i
print "(E)nter"
print "(D)isplay"
print "(U)pdate"
print "(Q)uit"
print "choose one: "
i-=1
return i

#enter items into the list
def enter():
#find the first free structure
for i in range(size):
if not(invtry[i].item==None):
break
#i will be size if list is full
if i==size:
print "List full."
return
input(i)

#Input the information
def input(i):
#Enter information; User input
invtry[i].item="Gloves"
invtry[i].cost=10
invtry[i].retail=25
invtry[i].on_hand=50

#Modify an existing item
def update():
name="Gloves"   #User input
for i in range(size):
if not(name==invtry[i].item):
break
if i==size:
return
print "Enter new information."
input(i)

#Display the list
def display():
for t in range(size):
if not(invtry[t].item==None):
print invtry[t].item
print "Cost: $",invtry[t].cost print "Retail:$",invtry[t].retail
print "On hand: ",invtry[t].on_hand
print "Resupply time: ",invtry[t].lead_time," days"

init_list()
while True:
if choice==4:
enter()
elif choice==3:
display()
elif choice==2:
update()
elif choice==1:
break

(E)nter
(D)isplay
(U)pdate
(Q)uit
choose one:
(E)nter
(D)isplay
(U)pdate
(Q)uit
choose one:
Gloves
Cost: $10 Retail:$ 25
On hand:  50
Resupply time:  10  days
(E)nter
(D)isplay
(U)pdate
(Q)uit
choose one:
Enter new information.
(E)nter
(D)isplay
(U)pdate
(Q)uit
choose one:


### Example 10.2, Page Number: 226

In [12]:
class sample:
a=None
ch=None

def f1(parm):
print parm.a," ",parm.ch

#declare arg
arg=sample()

#initialize arg
arg.a=1000
arg.ch='X'

#call function
f1(arg)


1000   X


### Example 10.3, Page Number: 227

In [13]:
class stype:
a=None
b=None

#Variable declaration
svar1=stype()
svar2=stype()

svar1.a=svar1.b=10
svar2.a=svar2.b=20

print "Structures before assignment."
print "svar1: ",svar1.a,' ',svar1.b
print "svar1: ",svar2.a,' ',svar2.b

svar2=svar1    #assign structures

#Result
print "\nStructures before assignment."
print "svar1: ",svar1.a,' ',svar1.b
print "svar1: ",svar2.a,' ',svar2.b

Structures before assignment.
svar1:  10   10
svar1:  20   20

Structures before assignment.
svar1:  10   10
svar1:  10   10


### Example 10.4, Page Number: 230

In [14]:
import datetime

date=datetime.datetime.now()

#Result
print date.time()

17:06:28.236000


### Example 10.5, Page Number: 231

In [15]:
import datetime

date=datetime.datetime.now()

#Result
print date.ctime()

Sat Sep 14 17:07:14 2013


### Example 10.6, Page Number: 232

In [16]:
class mystruct:
a=None
b=None

def f(var):
var[0].a=var[0].a*var[0].a
var[0].b=var[0].b/var[0].b
return var[0]

#Variable declaration
x=[]
x.append(mystruct())
y=mystruct()

#Initializing
x[0].a=10
x[0].b=20

print "Original x.a and x.b: ",x[0].a,' ',x[0].b

y=f(x)   #function call

#Result
print "Modified x.a and x.b: ",x[0].a,' ',x[0].b
print "Modified y.a and y.b: ",y.a,' ',y.b

Original x.a and x.b:  10   20
Modified x.a and x.b:  100   1
Modified y.a and y.b:  100   1


### Example 10.7, Page Number: 239

In [17]:
class swap_bytes:
ch=[0,0]

#Exchange of bytes
def disp_binary(u):
t=128
while not(t==0):
if u&t:
print "1 ",
else:
print "0 ",
t=t/2

#Variable declaration
sb=swap_bytes()

sb.ch[0]=15

print "Original bytes: ",
disp_binary(sb.ch[1])
disp_binary(sb.ch[0])

#Exchange bytes
temp=sb.ch[0]
sb.ch[0]=sb.ch[1]
sb.ch[1]=temp

#Result
print "\nExchanged bytes: ",
disp_binary(sb.ch[1])
disp_binary(sb.ch[0])


Original bytes:  0  0  0  0  0  0  0  0  0  0  0  0  1  1  1  1
Exchanged bytes:  0  0  0  0  1  1  1  1  0  0  0  0  0  0  0  0


### Example 10.8, Page Number: 240

In [18]:
ch='a'

while True:
print "\n",ch,
print bin(ord(ch))     #Display the bit pattern for each character
ch=chr(ord(ch)+1)
if ch=='r':
break

a 0b1100001

b 0b1100010

c 0b1100011

d 0b1100100

e 0b1100101

f 0b1100110

g 0b1100111

h 0b1101000

i 0b1101001

j 0b1101010

k 0b1101011

l 0b1101100

m 0b1101101

n 0b1101110

o 0b1101111

p 0b1110000

q 0b1110001


### Example 10.9, Page Number: 242

In [19]:
#Variable declaration
ch=['X','Y']
c=""
def disp_bits(u):
t=128
global c
while not(t==0):
if u&t:
c=c+"1"
else:
c=c+"0"
t=t/2
return c

#Result
print "union as chars: ",ch[0],ch[1]
print "union as integer: ",
c= disp_bits(ord(ch[1]))
c= disp_bits(ord(ch[0]))
print int(str(c),2)

union as chars:  X Y
union as integer:  22872

In [ ]: