{ "metadata": { "name": "", "signature": "sha256:13c473411b35212c9727620c7673dc52636d4bf1a83b0d4eeb082d160a75324d" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Chapter 12 : Union" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example: 1, Page No.5.87" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#program using union.\n", "\n", "class union_name:\n", " a=0\n", " b=[0,1]\n", " \n", "c=union_name()\n", "c.a=256\n", "\n", "print \"c.a value is %d\\n \"%c.a\n", "print \"c.b[0] value is %d\\n\"%c.b[0]\n", "print \"c.b[1] value is %d\\n\"%c.b[1]\n", "\n", "\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "c.a value is 256\n", " \n", "c.b[0] value is 0\n", "\n", "c.b[1] value is 1\n", "\n" ] } ], "prompt_number": 1 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example: 2, Page No.:5.87" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#program to find size of union and number of bytes\n", "\n", "import sys\n", "\n", "class union_result:\n", " marks=0\n", " grade=''\n", " \n", " \n", "class struct_res:\n", " name=[15]\n", " age=0\n", " sex=''\n", " address=''\n", " pincode=0\n", " perf=union_result()\n", " \n", "data=struct_res() \n", "\n", "#Result\n", "print \"Size of Union: %d\\n\"%sys.getsizeof(data.perf)\n", "print \"Size of Structure: %d\\n\"%sys.getsizeof(data)\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Size of Union: 32\n", "\n", "Size of Structure: 32\n", "\n" ] } ], "prompt_number": 5 }, { "cell_type": "code", "collapsed": false, "input": [ "#Pointer to Unions cannot convert as no pointer in python. Page no. 5.88" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example: 3, Page No.:5.89" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Program that includes of initial values to structure variable\n", "\n", "import sys\n", "\n", "\n", "class union_id:\n", " color='20'\n", " size=0\n", " \n", "\n", "class struct_clothes:\n", " manufacturer='30'\n", " cost=0.0\n", " description=union_id()\n", " \n", " \n", "shirt=struct_clothes()\n", "shirt.manufacturer='Indian'\n", "shirt.cost=35.00\n", "shirt.description.color='Black'\n", "\n", "print \"%d\\n\"%(sys.getsizeof(union_id))\n", "\n", "print \"%s %f \"%(shirt.manufacturer,shirt.cost),\"%s %d\\n \"%(shirt.description.color, shirt.description.size)\n", " \n", "\n", "shirt.description.size =12\n", "\n", "\n", "print \"%s %f \"%(shirt.manufacturer, shirt.cost),\"%s %d\\n \"%(shirt.description.color, shirt.description.size)\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "48\n", "\n", "Indian 35.000000 Black 0\n", " \n", "Indian 35.000000 Black 12\n", " \n" ] } ], "prompt_number": 6 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example: 4, Page no.: 5.92" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Program to read and write employee and their date of joining using nested structure. \n", "\n", "\n", "class date:\n", " day=0\n", " month=\"\"\n", " year=0\n", " \n", "class employee:\n", " code=0\n", " name=''\n", " salary=0.0\n", " doj=date()\n", " \n", " \n", "emp1=employee()\n", "\n", "emp1.code=input(\"Enter Employee Code: \")\n", "emp1.name=raw_input(\"Enter the Employee Name: \")\n", "emp1.salary=input(\"Enter the Employee Salary: \")\n", "\n", "print \"\\nEnter Date of joining in order\\n\"\n", "emp1.doj.day=input(\"Enter day: \")\n", "emp1.doj.month=raw_input(\"Enter month: \")\n", "emp1.doj.year=input(\"Enter year: \")\n", "\n", "print \"\\nThe Employee Code is: %d\"%emp1.code\n", "print \"\\nThe Employee Name is: %s\"%emp1.name\n", "print \"\\nThe Employee Name is: %f\"%emp1.salary\n", "print \"\\nThe Employee DOJ is: %d %s %d\"%(emp1.doj.day,emp1.doj.month,emp1.doj.year)\n", "\n" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter Employee Code: 200\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter the Employee Name: VIJI\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter the Employee Salary: 2000.00\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Enter Date of joining in order\n", "\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter day: 12\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter month: December\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter year: 2004\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "The Employee Code is: 200\n", "\n", "The Employee Name is: VIJI\n", "\n", "The Employee Name is: 2000.000000\n", "\n", "The Employee DOJ is: 12 December 2004\n" ] } ], "prompt_number": 7 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example: 5, Page No. 5.93" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Program to store 3 records in one structure\n", "\n", "\n", "class book:\n", " name=''\n", " price=0\n", " pages=0\n", " \n", " \n", "b=[book() for a in range(0,3)]\n", "\n", "for i in range(0,3):\n", " b[i].name=raw_input(\"Enter the Book Name: \")\n", " b[i].price=input(\"Enter the Book Price: \")\n", " b[i].pages=input(\"Enter the Book Pages: \")\n", " \n", "print '\\n' \n", "for i in range(0,3):\n", " print \"%s\\t\"%(b[i].name),\"%d\\t\"%(b[i].price),\"%d\\n\"%(b[i].pages)" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter the Book Name: English\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter the Book Price: 165\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter the Book Pages: 200\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter the Book Name: Maths\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter the Book Price: 300\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter the Book Pages: 450\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter the Book Name: Physics\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter the Book Price: 250\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter the Book Pages: 370\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "\n", "English\t165\t200\n", "\n", "Maths\t300\t450\n", "\n", "Physics\t250\t370\n", "\n" ] } ], "prompt_number": 8 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example: 6, Page No. 5.95" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Program to copy of entire structure to a function\n", "\n", "class std:\n", " no=0\n", " avg=0.0\n", "\n", "a=std()\n", "\n", "a.no=15\n", "a.avg=90.75\n", "\n", "def func(Struct_p):\n", " print \"Number is.... %d\\n\"%a.no\n", " print \"Average is... %f\"%a.avg\n", " \n", "func(a)\n", "\n", " " ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Number is.... 15\n", "\n", "Average is... 90.750000\n" ] } ], "prompt_number": 9 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example: 7, Page No.:5.97" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "#Programs shows how the structure can be accessed by a pointer variable.\n", "\n", "class student:\n", " roll_no=0\n", " name=''\n", " marks=0.0\n", "stud1=student()\n", "\n", "print \"Enter the student details: \\n\"\n", "stud1.roll_no=input()\n", "stud1.name=raw_input()\n", "stud1.marks=input()\n", "\n", "print \"\\nDisplay of structure using structure variable\\n\"\n", "print \"Roll No\\t Name\\t Marks\"\n", "print \"\\n-------\\t ----- -------\\n\"\n", "print \"%d\\t %s\\t %f\"%(stud1.roll_no,stud1.name,stud1.marks)\n", "\n", "pt=stud1\n", "\n", "print \"\\nDisplay of structure using pointer variable\\n\"\n", "print \"Roll No\\t Name\\t Marks\"\n", "print \"\\n-------\\t ----- -------\\n\"\n", "print \"%d\\t %s\\t %f\"%(pt.roll_no,pt.name,pt.marks)\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter the student details: \n", "\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "39\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Muni\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "77\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Display of structure using structure variable\n", "\n", "Roll No\t Name\t Marks\n", "\n", "-------\t ----- -------\n", "\n", "39\t Muni\t 77.000000\n", "\n", "Display of structure using pointer variable\n", "\n", "Roll No\t Name\t Marks\n", "\n", "-------\t ----- -------\n", "\n", "39\t Muni\t 77.000000\n" ] } ], "prompt_number": 6 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Case Study: 1, Page No.:5.98" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Program to transfer a structure to a function by passing the structure address to the function\n", "\n", "class struct:\n", " name=''\n", " no=0\n", " age=0\n", "record=struct()\n", "std=record\n", "std.name,std.no,std.age=[\"LAK\",15,25]\n", "print \"%s\\t%d\\t%d\"%(std.name,std.no,std.age)\n", "\n", "def fun(std):\n", " pt.name=\"Muni\"\n", " pt.no=16\n", " pt.age=26\n", " \n", "std.name=pt.name\n", "std.no=pt.no\n", "std.age=pt.age\n", "\n", "print \"%s\\t%d\\t%d\"%(std.name,std.no,std.age)\n", " \n", " " ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "LAK\t15\t25\n", "Muni\t16\t26\n" ] } ], "prompt_number": 24 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Case Study: 2, Page No.:5.100" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Program to pass an array of structure to a function, and return a pointer to a structure.\n", "import sys\n", "N=3\n", "NULL=0\n", "\n", "class struct:\n", " sno=0\n", " name=''\n", " age=0\n", " \n", "record=struct()\n", "std=record\n", "\n", "print \"Student Number Locater\"\n", "print \"To Exit the program enter '0' for student number\"\n", "stdno=input(\"Enter the Student Number: \")\n", "print \"\\n\"\n", "if stdno ==15:\n", "\n", " std.sno,std.name,std.age=[15,\"MUNI\",28]\n", " print \"Number .... %d \\n\"%(std.sno)\n", " print \"Name ...... %s\\n\"%(std.name)\n", " print \"Age ....... %d\\n\"%(std.age)\n", "if stdno == 16:\n", " std.sno,std.name,std.age=[16,\"LAK\",27]\n", " print \"Number .... %d \\n\"%(std.sno)\n", " print \"Name ...... %s\\n\"%(std.name)\n", " print \"Age ....... %d\\n\"%(std.age)\n", " \n", "if stdno == 17:\n", " std.sno,std.name,std.age=[17,\"RAJA\",31]\n", " print \"Number .... %d \\n\"%(std.sno)\n", " print \"Name ...... %s\\n\"%(std.name)\n", " print \"Age ....... %d\\n\"%(std.age)\n", " \n", "if stdno==0:\n", " exit\n", "\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Student Number Locater\n", "To Exit the program enter '0' for student number\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter the Student Number: 15\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "\n", "Number .... 15 \n", "\n", "Name ...... MUNI\n", "\n", "Age ....... 28\n", "\n" ] } ], "prompt_number": 5 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Case Study: 3, Page No.: 5.101" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Program to print student name and mark using structure\n", "\n", "class student:\n", " name=''\n", " marks=0.00\n", " \n", "s1=''\n", "f=0.00\n", "\n", "student1=student()\n", "\n", "student2=student()\n", "\n", "\n", "student1.name=raw_input(\"Enter Name: \")\n", "f=input(\"Enter Mark: \")\n", "\n", "student2.marks=f\n", "\n", "print \"\\nName is %s \\n\"%student1.name\n", "print \"Marks are %f \\n\"%student2.marks\n" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter Name: Venkat\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter Mark: 89\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Name is Venkat \n", "\n", "Marks are 89.000000 \n", "\n" ] } ], "prompt_number": 10 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Case Study 4, Page No.: 5.102" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Program to print marks, grade and their address using union\n", "\n", "\n", "\n", "class union_marks:\n", " perc=0.00\n", " grade=''\n", " \n", "student1=union_marks()\n", "\n", "student1.perc=98.5\n", "\n", "print \"Marks are %f \\t address is %8lu\\n\"%(student1.perc, id(student1.perc))\n", "\n", "student1.grade='A'\n", "\n", "print \"Grade is %c \\t address is %8lu\\n\"%(student1.grade,id(student1.grade))" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Marks are 98.500000 \t address is 158368916\n", "\n", "Grade is A \t address is 3073485064\n", "\n" ] } ], "prompt_number": 11 } ], "metadata": {} } ] }