Chapter 12: Structures and Unions

Example 12.1, Page 12.2

In [1]:
from ctypes import *

string=c_char*20

class account(Structure):
	_fields_=[('acct_no',c_int),('acct_type',c_char),('name',string),('balance',c_int)]

customer=account(12632,'r','Joseph',1200)

Example 12.4, Page 12.4

In [2]:
from ctypes import *

string=c_char*20

class date(Structure):
	_fields_=[('month',c_int),('day',c_int),('year',c_int)]
	
class account(Structure):
	_fields_=[('acct_no',c_int),('acct_type',c_char),('name',string),('balance',c_float),('lastpayment',date)]
	
customer=account(12345,'r','John W. Smith',586.30,date(5,24,90))

Example 12.6, Page 12.6

In [3]:
from ctypes import *

string=c_char*20

class date(Structure):
	
	_fields_=[('name',string),('month',c_int),('day',c_int),('year',c_int)]

birthday=[]
birthday.append(date('Amy',12,30,73))
birthday.append(date('Gail',5,13,66))
birthday.append(date('Marc',7,15,72))
birthday.append(date('Marla',11,29,70))
birthday.append(date('Megan',2,4,77))
birthday.append(date('Sharon',12,29,63))
birthday.append(date('Susan',4,12,69))

Example 12.14, Page 12.13

In [4]:
from ctypes import *

string=c_char*50

def writeoutput(obj):
	print "Name : ",obj.name,
	print "\t Account Number : ",obj.acct_no
	print "Street : ",obj.street
	print "City : ",obj.city
	print "Old Balance : %7.2f" %(obj.oldbalance)
	print "Payment : %7.2f" %(obj.payment)
	print "New Balance : %7.2f" %(obj.newbalance)
	print "Account Status : ",
	
	if obj.acct_type=='C':
		print "CURRENT\n\n"
	elif obj.acct_type=='O':
		print "OVERDUE\n\n"
	else:
		print "DELINQUENT\n\n"
		
	return


class date(Structure):
	_fields_=[('month',c_int),('day',c_int),('year',c_int)]
	

class account(Structure):
	_fields_=[('name',string),('street',string),('city',string),('acct_no',c_int),('oldbalance',c_float),('payment',c_float),('lastpayment',date),('newbalance',c_float),('acct_type',c_char)]
		
print "CUSOMER BILLING SYSTEM\n\n"
customer=[]
name='Steve Johnson'
street='123 Mountainview Drive '
city='Denver . CO'
acct_no=4208
oldbalance=247.88
payment=25.00
lastpay=date(6,14,1998)
customer.append(account(name,street,city,acct_no,oldbalance,payment,lastpay))

name='Susan Richards'
street='4383 Aligator Blvd'
city='Fort Lauderdale. FL'
acct_no=2219
oldbalance=135.00
payment=135.00
lastpay=date(8,10,2000)
customer.append(account(name,street,city,acct_no,oldbalance,payment,lastpay))

name='Martin Peterson'
street='1787 Pacific Parkway'
city='San Diego. CA'
acct_no=8452
oldbalance=387.42
payment=35.00
lastpay=date(9,22,1999)
customer.append(account(name,street,city,acct_no,oldbalance,payment,lastpay))

name='Phyllis Smith'
street='1000 Great White Way'
city='New York. NY'
acct_no=711
oldbalance=260.00
payment=0.00
lastpay=date(11,27,2001)
customer.append(account(name,street,city,acct_no,oldbalance,payment,lastpay))



for i in range(0,4):
	if customer[i].payment>0:
		if customer[i].payment<0.1*customer[i].oldbalance:
			customer[i].acct_type='O'
		else:
			customer[i].acct_type='C'
	else:
		if customer[i].oldbalance>0:
			customer[i].acct_type='D'
		else:
			customer[i].acct_type='C'
	
	customer[i].newbalance=customer[i].oldbalance-customer[i].payment
	
	writeoutput(customer[i])
CUSOMER BILLING SYSTEM


Name :  Steve Johnson 	 Account Number :  4208
Street :  123 Mountainview Drive 
City :  Denver . CO
Old Balance :  247.88
Payment :   25.00
New Balance :  222.88
Account Status :  CURRENT


Name :  Susan Richards 	 Account Number :  2219
Street :  4383 Aligator Blvd
City :  Fort Lauderdale. FL
Old Balance :  135.00
Payment :  135.00
New Balance :    0.00
Account Status :  CURRENT


