{ "metadata": { "name": "" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "

Chapter 12: Structures and Unions

" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 12.1, Page 12.2

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "from ctypes import *\n", "\n", "string=c_char*20\n", "\n", "class account(Structure):\n", "\t_fields_=[('acct_no',c_int),('acct_type',c_char),('name',string),('balance',c_int)]\n", "\n", "customer=account(12632,'r','Joseph',1200)\n", "\n" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 1 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 12.4, Page 12.4

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "from ctypes import *\n", "\n", "string=c_char*20\n", "\n", "class date(Structure):\n", "\t_fields_=[('month',c_int),('day',c_int),('year',c_int)]\n", "\t\n", "class account(Structure):\n", "\t_fields_=[('acct_no',c_int),('acct_type',c_char),('name',string),('balance',c_float),('lastpayment',date)]\n", "\t\n", "customer=account(12345,'r','John W. Smith',586.30,date(5,24,90))" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 2 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 12.6, Page 12.6

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "from ctypes import *\n", "\n", "string=c_char*20\n", "\n", "class date(Structure):\n", "\t\n", "\t_fields_=[('name',string),('month',c_int),('day',c_int),('year',c_int)]\n", "\n", "birthday=[]\n", "birthday.append(date('Amy',12,30,73))\n", "birthday.append(date('Gail',5,13,66))\n", "birthday.append(date('Marc',7,15,72))\n", "birthday.append(date('Marla',11,29,70))\n", "birthday.append(date('Megan',2,4,77))\n", "birthday.append(date('Sharon',12,29,63))\n", "birthday.append(date('Susan',4,12,69))" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 3 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 12.14, Page 12.13

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "from ctypes import *\n", "\n", "string=c_char*50\n", "\n", "def writeoutput(obj):\n", "\tprint \"Name : \",obj.name,\n", "\tprint \"\\t Account Number : \",obj.acct_no\n", "\tprint \"Street : \",obj.street\n", "\tprint \"City : \",obj.city\n", "\tprint \"Old Balance : %7.2f\" %(obj.oldbalance)\n", "\tprint \"Payment : %7.2f\" %(obj.payment)\n", "\tprint \"New Balance : %7.2f\" %(obj.newbalance)\n", "\tprint \"Account Status : \",\n", "\t\n", "\tif obj.acct_type=='C':\n", "\t\tprint \"CURRENT\\n\\n\"\n", "\telif obj.acct_type=='O':\n", "\t\tprint \"OVERDUE\\n\\n\"\n", "\telse:\n", "\t\tprint \"DELINQUENT\\n\\n\"\n", "\t\t\n", "\treturn\n", "\n", "\n", "class date(Structure):\n", "\t_fields_=[('month',c_int),('day',c_int),('year',c_int)]\n", "\t\n", "\n", "class account(Structure):\n", "\t_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)]\n", "\t\t\n", "print \"CUSOMER BILLING SYSTEM\\n\\n\"\n", "customer=[]\n", "name='Steve Johnson'\n", "street='123 Mountainview Drive '\n", "city='Denver . CO'\n", "acct_no=4208\n", "oldbalance=247.88\n", "payment=25.00\n", "lastpay=date(6,14,1998)\n", "customer.append(account(name,street,city,acct_no,oldbalance,payment,lastpay))\n", "\n", "name='Susan Richards'\n", "street='4383 Aligator Blvd'\n", "city='Fort Lauderdale. FL'\n", "acct_no=2219\n", "oldbalance=135.00\n", "payment=135.00\n", "lastpay=date(8,10,2000)\n", "customer.append(account(name,street,city,acct_no,oldbalance,payment,lastpay))\n", "\n", "name='Martin Peterson'\n", "street='1787 Pacific Parkway'\n", "city='San Diego. CA'\n", "acct_no=8452\n", "oldbalance=387.42\n", "payment=35.00\n", "lastpay=date(9,22,1999)\n", "customer.append(account(name,street,city,acct_no,oldbalance,payment,lastpay))\n", "\n", "name='Phyllis Smith'\n", "street='1000 Great White Way'\n", "city='New York. NY'\n", "acct_no=711\n", "oldbalance=260.00\n", "payment=0.00\n", "lastpay=date(11,27,2001)\n", "customer.append(account(name,street,city,acct_no,oldbalance,payment,lastpay))\n", "\n", "\n", "\n", "for i in range(0,4):\n", "\tif customer[i].payment>0:\n", "\t\tif customer[i].payment<0.1*customer[i].oldbalance:\n", "\t\t\tcustomer[i].acct_type='O'\n", "\t\telse:\n", "\t\t\tcustomer[i].acct_type='C'\n", "\telse:\n", "\t\tif customer[i].oldbalance>0:\n", "\t\t\tcustomer[i].acct_type='D'\n", "\t\telse:\n", "\t\t\tcustomer[i].acct_type='C'\n", "\t\n", "\tcustomer[i].newbalance=customer[i].oldbalance-customer[i].payment\n", "\t\n", "\twriteoutput(customer[i])\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "CUSOMER BILLING SYSTEM\n", "\n", "\n", "Name : Steve Johnson \t Account Number : 4208\n", "Street : 123 Mountainview Drive \n", "City : Denver . CO\n", "Old Balance : 247.88\n", "Payment : 25.00\n", "New Balance : 222.88\n", "Account Status : CURRENT\n", "\n", "\n", "Name : Susan Richards \t Account Number : 2219\n", "Street : 4383 Aligator Blvd\n", "City : Fort Lauderdale. FL\n", "Old Balance : 135.00\n", "Payment : 135.00\n", "New Balance : 0.00\n", "Account Status : CURRENT\n", "\n", "\n", "Name : Martin Peterson \t Account Number : 8452\n", "Street : 1787 Pacific Parkway\n", "City : San Diego. CA\n", "Old Balance : 387.42\n", "Payment : 35.00\n", "New Balance : 352.42\n", "Account Status : OVERDUE\n", "\n", "\n", "Name : Phyllis Smith \t Account Number : 711\n", "Street : 1000 Great White Way\n", "City : New York. NY\n", "Old Balance : 260.00\n", "Payment : 0.00\n", "New Balance : 260.00\n", "Account Status : DELINQUENT\n", "\n", "\n" ] } ], "prompt_number": 4 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 12.25, Page 12.29

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "from ctypes import *\n", "\n", "string=c_char*10\n", "\n", "class record(Structure):\n", "\t_fields_=[('name',string),('acct_no',c_int),('acct_type',c_char),('balance',c_float)]\n", "\t\n", "def adjust(obj):\n", "\tobj.name='Jones'\n", "\tobj.acct_no=9999\n", "\tobj.acct_type='R'\n", "\tobj.balance=99.99\n", "\treturn\n", "\n", "\n", "customer=record('Smith',3333,'C',33.33)\n", "print customer.name,customer.acct_no,customer.acct_type,\n", "print \"%.2f\" %customer.balance\n", "\n", "adjust(customer)\n", "\n", "print customer.name,customer.acct_no,customer.acct_type,\n", "print \"%.2f\" %customer.balance\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Smith 3333 C 33.33\n", "Jones 9999 R 99.99\n" ] } ], "prompt_number": 5 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 12.32, Page 12.42

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "class node():\n", "\t\n", "\tdef __init__(self,data):\n", "\t\tself.data=data\n", "\t\tself.next=None\n", "\n", "\t\t\n", "class list():\n", "\t\n", "\tdef __init__(self):\n", "\t\tself.head=None\n", "\t\tself.tail=None\n", "\t\n", "\tdef insert(self,x):\n", "\t\t\n", "\t\te=node(x)\n", "\t\t\n", "\t\tif self.head==None:\n", "\t\t\tself.head=e\n", "\t\t\tself.tail=e\n", "\t\t\t\n", "\t\telse:\n", "\t\t\tself.tail.next=e\n", "\t\t\tself.tail=e\n", "\t\t\t\n", "def display(ptr):\n", "\tnptr=ptr.head\n", "\twhile nptr!=None:\n", "\t\tprint nptr.data\n", "\t\tnptr=nptr.next\n", "\t\n", "\treturn\n", "\t\n", "def delete(ptr,element):\n", "\tnptr=ptr.head\n", "\twhile nptr.next.data!=element:\n", "\t\tnptr=nptr.next\n", "\t\tif nptr.next==None:\n", "\t\t\treturn\n", "\t\n", "\tdptr=nptr.next\n", "\tnptr.next=dptr.next\n", "\tdel dptr\n", "\treturn\n", "\t\n", "p=list()\n", "p.insert('BOSTON')\n", "p.insert('CHICAGO')\n", "p.insert('DENVER')\n", "p.insert('NEW YORK')\n", "p.insert('PITTSBURG')\n", "\n", "display(p)\n", "print '\\n\\nAFTER DELETING DENVER\\n\\n'\n", "delete(p,'DENVER')\n", "display(p)\n", "\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "BOSTON\n", "CHICAGO\n", "DENVER\n", "NEW YORK\n", "PITTSBURG\n", "\n", "\n", "AFTER DELETING DENVER\n", "\n", "\n", "BOSTON\n", "CHICAGO\n", "NEW YORK\n", "PITTSBURG\n" ] } ], "prompt_number": 8 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 12.35, Page 12.57

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "from ctypes import *\n", "\n", "string=c_char*20\n", "\n", "class id(Union):\n", "\t_fields_=[('color',c_char),('size',c_int)]\n", "\t\n", "class clothes(Structure):\n", "\t_fields_=[('manufacturer',string),('cost',c_float),('description',id)]\n", "\t\n", "shirt=clothes()\n", "shirt.description.color='w'\n", "print \"%c %d\\n\" %(shirt.description.color,shirt.description.size)\n", "\n", "shirt.description.size=12\n", "print \"%c %d\" %(shirt.description.color,shirt.description.size)\n", "\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "w 119\n", "\n", "\f", " 12\n" ] } ], "prompt_number": 7 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 12.37, Page 12.59

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "from ctypes import *\n", "import math\n", "\n", "\n", "class nvals(Union):\n", "\t_fields_=[('fexp',c_float),('nexp',c_int)]\n", "\t\n", "class values(Structure):\n", "\t_fields_=[('x',c_float),('flag',c_char),('exp',nvals)]\n", "\t\n", "def power(a):\n", "\ty=a.x\n", "\t\n", "\tif a.flag=='i':\n", "\t\tif a.exp.nexp==0:\n", "\t\t\ty=1.0\n", "\t\telse:\n", "\t\t\ti=1\n", "\t\t\twhile i