diff options
author | Jovina Dsouza | 2014-06-18 12:43:07 +0530 |
---|---|---|
committer | Jovina Dsouza | 2014-06-18 12:43:07 +0530 |
commit | 206d0358703aa05d5d7315900fe1d054c2817ddc (patch) | |
tree | f2403e29f3aded0caf7a2434ea50dd507f6545e2 /C++_from_the_Ground/Chapter_22(1).ipynb | |
parent | c6f0d6aeb95beaf41e4b679e78bb42c4ffe45a40 (diff) | |
download | Python-Textbook-Companions-206d0358703aa05d5d7315900fe1d054c2817ddc.tar.gz Python-Textbook-Companions-206d0358703aa05d5d7315900fe1d054c2817ddc.tar.bz2 Python-Textbook-Companions-206d0358703aa05d5d7315900fe1d054c2817ddc.zip |
adding book
Diffstat (limited to 'C++_from_the_Ground/Chapter_22(1).ipynb')
-rw-r--r-- | C++_from_the_Ground/Chapter_22(1).ipynb | 207 |
1 files changed, 207 insertions, 0 deletions
diff --git a/C++_from_the_Ground/Chapter_22(1).ipynb b/C++_from_the_Ground/Chapter_22(1).ipynb new file mode 100644 index 00000000..83be7161 --- /dev/null +++ b/C++_from_the_Ground/Chapter_22(1).ipynb @@ -0,0 +1,207 @@ +{ + "metadata": { + "name": "Chapter 22" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": "<h1>Chapter 22: The C++ Preprocessor<h1>" + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": "<h3>Example 22.1, Page Number: 550<h3>" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''Implement a function-like macro'''\n\ndef MIN(a,b):\n if a<b:\n return a\n else:\n return b\n\n#Variable declaration\nx=10\ny=20\n\n#Result\nprint \"The minimum is\",MIN(x,y)", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "The minimum is 10\n" + } + ], + "prompt_number": 2 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": "<h3>Example 22.2, Page Number: 551<h3>" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''Implement a function-like macro'''\n#Since macros is implemented here using functions,\n#it wont give a wrong answer.\n\ndef EVEN(a):\n if a%2==0:\n return 1\n else:\n return 0\n\nif EVEN(9+1):\n print \"is even\"\nelse:\n print \"is odd\"", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "is even\n" + } + ], + "prompt_number": 3 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": "<h3>Example 22.3, Page Number: 551<h3>" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''Implement a function-like macro'''\n\ndef EVEN(a):\n if a%2==0:\n return 1\n else:\n return 0\n\nif EVEN(9+1):\n print \"is even\"\nelse:\n print \"is odd\"", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "is even\n" + } + ], + "prompt_number": 4 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": "<h3>Example 22.4, Page Number: 553<h3>" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''Impementing #if'''\n\ndef MAX():\n return 100\n\nif MAX()>10:\n print \"Extra memory required.\"\n ", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Extra memory required.\n" + } + ], + "prompt_number": 5 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": "<h3>Example 22.5, Page Number: 554<h3>" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''Impementing #if/#else'''\n\ndef MAX():\n return 6\n\nif MAX()>10:\n print \"Extra memory required.\"\nelse:\n print \"Current memory OK.\"\n ", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Current memory OK.\n" + } + ], + "prompt_number": 7 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": "<h3>Example 22.6, Page Number: 556<h3>" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''Implementation of #ifdef and #ifndef'''\n\ndef TOM():\n pass\n\n\ntry:\n TOM()\nexcept NameError:\n print \"Programmer is unknown.\"\nelse:\n print \"Programmer is Tom.\"\n \ntry:\n RALPH()\nexcept NameError:\n print \"RALPH not defined.\"\n ", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Programmer is Tom.\nRALPH not defined.\n" + } + ], + "prompt_number": 8 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": "<h3>Example 22.7, Page Number: 558<h3>" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''Implement __LINE__ '''\n\nimport inspect\n\n#Returns the current line number in our program.\ndef lineno():\n return inspect.currentframe().f_back.f_lineno\n \nprint lineno()+200\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "209\n" + } + ], + "prompt_number": 9 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": "<h3>Example 22.8, Page Number: 559<h3>" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''Implemention # operator'''\n\ndef mkstr(s):\n return str(s)\n\n#Result\nprint mkstr('I like C++')\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "I like C++\n" + } + ], + "prompt_number": 10 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": "<h3>Example 22.9, Page Number: 560<h3>" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''Implemention # operator'''\n\ndef concat(a,b):\n return a+b\n#Variable declaration\nxy=10\n\n#Result\nexec(\"print %s\")%concat('x','y')", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "10\n" + } + ], + "prompt_number": 11 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "", + "language": "python", + "metadata": {}, + "outputs": [] + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file |