{ "metadata": { "name": "" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "

Chapter 10: Structures and Unions

" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 1, Page number: SAU - 5

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#student structure\n", "\n", "import sys\n", "\n", "class student:\n", " rno = 0\n", " sname = \"\"\n", " tot = 0 \n", " \n", "x = student()\n", "\n", "x.rno = int(raw_input(\"\\nEnter roll number, name and total marks\"))\n", "x.sname = raw_input(\"\")\n", "x.tot = int(raw_input(\"\"))\n", "\n", "sys.stdout.write(\"\\n\\tDetails entered are \\n\")\n", "sys.stdout.write(\"\\nRoll No. : %d\"%(x.rno))\n", "sys.stdout.write(\"\\nStudent Name : %s\"%(x.sname))\n", "sys.stdout.write(\"\\nTotal marks : %d\"%(x.tot))\n", "sys.stdout.write(\"\\n\\nPress any key to continue...\")" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "Enter roll number, name and total marks20147\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "PRADEEP\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "64\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "\tDetails entered are \n", "\n", "Roll No. : 20147\n", "Student Name : PRADEEP\n", "Total marks : 64\n", "\n", "Press any key to continue..." ] } ], "prompt_number": 3 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 2, Page number: SAU - 6

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "import sys\n", "\n", "class student:\n", " rno = 0 \n", " sname = \"\"\n", " tot = 0\n", " \n", "ch = 'y'\n", "std = [student() for i in range(0,50)]\n", "\n", "n = int(raw_input(\"\\nHow many students ? \"))\n", "\n", "for i in range(0,n):\n", " std[i].rno = int(raw_input(\"\\nRoll Number ? \"))\n", " std[i].sname = raw_input(\"\\nName ? \")\n", " std[i].tot = int(raw_input(\"\\nTotal marks ? \"))\n", " \n", "while ch == 'y' or ch == 'Y':\n", " temp = int(raw_input(\"\\nEnter student roll number to display marks : \"))\n", " flag = 0\n", " for i in range(0,n):\n", " if flag == 0:\n", " if std[i].rno == temp:\n", " sys.stdout.write(\"\\nMarks obtained by %d %s\"%(std[i].rno,std[i].sname))\n", " sys.stdout.write(\"\\nTotal : %d\"%(std[i].tot))\n", " flag = 1\n", " \n", " if flag == 0 :\n", " sys.stdout.write(\"\\n%d is not present in the list\"%(temp))\n", " ch = raw_input(\"\\npress y - to continue \\n any other key to stop.\")" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "How many students ? 3\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "Roll Number ? 20201\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "Name ? ARUN\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "Total marks ? 78\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "Roll Number ? 20208\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "Name ? DEEPAK\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "Total marks ? 69\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "Roll Number ? 20223\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "Name ? SUSMITHA\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "Total marks ? 88\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "Enter student roll number to display marks : 20208\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Marks obtained by 20208 DEEPAK\n", "Total : 69" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "press y - to continue \n", " any other key to stop.N\n" ] } ], "prompt_number": 5 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 3, Page number: SAU - 8

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "import sys\n", "\n", "class student:\n", " rno = 0 \n", " sname = \"\"\n", " tot = 0\n", " \n", "std = [student() for i in range(0,50)]\n", "\n", "n = int(raw_input(\"\\nHow many students ? \"))\n", "\n", "for i in range(0,n):\n", " std[i].rno = int(raw_input(\"\\nRoll Number ? \"))\n", " std[i].sname = raw_input(\"\\nName ? \")\n", " std[i].tot = int(raw_input(\"\\nTotal marks ? \"))\n", " \n", "print \"\\n-------------------------------------------------------\"\n", "print \"\\n Roll No. Name Total Marks\"\n", "print \"\\n-------------------------------------------------------\"\n", "\n", "for i in range(0,n):\n", " if std[i].tot >= 75:\n", " sys.stdout.write(\"\\n %-8d %-20s %5d\"%(std[i].rno,std[i].sname,std[i].tot))\n", " \n", "print \"\\n-------------------------------------------------------\"" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "How many students ? 4\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "Roll Number ? 30401\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "Name ? ANAND\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "Total marks ? 59\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "Roll Number ? 30404\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "Name ? NIRMAL\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "Total marks ? 64\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "Roll Number ? 30428\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "Name ? ISWARYA\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "Total marks ? 82\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "Roll Number ? 30432\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "Name ? VIVEKA\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "Total marks ? 79\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "-------------------------------------------------------\n", "\n", " Roll No. Name Total Marks\n", "\n", "-------------------------------------------------------\n", "\n", " 30428 ISWARYA 82\n", " 30432 VIVEKA 79\n", "-------------------------------------------------------\n" ] } ], "prompt_number": 7 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 4, Page number: SAU - 10

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "import sys\n", "\n", "class employee:\n", " eno = 0\n", " ename = \"\"\n", " epay = 0\n", " jdate = \"\"\n", " \n", "def revise(temp):\n", " if temp <= 2000:\n", " return (temp + (temp*0.15))\n", " else:\n", " if temp <= 5000:\n", " return (temp + (temp * 0.10))\n", " else:\n", " return temp\n", " \n", " \n", "employs = [employee() for i in range(0,25)]\n", "i = 0\n", "ch = 'y'\n", "\n", "while ch.upper() == 'Y':\n", " employs[i].eno = int(raw_input(\"\\nEmployee No.? \"))\n", " employs[i].ename = raw_input(\"\\nName ? \")\n", " employs[i].epay = int(raw_input(\"\\nExisting pay? \"))\n", " employs[i].jdate = raw_input(\"\\nJoining Date ? \")\n", " i += 1\n", " ch = raw_input(\"\\nPress y - to continue any other key to stop.\")\n", " \n", "n = i\n", "sys.stdout.write(\"\\n\\n%d records are entered\"%(n))\n", "sys.stdout.write(\"\\nPress any key to print the revised salary list\")\n", "\n", "for i in range(0,n):\n", " employs[i].epay = revise(employs[i].epay)\n", " \n", "print \"\\n Employees Revised Pay List\"\n", "print \"\\n----------------------------------------------------------------------------------\"\n", "print \"\\n S.No Number Name Joinint date Pay\"\n", "print \"\\n----------------------------------------------------------------------------------\"\n", "\n", "for i in range(0,n):\n", " sys.stdout.write(\"\\n %-4d %-d %-15s %10s %-8d\"%(i+1,employs[i].eno,employs[i].ename,employs[i].jdate,employs[i].epay))\n", " \n", "print \"\\n----------------------------------------------------------------------------------\"" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "Employee No.? 20101\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "Name ? ASHIKA\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "Existing pay? 1000\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "Joining Date ? 31/04/2001\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "Press y - to continue any other key to stop.Y\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "Employee No.? 20182\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "Name ? ASHWIN\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "Existing pay? 6000\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "Joining Date ? 11/12/1995\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "Press y - to continue any other key to stop.Y\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "Employee No.? 20204\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "Name ? PRAVEEN\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "Existing pay? 3000\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "Joining Date ? 18/06/1994\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "Press y - to continue any other key to stop.N\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "\n", "3 records are entered\n", "Press any key to print the revised salary list\n", " Employees Revised Pay List\n", "\n", "----------------------------------------------------------------------------------\n", "\n", " S.No Number Name Joinint date Pay\n", "\n", "----------------------------------------------------------------------------------\n", "\n", " 1 20101 ASHIKA 31/04/2001 1150 \n", " 2 20182 ASHWIN 11/12/1995 6000 \n", " 3 20204 PRAVEEN 18/06/1994 3300 \n", "----------------------------------------------------------------------------------\n" ] } ], "prompt_number": 17 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 5, Page number: SAU - 13

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "import sys\n", "\n", "class cricket:\n", " pname = \"\"\n", " tname = \"\"\n", " bavg = 0\n", " \n", "probable = [cricket() for i in range(0,30)]\n", "\n", "n = int(raw_input(\"\\nHow many players ? \"))\n", "\n", "for i in range(0,n):\n", " probable[i].pname = raw_input(\"\\nPlayer name ? \")\n", " probable[i].tname = raw_input(\"\\nWhich team ? \")\n", " probable[i].bavg = int(raw_input(\"\\nBatting average ? \"))\n", " \n", "j = 0\n", "teams = ['' for i in range(0,20)]\n", "\n", "teams[j] = probable[0].tname\n", "j += 1\n", "i = 1\n", "\n", "for i in range(1,n):\n", " flag = 0\n", " k = 0\n", " while k < j and flag == 0: \n", " if probable[i].tname == teams[k]:\n", " flag = 1\n", " k += 1\n", " if flag == 0:\n", " teams[j] = probable[i].tname\n", " j += 1\n", " i += 1\n", "\n", "for k in range(0,j):\n", " sys.stdout.write(\"\\n\\t%s\"%(teams[k]))\n", " print \"\\n-------------------------------------------------------\"\n", " for i in range(0,n):\n", " if probable[i].tname == teams[k]:\n", " sys.stdout.write(\"\\n %-15s %6d\"%(probable[i].pname,probable[i].bavg))\n", " print \"\\n-------------------------------------------------------\"" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "How many players ? 6\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "Player name ? KUMBLE\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "Which team ? KARNATAKA\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "Batting average ? 22\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "Player name ? KAMBLI\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "Which team ? MUMBAI\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "Batting average ? 39\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "Player name ? SRIKANTH\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "Which team ? TAMILNADU\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "Batting average ? 52\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "Player name ? SACHIN\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "Which team ? MUMBAI\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "Batting average ? 69\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "Player name ? RAHUL\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "Which team ? KARNATAKA\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "Batting average ? 57\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "Player name ? RAMESH\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "Which team ? TAMILNADU\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "Batting average ? 48\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "\tKARNATAKA\n", "-------------------------------------------------------\n", "\n", " KUMBLE 22\n", " RAHUL 57\n", "-------------------------------------------------------\n", "\n", "\tMUMBAI\n", "-------------------------------------------------------\n", "\n", " KAMBLI 39\n", " SACHIN 69\n", "-------------------------------------------------------\n", "\n", "\tTAMILNADU\n", "-------------------------------------------------------\n", "\n", " SRIKANTH 52\n", " RAMESH 48\n", "-------------------------------------------------------\n" ] } ], "prompt_number": 3 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 6, Page number: SAU - 19

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "import sys\n", "\n", "class student:\n", " roll_no = 0\n", " sname = \"\"\n", " marks = 0 \n", " \n", "std = [student() for i in range(0,10)]\n", "\n", "print \"\\n-----------------------------------------------------------------------\"\n", "print \"\\n Main Menu\"\n", "print \"\\n-----------------------------------------------------------------------\"\n", "print \"\\nPress 1 to enter roll numbers\"\n", "print \"\\n 2 to enter names\"\n", "print \"\\n 3 to enter marks\"\n", "print \"\\n 4 to stop\"\n", "ch = int(raw_input(\"\\nEnter your choice : \"))\n", "if ch < 4:\n", " n = int(raw_input(\"How many students ? \"))\n", "\n", " for i in range(0,n):\n", " if ch == 1:\n", " std[i].roll_no = int(raw_input(\"\\nRoll number ? \"))\n", " else:\n", " if ch == 2:\n", " std[i].sname = raw_input(\"\\nStudent name ? \")\n", " else:\n", " if ch == 3:\n", " std[i].marks = int(raw_input(\"\\nAverage marks ? \"))\n", " \n", " if ch == 1:\n", " print \"\\n-------------------------------------------------------------------\"\n", " print \"\\n Students roll number list\"\n", " print \"\\n-------------------------------------------------------------------\"\n", " for i in range(0,n):\n", " sys.stdout.write(\"\\n\\t%d\"%(std[i].roll_no))\n", " print \"\\n-------------------------------------------------------------------\"\n", " else:\n", " if ch == 2:\n", " print \"\\n-------------------------------------------------------------------\"\n", " print \"\\n Students name list\"\n", " print \"\\n-------------------------------------------------------------------\"\n", " for i in range(0,n):\n", " sys.stdout.write(\"\\n\\t%s\"%(std[i].sname))\n", " print \"\\n-------------------------------------------------------------------\"\n", " else:\n", " if ch == 3:\n", " print \"\\n-------------------------------------------------------------------\"\n", " print \"\\n Students mark list\"\n", " print \"\\n-------------------------------------------------------------------\"\n", " for i in range(0,n):\n", " sys.stdout.write(\"\\n\\t%d\"%(std[i].marks))\n", " print \"\\n-------------------------------------------------------------------\"" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "\n", "-----------------------------------------------------------------------\n", "\n", " Main Menu\n", "\n", "-----------------------------------------------------------------------\n", "\n", "Press 1 to enter roll numbers\n", "\n", " 2 to enter names\n", "\n", " 3 to enter marks\n", "\n", " 4 to stop\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "Enter your choice : 2\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "How many students ? 4\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "Student name ? AJITH\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "Student name ? RAJU\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "Student name ? VIGNESH\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "Student name ? DIVYA\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "-------------------------------------------------------------------\n", "\n", " Students name list\n", "\n", "-------------------------------------------------------------------\n", "\n", "\tAJITH\n", "\tRAJU\n", "\tVIGNESH\n", "\tDIVYA\n", "-------------------------------------------------------------------\n" ] } ], "prompt_number": 3 }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }