{
 "metadata": {
  "name": "",
  "signature": "sha256:9fe9374bd25aac8dfc98eab84988bb008dcfb464faf94260bebab61ae764fb98"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "heading",
     "level": 1,
     "metadata": {},
     "source": [
      "Chapter 16: Structures, Unions and Enumerations"
     ]
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example inventory.c, Page 391"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "NAME_LEN=25\n",
      "MAX_PARTS=100\n",
      "num_parts=0\n",
      "number1=[0]*100\n",
      "on_hand1=[0]*100\n",
      "name1=[0]*100\n",
      "def find_part(number):\n",
      "    i=0\n",
      "    for i in range (i,num_parts):\n",
      "        if(number1[i]==number):\n",
      "            return i\n",
      "    return -1\n",
      "def insert():\n",
      "    num_parts=0\n",
      "    if(num_parts==MAX_PARTS):\n",
      "        print \"Database is full, cant add more parts\"\n",
      "        return\n",
      "    part_number=int(raw_input(\"Enter part number: \"))\n",
      "    if(find_part(part_number)>=0):\n",
      "        print \"Part already exists\"\n",
      "        return\n",
      "    number1[num_parts]=part_number\n",
      "    name1[num_parts]=raw_input(\"Enter part name: \")\n",
      "    on_hand1[num_parts]=raw_input(\"Enter quality on hand: \")\n",
      "    num_parts=num_parts+1\n",
      "    \n",
      "def search():\n",
      "    number=int(raw_input(\"Enter part number: \"))\n",
      "    i=find_part(number)\n",
      "    if(i>=0):\n",
      "        print \"Part name: %s\" %name1[i]\n",
      "        print \"Quantity on hand: %d\"%on_hand1[i]\n",
      "    else:\n",
      "        print \"Part not found\"\n",
      "        \n",
      "def update():\n",
      "    number=int(raw_input(\"Enter part number: \"))\n",
      "    i=find_part(number)\n",
      "    if(i>=0):\n",
      "        change=int(raw_input(\"Enter change in quantity on hand: \")) \n",
      "        on_hand1[i]=on_hand1[i]+change\n",
      "    else:\n",
      "        print \"Part not found\"\n",
      "\n",
      "while(1):\n",
      "    code=raw_input(\"Enter operation code: \")\n",
      "    if code=='i':\n",
      "        insert()\n",
      "        break\n",
      "    elif code=='s':\n",
      "        search()\n",
      "        break\n",
      "    elif code=='u':\n",
      "        update()\n",
      "        break\n",
      "    elif(code=='p'):\n",
      "        print()\n",
      "        break\n",
      "\n",
      "print \"\""
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter operation code: i\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter part number: 528\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter part name: Disk Drive\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter quality on hand: 10\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "\n"
       ]
      }
     ],
     "prompt_number": 9
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example readline.c, Page 395"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "def read_line(str,n):\n",
      "    ch=0\n",
      "    i=0\n",
      "    EOF='\\n'\n",
      "    ch=raw_input()\n",
      "    while(isspace(ch)):\n",
      "        donothing=0\n",
      "    while(ch!='\\n' and ch!=EOF):\n",
      "        if(i<n):\n",
      "            str[i+1]=ch\n",
      "        ch=raw_input()\n",
      "    str[i]='\\0'\n",
      "    return i"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 3
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [],
     "language": "python",
     "metadata": {},
     "outputs": []
    }
   ],
   "metadata": {}
  }
 ]
}