diff options
Diffstat (limited to 'C++_from_the_Ground/Chapter_18(1).ipynb')
-rwxr-xr-x | C++_from_the_Ground/Chapter_18(1).ipynb | 881 |
1 files changed, 0 insertions, 881 deletions
diff --git a/C++_from_the_Ground/Chapter_18(1).ipynb b/C++_from_the_Ground/Chapter_18(1).ipynb deleted file mode 100755 index df4cabc2..00000000 --- a/C++_from_the_Ground/Chapter_18(1).ipynb +++ /dev/null @@ -1,881 +0,0 @@ -{ - "metadata": { - "name": "", - "signature": "sha256:85eae8322c208f1f26e859bb0f4dac6f86b35c3ac105ed98ac2d0fbf05287f0e" - }, - "nbformat": 3, - "nbformat_minor": 0, - "worksheets": [ - { - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h1>Chapter 18: The C++ I/O System<h1>" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 18.1, Page Number: 421<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "class three_d:\n", - " def __init__(self,a,b,c): #3D coordinates\n", - " self.x=a\n", - " self.y=b\n", - " self.z=c\n", - " #Display x,y,z coordinates - three_d inserter.\n", - " def __repr__(self):\n", - " return str(self.x)+\", \"+str(self.y)+\", \"+str(self.z)+\"\\n\"\n", - "\n", - "#Variable declaration\n", - "a=three_d(1,2,3)\n", - "b=three_d(3,4,5)\n", - "c=three_d(5,6,7)\n", - "\n", - "#Result\n", - "print a,b,c" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "1, 2, 3\n", - " 3, 4, 5\n", - " 5, 6, 7\n", - "\n" - ] - } - ], - "prompt_number": 1 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 18.2, Page Number: 423<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "class thrnee_d:\n", - " def __init__(self,a,b,c): #3D coordinates\n", - " self.x=a\n", - " self.y=b\n", - " self.z=c\n", - " #Display x,y,z coordinates - three_d inserter.\n", - " __repr__=repr \n", - " \n", - "#Friend function \n", - "def repr():\n", - " return str(self.x)+\", \"+str(self.y)+\", \"+str(self.z)+\"\\n\"\n", - "\n", - "#Variable declaration\n", - "a=three_d(1,2,3)\n", - "b=three_d(3,4,5)\n", - "c=three_d(5,6,7)\n", - "\n", - "print a,b,c" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "1, 2, 3\n", - " 3, 4, 5\n", - " 5, 6, 7\n", - "\n" - ] - } - ], - "prompt_number": 2 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 18.3, Page Number: 424<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "class three_d:\n", - " def __init__(self,a,b,c): #3D coordinates\n", - " self.x=a\n", - " self.y=b\n", - " self.z=c\n", - " #Display x,y,z coordinates - three_d inserter.\n", - " def __repr__(self):\n", - " return str(self.x)+\", \"+str(self.y)+\", \"+str(self.z)\n", - "\n", - "#Variable declaration\n", - "a=three_d(1,2,3)\n", - "\n", - "print a\n", - "\n", - "#User input\n", - "print \"Enter X,Y,Z values:\"\n", - "a=three_d(4,5,6) \n", - "print a" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "1, 2, 3\n", - "Enter X,Y,Z values:\n", - "4, 5, 6\n" - ] - } - ], - "prompt_number": 3 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 18.4, Page Number: 428<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "\n", - "print '{0:+d}'.format(123), #for ios::showpos\n", - "if(123.23>0):\n", - " i='{0:e}'.format(123.23) #for ios::scientific \n", - " i='+'+i\n", - " print i\n", - "else :\n", - " print '{0:e}'.format(123.23)\n", - " " - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "+123 +1.232300e+02\n" - ] - } - ], - "prompt_number": 4 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 18.5, Page Number: 430<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "import string\n", - "\n", - "print '{0:+d}'.format(123), #for ios::showpos\n", - "if(123.23>0):\n", - " i='{0:e}'.format(123.23) #for ios::scientific \n", - " i='+'+i\n", - " print i\n", - "else :\n", - " print '{0:e}'.format(123.23)\n", - "\n", - " \n", - "print '{:10.2f}'.format(123.23), #2 digits left of decimal\n", - "if(123.23>0):\n", - " i='{0:.2e}'.format(123.23) #for ios::scientific \n", - " i='+'+i\n", - " print i\n", - "else :\n", - " print '{0:.2e}'.format(123.23)\n", - " \n", - " \n", - "print '{:#>10}'.format(str(123)), #for ios::fill\n", - "if(123.23>0): \n", - " i='{0:.2e}'.format(123.23) #for ios::scientific \n", - " i='+'+i\n", - " print i\n", - "else :\n", - " print '{0:.2e}'.format(123.23)\n", - "\n" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "+123 +1.232300e+02\n", - " 123.23 +1.23e+02\n", - "#######123 +1.23e+02\n" - ] - } - ], - "prompt_number": 1 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 18.6, Page Number: 432<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "print '{0:.0e}'.format(1000.243) #for setprecision\n", - "print '{:>20}'.format(\"Hello There\") #to set width and right align\n" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "1e+03\n", - " Hello There\n" - ] - } - ], - "prompt_number": 5 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 18.7, Page Number: 433<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "print '{0:+d}'.format(123), #for ios::showpos\n", - "if(123.23>0):\n", - " i='{0:e}'.format(123.23) #for ios::scientific \n", - " i='+'+i\n", - " print i\n", - "else :\n", - " print '{0:e}'.format(123.23)\n", - " " - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "+123 +1.232300e+02\n" - ] - } - ], - "prompt_number": 6 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 18.8, Page Number: 433<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "#User input\n", - "s=\" Hello\"\n", - "\n", - "#Result\n", - "print s.lstrip() #lstrip removes leading spaces" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Hello\n" - ] - } - ], - "prompt_number": 7 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 18.9, Page Number: 434<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "def setup(s):\n", - " return '{:$<10}'.format(str(s))\n", - "\n", - "\n", - "#Result\n", - "print 10,setup(10)\n", - "\n", - "\n" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "10 10$$$$$$$$\n" - ] - } - ], - "prompt_number": 2 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 18.10, Page Number: 435<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "def prompt():\n", - " print \"Enter number using hex format:\"\n", - " hex=0x46\n", - " return hex\n", - "\n", - "\n", - "#Result\n", - "i=prompt()\n", - "print i\n", - "\n" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 18.11, Page Number: 438<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - " \n", - "out=open(\"test\",'w')\n", - "\n", - "\n", - "if(not(out)):\n", - " print \"Cannot open file.\"\n", - "else:\n", - " #Write to file\n", - " out.write(\"10 123.23\\n\")\n", - " out.write(\"This is a short text file.\")\n", - " #Close the file\n", - " out.close()" - ], - "language": "python", - "metadata": {}, - "outputs": [], - "prompt_number": 8 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 18.12, Page Number: 438<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - " \n", - "In=open(\"test\",'r')\n", - " \n", - "if(not(In)):\n", - " print \"Cannot open file.\"\n", - "else:\n", - " #Read file\n", - " i=In.read(2)\n", - " ch=In.read(1)\n", - " f=In.read(6)\n", - " str=In.read()\n", - " print i,f,ch\n", - " print str\n", - " #Close the file\n", - " out.close()" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "10 123.23 \n", - "\n", - "This is a short text file.\n" - ] - } - ], - "prompt_number": 9 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 18.13, Page Number: 439<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "\n", - "import sys\n", - " \n", - "if not(len(sys.argv)==2):\n", - " print \"Usage: PR <filename>\\n\"\n", - "else:\n", - " #Open a file\n", - " In=open(sys.argv[1],'r')\n", - "\n", - " #In case file cannot open\n", - " if(not(In)):\n", - " print \"Cannot open file.\"\n", - " else:\n", - " #Read file\n", - " ch=In.read()\n", - " print ch\n", - " In.close()\n", - " \n" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Usage: PR <filename>\n", - "\n" - ] - } - ], - "prompt_number": 10 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 18.14, Page Number: 440<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "import sys\n", - "\n", - "p=\"hello there\"\n", - "\n", - "out=open(\"test\",'w')\n", - "\n", - "#In case file cannot open\n", - "if(not(out)):\n", - " print \"Cannot open file.\"\n", - "else:\n", - " #Write to file\n", - " for i in range(len(p)):\n", - " out.write(p[i])\n" - ], - "language": "python", - "metadata": {}, - "outputs": [], - "prompt_number": 11 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 18.15, Page Number: 441<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "import sys\n", - "\n", - "n=[1,2,3,4,5]\n", - "\n", - "#Open a file 'test'\n", - "out=open(\"test\",'w')\n", - "\n", - "#In case file cannot open\n", - "if(not(out)):\n", - " print \"Cannot open file.\"\n", - "else:\n", - " #Write to file\n", - " for i in range(5):\n", - " out.write(chr(n[i]))\n", - " out.close()\n", - " \n", - "for i in range(5): #clear array\n", - " n[i]=0\n", - " \n", - "#Open the file\n", - "In=open(\"test\",'r')\n", - "\n", - "#In case file cannot open\n", - "if(not(In)):\n", - " print \"Cannot open file.\"\n", - "else:\n", - " #Read file\n", - " for i in range(5):\n", - " n[i]=ord(In.read(1))\n", - "\n", - "#Result, shows value from file\n", - "for i in range(5):\n", - " print n[i],\n", - " \n", - "In.close()" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "1 2 3 4 5\n" - ] - } - ], - "prompt_number": 12 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 18.16, Page Number: 442<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "import sys\n", - " \n", - "if not(len(sys.argv)==2):\n", - " print \"Usage: PR <filename>\\n\"\n", - "else:\n", - " #Open a file\n", - " In=open(sys.argv[1],'r')\n", - "\n", - " #In case file cannot open\n", - " if(not(In)):\n", - " print \"Cannot open file.\"\n", - " else:\n", - " #Read file\n", - " while True:\n", - " ch=In.read(1)\n", - " if not ch:\n", - " break\n", - " print ch,\n", - " #Close file\n", - " In.close()\n", - " " - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Usage: PR <filename>\n", - "\n" - ] - } - ], - "prompt_number": 13 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 18.17, Page Number: 443<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - " \n", - "out=open(\"test1\",'w')\n", - "out.write(\"Hello\")\n", - "out.close()\n", - " \n", - "out=open(\"test2\",'w')\n", - "out.write(\"There\")\n", - "out.close()\n", - "\n", - " \n", - "f1=open(\"test1\",'r')\n", - " \n", - "if(not(In)):\n", - " print \"Cannot open file.\"\n", - "\n", - " \n", - "f2=open(\"test2\",'r')\n", - " \n", - "if(not(In)):\n", - " print \"Cannot open file.\"\n", - " \n", - "print \"Comparing files...\"\n", - "\n", - "buf1=f1.read()\n", - "buf2=f2.read()\n", - "print buf1,buf2\n", - "\n", - "if len(buf1)==len(buf2):\n", - " print \"Files are of different sizes.\"\n", - " f1.close()\n", - " f2.close()\n", - "else:\n", - " \n", - " flag=1\n", - " for i in range(len(buf1)):\n", - " if not(buf1[i]==buf2[i]):\n", - " print \"Files differ.\"\n", - " f1.close()\n", - " f2.close()\n", - " flag=0\n", - " break\n", - " if flag==1:\n", - " print \"Files are the same.\"\n", - " \n", - " " - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Comparing files...\n", - "Hello There\n", - "Files are of different sizes.\n" - ] - } - ], - "prompt_number": 14 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 18.18, Page Number: 445<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "print \"Enter your name:\"\n", - " \n", - "str=\"hello world\"\n", - "\n", - "print str" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter your name:\n", - "hello world\n" - ] - } - ], - "prompt_number": 15 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 18.19, Page Number: 447<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - " out=open(\"test\",'r+b')\n", - "out.write(\"Hello\")\n", - "\n", - " \n", - "if(not(out)):\n", - " print \"Cannot open file.\"\n", - "else:\n", - " out.seek(2,0)\n", - " out.write('X')\n", - " out.close()\n", - " \n" - ], - "language": "python", - "metadata": {}, - "outputs": [], - "prompt_number": 2 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 18.20, Page Number: 447<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - " \n", - "out=open(\"test\",'r+b')\n", - "out.write(\"Hello\")\n", - "out.close()\n", - "\n", - "In=open(\"test\",'r')\n", - " \n", - "if(not(In)):\n", - " print \"Cannot open file.\"\n", - "else:\n", - " In.seek(2,0)\n", - " ch=In.read()\n", - " print ch\n", - " \n" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "llo\n" - ] - } - ], - "prompt_number": 3 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 18.21, Page Number: 450<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "class three_d:\n", - " def __init__(self,a,b,c):\n", - " self.__x=a\n", - " self.__y=b\n", - " self.__z=c\n", - " def __repr__(self):\n", - " c=(\"%d\"%self.__x)+\", \"+(\"%d\"%self.__y)+\", \"+(\"%d\"%self.__z)+\"\\n\"\n", - " return c \n", - "\n", - " \n", - "a=three_d(1,2,3)\n", - "b=three_d(3,4,5)\n", - "c=three_d(5,6,7)\n", - " \n", - "out=open(\"threed\",'w')\n", - " \n", - "if(not(out)):\n", - " print \"Cannot open file.\"\n", - "else:\n", - " out.write(a.__repr__())\n", - " out.write(b.__repr__())\n", - " out.write(c.__repr__())\n", - "\n", - " " - ], - "language": "python", - "metadata": {}, - "outputs": [], - "prompt_number": 18 - }, - { - "cell_type": "raw", - "metadata": {}, - "source": [] - } - ], - "metadata": {} - } - ] -}
\ No newline at end of file |