summaryrefslogtreecommitdiff
path: root/C++_from_the_Ground/Chapter_10(1).ipynb
blob: 6196736b6e7553ef3b1d0f6824fb2a14ad6c02a7 (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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
{
 "metadata": {
  "name": "Chapter 10"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": "<h1>Chapter 10: Structures and Unions<h1>"
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": "<h3>Example 10.1, Page Number: 223<h3>"
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": "'''A simple inventory prohram that uses an array of structures/ '''\n'''Implementing structures in python'''\n\n\nclass inv_type:\n    def __init__(self):\n        self.item=None\n        self.cost=0\n        self.retail=0\n        self.on_hand=0\n        self.lead_time=0\n\n#Variable declaration\nsize=100    \ninvtry = []*size\ni=5                    #User iput for menu selection\n\n#Initialize the array\ndef init_list():\n    for t in range(size):\n        invtry.append(inv_type())\n        \n#get a menu selection\ndef menu():\n    global i\n    print \"(E)nter\"\n    print \"(D)isplay\"\n    print \"(U)pdate\"\n    print \"(Q)uit\"\n    print \"choose one: \"\n    i-=1\n    return i\n\n#enter items into the list\ndef enter():\n    #find the first free structure\n    for i in range(size):\n        if not(invtry[i].item==None):\n            break\n    #i will be size if list is full\n    if i==size:\n        print \"List full.\"\n        return\n    input(i)\n    \n#Input the information\ndef input(i):\n    #Enter information; User input\n    invtry[i].item=\"Gloves\"\n    invtry[i].cost=10\n    invtry[i].retail=25\n    invtry[i].on_hand=50\n    invtry[i].lead_time=10\n    \n#Modify an existing item\ndef update():\n    name=\"Gloves\"   #User input\n    for i in range(size):\n        if not(name==invtry[i].item):\n            break\n    if i==size:\n        print \"Item not found.\"\n        return\n    print \"Enter new information.\"\n    input(i)\n    \n#Display the list\ndef display():\n    for t in range(size):\n        if not(invtry[t].item==None):\n            print invtry[t].item\n            print \"Cost: $\",invtry[t].cost\n            print \"Retail: $\",invtry[t].retail\n            print \"On hand: \",invtry[t].on_hand\n            print \"Resupply time: \",invtry[t].lead_time,\" days\"\n            \n\ninit_list()\nwhile True:\n    choice=menu()\n    if choice==4:\n        enter()\n    elif choice==3:\n        display()\n    elif choice==2:\n        update()\n    elif choice==1:\n        break",
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": "(E)nter\n(D)isplay\n(U)pdate\n(Q)uit\nchoose one: \n(E)nter\n(D)isplay\n(U)pdate\n(Q)uit\nchoose one: \nGloves\nCost: $ 10\nRetail: $ 25\nOn hand:  50\nResupply time:  10  days\n(E)nter\n(D)isplay\n(U)pdate\n(Q)uit\nchoose one: \nEnter new information.\n(E)nter\n(D)isplay\n(U)pdate\n(Q)uit\nchoose one: \n"
      }
     ],
     "prompt_number": 11
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": "<h3>Example 10.2, Page Number: 226<h3>"
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": "'''Pass a structure(class) to a function'''\n\n#Define a structure(class) type\nclass sample:\n    a=None\n    ch=None\n    \ndef f1(parm):\n    print parm.a,\" \",parm.ch\n\n#declare arg\narg=sample()  \n\n#initialize arg\narg.a=1000\narg.ch='X'\n\n#call function\nf1(arg)\n    ",
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": "1000   X\n"
      }
     ],
     "prompt_number": 12
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": "<h3>Example 10.3, Page Number: 227<h3>"
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": "'''Demonstrate structure assignments.'''\n\nclass stype:\n    a=None\n    b=None\n\n#Variable declaration\nsvar1=stype()\nsvar2=stype()\n\nsvar1.a=svar1.b=10\nsvar2.a=svar2.b=20\n\nprint \"Structures before assignment.\"\nprint \"svar1: \",svar1.a,' ',svar1.b\nprint \"svar1: \",svar2.a,' ',svar2.b\n\nsvar2=svar1    #assign structures\n\n#Result\nprint \"\\nStructures before assignment.\"\nprint \"svar1: \",svar1.a,' ',svar1.b\nprint \"svar1: \",svar2.a,' ',svar2.b",
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": "Structures before assignment.\nsvar1:  10   10\nsvar1:  20   20\n\nStructures before assignment.\nsvar1:  10   10\nsvar1:  10   10\n"
      }
     ],
     "prompt_number": 13
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": "<h3>Example 10.4, Page Number: 230<h3>"
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": "'''This program displays the current system time.'''\n\nimport datetime\n\ndate=datetime.datetime.now()\n\n#Result\nprint date.time()",
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": "17:06:28.236000\n"
      }
     ],
     "prompt_number": 14
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": "<h3>Example 10.5, Page Number: 231<h3>"
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": "'''This program displays the current system time.'''\n\nimport datetime\n\ndate=datetime.datetime.now()\n\n#Result\nprint date.ctime()",
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": "Sat Sep 14 17:07:14 2013\n"
      }
     ],
     "prompt_number": 15
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": "<h3>Example 10.6, Page Number: 232<h3>"
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": "'''Demonstrate a reference to a structure'''\n\nclass mystruct:\n    a=None\n    b=None\n\n#Recieve and return by reference\ndef f(var):\n    var[0].a=var[0].a*var[0].a\n    var[0].b=var[0].b/var[0].b\n    return var[0]\n    \n#Variable declaration\nx=[]\nx.append(mystruct())\ny=mystruct()\n\n#Initializing\nx[0].a=10\nx[0].b=20\n\nprint \"Original x.a and x.b: \",x[0].a,' ',x[0].b\n\ny=f(x)   #function call\n\n#Result\nprint \"Modified x.a and x.b: \",x[0].a,' ',x[0].b\nprint \"Modified y.a and y.b: \",y.a,' ',y.b\n\n",
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": "Original x.a and x.b:  10   20\nModified x.a and x.b:  100   1\nModified y.a and y.b:  100   1\n"
      }
     ],
     "prompt_number": 16
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": "<h3>Example 10.7, Page Number: 239<h3>"
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": "'''Use a union to exchange the bytes within a short integer'''\n#Class used in place of union \n\nclass swap_bytes:\n    ch=[0,0]\n\n#Exchange of bytes\ndef disp_binary(u):\n    t=128\n    while not(t==0):\n        if u&t:\n            print \"1 \",\n        else:\n            print \"0 \",\n        t=t/2\n\n#Variable declaration\nsb=swap_bytes()\n\nsb.ch[0]=15\n\nprint \"Original bytes: \",\ndisp_binary(sb.ch[1])\ndisp_binary(sb.ch[0])\n\n#Exchange bytes\ntemp=sb.ch[0]\nsb.ch[0]=sb.ch[1]\nsb.ch[1]=temp\n\n#Result\nprint \"\\nExchanged bytes: \",\ndisp_binary(sb.ch[1])\ndisp_binary(sb.ch[0])\n\n\n    ",
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": "Original bytes:  0  0  0  0  0  0  0  0  0  0  0  0  1  1  1  1  \nExchanged bytes:  0  0  0  0  1  1  1  1  0  0  0  0  0  0  0  0 \n"
      }
     ],
     "prompt_number": 17
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": "<h3>Example 10.8, Page Number: 240<h3>"
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": "'''Display the ASCII code in binary for characters'''\n\n#Variable declaration\nch='a'\n\nwhile True:\n    print \"\\n\",ch,\n    print bin(ord(ch))     #Display the bit pattern for each character\n    ch=chr(ord(ch)+1)\n    if ch=='r':\n        break",
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": "\na 0b1100001\n\nb 0b1100010\n\nc 0b1100011\n\nd 0b1100100\n\ne 0b1100101\n\nf 0b1100110\n\ng 0b1100111\n\nh 0b1101000\n\ni 0b1101001\n\nj 0b1101010\n\nk 0b1101011\n\nl 0b1101100\n\nm 0b1101101\n\nn 0b1101110\n\no 0b1101111\n\np 0b1110000\n\nq 0b1110001\n"
      }
     ],
     "prompt_number": 18
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": "<h3>Example 10.9, Page Number: 242<h3>"
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": "'''Implementation of an example of union in python'''\n\n#Variable declaration\nch=['X','Y']\nc=\"\"\ndef disp_bits(u):\n    t=128\n    global c\n    while not(t==0):\n        if u&t:\n            c=c+\"1\"\n        else:\n            c=c+\"0\"\n        t=t/2 \n    return c\n\n#Result\nprint \"union as chars: \",ch[0],ch[1]\nprint \"union as integer: \",\nc= disp_bits(ord(ch[1]))\nc= disp_bits(ord(ch[0]))\nprint int(str(c),2)\n\n\n",
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": "union as chars:  X Y\nunion as integer:  22872\n"
      }
     ],
     "prompt_number": 19
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": "",
     "language": "python",
     "metadata": {},
     "outputs": []
    }
   ],
   "metadata": {}
  }
 ]
}