summaryrefslogtreecommitdiff
path: root/Mastering_C/chapter9.ipynb
diff options
context:
space:
mode:
Diffstat (limited to 'Mastering_C/chapter9.ipynb')
-rw-r--r--Mastering_C/chapter9.ipynb1463
1 files changed, 1463 insertions, 0 deletions
diff --git a/Mastering_C/chapter9.ipynb b/Mastering_C/chapter9.ipynb
new file mode 100644
index 00000000..9eacd97f
--- /dev/null
+++ b/Mastering_C/chapter9.ipynb
@@ -0,0 +1,1463 @@
+{
+ "metadata": {
+ "name": "",
+ "signature": "sha256:2b328a00a423d96b629425895ccbcc90fde898bdf09e6f58382db5eb59593f31"
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "heading",
+ "level": 1,
+ "metadata": {},
+ "source": [
+ "Chapter 9: Structures & Unions"
+ ]
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 9.1, page no. 338"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "class date:\n",
+ " dat = 0\n",
+ " month = 0\n",
+ " year = 0\n",
+ "class person:\n",
+ " name = ''\n",
+ " lastnam = ''\n",
+ " birthday = date()\n",
+ " salary = 0.0\n",
+ "emprec = person()\n",
+ "birth = emprec.birthday\n",
+ "emprec.name = \"Tejaswi.V.\"\n",
+ "emprec.lastnam = \"Rajuk\"\n",
+ "emprec.birthday.day = 24\n",
+ "emprec.birthday.month = 7\n",
+ "emprec.birthday.year = 90\n",
+ "x = 6500.00\n",
+ "emprec.salary = x\n",
+ "print \"Employee Details: \"\n",
+ "print \"Name: \", emprec.name, \" \", emprec.lastnam\n",
+ "print \"Birthdate: \", emprec.birthday.day, \":\", emprec.birthday.month, \":\", emprec.birthday.year\n",
+ "print \"Salary: \", emprec.salary"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Employee Details: \n",
+ "Name: Tejaswi.V. Rajuk\n",
+ "Birthdate: 24 : 7 : 90\n",
+ "Salary: 6500.0\n"
+ ]
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 9.2, page no. 340"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "CURRENT_YEAR = 96\n",
+ "def increment(sal, year, inc):\n",
+ " if CURRENT_YEAR - year > 30:\n",
+ " sal += inc\n",
+ " return sal\n",
+ "class date:\n",
+ " day = 0\n",
+ " month = 0\n",
+ " year = 0\n",
+ "class person:\n",
+ " name = ''\n",
+ " birthday = date()\n",
+ " salary = 0.0\n",
+ "n = 500\n",
+ "emprec = person()\n",
+ "emprec.name = 'Arun R.'\n",
+ "emprec.birthday.day = 10\n",
+ "emprec.birthday.month = 8\n",
+ "emprec.birthday.year = 64\n",
+ "emprec.salary = 4000.00\n",
+ "print \"Employee Details: \"\n",
+ "print \"Name: \", emprec.name\n",
+ "print \"Birthdate: \", emprec.birthday.day, \":\", emprec.birthday.month, \":\", emprec.birthday.year\n",
+ "print \"Salary: \", emprec.salary\n",
+ "emprec.salary = increment(emprec.salary, emprec.birthday.year, n)\n",
+ "print \"Employee Details: \"\n",
+ "print \"Name: \", emprec.name\n",
+ "print \"Birthdate: \", emprec.birthday.day, \":\", emprec.birthday.month, \":\", emprec.birthday.year\n",
+ "print \"Salary: \", emprec.salary"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Employee Details: \n",
+ "Name: Arun R.\n",
+ "Birthdate: 10 : 8 : 64\n",
+ "Salary: 4000.0\n",
+ "Employee Details: \n",
+ "Name: Arun R.\n",
+ "Birthdate: 10 : 8 : 64\n",
+ "Salary: 4500.0\n"
+ ]
+ }
+ ],
+ "prompt_number": 3
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 9.3, page no. 342"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "CURRENT_YEAR = 96\n",
+ "def increment(emprec):\n",
+ " if CURRENT_YEAR - emprec.birthday.year > 30:\n",
+ " emprec.salary += 500\n",
+ " return emprec\n",
+ "class date:\n",
+ " day = 0\n",
+ " month = 0\n",
+ " year = 0\n",
+ "class person:\n",
+ " name = ''\n",
+ " birthday = date()\n",
+ " salary = 0.0\n",
+ "emprec = person()\n",
+ "emprec.name = 'Arun R.'\n",
+ "emprec.birthday.day = 10\n",
+ "emprec.birthday.month = 8\n",
+ "emprec.birthday.year = 64\n",
+ "emprec.salary = 4000.00\n",
+ "print \"Employee Details: \"\n",
+ "print \"Name: \", emprec.name\n",
+ "print \"Birthdate: \", emprec.birthday.day, \":\", emprec.birthday.month, \":\", emprec.birthday.year\n",
+ "print \"Salary: \", emprec.salary\n",
+ "emprec = increment(emprec)\n",
+ "print \"Employee Details: \"\n",
+ "print \"Name: \", emprec.name\n",
+ "print \"Birthdate: \", emprec.birthday.day, \":\", emprec.birthday.month, \":\", emprec.birthday.year\n",
+ "print \"Salary: \", emprec.salary"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Employee Details: \n",
+ "Name: Arun R.\n",
+ "Birthdate: 10 : 8 : 64\n",
+ "Salary: 4000.0\n",
+ "Employee Details: \n",
+ "Name: Arun R.\n",
+ "Birthdate: 10 : 8 : 64\n",
+ "Salary: 4500.0\n"
+ ]
+ }
+ ],
+ "prompt_number": 6
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 9.4, page no. 343"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "class date:\n",
+ " day = 0\n",
+ " month = 0\n",
+ " year = 0\n",
+ "\n",
+ "class person:\n",
+ " name = ''\n",
+ " birthday = date()\n",
+ " salary = 0.0\n",
+ "\n",
+ "def printout(per):\n",
+ " print \"Employee Details: \"\n",
+ " print \"Name: \", per.name\n",
+ " print \"Birthdate: %3d:%3d:%3d\" %(per.birthday.day, per.birthday.month, per.birthday.year)\n",
+ " print \"Salary: %6.2f\" %(per.salary)\n",
+ "\n",
+ "def readin(record):\n",
+ " record.name = raw_input(\"Enter the name: \")\n",
+ " print \"Enter Birthdate\"\n",
+ " record.birthday.day = int(raw_input(\"Day: \"))\n",
+ " record.birthday.month = int(raw_input(\"Month: \"))\n",
+ " record.birthday.year = int(raw_input(\"Year: \"))\n",
+ " record.salary = float(raw_input(\"Enter the salary: \"))\n",
+ "\n",
+ "test = person()\n",
+ "test.name = \"Arun\"\n",
+ "test.birthday.day = 10\n",
+ "test.birthday.month = 8\n",
+ "test.birthday.year = 75\n",
+ "test.salary = 4500.00\n",
+ "temp = person()\n",
+ "readin(temp)\n",
+ "printout(test)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter the name: Balaji\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter Birthdate\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Day: 24\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Month: 2\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Year: 74\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter the salary: 6500.00\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Employee Details: \n",
+ "Name: Arun\n",
+ "Birthdate: 24: 2: 74\n",
+ "Salary: 4500.00\n"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 9.5 page no. 344"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "class date:\n",
+ " day = 0\n",
+ " month = 0\n",
+ " year = 0\n",
+ "\n",
+ "class person:\n",
+ " name = ''\n",
+ " birthday = date()\n",
+ " salary = 0.0\n",
+ "\n",
+ "def printout(per):\n",
+ " print \"Employee Details: \"\n",
+ " print \"Name: \", per.name\n",
+ " print \"Birthdate: %3d:%3d:%3d\" %(per.birthday.day, per.birthday.month, per.birthday.year)\n",
+ " print \"Salary: %6.2f\" %(per.salary)\n",
+ "\n",
+ "def readin(record):\n",
+ " record.name = raw_input(\"Enter the name: \")\n",
+ " print \"Enter Birthdate\"\n",
+ " record.birthday.day = int(raw_input(\"Day: \"))\n",
+ " record.birthday.month = int(raw_input(\"Month: \"))\n",
+ " record.birthday.year = int(raw_input(\"Year: \"))\n",
+ " record.salary = float(raw_input(\"Enter the salary: \"))\n",
+ "\n",
+ "test = person()\n",
+ "test.name = \"Arun\"\n",
+ "test.birthday.day = 10\n",
+ "test.birthday.month = 8\n",
+ "test.birthday.year = 75\n",
+ "test.salary = 4500.00\n",
+ "temp = person()\n",
+ "readin(temp)\n",
+ "printout(temp)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter the name: Balaji\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter Birthdate\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Day: 24\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Month: 2\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Year: 74\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter the salary: 6500.00\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Employee Details: \n",
+ "Name: Balaji\n",
+ "Birthdate: 24: 2: 74\n",
+ "Salary: 6500.00\n"
+ ]
+ }
+ ],
+ "prompt_number": 3
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 9.6, page no. 346"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "class date:\n",
+ " day = 0\n",
+ " month = 0\n",
+ " year = 0\n",
+ "\n",
+ "class person:\n",
+ " name = ''\n",
+ " birthday = date()\n",
+ " salary = 0.0\n",
+ " \n",
+ " def __init__(self, n, dt, sal):\n",
+ " self.name = n\n",
+ " self.birthday.day = dt[0]\n",
+ " self.birthday.month = dt[1]\n",
+ " self.birthday.year = dt[2]\n",
+ " self.salary = sal\n",
+ "\n",
+ "def printout(per):\n",
+ " print \"Employee Details: \"\n",
+ " print \"Name: \", per.name\n",
+ " print \"Birthdate: %3d:%3d:%3d\" %(per.birthday.day, per.birthday.month, per.birthday.year)\n",
+ " print \"Salary: %6.2f\" %(per.salary)\n",
+ "\n",
+ "def highest(sals):\n",
+ " return max(sals)\n",
+ " \n",
+ "record = [[\"Arun R.\", 10, 8, 75, 4000.00],\n",
+ " [\"vinod S.\", 3, 10, 74, 3000.00],\n",
+ " [\"Tanuj M.\", 21, 1, 73, 5500.00]]\n",
+ "\n",
+ "emp1 = person(record[0][0], (record[0][1], record[0][2], record[0][3]), record[0][4])\n",
+ "emp2 = person(record[1][0], (record[1][1], record[1][2], record[1][3]), record[1][4])\n",
+ "emp3 = person(record[2][0], (record[2][1], record[2][2], record[2][3]), record[2][4])\n",
+ "\n",
+ "sals = [emp1.salary, emp2.salary, emp3.salary]\n",
+ "\n",
+ "highest = highest(sals)\n",
+ "print \"The highest salary being paid is %6.2f\" %highest\n",
+ "printout(emp3)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The highest salary being paid is 5500.00\n",
+ "Employee Details: \n",
+ "Name: Tanuj M.\n",
+ "Birthdate: 21: 1: 73\n",
+ "Salary: 5500.00\n"
+ ]
+ }
+ ],
+ "prompt_number": 5
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 9.7, page no. 347"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "class date:\n",
+ " day = 0\n",
+ " month = 0\n",
+ " year = 0\n",
+ "\n",
+ "class person:\n",
+ " name = ''\n",
+ " birthday = date()\n",
+ " salary = 0.0\n",
+ "\n",
+ "def readin(rec, list1):\n",
+ " rec.name = list1[0]\n",
+ " rec.birthday.day = list1[1]\n",
+ " rec.birthday.month = list1[2]\n",
+ " rec.birthday.year = list1[3]\n",
+ " rec.salary = list1[4]\n",
+ "\n",
+ " \n",
+ "def printout(per):\n",
+ " print \"Employee Details: \"\n",
+ " print \"Name: \", per.name\n",
+ " print \"Birthdate: %3d:%3d:%3d\" %(per.birthday.day, per.birthday.month, per.birthday.year)\n",
+ " print \"Salary: %6.2f\" %(per.salary)\n",
+ "\n",
+ "def highest(sals):\n",
+ " return max(sals)\n",
+ " \n",
+ "record = [[\"Tejaswi\", 24, 7, 70, 7000.00],\n",
+ " [\"vinod S.\", 3, 10, 74, 3000.00],\n",
+ " [\"Tanuj M.\", 21, 1, 73, 5500.00]]\n",
+ "\n",
+ "emp1 = person()\n",
+ "readin(emp1, record[0])\n",
+ "print emp1.birthday.day\n",
+ "emp2 = person()\n",
+ "readin(emp2, record[1])\n",
+ "emp3 = person()\n",
+ "readin(emp3, record[2])\n",
+ "\n",
+ "highest = highest([emp1.salary, emp2.salary, emp3.salary])\n",
+ "temp = emp1\n",
+ "print \"Highest salary being paid is \", highest\n",
+ "printout(temp)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "24\n",
+ "Highest salary being paid is 7000.0\n",
+ "Employee Details: \n",
+ "Name: Tejaswi\n",
+ "Birthdate: 21: 1: 73\n",
+ "Salary: 7000.00\n"
+ ]
+ }
+ ],
+ "prompt_number": 13
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 9.8, page no. 349"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "class date:\n",
+ " day = 0\n",
+ " month = 0\n",
+ " year = 0\n",
+ "\n",
+ "class person:\n",
+ " name = ''\n",
+ " birthday = date()\n",
+ " salary = 0.0\n",
+ "\n",
+ "def readin():\n",
+ " record = []\n",
+ " for i in range(3):\n",
+ " print \"Employee no. \", i+1\n",
+ " temp = person()\n",
+ " temp.name = raw_input(\"Enter the name: \")\n",
+ " print \"Enter Birthdate\"\n",
+ " temp.birthday.day = int(raw_input(\"Day: \"))\n",
+ " temp.birthday.month = int(raw_input(\"Month: \"))\n",
+ " temp.birthday.year = int(raw_input(\"Year: \"))\n",
+ " temp.salary = float(raw_input(\"Enter the salary: \"))\n",
+ " record.append(temp)\n",
+ " return record\n",
+ "\n",
+ "def printout(per):\n",
+ " print \"Employee Details: \"\n",
+ " print \"Name: \", per.name\n",
+ " print \"Birthdate: %3d:%3d:%3d\" %(per.birthday.day, per.birthday.month, per.birthday.year)\n",
+ " print \"Salary: %6.2f\" %(per.salary)\n",
+ "\n",
+ "record = readin()\n",
+ "for i in range(3):\n",
+ " printout(record[i])\n",
+ " print \"hit enter to continue...\"\n",
+ " raw_input()\n",
+ " \n",
+ "#there is a printing mistake in the book"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Employee no. 1\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter the name: Balaji\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter Birthdate\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Day: 24\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Month: 2\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Year: 74\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter the salary: 6500.50\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Employee no. 2\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter the name: Tarun\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter Birthdate\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Day: 30\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Month: 9\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Year: 69\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter the salary: 6890.00\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Employee no. 3\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter the name: Vinod\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter Birthdate\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Day: 7\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Month: 6\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Year: 70\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter the salary: 6700.00\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Employee Details: \n",
+ "Name: Balaji\n",
+ "Birthdate: 7: 6: 70\n",
+ "Salary: 6500.50\n",
+ "hit enter to continue...\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Employee Details: \n",
+ "Name: Tarun\n",
+ "Birthdate: 7: 6: 70\n",
+ "Salary: 6890.00\n",
+ "hit enter to continue...\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Employee Details: \n",
+ "Name: Vinod\n",
+ "Birthdate: 7: 6: 70\n",
+ "Salary: 6700.00\n",
+ "hit enter to continue...\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 17
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 9.9, page no. 351"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "class date:\n",
+ " day = 0\n",
+ " month = 0\n",
+ " year = 0\n",
+ "\n",
+ "class person:\n",
+ " name = ''\n",
+ " birthday = date()\n",
+ " salary = 0.0\n",
+ "\n",
+ "def readin(n):\n",
+ " record = []\n",
+ " for i in range(n):\n",
+ " print \"Employee no. \", i+1\n",
+ " temp = person()\n",
+ " temp.name = raw_input(\"Enter the name: \")\n",
+ " print \"Enter Birthdate\"\n",
+ " temp.birthday.day = int(raw_input(\"Day: \"))\n",
+ " temp.birthday.month = int(raw_input(\"Month: \"))\n",
+ " temp.birthday.year = int(raw_input(\"Year: \"))\n",
+ " temp.salary = float(raw_input(\"Enter the salary: \"))\n",
+ " record.append(temp)\n",
+ " return record\n",
+ "\n",
+ "def printout(record):\n",
+ " for i in range(n):\n",
+ " print \"Employee Details: \"\n",
+ " print \"Name: \", record[i].name\n",
+ " print \"Birthdate: %3d:%3d:%3d\" %(record[i].birthday.day, record[i].birthday.month, record[i].birthday.year)\n",
+ " print \"Salary: %6.2f\" %(record[i].salary)\n",
+ "\n",
+ "def sortalpha(rec):\n",
+ " for i in range(n-1):\n",
+ " for j in range(n-i-1):\n",
+ " if rec[j].name > rec[j+1].name:\n",
+ " rec[j], rec[j+1] = rec[j+1], rec[j]\n",
+ " return rec\n",
+ "\n",
+ "def sortage(rec):\n",
+ " for i in range(n-1):\n",
+ " for j in range(n-i-1):\n",
+ " if (rec[j].birthday.year > rec[j+1].birthday.year or rec[j].birthday.year == rec[j+1].birthday.year and\n",
+ " rec[j].birthday.month > rec[j+1].birthday.month or rec[j].birthday.month == rec[j+1].birthday.month and\n",
+ " rec[j].birthday.day > rec[j+1].birthday.day or rec[j].birthday.day == rec[j+1].birthday.day):\n",
+ " rec[j], rec[j+1] = rec[j+1], rec[j]\n",
+ " return rec\n",
+ "\n",
+ "def sortsal(rec):\n",
+ " for i in range(n-1):\n",
+ " for j in range(n-i-1):\n",
+ " if rec[j].salary > rec[j+1].salary:\n",
+ " rec[j], rec[j+1] = rec[j+1], rec[j]\n",
+ " return rec\n",
+ "\n",
+ "n = int(raw_input(\"Enter the number of records: \"))\n",
+ "record = readin(n)\n",
+ "\n",
+ "flag = True\n",
+ "while(flag):\n",
+ " print \"1: SORT BY NAME\"\n",
+ " print \"2: SORT BY AGE\"\n",
+ " print \"3: SORT BY SALARY\"\n",
+ " print \"4: QUIT \"\n",
+ " i = int(raw_input(\"Enter your choice: \"))\n",
+ " if i == 1:\n",
+ " sorted_rec = sortalpha(record)\n",
+ " printout(sorted_rec)\n",
+ " elif i == 2:\n",
+ " sorted_rec = sortage(record)\n",
+ " printout(sorted_rec)\n",
+ " elif i == 3:\n",
+ " sorted_rec = sortsal(record)\n",
+ " printout(sorted_rec)\n",
+ " elif i == 4:\n",
+ " flag = False\n",
+ " break\n",
+ " else:\n",
+ " print \"invalid choice...\""
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter the number of records: 5\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Employee no. 1\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter the name: Vinod\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter Birthdate\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Day: 23\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Month: 8\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Year: 74\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter the salary: 6500\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Employee no. 2\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter the name: Arun\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter Birthdate\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Day: 10\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Month: 5\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Year: 70\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter the salary: 7000\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Employee no. 3\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter the name: Puneet\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter Birthdate\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Day: 30\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Month: 6\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Year: 74\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter the salary: 6900\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Employee no. 4\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter the name: Tanuj\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter Birthdate\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Day: 7\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Month: 1\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Year: 70\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter the salary: 7700\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Employee no. 5\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter the name: Rohit\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter Birthdate\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Day: 17\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Month: 12\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Year: 73\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter the salary: 6800\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "1: SORT BY NAME\n",
+ "2: SORT BY AGE\n",
+ "3: SORT BY SALARY\n",
+ "4: QUIT \n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter your choice: 1\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Employee Details: \n",
+ "Name: Arun\n",
+ "Birthdate: 17: 12: 73\n",
+ "Salary: 7000.00\n",
+ "Employee Details: \n",
+ "Name: Puneet\n",
+ "Birthdate: 17: 12: 73\n",
+ "Salary: 6900.00\n",
+ "Employee Details: \n",
+ "Name: Rohit\n",
+ "Birthdate: 17: 12: 73\n",
+ "Salary: 6800.00\n",
+ "Employee Details: \n",
+ "Name: Tanuj\n",
+ "Birthdate: 17: 12: 73\n",
+ "Salary: 7700.00\n",
+ "Employee Details: \n",
+ "Name: Vinod\n",
+ "Birthdate: 17: 12: 73\n",
+ "Salary: 6500.00\n",
+ "1: SORT BY NAME\n",
+ "2: SORT BY AGE\n",
+ "3: SORT BY SALARY\n",
+ "4: QUIT \n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter your choice: 2\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Employee Details: \n",
+ "Name: Vinod\n",
+ "Birthdate: 17: 12: 73\n",
+ "Salary: 6500.00\n",
+ "Employee Details: \n",
+ "Name: Tanuj\n",
+ "Birthdate: 17: 12: 73\n",
+ "Salary: 7700.00\n",
+ "Employee Details: \n",
+ "Name: Rohit\n",
+ "Birthdate: 17: 12: 73\n",
+ "Salary: 6800.00\n",
+ "Employee Details: \n",
+ "Name: Puneet\n",
+ "Birthdate: 17: 12: 73\n",
+ "Salary: 6900.00\n",
+ "Employee Details: \n",
+ "Name: Arun\n",
+ "Birthdate: 17: 12: 73\n",
+ "Salary: 7000.00\n",
+ "1: SORT BY NAME\n",
+ "2: SORT BY AGE\n",
+ "3: SORT BY SALARY\n",
+ "4: QUIT \n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter your choice: 33\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "invalid choice...\n",
+ "1: SORT BY NAME\n",
+ "2: SORT BY AGE\n",
+ "3: SORT BY SALARY\n",
+ "4: QUIT \n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter your choice: 3\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Employee Details: \n",
+ "Name: Vinod\n",
+ "Birthdate: 17: 12: 73\n",
+ "Salary: 6500.00\n",
+ "Employee Details: \n",
+ "Name: Rohit\n",
+ "Birthdate: 17: 12: 73\n",
+ "Salary: 6800.00\n",
+ "Employee Details: \n",
+ "Name: Puneet\n",
+ "Birthdate: 17: 12: 73\n",
+ "Salary: 6900.00\n",
+ "Employee Details: \n",
+ "Name: Arun\n",
+ "Birthdate: 17: 12: 73\n",
+ "Salary: 7000.00\n",
+ "Employee Details: \n",
+ "Name: Tanuj\n",
+ "Birthdate: 17: 12: 73\n",
+ "Salary: 7700.00\n",
+ "1: SORT BY NAME\n",
+ "2: SORT BY AGE\n",
+ "3: SORT BY SALARY\n",
+ "4: QUIT \n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter your choice: 4\n"
+ ]
+ }
+ ],
+ "prompt_number": 23
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example mem.c, page no. 359"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Note: There is no concept of unions in Python. We will implment using classes\n",
+ "\n",
+ "import sys\n",
+ "\n",
+ "class emp:\n",
+ " name = ''\n",
+ " idno = 0\n",
+ " salary = 0.0\n",
+ "\n",
+ "class desc:\n",
+ " name = ''\n",
+ " idno = 0\n",
+ " salary = 0.0\n",
+ "\n",
+ "e = emp()\n",
+ "d = desc()\n",
+ " \n",
+ "print \"The size of the structure is \", sys.getsizeof(e)\n",
+ "print \"The size of the union is \", sys.getsizeof(d)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The size of the structure is 72\n",
+ "The size of the union is 72\n"
+ ]
+ }
+ ],
+ "prompt_number": 24
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example on page no. 359"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "class desc:\n",
+ " name = ''\n",
+ " idno = 0\n",
+ " salary = 0.0\n",
+ "\n",
+ "des = desc()\n",
+ "des.name = 'Vinod'\n",
+ "print \"Employee Details: \"\n",
+ "print \"The name is \", des.name\n",
+ "print \"The idno is \", des.idno\n",
+ "print \"The salary is \", des.salary\n",
+ "des.idno = 10\n",
+ "print \"Employee Details: \"\n",
+ "print \"The name is \", des.name\n",
+ "print \"The idno is \", des.idno\n",
+ "print \"The salary is \", des.salary\n",
+ "des.salary = 6500.00\n",
+ "print \"Employee Details: \"\n",
+ "print \"The name is \", des.name\n",
+ "print \"The idno is \", des.idno\n",
+ "print \"The salary is \", des.salary\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Employee Details: \n",
+ "The name is Vinod\n",
+ "The idno is 0\n",
+ "The salary is 0.0\n",
+ "Employee Details: \n",
+ "The name is Vinod\n",
+ "The idno is 10\n",
+ "The salary is 0.0\n",
+ "Employee Details: \n",
+ "The name is Vinod\n",
+ "The idno is 10\n",
+ "The salary is 6500.0\n"
+ ]
+ }
+ ],
+ "prompt_number": 2
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+} \ No newline at end of file