Name :  Martin Peterson 	 Account Number :  8452
Street :  1787 Pacific Parkway
City :  San Diego. CA
Old Balance :  387.42
Payment :   35.00
New Balance :  352.42
Account Status :  OVERDUE


Name :  Phyllis Smith 	 Account Number :  711
Street :  1000 Great White Way
City :  New York. NY
Old Balance :  260.00
Payment :    0.00
New Balance :  260.00
Account Status :  DELINQUENT


Example 12.25, Page 12.29

In [5]:
from ctypes import *

string=c_char*10

class record(Structure):
	_fields_=[('name',string),('acct_no',c_int),('acct_type',c_char),('balance',c_float)]
	
def adjust(obj):
	obj.name='Jones'
	obj.acct_no=9999
	obj.acct_type='R'
	obj.balance=99.99
	return


customer=record('Smith',3333,'C',33.33)
print customer.name,customer.acct_no,customer.acct_type,
print "%.2f" %customer.balance

adjust(customer)

print customer.name,customer.acct_no,customer.acct_type,
print "%.2f" %customer.balance
Smith 3333 C 33.33
Jones 9999 R 99.99

Example 12.32, Page 12.42

In [8]:
class node():
	
	def __init__(self,data):
		self.data=data
		self.next=None

		
class list():
	
	def __init__(self):
		self.head=None
		self.tail=None
	
	def insert(self,x):
		
		e=node(x)
		
		if self.head==None:
			self.head=e
			self.tail=e
			
		else:
			self.tail.next=e
			self.tail=e
			
def display(ptr):
	nptr=ptr.head
	while nptr!=None:
		print nptr.data
		nptr=nptr.next
	
	return
	
def delete(ptr,element):
	nptr=ptr.head
	while nptr.next.data!=element:
		nptr=nptr.next
		if nptr.next==None:
			return
	
	dptr=nptr.next
	nptr.next=dptr.next
	del dptr
	return
	
p=list()
p.insert('BOSTON')
p.insert('CHICAGO')
p.insert('DENVER')
p.insert('NEW YORK')
p.insert('PITTSBURG')

display(p)
print '\n\nAFTER DELETING DENVER\n\n'
delete(p,'DENVER')
display(p)
BOSTON
CHICAGO
DENVER
NEW YORK
PITTSBURG


AFTER DELETING DENVER


BOSTON
CHICAGO
NEW YORK
PITTSBURG

Example 12.35, Page 12.57

In [7]:
from ctypes import *

string=c_char*20

class id(Union):
	_fields_=[('color',c_char),('size',c_int)]
	
class clothes(Structure):
	_fields_=[('manufacturer',string),('cost',c_float),('description',id)]
	
shirt=clothes()
shirt.description.color='w'
print "%c %d\n" %(shirt.description.color,shirt.description.size)

shirt.description.size=12
print "%c %d" %(shirt.description.color,shirt.description.size)
w 119

 12

Example 12.37, Page 12.59

In [6]:
from ctypes import *
import math


class nvals(Union):
	_fields_=[('fexp',c_float),('nexp',c_int)]
	
class values(Structure):
	_fields_=[('x',c_float),('flag',c_char),('exp',nvals)]
	
def power(a):
	y=a.x
	
	if a.flag=='i':
		if a.exp.nexp==0:
			y=1.0
		else:
			i=1
			while i<abs(a.exp.nexp):
				y*=a.x
				i+=1
			
			if a.exp.nexp<0:
				y=1.0/y
	
	else:
		y=math.exp(a.exp.fexp*math.log(a.x))
	
	return y
	
def main(in1,n):
	a=values()
	a.x=in1
	
	i=int(n)
	
	if i==n:
		a.flag='i'
	else:
		a.flag='f'
	
	if a.flag=='i':
		a.exp.nexp=i
	else:
		a.exp.fexp=n
		
	if a.flag=='f' and a.x<=0.0:
		print "ERROR - Cannot raise a non-positive number to a floating point power"
	else:
		y=power(a)
		print "y=%.4f" %y
	
	return

print "\nx=2\nn=3"
main(2,3)
print "\nx=-2\nn=3"
main(-2,3)
print "\nx=2.2\nn=3.3"
main(2.2,3.3)
print "\nx=-2.2\nn=3.3"
main(-2.2,3.3)


	
x=2
n=3
y=8.0000

x=-2
n=3
y=-8.0000

x=2.2
n=3.3
y=13.4895

x=-2.2
n=3.3
ERROR - Cannot raise a non-positive number to a floating point power
In [ ]: