{ "metadata": { "name": "" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Chapter 11: Structures and Unions" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 11.1, Page number: 388" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Structure declaration\n", "class student :\n", "\tid_no = ''\n", "\tname = ''\n", "\taddress = ''\n", "\tage = ''\n", "\t\n", "newstudent = student()\n", "\n", "print ('Enter the student information ')\n", "newstudent.id_no = int(raw_input('Enter student id_no : '))\n", "newstudent.name = raw_input('Enter name of the student : ')\n", "newstudent.address = raw_input('Enter the address of the student : ')\n", "newstudent.age = int(raw_input('Enter the age of the student : '))\n", "\n", "print ('You have entered following information...')\n", "print ('Student id_number = %d ' % newstudent.id_no)\n", "print ('Student name = %s ' % newstudent.name)\n", "print ('Student address = %s ' % newstudent.address)\n", "print ('Age of student = %d ' % newstudent.age)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter the student information \n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter student id_no : 1\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter name of the student : Amanjeet\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter the address of the student : Delhi\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter the age of the student : 20\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "You have entered following information...\n", "Student id_number = 1 \n", "Student name = Amanjeet \n", "Student address = Delhi \n", "Age of student = 20 \n" ] } ], "prompt_number": 1 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 11.2, Page number: 390" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Structure declaration\n", "class student :\n", "\tid_no = ''\n", "\tname = ''\n", "\taddress = ''\n", "\tage = ''\n", "\t\n", "newstudent = student()\n", "\n", "def putdata (newstudent) : \n", " print ('You have entered following information...')\n", " print ('Student id_number = %d ' % newstudent.id_no)\n", " print ('Student name = %s ' % newstudent.name)\n", " print ('Student address = %s ' % newstudent.address)\n", " print ('Age of student = %d ' % newstudent.age)\n", " \n", "print ('Enter the student information ') \n", "newstudent.id_no = int(raw_input('Enter student id_no : '))\n", "newstudent.name = raw_input('Enter name of the student : ')\n", "newstudent.address = raw_input('Enter the address of the student : ')\n", "newstudent.age = int(raw_input('Enter the age of the student : '))\n", "\n", "putdata (newstudent)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter the student information \n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter student id_no : 2\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter name of the student : Deepti\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter the address of the student : Noida\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter the age of the student : 23\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "You have entered following information...\n", "Student id_number = 2 \n", "Student name = Deepti \n", "Student address = Noida \n", "Age of student = 23 \n" ] } ], "prompt_number": 2 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 11.3, Page number: 392" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Structure declaration\n", "class student :\n", "\tid_no = ''\n", "\tname = ''\n", "\taddress = ''\n", "\tage = ''\n", "\t\n", "newstudent = student()\n", "\n", "def putdata (idno, name, add, age) :\n", " print ('You have entered following information...')\n", " print ('Student id_number = %d ' % idno)\n", " print ('Student name = %s ' % name)\n", " print ('Student address = %s ' % add)\n", " print ('Age of student = %d ' % age)\n", " \n", "print ('Enter the student information ')\n", "newstudent.id_no = int(raw_input('Enter student id_no : '))\n", "newstudent.name = raw_input('Enter name of the student : ')\n", "newstudent.address = raw_input('Enter the address of the student : ')\n", "newstudent.age = int(raw_input('Enter the age of the student : '))\n", "\n", "putdata (newstudent.id_no, newstudent.name, newstudent.address, newstudent.age)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter the student information \n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter student id_no : 3\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter name of the student : Kushagra\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter the address of the student : Bangalore\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter the age of the student : 21\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "You have entered following information...\n", "Student id_number = 3 \n", "Student name = Kushagra \n", "Student address = Bangalore \n", "Age of student = 21 \n" ] } ], "prompt_number": 3 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 11.4, Page number: 395" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Structure declaration\n", "class student :\n", "\tid_no = 0\n", "\tname = 'Unknown'\n", "\taddress = 'Unknown'\n", "\tage = 0\n", "\t\n", "newstudent = student()\n", "\n", "def readdata (newstudent) :\n", " print ('Enter the student information ')\n", " newstudent.id_no = int(raw_input('Enter student id_no : '))\n", " newstudent.name = raw_input('Enter name of the student : ')\n", " newstudent.address = raw_input('Enter the address of the student : ')\n", " newstudent.age = int(raw_input('Enter the age of the student : '))\n", "\n", "readdata (newstudent)\n", "print ('You have entered following information...')\n", "print ('Student id_number = %d ' % newstudent.id_no)\n", "print ('Student name = %s ' % newstudent.name)\n", "print ('Student address = %s ' % newstudent.address)\n", "print ('Age of student = %d ' % newstudent.age)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter the student information \n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter student id_no : 4\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter name of the student : Vipul\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter the address of the student : Chandigarh\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter the age of the student : 25\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "You have entered following information...\n", "Student id_number = 4 \n", "Student name = Vipul \n", "Student address = Chandigarh \n", "Age of student = 25 \n" ] } ], "prompt_number": 4 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 11.5, Page number: 397" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Structure declaration\n", "class student :\n", "\tid_no = 0\n", "\tname = 'Unknown'\n", "\taddress = 'Unknown'\n", "\tage = 0\n", "\t\n", "def readdata () :\n", " x = student()\n", " print ('Enter the student information ')\n", " x.id_no = int(raw_input('Enter student id_no : '))\n", " x.name = raw_input('Enter name of the student : ')\n", " x.address = raw_input('Enter the address of the student : ')\n", " x.age = int(raw_input('Enter the age of the student : '))\n", " return x\n", "\n", "newstudent = readdata()\n", "\n", "print ('You have entered following information...')\n", "print ('Student id_number = %d ' % newstudent.id_no)\n", "print ('Student name = %s ' % newstudent.name)\n", "print ('Student address = %s ' % newstudent.address)\n", "print ('Age of student = %d ' % newstudent.age)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter the student information \n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter student id_no : 5\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter name of the student : Ujjwal\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter the address of the student : Jaipur\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter the age of the student : 20\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "You have entered following information...\n", "Student id_number = 5 \n", "Student name = Ujjwal \n", "Student address = Jaipur \n", "Age of student = 20 \n" ] } ], "prompt_number": 6 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 11.6, Page number: 400" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Structure declaration\n", "class student :\n", "\tid_no = 0\n", "\tname = 'Unknown'\n", "\taddress = 'Unknown'\n", "\tage = 0\n", "\t\n", "x = student()\n", "\n", "def readdata (x) :\n", " print ('Enter the student information ')\n", " x.id_no = int(raw_input('Enter student id_no : '))\n", " x.name = raw_input('Enter name of the student : ')\n", " x.address = raw_input('Enter the address of the student : ')\n", " x.age = int(raw_input('Enter the age of the student : '))\n", "\n", "readdata(x) \n", "print ('You have entered following information...')\n", "print ('Student id_number = %d ' % x.id_no)\n", "print ('Student name = %s ' % x.name)\n", "print ('Student address = %s ' % x.address)\n", "print ('Age of student = %d ' % x.age)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter the student information \n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter student id_no : 6\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter name of the student : Karan\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter the address of the student : Chennai\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter the age of the student : 24\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "You have entered following information...\n", "Student id_number = 6 \n", "Student name = Karan \n", "Student address = Chennai \n", "Age of student = 24 \n" ] } ], "prompt_number": 8 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 11.7, Page number: 403" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Structure declaration\n", "class student (object) :\n", "\tdef __init__(self, roll=None, name=None, gender=None, marks=[0]*3) :\n", "\t\tself.roll = roll\n", "\t\tself.name = name\n", "\t\tself.gender = gender\n", "\t\tself.marks = marks\n", "\n", "def putdata (stud, count) :\n", "\tprint ('RollNo\\t Name\\t\\t Gender\\t Sub1\\t Sub2\\t Sub3 ')\n", "\tprint ('---------------------------------------------------------------------')\n", "\t\n", "\tfor i in range (0, count) :\n", "\t\tprint ('%-10d' % stud[i].roll),\n", "\t\tprint ('%-15s' % stud[i].name),\n", "\t\t\n", "\t\tif stud[i].gender == 0 :\n", "\t\t\tprint ('%-15s' % 'Female'),\n", "\t\telse :\n", "\t\t\tprint ('%-15s' % 'Male'),\n", "\t\t\n", "\t\tfor j in range (0, 3) :\n", "\t\t\tprint ('%-10d' % stud[i].marks[j]),\n", "\t\tprint\n", "\n", "def calculateper (stud, count) :\n", "\taverage = 0\n", "\tfor i in range (0, count) :\n", "\t\tsum = 0\n", "\t\tfor j in range (0, 3) :\n", "\t\t\tsum = sum + stud[i].marks[j]\n", "\t\taverage = average + sum/3\n", "\treturn average/count\n", "\n", "stud = []\t\n", "stud.append (student (1, 'Kapildev', 1, (45, 56, 67)))\n", "stud.append (student (2, 'Vidya', 0, (87, 45, 23)))\n", "stud.append (student (3, 'Sharada', 0, (67, 94, 56)))\n", "stud.append (student (4, 'Saismaran', 1, (56, 38, 84)))\n", "stud.append (student (5, 'Reshma', 0, (67, 98, 76)))\n", "\n", "average_percentage = calculateper (stud, 5)\n", "putdata (stud, 5)\n", "print ('\\nAverage percentage of the class is : %.2f ' % average_percentage)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "RollNo\t Name\t\t Gender\t Sub1\t Sub2\t Sub3 \n", "---------------------------------------------------------------------\n", "1 Kapildev Male 45 56 67 \n", "2 Vidya Female 87 45 23 \n", "3 Sharada Female 67 94 56 \n", "4 Saismaran Male 56 38 84 \n", "5 Reshma Female 67 98 76 \n", "\n", "Average percentage of the class is : 63.00 \n" ] } ], "prompt_number": 8 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 11.8, Page number: 406" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Structure declaration\n", "class student (object) :\n", "\tdef __init__(self, roll=None, name=None, gender=None, marks=[0]*3) :\n", "\t\tself.roll = roll\n", "\t\tself.name = name\n", "\t\tself.gender = gender\n", "\t\tself.marks = marks\n", "\t\n", "def putdata (stud, count) :\n", "\tprint ('\\nRollNo\\t Name\\t\\t Gender\\t Sub1\\t Sub2\\t Sub3 ')\n", "\tprint ('---------------------------------------------------------------------')\n", "\t\n", "\tfor i in range (0, count) :\n", "\t\tprint ('%-10d' % stud[i].roll),\n", "\t\tprint ('%-15s' % stud[i].name),\n", "\t\t\n", "\t\tif stud[i].gender == 0 :\n", "\t\t\tprint ('%-15s' % 'Female'),\n", "\t\telse :\n", "\t\t\tprint ('%-15s' % 'Male'),\n", "\t\t\n", "\t\tfor j in range (0, 3) :\n", "\t\t\tprint ('%-10d' % stud[i].marks[j]),\n", "\t\tprint\n", "\n", "def calculateper (stud, count) :\n", "\taverage = 0\n", "\tfor i in range (0, count) :\n", "\t\tsum = 0\n", "\t\tfor j in range (0, 3) :\n", "\t\t\tsum = sum + stud[i].marks[j]\n", "\t\taverage = average + sum/3\n", "\treturn average/count\n", "\n", "count = int(raw_input('Enter the number of students in a class : '))\n", "stud = [student() for i in range (0, count)]\n", "\n", "for i in range (0, count) :\n", "\tstud[i].roll = int(raw_input('\\nEnter roll number of student %d: ' % (i+1)))\n", "\tstud[i].name = raw_input('Enter name of student %d: ' % (i+1))\n", "\tstud[i].gender = int(raw_input('Enter gender[0/1] of student %d: ' % (i+1)))\n", "\tprint ('Enter marks of three subjects of student %d: ' % (i+1))\n", "\tfor j in range (0, 3) :\n", "\t\tstud[i].marks[j] = int(raw_input('Subject %d: ' % (j+1)))\n", "\n", "putdata (stud, count)\n", "average_percentage = calculateper (stud, count)\n", "print ('\\nAverage percentage of the class is : %.2f ' % average_percentage)" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter the number of students in a class : 1\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "Enter roll number of student 1: 24\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter name of student 1: Vidya\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter gender[0/1] of student 1: 0\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Enter marks of three subjects of student 1: \n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Subject 1: 78\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Subject 2: 96\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Subject 3: 75\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "RollNo\t Name\t\t Gender\t Sub1\t Sub2\t Sub3 \n", "---------------------------------------------------------------------\n", "24 Vidya Female 78 96 75 \n", "\n", "Average percentage of the class is : 83.00 \n" ] } ], "prompt_number": 11 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 11.9, Page number: 408" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Structure declaration\n", "class student (object) :\n", "\tdef __init__(self, roll=None, name=None, gender=None, marks=[0]*3) :\n", "\t\tself.roll = roll\n", "\t\tself.name = name\n", "\t\tself.gender = gender\n", "\t\tself.marks = marks\n", "\n", "def readdata (stud, count) :\n", "\tfor i in range (0, count) :\n", "\t\tstud[i].roll = int(raw_input('\\nEnter roll number of student %d: ' % (i+1)))\n", "\t\tstud[i].name = raw_input('Enter name of student %d: ' % (i+1))\n", "\t\tstud[i].gender = int(raw_input('Enter gender[0/1] of student %d: ' % (i+1)))\n", "\t\tprint ('Enter marks of three subjects of student %d: ' % (i+1))\n", "\t\tfor j in range (0, 3) :\n", "\t\t\tstud[i].marks[j] = int(raw_input('Subject %d: ' % (j+1)))\n", "\t\n", "def putdata (stud, count) :\n", "\tprint ('\\nRollNo\\t Name\\t\\t Gender\\t Sub1\\t Sub2\\t Sub3 ')\n", "\tprint ('---------------------------------------------------------------------')\n", "\t\n", "\tfor i in range (0, count) :\n", "\t\tprint ('%-10d' % stud[i].roll),\n", "\t\tprint ('%-15s' % stud[i].name),\n", "\t\t\n", "\t\tif stud[i].gender == 0 :\n", "\t\t\tprint ('%-15s' % 'Female'),\n", "\t\telse :\n", "\t\t\tprint ('%-15s' % 'Male'),\n", "\t\t\n", "\t\tfor j in range (0, 3) :\n", "\t\t\tprint ('%-10d' % stud[i].marks[j]),\n", "\t\tprint\n", "\n", "def calculateper (stud, count) :\n", "\taverage = 0\n", "\tfor i in range (0, count) :\n", "\t\tsum = 0\n", "\t\tfor j in range (0, 3) :\n", "\t\t\tsum = sum + stud[i].marks[j]\n", "\t\taverage = average + sum/3\n", "\treturn average/count\n", "\n", "count = int(raw_input('Enter the number of students in a class : '))\n", "stud = [student() for i in range (0, count)]\n", "readdata (stud, count)\n", "\n", "average_percentage = calculateper (stud, count)\n", "putdata (stud, count)\n", "print ('\\nAverage percentage of the class is : %.2f ' % average_percentage)" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter the number of students in a class : 1\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "Enter roll number of student 1: 25\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter name of student 1: Kapildev\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter gender[0/1] of student 1: 1\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Enter marks of three subjects of student 1: \n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Subject 1: 45\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Subject 2: 70\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Subject 3: 93\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "RollNo\t Name\t\t Gender\t Sub1\t Sub2\t Sub3 \n", "---------------------------------------------------------------------\n", "25 Kapildev Male 45 70 93 \n", "\n", "Average percentage of the class is : 69.00 \n" ] } ], "prompt_number": 13 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 11.10, Page number: 412" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Structure declaration\n", "class time (object) :\n", "\tdef __init__(self, hours=None, minutes=None, seconds=None) :\n", "\t\tself.hours = hours\n", "\t\tself.minutes = minutes\n", "\t\tself.seconds = seconds\n", "\n", "def AcceptTime (t) :\n", "\tprint ('Enter the time in hh mm ss format: ')\n", "\tt.hours = int(raw_input('Hours = '))\n", "\tt.minutes = int(raw_input('Minutes = '))\n", "\tt.seconds = int(raw_input('Seconds = '))\n", "\n", "def ValidateTime (t) :\n", "\tif t.hours < 0 or t.hours > 23 :\n", "\t\treturn 0\n", "\tif t.minutes < 0 or t.minutes > 60 :\n", "\t\treturn 0\n", "\tif t.seconds < 0 or t.seconds > 60 :\n", "\t\treturn 0\n", "\treturn 1\n", "\n", "def DisplayTime (t) :\n", "\tprint ('Time you entered is %d:%d:%d ' % (t.hours, t.minutes, t.seconds))\n", "\t\n", "condition = True\n", "while condition :\n", "\tt = time ()\n", "\tAcceptTime (t)\n", "\t\n", "\tif ValidateTime (t) == 1 :\n", "\t\tcondition = False\n", "\telse :\n", "\t\tprint ('\\nTime is incorrect. Enter again ')\n", "\t\t\n", "DisplayTime (t)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter the time in hh mm ss format: \n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Hours = 13\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Minutes = 45\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Seconds = 53\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Time you entered is 13:45:53 \n" ] } ], "prompt_number": 14 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 11.11, Page number: 414" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Structure declaration\n", "class date (object) :\n", "\tdef __init__(self, dd=None, mm=None, yy=None) :\n", "\t\tself.dd = dd\n", "\t\tself.mm = mm\n", "\t\tself.yy = yy\n", "\n", "def AcceptDate (d) :\n", "\tprint ('Enter date in dd mm yy format: ')\n", "\td.dd = int(raw_input('Date = '))\n", "\td.mm = int(raw_input('Month = '))\n", "\td.yy = int(raw_input('Year = '))\n", "\n", "def isleap (val) :\n", "\tif val % 4 == 0 and val % 100 or val % 400 == 0 :\n", "\t\treturn 1\n", "\treturn 0\n", "\t\n", "def ValidateDate (d) :\n", "\tif d.dd < 1 or d.dd > 31 :\n", "\t\treturn 0\n", "\tif d.mm < 1 or d.mm > 12 :\n", "\t\treturn 0\n", "\tif d.yy < 1 :\n", "\t\treturn 0\n", "\tif d.mm == 2 :\n", "\t\tif isleap (d.yy) :\n", "\t\t\tif d.dd > 29 :\n", "\t\t\t\treturn 0\n", "\t\telif d.dd > 28 :\n", "\t\t\treturn 0\n", "\tif d.mm == 4 or d.mm == 6 or d.mm == 9 or d.mm == 11 and d.dd > 30 :\n", "\t\treturn 0\n", "\treturn 1\n", "\n", "def DisplayMsg (d1, d2) :\n", "\tif d1.dd == d2.dd and d1.mm == d2.mm and d1.yy == d2.yy :\n", "\t\tprint ('Both dates are Equal ')\n", "\telse :\n", "\t\tprint ('Both dates are Not Equal ')\n", "\t\n", "condition = True\n", "while condition :\n", "\td1 = date ()\n", "\tAcceptDate (d1)\n", "\t\n", "\tif ValidateDate (d1) == 1 :\n", "\t\tcondition = False\n", "\telse :\n", "\t\tprint ('\\nDate is incorrect. Enter again ')\n", "\n", "condition = True\n", "while condition :\n", "\td2 = date ()\n", "\tAcceptDate (d2)\n", "\t\n", "\tif ValidateDate (d2) == 1 :\n", "\t\tcondition = False\n", "\telse :\n", "\t\tprint ('\\nDate is incorrect. Enter again ')\n", "\t\t\n", "DisplayMsg (d1, d2)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter date in dd mm yy format: \n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Date = 12\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Month = 11\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Year = 92\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Enter date in dd mm yy format: \n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Date = 12\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Month = 11\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Year = 92\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Both dates are Equal \n" ] } ], "prompt_number": 15 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 11.12, Page number: 415" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Structure declaration\n", "class date (object) :\n", "\tdef __init__(self, dd=None, mm=None, yy=None) :\n", "\t\tself.dd = dd\n", "\t\tself.mm = mm\n", "\t\tself.yy = yy\n", "\n", "def AcceptDate (d) :\n", "\tprint ('Enter date in dd mm yy format: ')\n", "\td.dd = int(raw_input('Date = '))\n", "\td.mm = int(raw_input('Month = '))\n", "\td.yy = int(raw_input('Year = '))\n", "\n", "def isleap (val) :\n", "\tif val % 4 == 0 and val % 100 or val % 400 == 0 :\n", "\t\treturn 1\n", "\treturn 0\n", "\t\n", "def ValidateDate (d) :\n", "\tif d.dd < 1 or d.dd > 31 :\n", "\t\treturn 0\n", "\tif d.mm < 1 or d.mm > 12 :\n", "\t\treturn 0\n", "\tif d.yy < 1 :\n", "\t\treturn 0\n", "\tif d.mm == 2 :\n", "\t\tif isleap (d.yy) :\n", "\t\t\tif d.dd > 29 :\n", "\t\t\t\treturn 0\n", "\t\telif d.dd > 28 :\n", "\t\t\treturn 0\n", "\tif d.mm == 4 or d.mm == 6 or d.mm == 9 or d.mm == 11 and d.dd > 30 :\n", "\t\treturn 0\n", "\treturn 1\n", "\n", "def CalcDate (d1, d2) :\n", "\tdate1 = date2 = 0\n", "\tfor i in range (-d1.yy, d1.yy) :\n", "\t\tdate1 = date1 + 366 if isleap (d1.yy) else 365\n", "\t\td1.yy = d1.yy - 1\n", "\t\t\n", "\tfor i in range (-d1.mm, d1.mm) :\n", "\t\tdate1 = date1 + (30 if (d1.mm==4 or d1.mm==6 or d1.mm==9 or d1.mm==11) else ((29 if isleap(d1.yy) else 28) if (d1.mm==2) else 31))\n", "\t\n", "\tdate1 = date1 + d1.dd\n", "\t\n", "\tfor i in range (-d2.yy, d2.yy) :\n", "\t\tdate2 = date2 + 366 if isleap(d2.yy) else 365\n", "\t\n", "\tfor i in range (-d2.mm, d2.mm) :\n", "\t\tdate2 = date2 + (30 if (d2.mm==4 or d2.mm==6 or d2.mm==9 or d2.mm==11) else ((29 if isleap(d2.yy) else 28) if d2.mm==2 else 31))\n", "\t\n", "\tdate2 = date2 + d2.dd\n", "\t\n", "\treturn (date2 - date1)/2\n", "\t\t\n", "def DisplayMsg (d1, d2, diff) :\n", "\tprint ('The difference between %d %d %d and %d %d %d is %d days ' % (d1.dd, d1.mm, -d1.yy, d2.dd, d2.mm, d2.yy, diff))\n", "\t\n", "condition = True\n", "while condition :\n", "\tprint ('\\nEnter first date: ')\n", "\td1 = date ()\n", "\tAcceptDate (d1)\n", "\t\n", "\tif ValidateDate (d1) == 1 :\n", "\t\tcondition = False\n", "\telse :\n", "\t\tprint ('\\nDate is incorrect. Enter again ')\n", "\n", "condition = True\n", "while condition :\n", "\tprint ('\\nEnter second date: ')\n", "\td2 = date ()\n", "\tAcceptDate (d2)\n", "\t\n", "\tif ValidateDate (d2) == 1 :\n", "\t\tcondition = False\n", "\telse :\n", "\t\tprint ('\\nDate is incorrect. Enter again ')\n", "\n", "diff = CalcDate (d1, d2)\n", "DisplayMsg (d1, d2, diff)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Enter first date: \n", "Enter date in dd mm yy format: \n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Date = 01\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Month = 01\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Year = 01\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Enter second date: \n", "Enter date in dd mm yy format: \n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Date = 25\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Month = 12\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Year = 08\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "The difference between 1 1 1 and 25 12 8 is 2915 days \n" ] } ], "prompt_number": 16 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 11.13, Page number: 419" ] }, { "cell_type": "code", "collapsed": false, "input": [ "from ctypes import *\n", "\n", "# Structure declaration\n", "class stream (Union) :\n", "\t_fields_ = [('grade', c_char), \n", "\t\t\t\t\t('class_std', c_int), \n", "\t\t\t\t\t('percentage', c_float)]\n", "\t\n", "class student (Structure) :\n", "\t_fields_ = [('roll', c_int), \n", "\t\t\t\t\t('name', c_char_p), \n", "\t\t\t\t\t('gender', c_int), \n", "\t\t\t\t\t('marks', c_int * 3), \n", "\t\t\t\t\t('st_stream', c_char), \n", "\t\t\t\t\t('st', stream)]\n", "\t\n", "def putdata (stud, count) :\n", "\tprint ('\\nStream\\t Roll\\t Name\\t Gender\\t Result\\t ')\n", "\tprint ('---------------------------------------------------------------------')\n", "\t\n", "\tfor i in range (0, count) :\n", "\t\tif stud[i].st_stream == 'A' :\n", "\t\t\tprint ('Arts\\t '),\n", "\t\telif stud[i].st_stream == 'S' :\n", "\t\t\tprint ('Science\\t '),\n", "\t\telse :\n", "\t\t\tprint ('Commerce '),\n", "\t\t\t\n", "\t\tprint ('%-5d ' % stud[i].roll),\n", "\t\tprint ('%-3s ' % stud[i].name),\n", "\t\t\n", "\t\tif stud[i].gender == 0 :\n", "\t\t\tprint ('\\tFemale'),\n", "\t\telse :\n", "\t\t\tprint ('\\tMale '),\n", "\t\t\n", "\t\tif stud[i].st_stream == 'A' :\n", "\t\t\tprint (' %d Class ' % stud[i].st.class_std),\n", "\t\telif stud[i].st_stream == 'S' :\n", "\t\t\tprint (' %c Grade ' % stud[i].st.grade),\n", "\t\telse :\n", "\t\t\tprint (' %.2f Percent ' % stud[i].st.percentage),\n", "\t\tprint\n", "\n", "def calculateper (stud, count) :\n", "\taverage = 0\n", "\tfor i in range (0, count) :\n", "\t\tsum = 0\n", "\t\tfor j in range (0, 3) :\n", "\t\t\tsum = sum + stud[i].marks[j]\n", "\t\taverage = (float)(sum/3)\n", "\t\t\n", "\t\tif stud[i].st_stream == 'A' :\n", "\t\t\tif average >= 60 :\n", "\t\t\t\tstud[i].st.class_std = 1\n", "\t\t\telse :\n", "\t\t\t\tstud[i].st.class_std = 2\n", "\t\t\t\t\n", "\t\telif stud[i].st_stream == 'S' :\n", "\t\t\tif average >= 60 :\n", "\t\t\t\tstud[i].st.grade = 'A'\n", "\t\t\telse :\n", "\t\t\t\tstud[i].st.class_std = 'B'\n", "\t\telse : \n", "\t\t\tstud[i].st.percentage = average\n", "\n", "count = int(raw_input('Enter the number of students in a class : '))\n", "stud = [student() for i in range (0, count)]\n", "\n", "for i in range (0, count) :\n", "\tstud[i].st_stream = raw_input('\\nEnter stream of student %d (A/C/S) : ' % (i+1))\n", "\tstud[i].roll = int(raw_input('Enter roll number of student %d : ' % (i+1)))\n", "\tstud[i].name = raw_input('Enter name of student %d : ' % (i+1))\n", "\tstud[i].gender = int(raw_input('Enter gender of student %d [0/1] : ' % (i+1)))\n", "\tprint ('Enter marks of three subjects of student %d : ' % (i+1))\n", "\tfor j in range (0, 3) :\n", "\t\tstud[i].marks[j] = int(raw_input('Subject %d : ' % (j+1)))\n", "\t\n", "calculateper (stud, count)\n", "putdata (stud, count)" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter the number of students in a class : 3\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "Enter stream of student 1 (A/C/S) : A\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter roll number of student 1 : 1\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter name of student 1 : Saismaran\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter gender of student 1 [0/1] : 1\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Enter marks of three subjects of student 1 : \n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Subject 1 : 65\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Subject 2 : 83\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Subject 3 : 59\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "Enter stream of student 2 (A/C/S) : C\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter roll number of student 2 : 2\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter name of student 2 : Vidya\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter gender of student 2 [0/1] : 0\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Enter marks of three subjects of student 2 : \n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Subject 1 : 84\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Subject 2 : 75\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Subject 3 : 93\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "Enter stream of student 3 (A/C/S) : S\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter roll number of student 3 : 3\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter name of student 3 : Pranav\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter gender of student 3 [0/1] : 1\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Enter marks of three subjects of student 3 : \n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Subject 1 : 84\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Subject 2 : 95\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Subject 3 : 83\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Stream\t Roll\t Name\t Gender\t Result\t \n", "---------------------------------------------------------------------\n", "Arts\t 1 Saismaran \tMale 1 Class \n", "Commerce 2 Vidya \tFemale 84.00 Percent \n", "Science\t 3 Pranav \tMale A Grade \n" ] } ], "prompt_number": 17 } ], "metadata": {} } ] }