summaryrefslogtreecommitdiff
path: root/C_Programming:_A_Modern_Approach_by_K.N._King/Chapter16.ipynb
blob: 20e3b1e5cd780cdf382edcf29c78f6d9d724ba18 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
{
 "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": {}
  }
 ]
}