summaryrefslogtreecommitdiff
path: root/The_C_Book/Chapter6.ipynb
blob: f0f634c371cad466af00a4603d5529f5dd9f0a02 (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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
{
 "metadata": {
  "name": "",
  "signature": "sha256:899f4d53f2b8fecda4de14fb928c82b270754cca6abc5b32e76cbdd24e164f18"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "heading",
     "level": 1,
     "metadata": {},
     "source": [
      "Chapter 6: Structured Data Types"
     ]
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 6.1, page no. 173"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "'''\n",
      "Example 6.1 and example 6.2 are same for python.\n",
      "'''\n",
      "\n",
      "\n",
      "class wp_char:\n",
      "    wp_cval = ''\n",
      "    wp_font = 0\n",
      "    wp_psize = 0\n",
      "\n",
      "    def __init__(self):\n",
      "        wp_cval = ''\n",
      "        wp_font = 0\n",
      "        wp_psize = 0\n",
      "\n",
      "\n",
      "def infun():\n",
      "    w = wp_char()\n",
      "    #w.wp_cval = raw_input(\"Enter any Character.. :\")\n",
      "    w.wp_font = 2\n",
      "    w.wp_psize = 10\n",
      "    return w\n",
      "\n",
      "ARSIZE = 10\n",
      "icount=0\n",
      "lo_indx=0\n",
      "hi_indx=0\n",
      "ar = []\n",
      "for icount in range(ARSIZE):\n",
      "    ar.append(infun())\n",
      "    if(ar[icount].wp_cval == '\\n'):\n",
      "        ''' /*\n",
      "        * Leave the loop.\n",
      "        * not incrementing icount means that the\n",
      "        * '\\n' is ignored in the sort\n",
      "        */\n",
      "        '''\n",
      "        break;\n",
      "\n",
      "\n",
      "#/* now a simple exchange sort */\n",
      "for lo_indx in range(icount-1):\n",
      "    for hi_indx in range(icount):\n",
      "        if (ar[lo_indx].wp_cval > ar[hi_indx].wp_cval):\n",
      "            #Swap the two structures.\n",
      "            wp_tmp = ar[lo_indx]\n",
      "            ar[lo_indx] = ar[hi_indx]\n",
      "            ar[hi_indx] = wp_tmp\n",
      "\n",
      "\n",
      "for lo_indx in range(icount+1):\n",
      "    print \"%s %d %d\\n\" % (ar[lo_indx].wp_cval,ar[lo_indx].wp_font,ar[lo_indx].wp_psize)\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        " 2 10\n",
        "\n",
        " 2 10\n",
        "\n",
        " 2 10\n",
        "\n",
        " 2 10\n",
        "\n",
        " 2 10\n",
        "\n",
        " 2 10\n",
        "\n",
        " 2 10\n",
        "\n",
        " 2 10\n",
        "\n",
        " 2 10\n",
        "\n",
        " 2 10\n",
        "\n"
       ]
      }
     ],
     "prompt_number": 1
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 6.3, page no. 183"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "'''\n",
      "This wont give any output.\n",
      "\n",
      "Note: while compiling this program. comment either of the s_1 class structure.\n",
      "'''\n",
      "\n",
      "\n",
      "class s_1: #incomplete type\n",
      "    pass\n",
      "\n",
      "class s_2:\n",
      "    something = 0\n",
      "    s = s_1()\n",
      "\n",
      "class s_1:\n",
      "    something = 0.0\n",
      "    s =  s_2()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 4
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 6.4, page no. 183"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "'''\n",
      "This wont give any output.\n",
      "'''\n",
      "\n",
      "class x:\n",
      "    f = 0.0\n",
      "\n",
      "def func():\n",
      "    a = x()\n",
      "    a.f = 0 \n",
      "    return a\n",
      "\n",
      "\n",
      "def f2():\n",
      "    o = func()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 5
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 6.5, page no. 185"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "'''\n",
      "this wont give any output.\n",
      "'''\n",
      "\n",
      "class list_ele:\n",
      "    def __init__(self,d=0,p=None):\n",
      "        data=d\n",
      "        pointer = p\n",
      "ar=[]\n",
      "\n",
      "ar.append(list_ele())\n",
      "ar.append(list_ele())\n",
      "ar.append(list_ele())\n",
      "ar[0].data = 5;\n",
      "ar[0].pointer = ar[2]\n",
      "ar[1].data = 99\n",
      "ar[1].pointer = ar[2]\n",
      "ar[2].data = -7\n",
      "ar[2].pointer = None"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 1
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 6.6, page no. 185"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "class list_ele:\n",
      "    def __init__(self,d=0,p=None):\n",
      "        data=d\n",
      "        pointer = p\n",
      "ar=[]\n",
      "\n",
      "ar.append(list_ele())\n",
      "ar.append(list_ele())\n",
      "ar.append(list_ele())\n",
      "ar[0].data = 5;\n",
      "ar[0].pointer = ar[2]\n",
      "ar[1].data = 99\n",
      "ar[1].pointer = ar[2]\n",
      "ar[2].data = -7\n",
      "ar[2].pointer = None\n",
      "\n",
      "for i in ar:\n",
      "    print \"contents %d\\n\" %(i.data)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "contents 5\n",
        "\n",
        "contents 99\n",
        "\n",
        "contents -7\n",
        "\n"
       ]
      }
     ],
     "prompt_number": 2
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 6.7, page no. 187"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "'''\n",
      "This wont compile as we do not have list_ele class here.\n",
      "'''\n",
      "\n",
      "def sortfun(list ):\n",
      "    exchange = True\n",
      "    nextp = []\n",
      "    thisp = None\n",
      "    dummy = list_ele()\n",
      "    '''\n",
      "    * Algorithm is this:\n",
      "    * Repeatedly scan list.\n",
      "    * If two list items are out of order,\n",
      "    * link them in the other way round.\n",
      "    * Stop if a full pass is made and no\n",
      "    * exchanges are required.\n",
      "    * The whole business is confused by\n",
      "    * working one element behind the\n",
      "    * first one of interest.\n",
      "    * This is because of the simple mechanics of\n",
      "    * linking and unlinking elements.\n",
      "    */ '''\n",
      "    dummy.pointer = list;\n",
      "    while(exchange):\n",
      "        exchange = False\n",
      "        thisp=dummy\n",
      "        while( (nextp == thisp.pointer) and nextp.pointer):\n",
      "            if(nextp.data < nextp.pointer.data):\n",
      "                #/* exchange */\n",
      "                exchange = 1\n",
      "                thisp.pointer = nextp.pointer\n",
      "                nextp.pointer =thisp.pointer.pointer\n",
      "                thisp.pointer.pointer = nextp\n",
      "\n",
      "            thisp = thisp.pointer;\n",
      "\n",
      "    return(dummy.pointer);"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 2
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 6.8, page no. 190"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "'''\n",
      "Examples 6.8 and Example 6.9\n",
      "Both are same for Python as we do not have any concept of pointer.\n",
      "Python will go same for both.\n",
      "'''\n",
      "\n",
      "\n",
      "class Tree:\n",
      "    def __init__(self, cargo, left=None, right=None):\n",
      "        self.cargo = cargo\n",
      "        self.left  = left\n",
      "        self.right = right\n",
      "\n",
      "    def __str__(self):\n",
      "        return str(self.cargo)\n",
      "'''\n",
      "/*\n",
      "* Tree search algorithm.\n",
      "* Searches for value 'v' in tree,\n",
      "* returns pointer to first node found containing\n",
      "* the value otherwise 0.\n",
      "*/\n",
      "\n",
      "'''\n",
      "def t_search(root,v):\n",
      "    while(root):\n",
      "        if(root.cargo == v):\n",
      "            return(root)\n",
      "        if(root.cargo > v):\n",
      "            root = root.left\n",
      "        else:\n",
      "            root = root.right\n",
      "\n",
      "\n",
      "\n",
      "#/* construct tree by hand */\n",
      "tp = None\n",
      "root_p = None\n",
      "tree=[]\n",
      "for i in range(7):\n",
      "    j = 0\n",
      "    j = i+1\n",
      "    tree.append(Tree(j))\n",
      "    \n",
      "    if(j == 2 or j == 6):\n",
      "        tree[i].left = tree[i-1]\n",
      "        tree[i-1].right = tree[i]\n",
      "\n",
      "\n",
      "root_p = tree[3]\n",
      "root_p.left = tree[1]\n",
      "root_p.right = tree[5]\n",
      "# /* try the search */\n",
      "tp = t_search(root_p, 9)\n",
      "if(tp!=None):\n",
      "    print \"found at position %d\\n\" %(tp-tree)\n",
      "else:\n",
      "    print \"value not found\\n\""
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "value not found\n",
        "\n"
       ]
      }
     ],
     "prompt_number": 3
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 6.10, page no. 192"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "'''\n",
      "Note : this wont give any output.\n",
      "'''\n",
      "\n",
      "\n",
      "def t_walk(r):\n",
      "    if(root_p == 0):\n",
      "        return\n",
      "    t_walk(root_p.left)\n",
      "    print \"%d\\n\" %(root_p.data)\n",
      "    t_walk(root_p.right)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 4
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 6.11, page no. 193"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "class A:\n",
      "    def __init__(self):\n",
      "        u_f = 0.0\n",
      "        u_i = 0\n",
      "\n",
      "var = A()\n",
      "var.u_f = 23.5\n",
      "print \"value is %f\\n\" %(var.u_f)\n",
      "var.u_i = 5\n",
      "print \"value is %d\\n\" %(var.u_i)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "value is 23.500000\n",
        "\n",
        "value is 5\n",
        "\n"
       ]
      }
     ],
     "prompt_number": 5
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 6.12, page no. 194"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "\n",
      "FLOAT_TYPE = 1\n",
      "CHAR_TYPE = 2\n",
      "INT_TYPE = 3\n",
      "class var_type:\n",
      "    def __init__(self):\n",
      "        type_in_union = 0\n",
      "        un_float = 0.0\n",
      "        un_char = '0'\n",
      "        un_int = 0\n",
      "\n",
      "        \n",
      "\n",
      "v = var_type()\n",
      "def print_vt():\n",
      "    if(v.type_in_union == FLOAT_TYPE):\n",
      "        print \"%f\\n\" %(v.un_float)\n",
      "    else:\n",
      "        if(v.type_in_union == CHAR_TYPE):\n",
      "            print \"%s\\n\" %(v.un_char)\n",
      "        else:\n",
      "            if(v.type_in_union == INT_TYPE):\n",
      "                print \"%d\\n\" %(v.un_int)\n",
      "            else:\n",
      "                print \"Unknown type in union\\n\"\n",
      "\n",
      "\n",
      "v.type_in_union = FLOAT_TYPE \n",
      "v.un_float = 3.5 \n",
      "print_vt() \n",
      "v.type_in_union = CHAR_TYPE \n",
      "v.un_char = 'a' \n",
      "print_vt() "
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "3.500000\n",
        "\n",
        "a\n",
        "\n"
       ]
      }
     ],
     "prompt_number": 6
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 6.13, page no. 195"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "'''\n",
      "This wont give any output.\n",
      "'''\n",
      "\n",
      "\n",
      "class Abc:\n",
      "    field1 = 4\n",
      "    a = 3\n",
      "    field2 = 1\n",
      "    igned = 0\n",
      "    field3 = 6"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 7
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 6.14, page no. 200"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "NMONTHS=12\n",
      "month = 0\n",
      "month_days =[31,28,31,30,31,30,31,31,30,31,30,31]\n",
      "mnames=[\"January\", \"February\",\"March\", \"April\",\"May\", \"June\",\"July\", \"August\",\"September\", \"October\",\"November\", \"December\"]\n",
      "\n",
      "day_count = month;\n",
      "for day_count in range(NMONTHS):\n",
      "    print \"%d days in %s\\n\" %(month_days[day_count],mnames[day_count])"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "31 days in January\n",
        "\n",
        "28 days in February\n",
        "\n",
        "31 days in March\n",
        "\n",
        "30 days in April\n",
        "\n",
        "31 days in May\n",
        "\n",
        "30 days in June\n",
        "\n",
        "31 days in July\n",
        "\n",
        "31 days in August\n",
        "\n",
        "30 days in September\n",
        "\n",
        "31 days in October\n",
        "\n",
        "30 days in November\n",
        "\n",
        "31 days in December\n",
        "\n"
       ]
      }
     ],
     "prompt_number": 8
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 6.15, page no. 201"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "class s:\n",
      "    a = 0\n",
      "    b = '0'\n",
      "    cp = '0'\n",
      "    def set(self,aa,bb,c):\n",
      "        a = aa\n",
      "        b = bb\n",
      "        cp = c\n",
      "ex_s = s()\n",
      "ex_s.set(1, 'a', \"hello\")\n",
      "\n",
      "first = ex_s\n",
      "second = s()\n",
      "second.set(2, 'b', \"byebye\")\n",
      "\n",
      "print first\n",
      "\n",
      "print second"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "<__main__.s instance at 0x7f29180251b8>\n",
        "<__main__.s instance at 0x7f29180252d8>\n"
       ]
      }
     ],
     "prompt_number": 9
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 6.16, page no. 202"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "class s:\n",
      "    a = 0\n",
      "    class ss:\n",
      "        c = 0\n",
      "        d = '0'\n",
      "\n",
      "e = s()\n",
      "e.a = 1\n",
      "e.c = 2\n",
      "e.d ='a'\n",
      "\n",
      "print e"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "<__main__.s instance at 0x7f29180253b0>\n"
       ]
      }
     ],
     "prompt_number": 10
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 6.18, page no. 202"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "y = [[1, 3, 5],[2, 4, 6],[3, 5, 7]]\n",
      "\n",
      "print y"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "[[1, 3, 5], [2, 4, 6], [3, 5, 7]]\n"
       ]
      }
     ],
     "prompt_number": 11
    }
   ],
   "metadata": {}
  }
 ]
}