{ "metadata": { "name": "", "signature": "sha256:0724a7e756186f06943f5403b91daf5f178ee495f014e11c1fad0330aa0d254c" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Chapter 18 : Pointers and Structures" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example: 1, Page No.: 5.130" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Program to print details of person using structure and pointer\n", "\n", "class account:\n", " name=\"\"\n", " place=\"\"\n", " acctno=0\n", " \n", "m4=account()\n", "m4.name=\"Venkat\"\n", "m4.place=\"Arcot\"\n", "m4.acctno=24201\n", "\n", "print \"%s\\t%s\\t%d\\n\"%(m4.name,m4.place,m4.acctno)\n", "\n", "contents=m4\n", "\n", "print \"%s\\t%s\\t%d\\n\"%(contents.name,contents.place,contents.acctno)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Venkat\tArcot\t24201\n", "\n", "Venkat\tArcot\t24201\n", "\n" ] } ], "prompt_number": 2 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example: 2, Page No.: 5.131" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Program to print name, place, and account number using structure and pointer\n", "\n", "class account:\n", " name=\"\"\n", " place=\"\"\n", " acctno=0\n", " \n", "m4=account()\n", "m4.name=\"Madhavan\"\n", "m4.place=\"Chennai\"\n", "m4.acctno=241\n", "a=m4\n", "\n", "print \"%s\\n%s\\n%d\\n\"%(a.name,a.place,a.acctno)\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Madhavan\n", "Chennai\n", "241\n", "\n" ] } ], "prompt_number": 7 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example: 3, Page No.: 5.132" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Program to show how the int_pointers structure can be handled.\n", "\n", "\n", "class int_pointers:\n", " p1=0\n", " p2=0\n", " \n", "pointers=int_pointers()\n", "m1= 80\n", "pointers.p1=m1\n", "pointers.p2=-40\n", "m2=pointers.p2\n", "print \"m1= %i, *pointers.p1 = %i\\n\"% (m1,pointers.p1)\n", "print \"m2= %i, *pointers.p2 = %i\\n\"% (m2,pointers.p2)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "m1= 80, *pointers.p1 = 80\n", "\n", "m2= -40, *pointers.p2 = -40\n", "\n" ] } ], "prompt_number": 15 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Case Study 1: Page No.5.133" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Program to interchange the values stored in two variables using function with pointers. \n", "\n", "def interchange(a,b):\n", " t=a\n", " a=b\n", " b=t\n", " return b,a\n", " \n", "a=39\n", "b=77\n", "\n", "print \"Before Interchange: a=%d, b=%d\"%(a,b)\n", "\n", "\n", "print \"After Interchange : a=%d, b=%d\"%(interchange(b,a))\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Before Interchange: a=39, b=77\n", "After Interchange : a=77, b=39\n" ] } ], "prompt_number": 16 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Case Study: 2, Page No.:5.134" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Program to demonstrate the use of structure pointers\n", "\n", "class book:\n", " bno=0\n", " bname=''\n", " cost=0.0\n", "\n", "p=[book()for i in range(0,3)]\n", "print \"Enter the Book details... \"\n", "print \"Enter Book number, Book Name, Book Costs... \"\n", "for i in range(0,3):\n", " p[i].bno=input()\n", " p[i].bname=raw_input()\n", " p[i].cost=input()\n", " \n", "for i in range(0,3):\n", " \n", " print \"\\n%d\\t%s\\t\\t%d\"%(p[i].bno,p[i].bname,p[i].cost)\n", " \n", "print \"\\nStructure Output\\n\"\n", "ptr=p\n", "\n", "for i in range(0,3):\n", " print \"%d\\t%s\\t\\t%d\"%(ptr[i].bno,ptr[i].bname,ptr[i].cost)\n", " \n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter the Book details... \n", "Enter Book number, Book Name, Book Costs... \n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "101\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Computer Practice_1\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "240\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "201\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Computer Practice_2\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "250\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "202\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "C++ and JAVA\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "280\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "101\tComputer Practice_1\t240\n", "\n", "201\tComputer Practice_2\t250\n", "\n", "202\tC++ and JAVA\t280\n", "\n", "Structure Output\n", "\n", "101\tComputer Practice_1\t240\n", "201\tComputer Practice_2\t250\n", "202\tC++ and JAVA\t280\n" ] } ], "prompt_number": 46 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Case Study 3: Page No: 5.136" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Program to count number of vowels,consonants, digits,spaces and other characters in a line of text.\n", "\n", "vowels='aeiou'\n", "consonants='bcdfghjklmnpqrstvwxyz'\n", "spaces =' '\n", "digits='1234567890'\n", "special_characters='!@#$%^&*()_'\n", "\n", "text=raw_input(\"Enter the text in lower case: \")\n", "\n", "print \"\\nNumber of vowels: \",dict((v, text.count(v)) for v in text if v in vowels)\n", "print \"Number of consonants: \",dict((c, text.count(c)) for c in text if c in consonants)\n", "print \"Number of Spaces: \",dict((s, text.count(s)) for s in text if s in spaces)\n", "print \"Number of digits: \",dict((d, text.count(d)) for d in text if d in digits)\n", "print \"Number of special_characters: \",dict((sc, text.count(sc)) for sc in text if sc in special_characters)\n" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter the text in lower case: 123 vrb publishers @#$%^&*()\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Number of vowels: {'i': 1, 'e': 1, 'u': 1}\n", "Number of consonants: {'b': 2, 'h': 1, 'l': 1, 'p': 1, 's': 2, 'r': 2, 'v': 1}\n", "Number of Spaces: {' ': 3}\n", "Number of digits: {'1': 1, '3': 1, '2': 1}\n", "Number of special_characters: {'@': 1, '#': 1, '%': 1, '$': 1, '&': 1, ')': 1, '(': 1, '*': 1, '^': 1}\n" ] } ], "prompt_number": 3 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Case Study 4: Page No.: 5.136" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Program to demonstrate malloc() function\n", "\n", "NULL=0\n", "string = ''\n", "if string==NULL:\n", " print \"Not Enough memory to allocate buffer\\n\"\n", "else:\n", " \n", " def strcpy(cstring1):\n", " import copy\n", " cstring2=copy.copy(cstring1)\n", " return cstring2 \n", "string=\"Hello\"\n", "print \"String is %s\" %string" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "String is Hello\n" ] } ], "prompt_number": 22 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Case Study 5: Page No.: 5.137" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Program to demonstrate calloc() function\n", "\n", "string=''\n", "\n", "def strcpy(cstring1):\n", " import copy\n", " cstring2=copy.copy(cstring1)\n", " return cstring2 \n", "stri=\"BHAVANA\"\n", "string=strcpy(stri)\n", "print \"String is %s\" %string" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "String is BHAVANA\n" ] } ], "prompt_number": 20 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Case Study 6: Page No.:5.138" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Program to demonstrate realloc() function\n", "#Cannot do in python" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 29 } ], "metadata": {} } ] }