{ "metadata": { "name": "", "signature": "sha256:c550564369c57fef1a3abbaae1e7e03e923c7091530a87d2bf35262354fa8d51" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "CHAPTER 7:THE C PREPROCESSOR" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "EXAMPLE ON PAGE:226" ] }, { "cell_type": "code", "collapsed": false, "input": [ " \n", "def UPPER(): #set UPPER() as 25\n", " return 25\n", "for i in range(1,UPPER()+1,1):\n", " print \"%d\\n\" % (i)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "1\n", "\n", "2\n", "\n", "3\n", "\n", "4\n", "\n", "5\n", "\n", "6\n", "\n", "7\n", "\n", "8\n", "\n", "9\n", "\n", "10\n", "\n", "11\n", "\n", "12\n", "\n", "13\n", "\n", "14\n", "\n", "15\n", "\n", "16\n", "\n", "17\n", "\n", "18\n", "\n", "19\n", "\n", "20\n", "\n", "21\n", "\n", "22\n", "\n", "23\n", "\n", "24\n", "\n", "25\n", "\n" ] } ], "prompt_number": 1 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "EXAMPLE ON PAGE:227" ] }, { "cell_type": "code", "collapsed": false, "input": [ " \n", "def PI(): #set PI() as 3.14\n", " return 3.14\n", "r=6.25\n", "area=PI()*r*r\n", "print \"Area of circle=%f\\n\" % (area)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Area of circle=122.656250\n", "\n" ] } ], "prompt_number": 2 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "EXAMPLE ON PAGE:229" ] }, { "cell_type": "code", "collapsed": false, "input": [ " \n", "def AND(x,y): #set AND(x,y) for \"and\" operation\n", " return (x and y)\n", "def OR(a,b): #set OR(a,b) for \"or\" operation\n", " return (a or b)\n", "f=1\n", "x=4\n", "y=90\n", "if (AND(f<5,OR(x<=20,y<=45))):\n", " print \"Your PC will always work fine...\\n\"\n", "else:\n", " print \"In front of the maintenance man\\n\"" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Your PC will always work fine...\n", "\n" ] } ], "prompt_number": 3 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "EXAMPLE ON PAGE:229" ] }, { "cell_type": "code", "collapsed": false, "input": [ " \n", "def AND(x,y): #set AND(x,y) for \"and\" operation\n", " return (x and y)\n", "def ARANGE():\n", " return (AND(a>25,a<50)) #check for a>25 and a<50\n", "a=30\n", "if (ARANGE()):\n", " print \"within range\\n\"\n", "else:\n", " print \"out of range\\n\"" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "within range\n", "\n" ] } ], "prompt_number": 4 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "EXAMPLE ON PAGE:230" ] }, { "cell_type": "code", "collapsed": false, "input": [ " \n", "def FOUND():\n", " print \"The Yankee Doodle Virus\\n\"\n", "signature='Y'\n", "if (signature=='Y'):\n", " FOUND()\n", "else:\n", " print \"Safe... as yet!\\n\"" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "The Yankee Doodle Virus\n", "\n" ] } ], "prompt_number": 5 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "EXAMPLE ON PAGE:230" ] }, { "cell_type": "code", "collapsed": false, "input": [ " \n", "def AREA(x):\n", " return (3.14*x*x)\n", "r1=6.25\n", "r2=2.5\n", "a=AREA(r1)\n", "print \"Area of circle=%f\\n\" % (a)\n", "a=AREA(r2)\n", "print \"Area of circle=%f\\n\" % (a)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Area of circle=122.656250\n", "\n", "Area of circle=19.625000\n", "\n" ] } ], "prompt_number": 6 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "EXAMPLE ON PAGE:231" ] }, { "cell_type": "code", "collapsed": false, "input": [ " \n", "r1=6.25\n", "r2=2.5\n", "a=3.14*r1*r1\n", "print \"Area of circle=%f\\n\" % (a)\n", "a=3.14*r2*r2\n", "print \"Area of circle=%f\\n\" % (a)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Area of circle=122.656250\n", "\n", "Area of circle=19.625000\n", "\n" ] } ], "prompt_number": 7 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "EXAMPLE ON PAGE:231" ] }, { "cell_type": "code", "collapsed": false, "input": [ " \n", "def ISDIGIT(y):\n", " return (y>=48 and y<=57)\n", "print \"Enter any digit\"\n", "ch=raw_input()\n", "if (ISDIGIT(ord(ch))):\n", " print \"You entered a digit\\n\"\n", "else:\n", " print \"Illegal input\\n\"" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter any digit\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "7\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "You entered a digit\n", "\n" ] } ], "prompt_number": 8 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "EXAMPLE ON PAGE:232" ] }, { "cell_type": "code", "collapsed": false, "input": [ " \n", "def SQUARE(n):\n", " return (n*n)\n", "j=64/SQUARE(4) #it's function not macros ,so j=4\n", "print \"j=%d\\n\" % (j)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "j=4\n", "\n" ] } ], "prompt_number": 4 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "EXAMPLE ON PAGE:233" ] }, { "cell_type": "code", "collapsed": false, "input": [ " \n", "def HLINE():\n", " for i in range(0,79,1):\n", " print \"%c\" % 45, #characters are bytes and therefore encoding-dependent(non utf-8)\n", " print \"\\n\"\n", "def VLINE(X,Y):\n", " print \"%c\" % 124 #If you want to write 179 in place of 124 then use utf-8 encoding\n", "HLINE()\n", "for y in range(1,25,1):\n", " VLINE(39,y)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \n", "\n", "|\n", "|\n", "|\n", "|\n", "|\n", "|\n", "|\n", "|\n", "|\n", "|\n", "|\n", "|\n", "|\n", "|\n", "|\n", "|\n", "|\n", "|\n", "|\n", "|\n", "|\n", "|\n", "|\n", "|\n" ] } ], "prompt_number": 3 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "EXAMPLE ON PAGE:236-237" ] }, { "cell_type": "code", "collapsed": false, "input": [ " \n", "OKAY=False\n", "if OKAY:\n", " import logging\n", " print \"statement 1\"\n", " print \"statement 2\" #detects virus\n", " print \"statement 3\" \n", " print \"statement 4\" #specific to stone virus\n", "print \"statement 5\"\n", "print \"statement 6\"\n", "print \"statement 7\"" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "statement 5\n", "statement 6\n", "statement 7\n" ] } ], "prompt_number": 5 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "EXAMPLE ON PAGE:237" ] }, { "cell_type": "code", "collapsed": false, "input": [ " \n", "INTEL=False\n", "if INTEL:\n", " print \"code suitable for an Intel PC\"\n", "else:\n", " print \"code suitable for a Motorola PC\"\n", "print \"code common to both the computers\"" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "code suitable for a Motorola PC\n", "code common to both the computers\n" ] } ], "prompt_number": 14 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "EXAMPLE ON PAGE:238" ] }, { "cell_type": "code", "collapsed": false, "input": [ " \n", "INTEL=True\n", "if INTEL:\n", " print \"code suitable for a Intel PC\"\n", "else:\n", " print \"code suitable for a motorola PC\"\n", "print \"code common to both the computers\"" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "code suitable for a Intel PC\n", "code common to both the computers\n" ] } ], "prompt_number": 15 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "EXAMPLE ON PAGE:239" ] }, { "cell_type": "code", "collapsed": false, "input": [ " \n", "TEST=6\n", "if TEST<=5:\n", " print \"statement 1\"\n", " print \"statement 2\"\n", " print \"statement 3\"\n", "else:\n", " print \"statement 4\"\n", " print \"statement 5\"\n", " print \"statement 6\"" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "statement 4\n", "statement 5\n", "statement 6\n" ] } ], "prompt_number": 16 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "EXAMPLE ON PAGE:239" ] }, { "cell_type": "code", "collapsed": false, "input": [ " \n", "ADAPTER='SVGA'\n", "if ADAPTER=='VGA':\n", " print \"code for video graphics array\"\n", "else:\n", " if ADAPTER=='SVGA':\n", " print \"code for super video graphics array\"\n", " else:\n", " print \"code for extended graphics array\"" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "code for super video graphics array\n" ] } ], "prompt_number": 17 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "EXAMPLE ON PAGE:239-240" ] }, { "cell_type": "code", "collapsed": false, "input": [ " \n", "ADAPTER='SVGA'\n", "if ADAPTER=='VGA':\n", " print \"code for video graphics array\"\n", "elif ADAPTER=='SVGA':\n", " print \"code for super video graphics array\"\n", "else:\n", " print \"code for extented graphics adapter\"" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "code for super video graphics array\n" ] } ], "prompt_number": 18 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "EXAMPLE ON PAGE:241" ] }, { "cell_type": "code", "collapsed": false, "input": [ " \n", "def fun1():\n", " print \"Inside fun1\\n\"\n", "def fun2():\n", " print \"Inside fun 2\\n\"\n", "def main():\n", " print \"Inside main\\n\"\n", "fun1()\n", "main()\n", "fun2()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Inside fun1\n", "\n", "Inside main\n", "\n", "Inside fun 2\n", "\n" ] } ], "prompt_number": 19 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "EXAMPLE ON PAGE:242" ] }, { "cell_type": "code", "collapsed": false, "input": [ " \n", "def f1():\n", " a=5\n", "def f2(x):\n", " print \"Inside f2\" #passed parameter is not being used\n", "def f3():\n", " x=6\n", " return x\n", " x+=1 #control can never reach this statement\n", "f1()\n", "f2(7)\n", "f3()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Inside f2\n" ] }, { "metadata": {}, "output_type": "pyout", "prompt_number": 20, "text": [ "6" ] } ], "prompt_number": 20 } ], "metadata": {} } ] }