diff options
Diffstat (limited to 'ANSI_C_Programming_by_Yashwant_Kanetkar/chapter15.ipynb')
-rwxr-xr-x | ANSI_C_Programming_by_Yashwant_Kanetkar/chapter15.ipynb | 692 |
1 files changed, 692 insertions, 0 deletions
diff --git a/ANSI_C_Programming_by_Yashwant_Kanetkar/chapter15.ipynb b/ANSI_C_Programming_by_Yashwant_Kanetkar/chapter15.ipynb new file mode 100755 index 00000000..6421f521 --- /dev/null +++ b/ANSI_C_Programming_by_Yashwant_Kanetkar/chapter15.ipynb @@ -0,0 +1,692 @@ +{ + "metadata": { + "name": "", + "signature": "sha256:831402bdcf6a50366e6a400bf971e09b25983de0ebf01458d42a693aa22990c1" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "CHAPTER 15: MISCELLANEOUS FEATURES" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:473-474" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "def emp_dept(*sequential, **named): #definition of enumeration\n", + " enums = dict(zip(sequential, range(len(sequential))), **named)\n", + " return type('Enum', (), enums)\n", + "class employee(): #structure\n", + " def __init__(self,**kwds):\n", + " self.__dict__.update(kwds)\n", + "department=emp_dept('assembly','manufacturing','accounts','stores') #defining a variable of type enum emp_dept\n", + "e=employee(name=\"Lothar Mattheus\",age=28,bs=5575.50,department=department.manufacturing)\n", + "print \"Name=%s\\n\" % (e.name)\n", + "print \"Age=%d\\n\" % (e.age)\n", + "print \"Basic salary=%f\\n\" % (e.bs)\n", + "print \"Dept=%d\\n\" % (e.department)\n", + "if e.department==department.accounts:\n", + " print \"%s is an accountant\\n\" % (e.name)\n", + "else:\n", + " print \"%s is not an accountant\\n\" % (e.name)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Name=Lothar Mattheus\n", + "\n", + "Age=28\n", + "\n", + "Basic salary=5575.500000\n", + "\n", + "Dept=1\n", + "\n", + "Lothar Mattheus is not an accountant\n", + "\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:475" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "def ASSEMBLY():\n", + " return 0\n", + "def MANUFACTURING():\n", + " return 1\n", + "def ACCOUNTS():\n", + " return 2\n", + "def STORES():\n", + " return 3\n", + "class employee():\n", + " def __init__(self,**kwds):\n", + " self.__dict__.update(kwds)\n", + "e=employee()\n", + "e.name=\"Lothar Mattheus\"\n", + "e.age=28\n", + "e.bs=5575.50\n", + "e.department=MANUFACTURING()\n", + "print \"Name=%s\\n\" % (e.name)\n", + "print \"Age=%d\\n\" % (e.age)\n", + "print \"Basic salary=%f\\n\" % (e.bs)\n", + "print \"Dept=%d\\n\" % (e.department)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Name=Lothar Mattheus\n", + "\n", + "Age=28\n", + "\n", + "Basic salary=5575.500000\n", + "\n", + "Dept=1\n", + "\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:477-478" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "x=6\n", + "y=4\n", + "a=x/y #int\n", + "print \"Value of a=%f\\n\" % (a)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Value of a=1.000000\n", + "\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:478" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "x=6\n", + "y=4\n", + "a=float(x)/y #float\n", + "print \"Value of a=%f\\n\" % (a)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Value of a=1.500000\n", + "\n" + ] + } + ], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:478-479" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "a=6.35\n", + "print \"Value of a on type casting=%d\\n\" % (int(a)) #converts float to int\n", + "print \"Value of a=%f\\n\" % (a) " + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Value of a on type casting=6\n", + "\n", + "Value of a=6.350000\n", + "\n" + ] + } + ], + "prompt_number": 5 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:480" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "from ctypes import *\n", + "def MALE():\n", + " return 0\n", + "def FEMALE():\n", + " return 1\n", + "def SINGLE():\n", + " return 0\n", + "def MARRIED():\n", + " return 1\n", + "def DIVORCED():\n", + " return 2\n", + "def WIDOWED():\n", + " return 3\n", + "class employee(Structure):\n", + " _fields_= [(\"gender\",c_short, 1), #1 bit size for storage\n", + " (\"mar_stat\", c_short, 2), #2 bit size for storage\n", + " (\"hobby\",c_short, 3), #3 bit size for storage\n", + " (\"scheme\",c_short, 4)] #4 bit size for storage\n", + "e=employee()\n", + "e.gender=MALE()\n", + "e.mar_status=DIVORCED()\n", + "e.hobby=5\n", + "e.scheme=9\n", + "print \"Gender=%d\\n\" % (e.gender)\n", + "print \"Marital status=%d\\n\" % (e.mar_status)\n", + "import ctypes\n", + "print \"Bytes occupied by e=%d\\n\" % (ctypes.sizeof(e))" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Gender=0\n", + "\n", + "Marital status=2\n", + "\n", + "Bytes occupied by e=2\n", + "\n" + ] + } + ], + "prompt_number": 6 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:481" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "def display():\n", + " print \"Long live viruses!!\\n\"\n", + "print \"Address of function display is %s\\n\" % (display)\n", + "display()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Address of function display is <function display at 0x02D93DF0>\n", + "\n", + "Long live viruses!!\n", + "\n" + ] + } + ], + "prompt_number": 7 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:481-482" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "def display():\n", + " print \"\\nLong live viruses!!\"\n", + "func_ptr=display\n", + "print \"Address of function display is %s\\n\" % (display)\n", + "func_ptr()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Address of function display is <function display at 0x05696730>\n", + "\n", + "\n", + "Long live viruses!!\n" + ] + } + ], + "prompt_number": 8 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:483" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "def fun():\n", + " i=20\n", + " return (id(i))\n", + "p=fun\n", + "p()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "metadata": {}, + "output_type": "pyout", + "prompt_number": 9, + "text": [ + "20688356" + ] + } + ], + "prompt_number": 9 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:483-484" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "def copy(t,s):\n", + " t=\"\\0\"\n", + " for item in s:\n", + " t=t+item\n", + " return t\n", + "source=\"Jaded\"\n", + "target=[]\n", + "str1=copy(target,source)\n", + "print \"%s\\n\" % (str1)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\u0000Jaded\n", + "\n" + ] + } + ], + "prompt_number": 18 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:485" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "def findmax(tot_num,*num): #tuples\n", + " max=0\n", + " for count in range(0,len(num),1):\n", + " if num[count]>max:\n", + " max=num[count]\n", + " return max\n", + "max=findmax(5,23,15,1,92,50)\n", + "print \"maximum=%d\\n\" % (max)\n", + "max=findmax(3,100,300,29)\n", + "print \"maximum=%d\\n\" % (max)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "maximum=92\n", + "\n", + "maximum=300\n", + "\n" + ] + } + ], + "prompt_number": 19 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:486-487" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "def display(type,num,*t):\n", + " def integer():\n", + " for j in range(0,num,1):\n", + " print \"%d\" % (t[j])\n", + " def char():\n", + " for j in range(0,num,1):\n", + " print \"%c\" % (t[j])\n", + " def floating():\n", + " for j in range(0,num,1):\n", + " print \"%f\" % (t[j])\n", + " def switch(ch):\n", + " return {1: integer,\n", + " 2: char,\n", + " 3: floating,\n", + " }[ch]()\n", + " switch(type)\n", + "display(1,2,5,6)\n", + "display(2,4,'A','a','b','c')\n", + "display(3,3,2.5,299.3,-1.0)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "5\n", + "6\n", + "A\n", + "a\n", + "b\n", + "c\n", + "2.500000\n", + "299.300000\n", + "-1.000000\n" + ] + } + ], + "prompt_number": 21 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:488" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "from ctypes import *\n", + "class a(Union): #definition of union\n", + " _fields_= [(\"i\", c_short),\n", + " (\"ch\",c_byte*2)]\n", + "key=a()\n", + "key.i=512\n", + "print key.i\n", + "print key.ch[0]\n", + "print key.ch[1]" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "512\n", + "0\n", + "2\n" + ] + } + ], + "prompt_number": 22 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:491" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "from ctypes import *\n", + "class a(Union):\n", + " _fields_= [(\"i\", c_short),\n", + " (\"ch\",c_byte*2)]\n", + "key=a()\n", + "key.i=512\n", + "print key.i\n", + "print key.ch[0]\n", + "print key.ch[1]\n", + "key.ch[0]=50\n", + "print key.i\n", + "print key.ch[0]\n", + "print key.ch[1]" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "512\n", + "0\n", + "2\n", + "562\n", + "50\n", + "2\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:492-493" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "from ctypes import *\n", + "class a(Structure):\n", + " _fields_ = [(\"i\", c_short),\n", + " (\"c\", c_byte*2)]\n", + "\n", + "class b(Structure):\n", + " _fields_ = [(\"j\", c_short),\n", + " (\"d\", c_byte*2)]\n", + "\n", + "class z(Union):\n", + " _fields_ = [(\"key\", a),\n", + " (\"data\", b)]\n", + "strange = z()\n", + "strange.key.i=512\n", + "strange.data.d[0]=0\n", + "strange.data.d[1]=32\n", + "print strange.key.i\n", + "print strange.data.j\n", + "print strange.key.c[0]\n", + "print strange.data.d[0]\n", + "print strange.key.c[1]\n", + "print strange.data.d[1]" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "512\n", + "512\n", + "0\n", + "0\n", + "32\n", + "32\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:494-495" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "from ctypes import *\n", + "class info1(Structure):\n", + " _fields_ = [(\"hobby\", c_byte*10),\n", + " (\"crcardno\", c_short)]\n", + "\n", + "class info2(Structure):\n", + " _fields_ = [(\"vehno\", c_byte*10),\n", + " (\"dist\", c_short)]\n", + "\n", + "class info(Union):\n", + " _fields_ = [(\"a\", info1),\n", + " (\"b\", info2)]\n", + "class emp(Structure):\n", + " _fields_ = [(\"n\", c_byte*20),\n", + " (\"grade\", c_byte*4),\n", + " (\"age\", c_short),\n", + " (\"f\", info)]\n", + "e=emp()" + ], + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 3 + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file |