summaryrefslogtreecommitdiff
path: root/ANSI_C_Programming/chapter10.ipynb
diff options
context:
space:
mode:
Diffstat (limited to 'ANSI_C_Programming/chapter10.ipynb')
-rw-r--r--ANSI_C_Programming/chapter10.ipynb768
1 files changed, 768 insertions, 0 deletions
diff --git a/ANSI_C_Programming/chapter10.ipynb b/ANSI_C_Programming/chapter10.ipynb
new file mode 100644
index 00000000..51080026
--- /dev/null
+++ b/ANSI_C_Programming/chapter10.ipynb
@@ -0,0 +1,768 @@
+{
+ "metadata": {
+ "name": "chapter10.ipynb"
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "heading",
+ "level": 1,
+ "metadata": {},
+ "source": [
+ "CHAPTER 10: STRUCTURES"
+ ]
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "EXAMPLE ON PAGE:342-343"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#storing & printing names,prices & pages of books\n",
+ "name=[]\n",
+ "price=[]\n",
+ "pages=[]\n",
+ "for i in range(3):\n",
+ " name.append(\"\\0\")\n",
+ " price.append(0)\n",
+ " pages.append(0)\n",
+ "print \"Enter names,prices and no. of pages of 3 books\\n\"\n",
+ "for i in range(0,3,1):\n",
+ " name[i]=raw_input()\n",
+ " price[i]=eval(raw_input())\n",
+ " pages[i]=eval(raw_input())\n",
+ "print \"\\nAnd this is what you entered\\n\"\n",
+ "for i in range(0,3,1):\n",
+ " print \"%c %f %d\\n\" % (name[i],price[i],pages[i])"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter names,prices and no. of pages of 3 books\n",
+ "\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "A\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "100.00\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "354\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "C\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "256.50\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "682\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "F\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "233.70\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "512\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "And this is what you entered\n",
+ "\n",
+ "A 100.000000 354\n",
+ "\n",
+ "C 256.500000 682\n",
+ "\n",
+ "F 233.700000 512\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "EXAMPLE ON PAGE:343-344"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#storing & printing names,prices & pages of books using structure\n",
+ "class book: #YOU CAN ALSO USE CTYPES FOR IMPLEMENTING STRUCTURES\n",
+ " def __init__(self, **kwds):\n",
+ " self.__dict__.update(kwds)\n",
+ "b1=book()\n",
+ "b2=book()\n",
+ "b3=book()\n",
+ "print \"Enter names,prices & no. of pages of 3 books\\n\"\n",
+ "b1.name=raw_input()\n",
+ "b1.price=eval(raw_input())\n",
+ "b1.pages=eval(raw_input())\n",
+ "b2.name=raw_input()\n",
+ "b2.price=eval(raw_input())\n",
+ "b2.pages=eval(raw_input())\n",
+ "b3.name=raw_input()\n",
+ "b3.price=eval(raw_input())\n",
+ "b3.pages=eval(raw_input())\n",
+ "print \"And this is what you entered\\n\"\n",
+ "print \"%c %f %d\\n\" % (b1.name,b1.price,b1.pages)\n",
+ "print \"%c %f %d\\n\" % (b2.name,b2.price,b2.pages)\n",
+ "print \"%c %f %d\\n\" % (b3.name,b3.price,b3.pages)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter names,prices & no. of pages of 3 books\n",
+ "\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "A\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "100.00\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "354\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "C\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "256.50\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "682\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "F\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "233.70\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "512\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "And this is what you entered\n",
+ "\n",
+ "A 100.000000 354\n",
+ "\n",
+ "C 256.500000 682\n",
+ "\n",
+ "F 233.700000 512\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "EXAMPLE ON PAGE:347"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Memory map of structure elements\n",
+ "class book():\n",
+ " def __init__(self,**kwds):\n",
+ " self.__dict__.update(kwds)\n",
+ "b1=book(name='B',price=130.00,pages=550)\n",
+ "print \"Address of name=%u\\n\" % (id(b1.name))\n",
+ "print \"Address of price=%u\\n\" % (id(b1.price))\n",
+ "print \"Address of pages=%u\\n\" % (id(b1.pages))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Address of name=20788864\n",
+ "\n",
+ "Address of price=88371144\n",
+ "\n",
+ "Address of pages=88859008\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 3
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "EXAMPLE ON PAGE:348-349"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Usage of an array of stuctures\n",
+ "def linkfloat():\n",
+ " a=0\n",
+ " b=id(a) #cause emulator to be linked\n",
+ "class book():\n",
+ " def __init__(self,**kwds):\n",
+ " self.__dict__.update(kwds)\n",
+ "b=[]\n",
+ "for i in range(3):\n",
+ " b.append(0) #setting initial value as zero\n",
+ "for i in range(0,3,1):\n",
+ " b[i]=book()\n",
+ "for i in range(0,3,1):\n",
+ " print \"Enter name price and pages\"\n",
+ " b[i].name=raw_input()\n",
+ " b[i].price=eval(raw_input())\n",
+ " b[i].pages=eval(raw_input())\n",
+ "for i in range(0,3,1):\n",
+ " print \"%c %f %d\\n\" % (b[i].name,b[i].price,b[i].pages)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter name price and pages\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "A\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "100.00\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "354\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter name price and pages\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "C\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "256.50\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "682\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter name price and pages\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "F\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "233.70\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "512\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "A 100.000000 354\n",
+ "\n",
+ "C 256.500000 682\n",
+ "\n",
+ "F 233.700000 512\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 4
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "EXAMPLE ON PAGE:350-351"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Copying elements of an structure into another structure\n",
+ "class employee():\n",
+ " def __init__(self,**kwds):\n",
+ " self.__dict__.update(kwds)\n",
+ "e1=employee(name=\"Sanjay\",age=30,salary=5500.50)\n",
+ "e2=employee()\n",
+ "e3=employee()\n",
+ "#piece-meal copying\n",
+ "import copy\n",
+ "e2.name=copy.copy(e1.name)\n",
+ "e2.age=e1.age\n",
+ "e2.salary=e1.salary\n",
+ "#copying all elements at one go\n",
+ "e3=e2\n",
+ "print \"%s %d %f\\n\" % (e1.name,e1.age,e1.salary)\n",
+ "print \"%s %d %f\\n\" % (e2.name,e2.age,e2.salary)\n",
+ "print \"%s %d %f\\n\" % (e3.name,e3.age,e3.salary)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Sanjay 30 5500.500000\n",
+ "\n",
+ "Sanjay 30 5500.500000\n",
+ "\n",
+ "Sanjay 30 5500.500000\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 5
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "EXAMPLE ON PAGE:351-352"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#declaring a structure inside another structure\n",
+ "class address():\n",
+ " def __init__(self, **kwds):\n",
+ " self.__dict__.update(kwds)\n",
+ "class emp():\n",
+ " def __init__(self, **kwds):\n",
+ " self.__dict__.update(kwds)\n",
+ "a = address(phone=\"531046\", city=\"nagpur\", pin=10)\n",
+ "e = emp(name=\"jeru\", address=a)\n",
+ "print \"name=%s phone=%s\\n\" % (e.name,e.address.phone)\n",
+ "print \"city=%s pin=%d\\n\" % (e.address.city,e.address.pin)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "name=jeru phone=531046\n",
+ "\n",
+ "city=nagpur pin=10\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 6
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "EXAMPLE ON PAGE:353"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#passing individual structure elements to a function\n",
+ "class book():\n",
+ " def __init__(self, **kwds):\n",
+ " self.__dict__.update(kwds)\n",
+ "def display(s,t,n):\n",
+ " print \"%s %s %d\\n\" % (s,t,n)\n",
+ "b1=book(name=\"Let us C\",author=\"YPK\",callno=101)\n",
+ "display(b1.name,b1.author,b1.callno)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Let us C YPK 101\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 7
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "EXAMPLE ON PAGE:353-354"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#passing whole structure to a function\n",
+ "class book():\n",
+ " def __init__(self, **kwds):\n",
+ " self.__dict__.update(kwds)\n",
+ "def display(b):\n",
+ " print \"%s %s %d\\n\" % (b.name,b.author,b.callno)\n",
+ "b1=book(name=\"Let us C\",author=\"YPK\",callno=101)\n",
+ "display(b1)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Let us C YPK 101\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 8
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "EXAMPLE ON PAGE:354-355"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#printing structure elements to a function\n",
+ "class book():\n",
+ " def __init__(self, **kwds):\n",
+ " self.__dict__.update(kwds)\n",
+ "b1=book(name=\"Let us C\",author=\"YPK\",callno=101)\n",
+ "ptr=book()\n",
+ "ptr=b1\n",
+ "print \"%s %s %d\\n\" % (b1.name,b1.author,b1.callno)\n",
+ "print \"%s %s %d\\n\" % (ptr.name,ptr.author,ptr.callno)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Let us C YPK 101\n",
+ "\n",
+ "Let us C YPK 101\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 9
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "EXAMPLE ON PAGE:355-356"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#passing address of a structure variable\n",
+ "class book():\n",
+ " def __init__(self, **kwds):\n",
+ " self.__dict__.update(kwds)\n",
+ "def display(b):\n",
+ " print \"%s %s %d\\n\" % (b.name,b.author,b.callno)\n",
+ "b1=book(name=\"Let us C\",author=\"YPK\",callno=101)\n",
+ "display(b1)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Let us C YPK 101\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 10
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "EXAMPLE ON PAGE:356"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#printing address of elements of structure\n",
+ "class emp():\n",
+ " def __init__(self, **kwds):\n",
+ " self.__dict__.update(kwds)\n",
+ "e=emp(a=0,ch='\\0',s=0.0)\n",
+ "print \"%u %u %u\\n\" % (id(e.a),id(e.ch),id(e.s))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "20623060 20204312 88374024\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 11
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "EXAMPLE ON PAGE:357-358"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Implementation of structure\n",
+ "#pragma directive is not present in python\n",
+ "class emp():\n",
+ " def __init__(self,a,ch,s): #Structure without using dictionary\n",
+ " self.a=a\n",
+ " self.ch=ch\n",
+ " self.s=s\n",
+ "e=emp(a=0,ch='\\0',s=0.0)\n",
+ "print \"%u %u %u\\n\" % (id(e.a),id(e.ch),id(e.s))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "20360916 19942168 88177544\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 10
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+} \ No newline at end of file