summaryrefslogtreecommitdiff
path: root/A_First_course_in_Programming_with_C/Chapter13.ipynb
blob: 84f2242b60e0ad86e869624c251ac6a689fec1ce (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
{
 "metadata": {
  "name": ""
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "heading",
     "level": 1,
     "metadata": {},
     "source": [
      "Chapter 13: C Preprocessor and Command Line Arguments"
     ]
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 1, Page Number : PCL-3"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "def BIG(a,b):\n",
      "    if a > b:\n",
      "        return a\n",
      "    else:\n",
      "        return b\n",
      "    \n",
      "def WAITMSG():\n",
      "    return \"\\nPress any key to continue...\"\n",
      "    \n",
      "x = int(raw_input(\"\\nEnter two integers : \"))\n",
      "y = int(raw_input(\"\"))\n",
      "\n",
      "print \"\\nBiggest integer is \",BIG(x,y),WAITMSG()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "\n",
        "Enter two integers : 5\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "8\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "\n",
        "Biggest integer is  8 \n",
        "Press any key to continue...\n"
       ]
      }
     ],
     "prompt_number": 3
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 2, Page Number : PCL-6"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import time\n",
      "\n",
      "argv1 = \"SAMPLE.TXT\"\n",
      "argv2 = \"SAMPLE.BAK\"\n",
      "\n",
      "source_fptr = open(\"SAMPLE.TXT\",\"r\")\n",
      "target_fptr = open(\"SAMPLE.BAK\",\"w+\")\n",
      "\n",
      "if source_fptr == 0:\n",
      "    print \"\\nNo content/source file!!!\"\n",
      "    print \"\\n      press any key to continue...\"\n",
      "else:\n",
      "    print \"\\nCopying source file \",argv1,\" to target file \",argv2\n",
      "    print \"\\n  please wait.\"\n",
      "    print \".\"\n",
      "    print \".\"\n",
      "    print \".\"\n",
      "    print \".\"\n",
      "\n",
      "    if target_fptr == 0:\n",
      "        print \"\\nNo content/target file!!!\"\n",
      "        print \"\\n\\n  Press any key to continue...\"\n",
      "    else:\n",
      "        while True:\n",
      "            ch = source_fptr.read(1)\n",
      "            if not ch:\n",
      "                break\n",
      "            target_fptr.write(\"%c\"%(ch))\n",
      "        print \"\\n\\nCopy completed, the target file contents\"\n",
      "        print \"\\n----------------------------------------\\n\"\n",
      "        target_fptr.close()\n",
      "        target_fptr = open(\"SAMPLE.BAK\",\"r\")\n",
      "        ch = target_fptr.readlines()\n",
      "        for line in ch:\n",
      "            print line\n",
      "        source_fptr.close()\n",
      "        target_fptr.close()\n",
      "        print \"\\n\\n press any key to continue...\""
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "\n",
        "Copying source file  SAMPLE.TXT  to target file  SAMPLE.BAK\n",
        "\n",
        "  please wait.\n",
        ".\n",
        ".\n",
        ".\n",
        ".\n",
        "\n",
        "\n",
        "Copy completed, the target file contents\n",
        "\n",
        "----------------------------------------\n",
        "\n",
        "Computer Programming in C language is widely used for Science and Engineering applications.\n",
        "\n",
        "\n",
        " press any key to continue...\n"
       ]
      }
     ],
     "prompt_number": 2
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [],
     "language": "python",
     "metadata": {},
     "outputs": []
    }
   ],
   "metadata": {}
  }
 ]
}