summaryrefslogtreecommitdiff
path: root/C++_from_the_Ground/Chapter_18(1).ipynb
diff options
context:
space:
mode:
Diffstat (limited to 'C++_from_the_Ground/Chapter_18(1).ipynb')
-rw-r--r--C++_from_the_Ground/Chapter_18(1).ipynb600
1 files changed, 539 insertions, 61 deletions
diff --git a/C++_from_the_Ground/Chapter_18(1).ipynb b/C++_from_the_Ground/Chapter_18(1).ipynb
index 3ad3c39c..e2cae07f 100644
--- a/C++_from_the_Ground/Chapter_18(1).ipynb
+++ b/C++_from_the_Ground/Chapter_18(1).ipynb
@@ -1,6 +1,7 @@
{
"metadata": {
- "name": "Chapter 18"
+ "name": "",
+ "signature": "sha256:3dbb4b61b32533eb72607c7c790e9f8f91b9c2e123ddbd5190ffe13a1b2b7647"
},
"nbformat": 3,
"nbformat_minor": 0,
@@ -10,24 +11,51 @@
{
"cell_type": "markdown",
"metadata": {},
- "source": "<h1>Chapter 18: The C++ I/O System<h1>"
+ "source": [
+ "<h1>Chapter 18: The C++ I/O System<h1>"
+ ]
},
{
"cell_type": "markdown",
"metadata": {},
- "source": "<h3>Example 18.1, Page Number: 421<h3>"
+ "source": [
+ "<h3>Example 18.1, Page Number: 421<h3>"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "'''Implementation of overloading << for print in python'''\n\nclass 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\na=three_d(1,2,3)\nb=three_d(3,4,5)\nc=three_d(5,6,7)\n\n#Result\nprint a,b,c",
+ "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"
+ "text": [
+ "1, 2, 3\n",
+ " 3, 4, 5\n",
+ " 5, 6, 7\n",
+ "\n"
+ ]
}
],
"prompt_number": 1
@@ -35,19 +63,46 @@
{
"cell_type": "markdown",
"metadata": {},
- "source": "<h3>Example 18.2, Page Number: 423<h3>"
+ "source": [
+ "<h3>Example 18.2, Page Number: 423<h3>"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "'''Using a friend function implement overloading of << in python'''\n\nclass 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 \ndef repr():\n return str(self.x)+\", \"+str(self.y)+\", \"+str(self.z)+\"\\n\"\n\n#Variable declaration\na=three_d(1,2,3)\nb=three_d(3,4,5)\nc=three_d(5,6,7)\n\nprint a,b,c",
+ "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"
+ "text": [
+ "1, 2, 3\n",
+ " 3, 4, 5\n",
+ " 5, 6, 7\n",
+ "\n"
+ ]
}
],
"prompt_number": 2
@@ -55,19 +110,45 @@
{
"cell_type": "markdown",
"metadata": {},
- "source": "<h3>Example 18.3, Page Number: 424<h3>"
+ "source": [
+ "<h3>Example 18.3, Page Number: 424<h3>"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "'''Implementation of overloading << and >> in python'''\n\nclass 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\na=three_d(1,2,3)\n\nprint a\n\n#User input\nprint \"Enter X,Y,Z values:\"\na=three_d(4,5,6) \nprint a",
+ "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\nEnter X,Y,Z values:\n4, 5, 6\n"
+ "text": [
+ "1, 2, 3\n",
+ "Enter X,Y,Z values:\n",
+ "4, 5, 6\n"
+ ]
}
],
"prompt_number": 3
@@ -75,19 +156,34 @@
{
"cell_type": "markdown",
"metadata": {},
- "source": "<h3>Example 18.4, Page Number: 428<h3>"
+ "source": [
+ "<h3>Example 18.4, Page Number: 428<h3>"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "'''Implementing some ios flags in python'''\n\n\nprint '{0:+d}'.format(123), #for ios::showpos\nif(123.23>0):\n i='{0:e}'.format(123.23) #for ios::scientific \n i='+'+i\n print i\nelse :\n print '{0:e}'.format(123.23)\n ",
+ "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"
+ "text": [
+ "+123 +1.232300e+02\n"
+ ]
}
],
"prompt_number": 4
@@ -95,19 +191,55 @@
{
"cell_type": "markdown",
"metadata": {},
- "source": "<h3>Example 18.5, Page Number: 430<h3>"
+ "source": [
+ "<h3>Example 18.5, Page Number: 430<h3>"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "'''Implementing some ios flags in python'''\n\nimport string\n\nprint '{0:+d}'.format(123), #for ios::showpos\nif(123.23>0):\n i='{0:e}'.format(123.23) #for ios::scientific \n i='+'+i\n print i\nelse :\n print '{0:e}'.format(123.23)\n\n \nprint '{:10.2f}'.format(123.23), #2 digits left of decimal\nif(123.23>0):\n i='{0:.2e}'.format(123.23) #for ios::scientific \n i='+'+i\n print i\nelse :\n print '{0:.2e}'.format(123.23)\n \n \nprint '{:#>10}'.format(str(123)), #for ios::fill\nif(123.23>0): \n i='{0:.2e}'.format(123.23) #for ios::scientific \n i='+'+i\n print i\nelse :\n print '{0:.2e}'.format(123.23)\n\n",
+ "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"
+ "text": [
+ "+123 +1.232300e+02\n",
+ " 123.23 +1.23e+02\n",
+ "#######123 +1.23e+02\n"
+ ]
}
],
"prompt_number": 1
@@ -115,19 +247,28 @@
{
"cell_type": "markdown",
"metadata": {},
- "source": "<h3>Example 18.6, Page Number: 432<h3>"
+ "source": [
+ "<h3>Example 18.6, Page Number: 432<h3>"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "'''Implement io manipulators in python'''\n\nprint '{0:.0e}'.format(1000.243) #for setprecision\nprint '{:>20}'.format(\"Hello There\") #to set width and right align\n",
+ "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"
+ "text": [
+ "1e+03\n",
+ " Hello There\n"
+ ]
}
],
"prompt_number": 5
@@ -135,19 +276,33 @@
{
"cell_type": "markdown",
"metadata": {},
- "source": "<h3>Example 18.7, Page Number: 433<h3>"
+ "source": [
+ "<h3>Example 18.7, Page Number: 433<h3>"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "'''Implementing some ios flags in python'''\n\nprint '{0:+d}'.format(123), #for ios::showpos\nif(123.23>0):\n i='{0:e}'.format(123.23) #for ios::scientific \n i='+'+i\n print i\nelse :\n print '{0:e}'.format(123.23)\n ",
+ "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"
+ "text": [
+ "+123 +1.232300e+02\n"
+ ]
}
],
"prompt_number": 6
@@ -155,19 +310,30 @@
{
"cell_type": "markdown",
"metadata": {},
- "source": "<h3>Example 18.8, Page Number: 433<h3>"
+ "source": [
+ "<h3>Example 18.8, Page Number: 433<h3>"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "'''Removing leading whitespaces from a string'''\n\n#User input\ns=\" Hello\"\n\n#Result\nprint s.lstrip() #lstrip removes leading spaces",
+ "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"
+ "text": [
+ "Hello\n"
+ ]
}
],
"prompt_number": 7
@@ -175,19 +341,33 @@
{
"cell_type": "markdown",
"metadata": {},
- "source": "<h3>Example 18.9, Page Number: 434<h3>"
+ "source": [
+ "<h3>Example 18.9, Page Number: 434<h3>"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "'''Implementing some ios flags in python'''\n\ndef setup(s):\n return '{:$<10}'.format(str(s))\n\n\n#Result\nprint 10,setup(10)\n\n\n",
+ "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"
+ "text": [
+ "10 10$$$$$$$$\n"
+ ]
}
],
"prompt_number": 2
@@ -195,12 +375,26 @@
{
"cell_type": "markdown",
"metadata": {},
- "source": "<h3>Example 18.10, Page Number: 435<h3>"
+ "source": [
+ "<h3>Example 18.10, Page Number: 435<h3>"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "'''Prompting the user to enter number in hexadecimal form'''\n\ndef prompt():\n print \"Enter number using hex format:\"\n hex=0x46\n return hex\n\n\n#Result\ni=prompt()\nprint i\n\n",
+ "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": []
@@ -208,12 +402,28 @@
{
"cell_type": "markdown",
"metadata": {},
- "source": "<h3>Example 18.11, Page Number: 438<h3>"
+ "source": [
+ "<h3>Example 18.11, Page Number: 438<h3>"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "'''Program to write to a file'''\n\n#Open a file, creating one if it does not already exist\nout=open(\"test\",'w')\n\n#In case file cannot open\nif(not(out)):\n print \"Cannot open file.\"\nelse:\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()",
+ "input": [
+ "\n",
+ "#Open a file, creating one if it does not already exist\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",
+ " 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": [],
@@ -222,19 +432,43 @@
{
"cell_type": "markdown",
"metadata": {},
- "source": "<h3>Example 18.12, Page Number: 438<h3>"
+ "source": [
+ "<h3>Example 18.12, Page Number: 438<h3>"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "'''Program to read to a file'''\n\n#Open a file, creating one if it does not already exist\nIn=open(\"test\",'r')\n\n#In case file cannot open\nif(not(In)):\n print \"Cannot open file.\"\nelse:\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()",
+ "input": [
+ "\n",
+ "#Open a file, creating one if it does not already exist\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",
+ " 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\nThis is a short text file.\n"
+ "text": [
+ "10 123.23 \n",
+ "\n",
+ "This is a short text file.\n"
+ ]
}
],
"prompt_number": 9
@@ -242,19 +476,44 @@
{
"cell_type": "markdown",
"metadata": {},
- "source": "<h3>Example 18.13, Page Number: 439<h3>"
+ "source": [
+ "<h3>Example 18.13, Page Number: 439<h3>"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "'''Program to read to a file provided on the screen'''\n#Program cannot be provided here, so error is obtained\n\nimport sys\n \nif not(len(sys.argv)==2):\n print \"Usage: PR <filename>\\n\"\nelse:\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",
+ "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"
+ "text": [
+ "Usage: PR <filename>\n",
+ "\n"
+ ]
}
],
"prompt_number": 10
@@ -262,12 +521,29 @@
{
"cell_type": "markdown",
"metadata": {},
- "source": "<h3>Example 18.14, Page Number: 440<h3>"
+ "source": [
+ "<h3>Example 18.14, Page Number: 440<h3>"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "'''Program to write to a file'''\n\nimport sys\n\np=\"hello there\"\n\nout=open(\"test\",'w')\n\n#In case file cannot open\nif(not(out)):\n print \"Cannot open file.\"\nelse:\n #Write to file\n for i in range(len(p)):\n out.write(p[i])\n",
+ "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": [],
@@ -276,19 +552,60 @@
{
"cell_type": "markdown",
"metadata": {},
- "source": "<h3>Example 18.15, Page Number: 441<h3>"
+ "source": [
+ "<h3>Example 18.15, Page Number: 441<h3>"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "'''Program to read and write in a file'''\n\nimport sys\n\nn=[1,2,3,4,5]\n\n#Open a file 'test'\nout=open(\"test\",'w')\n\n#In case file cannot open\nif(not(out)):\n print \"Cannot open file.\"\nelse:\n #Write to file\n for i in range(5):\n out.write(chr(n[i]))\n out.close()\n \nfor i in range(5): #clear array\n n[i]=0\n \n#Open the file\nIn=open(\"test\",'r')\n\n#In case file cannot open\nif(not(In)):\n print \"Cannot open file.\"\nelse:\n #Read file\n for i in range(5):\n n[i]=ord(In.read(1))\n\n#Result, shows value from file\nfor i in range(5):\n print n[i],\n \nIn.close()",
+ "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"
+ "text": [
+ "1 2 3 4 5\n"
+ ]
}
],
"prompt_number": 12
@@ -296,19 +613,46 @@
{
"cell_type": "markdown",
"metadata": {},
- "source": "<h3>Example 18.16, Page Number: 442<h3>"
+ "source": [
+ "<h3>Example 18.16, Page Number: 442<h3>"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "'''Detect end-of-file for a file on the screen'''\n#File cannot be provided here, so program wont work\nimport sys\n \nif not(len(sys.argv)==2):\n print \"Usage: PR <filename>\\n\"\nelse:\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 ",
+ "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"
+ "text": [
+ "Usage: PR <filename>\n",
+ "\n"
+ ]
}
],
"prompt_number": 13
@@ -316,19 +660,74 @@
{
"cell_type": "markdown",
"metadata": {},
- "source": "<h3>Example 18.17, Page Number: 443<h3>"
+ "source": [
+ "<h3>Example 18.17, Page Number: 443<h3>"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "'''Program to compare files'''\n\n#Since file names cannot be provided, two \n#files are being created test1 and test2\n\n#creating test1\nout=open(\"test1\",'w')\nout.write(\"Hello\")\nout.close()\n\n#creating test2\nout=open(\"test2\",'w')\nout.write(\"There\")\nout.close()\n\n\n#Open test1\nf1=open(\"test1\",'r')\n#In case file cannot open\nif(not(In)):\n print \"Cannot open file.\"\n\n#Open test2\nf2=open(\"test2\",'r')\n#In case file cannot open\nif(not(In)):\n print \"Cannot open file.\"\n \nprint \"Comparing files...\"\n\nbuf1=f1.read()\nbuf2=f2.read()\nprint buf1,buf2\n\nif len(buf1)==len(buf2):\n print \"Files are of different sizes.\"\n f1.close()\n f2.close()\nelse:\n #compare contents of buffers\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 ",
+ "input": [
+ "\n",
+ "#creating test1\n",
+ "out=open(\"test1\",'w')\n",
+ "out.write(\"Hello\")\n",
+ "out.close()\n",
+ "\n",
+ "#creating test2\n",
+ "out=open(\"test2\",'w')\n",
+ "out.write(\"There\")\n",
+ "out.close()\n",
+ "\n",
+ "\n",
+ "#Open test1\n",
+ "f1=open(\"test1\",'r')\n",
+ "#In case file cannot open\n",
+ "if(not(In)):\n",
+ " print \"Cannot open file.\"\n",
+ "\n",
+ "#Open test2\n",
+ "f2=open(\"test2\",'r')\n",
+ "#In case file cannot open\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",
+ " #compare contents of buffers\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...\nHello There\nFiles are of different sizes.\n"
+ "text": [
+ "Comparing files...\n",
+ "Hello There\n",
+ "Files are of different sizes.\n"
+ ]
}
],
"prompt_number": 14
@@ -336,19 +735,32 @@
{
"cell_type": "markdown",
"metadata": {},
- "source": "<h3>Example 18.18, Page Number: 445<h3>"
+ "source": [
+ "<h3>Example 18.18, Page Number: 445<h3>"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "'''Read a string and print it'''\n\nprint \"Enter your name:\"\n\n#User imput\nstr=\"hello world\"\n\nprint str",
+ "input": [
+ "\n",
+ "print \"Enter your name:\"\n",
+ "\n",
+ "#User imput\n",
+ "str=\"hello world\"\n",
+ "\n",
+ "print str"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "Enter your name:\nhello world\n"
+ "text": [
+ "Enter your name:\n",
+ "hello world\n"
+ ]
}
],
"prompt_number": 15
@@ -356,12 +768,29 @@
{
"cell_type": "markdown",
"metadata": {},
- "source": "<h3>Example 18.19, Page Number: 447<h3>"
+ "source": [
+ "<h3>Example 18.19, Page Number: 447<h3>"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "'''Demonstrate random access by writing X at a specified location'''\n\n#Since arguments cannot be provided file test \n#is created and X is written in the 3nd location.\n\n#Open test\nout=open(\"test\",'r+b')\nout.write(\"Hello\")\n\n\n#In case file cannot open\nif(not(out)):\n print \"Cannot open file.\"\nelse:\n out.seek(2,0)\n out.write('X')\n out.close()\n \n",
+ "input": [
+ "\n",
+ "#Open test\n",
+ "out=open(\"test\",'r+b')\n",
+ "out.write(\"Hello\")\n",
+ "\n",
+ "\n",
+ "#In case file cannot open\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": [],
@@ -370,19 +799,39 @@
{
"cell_type": "markdown",
"metadata": {},
- "source": "<h3>Example 18.20, Page Number: 447<h3>"
+ "source": [
+ "<h3>Example 18.20, Page Number: 447<h3>"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "'''Display a file from a given point'''\n\n#Here, file is displayed from 3nd position\n\n#Open test\nout=open(\"test\",'r+b')\nout.write(\"Hello\")\nout.close()\n\nIn=open(\"test\",'r')\n#In case file cannot open\nif(not(In)):\n print \"Cannot open file.\"\nelse:\n In.seek(2,0)\n ch=In.read()\n print ch\n \n",
+ "input": [
+ "\n",
+ "#Open test\n",
+ "out=open(\"test\",'r+b')\n",
+ "out.write(\"Hello\")\n",
+ "out.close()\n",
+ "\n",
+ "In=open(\"test\",'r')\n",
+ "#In case file cannot open\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"
+ "text": [
+ "llo\n"
+ ]
}
],
"prompt_number": 3
@@ -390,12 +839,41 @@
{
"cell_type": "markdown",
"metadata": {},
- "source": "<h3>Example 18.21, Page Number: 450<h3>"
+ "source": [
+ "<h3>Example 18.21, Page Number: 450<h3>"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "'''Implement use of overloaded inserter to write three_d'''\n\nclass 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#Variable declaration\na=three_d(1,2,3)\nb=three_d(3,4,5)\nc=three_d(5,6,7)\n\n#Open threed\nout=open(\"threed\",'w')\n#In case file cannot open\nif(not(out)):\n print \"Cannot open file.\"\nelse:\n out.write(a.__repr__())\n out.write(b.__repr__())\n out.write(c.__repr__())\n\n ",
+ "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",
+ "#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",
+ "#Open threed\n",
+ "out=open(\"threed\",'w')\n",
+ "#In case file cannot open\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": [],
@@ -404,7 +882,7 @@
{
"cell_type": "raw",
"metadata": {},
- "source": ""
+ "source": []
}
],
"metadata": {}