summaryrefslogtreecommitdiff
path: root/C++_Programming_In_Easy_Steps/Chapter9.ipynb
blob: 18bdfd1715f953730a9dbee957cbffad9fae80ed (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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
{
 "metadata": {
  "name": "",
  "signature": "sha256:53d2aae81c824182838b074246429ee4c039bafa2ea2a0b5b38839beb8288df9"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "heading",
     "level": 1,
     "metadata": {},
     "source": [
      "Chapter 9: Processing macros"
     ]
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 9.1, Page No.153"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "print \"This is a simple test program\""
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "This is a simple test program\n"
       ]
      }
     ],
     "prompt_number": 1
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 9.2, Page No.154"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "BOOK=\"C++ Programming in easy steps\"\n",
      "NUM=200\n",
      "RULE=\"*****************************\"\n",
      "print RULE\n",
      "print BOOK\n",
      "print RULE\n",
      "print \"NUM is:\",NUM\n",
      "print \"Double NUM:\",NUM*2"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "*****************************\n",
        "C++ Programming in easy steps\n",
        "*****************************\n",
        "NUM is: 200\n",
        "Double NUM: 400\n"
       ]
      }
     ],
     "prompt_number": 7
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 9.3, Page No.156"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "if not 'BOOK' in locals():\n",
      "    BOOK=\"C++ Programming in easy steps\"\n",
      "\n",
      "print BOOK,\n",
      "if not 'AUTHOR' in locals():\n",
      "    AUTHOR=\"Mike McGrath\"\n",
      "print \" by \",AUTHOR\n",
      "if not 'BOOK' in locals():\n",
      "    BOOK=\"\"\n",
      "if 'BOOK' in locals():\n",
      "    BOOK=\"Linux in easy steps\"\n",
      "\n",
      "print BOOK,\" by \",AUTHOR"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Linux in easy steps  by  Mike McGrath\n",
        "Linux in easy steps  by  Mike McGrath\n"
       ]
      }
     ],
     "prompt_number": 26
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 9.4, Page No.159"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "import platform\n",
      "plat=platform.system()\n",
      "print plat,\" system\"\n",
      "if plat==\"Windows\":\n",
      "    print \"Performing Windows-only tasks\"\n",
      "else:\n",
      "    print \"Performing Linux-only tasks\""
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Windows  system\n",
        "Performing Windows-only tasks\n"
       ]
      }
     ],
     "prompt_number": 32
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 9.5, Page No.160"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "def add(x,y):\n",
      "    return x+y\n",
      "def triple(x):\n",
      "    return (add(x,add(x,x)))\n",
      "print \"9 + 3 = \",add(9,3)\n",
      "print \"9 x 3 = \",triple(9)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "9 + 3 =  12\n",
        "9 x 3 =  27\n"
       ]
      }
     ],
     "prompt_number": 34
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 9.6, Page No.162"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "def SQUARE(n):\n",
      "    return n*n\n",
      "def CUBE(n):\n",
      "    return n*n*n\n",
      "num=5\n",
      "print \"Macro SQUARE:\",SQUARE(num)\n",
      "print \"inline square:\",SQUARE(num)\n",
      "print \"Macro CUBE:\",CUBE(num)\n",
      "print \"inline CUBE:\",CUBE(num)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Macro SQUARE: 25\n",
        "inline square: 25\n",
        "Macro CUBE: 125\n",
        "inline CUBE: 125\n"
       ]
      }
     ],
     "prompt_number": 35
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 9.7, Page No.164"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "def LINEOUT(str):\n",
      "    print str\n",
      "def GLUEOUT(a,b):\n",
      "    print a,\" \",b\n",
      "longer=\"They carried a net \"\n",
      "line=\"and their hearts \"\n",
      "LINEOUT(\"In a bowl to sea went wise men three\")\n",
      "LINEOUT(\"On a brilliant night in      June\")\n",
      "GLUEOUT(longer,line)\n",
      "LINEOUT(\"On fishing up the moon.\")"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "In a bowl to sea went wise men three\n",
        "On a brilliant night in      June\n",
        "They carried a net    and their hearts \n",
        "On fishing up the moon.\n"
       ]
      }
     ],
     "prompt_number": 36
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 9.8, Page No.166"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "DEBUG=0\n",
      "if DEBUG == 1:\n",
      "    def ASSERT(expr):\n",
      "        print expr, \"...\", num\n",
      "        if expr != True:\n",
      "            print \"Fails in  file: \"# There is no concept of line in python\n",
      "            print \"at line\"# There is no concept of line in python\n",
      "        else:\n",
      "            print \"Succeeds\"\n",
      "elif DEBUG == 0:\n",
      "    def ASSERT(expr):\n",
      "        print \"Number is \",num\n",
      "num = 9\n",
      "ASSERT(num < 10)\n",
      "num = num + num\n",
      "ASSERT(num < 10)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Number is  9\n",
        "Number is  18\n"
       ]
      }
     ],
     "prompt_number": 10
    }
   ],
   "metadata": {}
  }
 ]
}