{ "metadata": { "name": "", "signature": "sha256:95f715496e847b4ccbd328c0da6129f101af5cd4b3c551a2f5644703db14657d" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Hour 23 : Compiling Programs: The C Preprocessor" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 23.1, Page No 394" ] }, { "cell_type": "code", "collapsed": false, "input": [ "METHOD = \"ABS\"\n", "def ABS(val):\n", " if val < 0:\n", " return -(val)\n", " else:\n", " return (val)\n", "MAX_LEN = 8\n", "NEGATIVE_NUM = -10\n", "array = range(MAX_LEN)\n", "print \"The orignal values in array:\"\n", "for i in range(MAX_LEN):\n", " array[i] = (i + 1) * NEGATIVE_NUM;\n", " print \"array[\",i,\"]:\",array[i]\n", "print \"\\nApplying the \",METHOD,\" macro:\\n\"\n", "for i in range(MAX_LEN):\n", " print \"ABS(%d) : \"%(array[i]), ABS(array[i])" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "The orignal values in array:\n", "array[ 0 ]: -10\n", "array[ 1 ]: -20\n", "array[ 2 ]: -30\n", "array[ 3 ]: -40\n", "array[ 4 ]: -50\n", "array[ 5 ]: -60\n", "array[ 6 ]: -70\n", "array[ 7 ]: -80\n", "\n", "Applying the ABS macro:\n", "\n", "ABS(-10) : 10\n", "ABS(-20) : 20\n", "ABS(-30) : 30\n", "ABS(-40) : 40\n", "ABS(-50) : 50\n", "ABS(-60) : 60\n", "ABS(-70) : 70\n", "ABS(-80) : 80\n" ] } ], "prompt_number": 9 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 23.2, Page No 398" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#There is no concept of the mentioned directives in Python.\n", "UPPER_CASE = True\n", "LOWER_CASE = False\n", "if UPPER_CASE:\n", " print \"THIS LINE IS PRINTED OUT,\\n\"\n", " print \"BECAUSE UPPER_CASE IS DEFINED.\\n\"\n", "if not LOWER_CASE:\n", " print \"\\nThis line is printed out,\\n\"\n", " print \"because LOWER_CASE is not defined.\\n\"" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "THIS LINE IS PRINTED OUT,\n", "\n", "BECAUSE UPPER_CASE IS DEFINED.\n", "\n", "\n", "This line is printed out,\n", "\n", "because LOWER_CASE is not defined.\n", "\n" ] } ], "prompt_number": 13 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 23.3, Page No 401" ] }, { "cell_type": "code", "collapsed": false, "input": [ "C_LANG = 'C'\n", "B_LANG = 'B'\n", "NO_ERROR = 0\n", "if C_LANG == 'C' and B_LANG == 'B':\n", " del C_LANG\n", " C_LANG = \"I only know C language.\\n\"\n", " print C_LANG\n", " del B_LANG\n", " B_LANG = \"I know BASIC.\\n\"\n", " print C_LANG,B_LANG\n", "elif C_LANG == 'C':\n", " del C_LANG\n", " C_LANG = \"I only know C language.\\n\"\n", " print C_LANG\n", "elif B_LANG == 'B':\n", " del B_LANG\n", " B_LANG = \"I only know BASIC.\\n\"\n", " print B_LANG\n", "else:\n", " print \"\u201cI don\u2019t know C or BASIC.\\n\"" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "I only know C language.\n", "\n", "I only know C language.\n", "I know BASIC.\n", "\n" ] } ], "prompt_number": 5 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 23.4, Page No 403" ] }, { "cell_type": "code", "collapsed": false, "input": [ "ZERO = 0\n", "ONE = 1\n", "TWO = ONE + ONE\n", "THREE = ONE + TWO\n", "TEST_1 = ONE\n", "TEST_2 = TWO\n", "TEST_3 = THREE\n", "MAX_NUM = THREE\n", "NO_ERROR = ZERO\n", "def StrPrint(ptr_s,max1):\n", " for i in range(max1):\n", " print \"Content: \",ptr_s[i],\"\\n\"\n", "\n", "str1 = [\"The choice of a point of view\",\"is the initial act of culture.\",\"--- by O. Gasset\"]\n", "if TEST_1 == 1:\n", " if TEST_2 == 2:\n", " if TEST_3 == 3:\n", " StrPrint(str1,MAX_NUM)\n", " else:\n", " StrPrint(str1,MAX_NUM - ONE)\n", " else:\n", " StrPrint(str1,MAX_NUM - ONE)\n", "else:\n", " \"No TEST macro has been set.\\n\"" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Content: The choice of a point of view \n", "\n", "Content: is the initial act of culture. \n", "\n", "Content: --- by O. Gasset \n", "\n" ] } ], "prompt_number": 9 } ], "metadata": {} } ] }