summaryrefslogtreecommitdiff
path: root/C++_from_the_Ground/Chapter_10.ipynb
diff options
context:
space:
mode:
authorJovina Dsouza2014-07-08 17:19:20 +0530
committerJovina Dsouza2014-07-08 17:19:20 +0530
commit84eaa5fbfb3090e912ebe12de1906b7c0bdde908 (patch)
treef98362fb5e4e43848c150834c3937dc4bde1e176 /C++_from_the_Ground/Chapter_10.ipynb
parent80751050da776de062000a7d2a5b4e045bfbc9f8 (diff)
downloadPython-Textbook-Companions-84eaa5fbfb3090e912ebe12de1906b7c0bdde908.tar.gz
Python-Textbook-Companions-84eaa5fbfb3090e912ebe12de1906b7c0bdde908.tar.bz2
Python-Textbook-Companions-84eaa5fbfb3090e912ebe12de1906b7c0bdde908.zip
adding book
Diffstat (limited to 'C++_from_the_Ground/Chapter_10.ipynb')
-rw-r--r--C++_from_the_Ground/Chapter_10.ipynb560
1 files changed, 560 insertions, 0 deletions
diff --git a/C++_from_the_Ground/Chapter_10.ipynb b/C++_from_the_Ground/Chapter_10.ipynb
new file mode 100644
index 00000000..b19c3a4c
--- /dev/null
+++ b/C++_from_the_Ground/Chapter_10.ipynb
@@ -0,0 +1,560 @@
+{
+ "metadata": {
+ "name": "",
+ "signature": "sha256:73c04e2c14bfab695e4acf07dc7752334b6372d624570a4e051fd91aeeb761f7"
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h1>Chapter 10: Structures and Unions<h1>"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 10.1, Page Number: 223<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "class inv_type:\n",
+ " def __init__(self):\n",
+ " self.item=None\n",
+ " self.cost=0\n",
+ " self.retail=0\n",
+ " self.on_hand=0\n",
+ " self.lead_time=0\n",
+ "\n",
+ "#Variable declaration\n",
+ "size=100 \n",
+ "invtry = []*size\n",
+ "i=5 #User iput for menu selection\n",
+ "\n",
+ "#Initialize the array\n",
+ "def init_list():\n",
+ " for t in range(size):\n",
+ " invtry.append(inv_type())\n",
+ " \n",
+ "#get a menu selection\n",
+ "def menu():\n",
+ " global i\n",
+ " print \"(E)nter\"\n",
+ " print \"(D)isplay\"\n",
+ " print \"(U)pdate\"\n",
+ " print \"(Q)uit\"\n",
+ " print \"choose one: \"\n",
+ " i-=1\n",
+ " return i\n",
+ "\n",
+ "#enter items into the list\n",
+ "def enter():\n",
+ " #find the first free structure\n",
+ " for i in range(size):\n",
+ " if not(invtry[i].item==None):\n",
+ " break\n",
+ " #i will be size if list is full\n",
+ " if i==size:\n",
+ " print \"List full.\"\n",
+ " return\n",
+ " input(i)\n",
+ " \n",
+ "#Input the information\n",
+ "def input(i):\n",
+ " #Enter information; User input\n",
+ " invtry[i].item=\"Gloves\"\n",
+ " invtry[i].cost=10\n",
+ " invtry[i].retail=25\n",
+ " invtry[i].on_hand=50\n",
+ " invtry[i].lead_time=10\n",
+ " \n",
+ "#Modify an existing item\n",
+ "def update():\n",
+ " name=\"Gloves\" #User input\n",
+ " for i in range(size):\n",
+ " if not(name==invtry[i].item):\n",
+ " break\n",
+ " if i==size:\n",
+ " print \"Item not found.\"\n",
+ " return\n",
+ " print \"Enter new information.\"\n",
+ " input(i)\n",
+ " \n",
+ "#Display the list\n",
+ "def display():\n",
+ " for t in range(size):\n",
+ " if not(invtry[t].item==None):\n",
+ " print invtry[t].item\n",
+ " print \"Cost: $\",invtry[t].cost\n",
+ " print \"Retail: $\",invtry[t].retail\n",
+ " print \"On hand: \",invtry[t].on_hand\n",
+ " print \"Resupply time: \",invtry[t].lead_time,\" days\"\n",
+ " \n",
+ "\n",
+ "init_list()\n",
+ "while True:\n",
+ " choice=menu()\n",
+ " if choice==4:\n",
+ " enter()\n",
+ " elif choice==3:\n",
+ " display()\n",
+ " elif choice==2:\n",
+ " update()\n",
+ " elif choice==1:\n",
+ " break"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "(E)nter\n",
+ "(D)isplay\n",
+ "(U)pdate\n",
+ "(Q)uit\n",
+ "choose one: \n",
+ "(E)nter\n",
+ "(D)isplay\n",
+ "(U)pdate\n",
+ "(Q)uit\n",
+ "choose one: \n",
+ "Gloves\n",
+ "Cost: $ 10\n",
+ "Retail: $ 25\n",
+ "On hand: 50\n",
+ "Resupply time: 10 days\n",
+ "(E)nter\n",
+ "(D)isplay\n",
+ "(U)pdate\n",
+ "(Q)uit\n",
+ "choose one: \n",
+ "Enter new information.\n",
+ "(E)nter\n",
+ "(D)isplay\n",
+ "(U)pdate\n",
+ "(Q)uit\n",
+ "choose one: \n"
+ ]
+ }
+ ],
+ "prompt_number": 11
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 10.2, Page Number: 226<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "class sample:\n",
+ " a=None\n",
+ " ch=None\n",
+ " \n",
+ "def f1(parm):\n",
+ " print parm.a,\" \",parm.ch\n",
+ "\n",
+ "#declare arg\n",
+ "arg=sample() \n",
+ "\n",
+ "#initialize arg\n",
+ "arg.a=1000\n",
+ "arg.ch='X'\n",
+ "\n",
+ "#call function\n",
+ "f1(arg)\n",
+ " "
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "1000 X\n"
+ ]
+ }
+ ],
+ "prompt_number": 12
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 10.3, Page Number: 227<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "class stype:\n",
+ " a=None\n",
+ " b=None\n",
+ "\n",
+ "#Variable declaration\n",
+ "svar1=stype()\n",
+ "svar2=stype()\n",
+ "\n",
+ "svar1.a=svar1.b=10\n",
+ "svar2.a=svar2.b=20\n",
+ "\n",
+ "print \"Structures before assignment.\"\n",
+ "print \"svar1: \",svar1.a,' ',svar1.b\n",
+ "print \"svar1: \",svar2.a,' ',svar2.b\n",
+ "\n",
+ "svar2=svar1 #assign structures\n",
+ "\n",
+ "#Result\n",
+ "print \"\\nStructures before assignment.\"\n",
+ "print \"svar1: \",svar1.a,' ',svar1.b\n",
+ "print \"svar1: \",svar2.a,' ',svar2.b"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Structures before assignment.\n",
+ "svar1: 10 10\n",
+ "svar1: 20 20\n",
+ "\n",
+ "Structures before assignment.\n",
+ "svar1: 10 10\n",
+ "svar1: 10 10\n"
+ ]
+ }
+ ],
+ "prompt_number": 13
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 10.4, Page Number: 230<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "import datetime\n",
+ "\n",
+ "date=datetime.datetime.now()\n",
+ "\n",
+ "#Result\n",
+ "print date.time()"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "17:06:28.236000\n"
+ ]
+ }
+ ],
+ "prompt_number": 14
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 10.5, Page Number: 231<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "import datetime\n",
+ "\n",
+ "date=datetime.datetime.now()\n",
+ "\n",
+ "#Result\n",
+ "print date.ctime()"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Sat Sep 14 17:07:14 2013\n"
+ ]
+ }
+ ],
+ "prompt_number": 15
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 10.6, Page Number: 232<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "class mystruct:\n",
+ " a=None\n",
+ " b=None\n",
+ "\n",
+ "def f(var):\n",
+ " var[0].a=var[0].a*var[0].a\n",
+ " var[0].b=var[0].b/var[0].b\n",
+ " return var[0]\n",
+ " \n",
+ "#Variable declaration\n",
+ "x=[]\n",
+ "x.append(mystruct())\n",
+ "y=mystruct()\n",
+ "\n",
+ "#Initializing\n",
+ "x[0].a=10\n",
+ "x[0].b=20\n",
+ "\n",
+ "print \"Original x.a and x.b: \",x[0].a,' ',x[0].b\n",
+ "\n",
+ "y=f(x) #function call\n",
+ "\n",
+ "#Result\n",
+ "print \"Modified x.a and x.b: \",x[0].a,' ',x[0].b\n",
+ "print \"Modified y.a and y.b: \",y.a,' ',y.b\n",
+ "\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Original x.a and x.b: 10 20\n",
+ "Modified x.a and x.b: 100 1\n",
+ "Modified y.a and y.b: 100 1\n"
+ ]
+ }
+ ],
+ "prompt_number": 16
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 10.7, Page Number: 239<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "class swap_bytes:\n",
+ " ch=[0,0]\n",
+ "\n",
+ "#Exchange of bytes\n",
+ "def disp_binary(u):\n",
+ " t=128\n",
+ " while not(t==0):\n",
+ " if u&t:\n",
+ " print \"1 \",\n",
+ " else:\n",
+ " print \"0 \",\n",
+ " t=t/2\n",
+ "\n",
+ "#Variable declaration\n",
+ "sb=swap_bytes()\n",
+ "\n",
+ "sb.ch[0]=15\n",
+ "\n",
+ "print \"Original bytes: \",\n",
+ "disp_binary(sb.ch[1])\n",
+ "disp_binary(sb.ch[0])\n",
+ "\n",
+ "#Exchange bytes\n",
+ "temp=sb.ch[0]\n",
+ "sb.ch[0]=sb.ch[1]\n",
+ "sb.ch[1]=temp\n",
+ "\n",
+ "#Result\n",
+ "print \"\\nExchanged bytes: \",\n",
+ "disp_binary(sb.ch[1])\n",
+ "disp_binary(sb.ch[0])\n",
+ "\n",
+ "\n",
+ " "
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Original bytes: 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 \n",
+ "Exchanged bytes: 0 0 0 0 1 1 1 1 0 0 0 0 0 0 0 0 \n"
+ ]
+ }
+ ],
+ "prompt_number": 17
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 10.8, Page Number: 240<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "ch='a'\n",
+ "\n",
+ "while True:\n",
+ " print \"\\n\",ch,\n",
+ " print bin(ord(ch)) #Display the bit pattern for each character\n",
+ " ch=chr(ord(ch)+1)\n",
+ " if ch=='r':\n",
+ " break"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "a 0b1100001\n",
+ "\n",
+ "b 0b1100010\n",
+ "\n",
+ "c 0b1100011\n",
+ "\n",
+ "d 0b1100100\n",
+ "\n",
+ "e 0b1100101\n",
+ "\n",
+ "f 0b1100110\n",
+ "\n",
+ "g 0b1100111\n",
+ "\n",
+ "h 0b1101000\n",
+ "\n",
+ "i 0b1101001\n",
+ "\n",
+ "j 0b1101010\n",
+ "\n",
+ "k 0b1101011\n",
+ "\n",
+ "l 0b1101100\n",
+ "\n",
+ "m 0b1101101\n",
+ "\n",
+ "n 0b1101110\n",
+ "\n",
+ "o 0b1101111\n",
+ "\n",
+ "p 0b1110000\n",
+ "\n",
+ "q 0b1110001\n"
+ ]
+ }
+ ],
+ "prompt_number": 18
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 10.9, Page Number: 242<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "#Variable declaration\n",
+ "ch=['X','Y']\n",
+ "c=\"\"\n",
+ "def disp_bits(u):\n",
+ " t=128\n",
+ " global c\n",
+ " while not(t==0):\n",
+ " if u&t:\n",
+ " c=c+\"1\"\n",
+ " else:\n",
+ " c=c+\"0\"\n",
+ " t=t/2 \n",
+ " return c\n",
+ "\n",
+ "#Result\n",
+ "print \"union as chars: \",ch[0],ch[1]\n",
+ "print \"union as integer: \",\n",
+ "c= disp_bits(ord(ch[1]))\n",
+ "c= disp_bits(ord(ch[0]))\n",
+ "print int(str(c),2)\n",
+ "\n",
+ "\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "union as chars: X Y\n",
+ "union as integer: 22872\n"
+ ]
+ }
+ ],
+ "prompt_number": 19
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [],
+ "language": "python",
+ "metadata": {},
+ "outputs": []
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+} \ No newline at end of file