diff options
author | kinitrupti | 2017-05-12 18:53:46 +0530 |
---|---|---|
committer | kinitrupti | 2017-05-12 18:53:46 +0530 |
commit | 6279fa19ac6e2a4087df2e6fe985430ecc2c2d5d (patch) | |
tree | 22789c9dbe468dae6697dcd12d8e97de4bcf94a2 /Programming_With_C_by_Byron_S._Gottfried/chapter12.ipynb | |
parent | d36fc3b8f88cc3108ffff6151e376b619b9abb01 (diff) | |
download | Python-Textbook-Companions-6279fa19ac6e2a4087df2e6fe985430ecc2c2d5d.tar.gz Python-Textbook-Companions-6279fa19ac6e2a4087df2e6fe985430ecc2c2d5d.tar.bz2 Python-Textbook-Companions-6279fa19ac6e2a4087df2e6fe985430ecc2c2d5d.zip |
Removed duplicates
Diffstat (limited to 'Programming_With_C_by_Byron_S._Gottfried/chapter12.ipynb')
-rwxr-xr-x | Programming_With_C_by_Byron_S._Gottfried/chapter12.ipynb | 565 |
1 files changed, 565 insertions, 0 deletions
diff --git a/Programming_With_C_by_Byron_S._Gottfried/chapter12.ipynb b/Programming_With_C_by_Byron_S._Gottfried/chapter12.ipynb new file mode 100755 index 00000000..09e4c7e9 --- /dev/null +++ b/Programming_With_C_by_Byron_S._Gottfried/chapter12.ipynb @@ -0,0 +1,565 @@ +{
+ "metadata": {
+ "name": ""
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h1>Chapter 12: Structures and Unions<h1>"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 12.1, Page 12.2<h3>"
+ ]
+ },
+ {
+ "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": [
+ "<h3>Example 12.4, Page 12.4<h3>"
+ ]
+ },
+ {
+ "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": [
+ "<h3>Example 12.6, Page 12.6<h3>"
+ ]
+ },
+ {
+ "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": [
+ "<h3>Example 12.14, Page 12.13<h3>"
+ ]
+ },
+ {
+ "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": [
+ "<h3>Example 12.25, Page 12.29<h3>"
+ ]
+ },
+ {
+ "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": [
+ "<h3>Example 12.32, Page 12.42<h3>"
+ ]
+ },
+ {
+ "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": [
+ "<h3>Example 12.35, Page 12.57<h3>"
+ ]
+ },
+ {
+ "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": [
+ "<h3>Example 12.37, Page 12.59<h3>"
+ ]
+ },
+ {
+ "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<abs(a.exp.nexp):\n",
+ "\t\t\t\ty*=a.x\n",
+ "\t\t\t\ti+=1\n",
+ "\t\t\t\n",
+ "\t\t\tif a.exp.nexp<0:\n",
+ "\t\t\t\ty=1.0/y\n",
+ "\t\n",
+ "\telse:\n",
+ "\t\ty=math.exp(a.exp.fexp*math.log(a.x))\n",
+ "\t\n",
+ "\treturn y\n",
+ "\t\n",
+ "def main(in1,n):\n",
+ "\ta=values()\n",
+ "\ta.x=in1\n",
+ "\t\n",
+ "\ti=int(n)\n",
+ "\t\n",
+ "\tif i==n:\n",
+ "\t\ta.flag='i'\n",
+ "\telse:\n",
+ "\t\ta.flag='f'\n",
+ "\t\n",
+ "\tif a.flag=='i':\n",
+ "\t\ta.exp.nexp=i\n",
+ "\telse:\n",
+ "\t\ta.exp.fexp=n\n",
+ "\t\t\n",
+ "\tif a.flag=='f' and a.x<=0.0:\n",
+ "\t\tprint \"ERROR - Cannot raise a non-positive number to a floating point power\"\n",
+ "\telse:\n",
+ "\t\ty=power(a)\n",
+ "\t\tprint \"y=%.4f\" %y\n",
+ "\t\n",
+ "\treturn\n",
+ "\n",
+ "print \"\\nx=2\\nn=3\"\n",
+ "main(2,3)\n",
+ "print \"\\nx=-2\\nn=3\"\n",
+ "main(-2,3)\n",
+ "print \"\\nx=2.2\\nn=3.3\"\n",
+ "main(2.2,3.3)\n",
+ "print \"\\nx=-2.2\\nn=3.3\"\n",
+ "main(-2.2,3.3)\n",
+ "\n",
+ "\n",
+ "\t\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "x=2\n",
+ "n=3\n",
+ "y=8.0000\n",
+ "\n",
+ "x=-2\n",
+ "n=3\n",
+ "y=-8.0000\n",
+ "\n",
+ "x=2.2\n",
+ "n=3.3\n",
+ "y=13.4895\n",
+ "\n",
+ "x=-2.2\n",
+ "n=3.3\n",
+ "ERROR - Cannot raise a non-positive number to a floating point power\n"
+ ]
+ }
+ ],
+ "prompt_number": 6
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [],
+ "language": "python",
+ "metadata": {},
+ "outputs": []
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+}
\ No newline at end of file |