{ "metadata": { "name": "", "signature": "sha256:8ebda07d5e13c6b4a8132b67344e9246eeb8703f2dcc433e1b4e350db4cace07" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Chapter 17: Advanced Uses of Pointers" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example remind2.c, Page 418" ] }, { "cell_type": "code", "collapsed": false, "input": [ "MAX_REMIND=50\n", "MSG_LEN=60\n", "reminders = [['' for i in xrange(MSG_LEN+3)] for i in xrange(MAX_REMIND)]\n", "num_remind=0\n", "while(1):\n", " if(num_remind==MAX_REMIND):\n", " print \"--No space left --\"\n", " break\n", " li=raw_input( \"Enter day and reminder: \")\n", " li1=li.split()\n", " day_str=li1[0]\n", " if(day_str=='0'):\n", " breakb n\n", " msg_str=li1[1:]\n", " i=0\n", " for i in range(num_remind):\n", " if(day_stri):\n", " reminders[j]=reminders[j-1]\n", " j=j-1\n", " reminders[i]=day_str\n", " reminders[i]=str(reminders[i])+str(msg_str)\n", " \n", " num_remind=num_remind+1\n", "print \"\"\n", "print \"Day Reminder\"\n", "for i in range(num_remind):\n", " print \"%s\"%reminders[i]" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example inventory2.c, Page 434" ] }, { "cell_type": "code", "collapsed": false, "input": [ "NAME_LEN=25\n", "MAX_PARTS=100\n", "num_parts=0\n", "number1=[0]*100\n", "on_hand1=[0]*100\n", "name1=[0]*100\n", "def find_part(number):\n", " i=0\n", " for i in range (i,num_parts):\n", " if(number1[i]==number):\n", " return i\n", " return -1\n", "def insert():\n", " num_parts=0\n", " if(num_parts==MAX_PARTS):\n", " print \"Database is full, cant add more parts\"\n", " return\n", " part_number=int(raw_input(\"Enter part number: \"))\n", " if(find_part(part_number)>=0):\n", " print \"Part already exists\"\n", " return\n", " number1[num_parts]=part_number\n", " name1[num_parts]=raw_input(\"Enter part name: \")\n", " on_hand1[num_parts]=raw_input(\"Enter quality on hand: \")\n", " num_parts=num_parts+1\n", " \n", "def search():\n", " number=int(raw_input(\"Enter part number: \"))\n", " i=find_part(number)\n", " if(i>=0):\n", " print \"Part name: %s\" %name1[i]\n", " print \"Quantity on hand: %d\"%on_hand1[i]\n", " else:\n", " print \"Part not found\"\n", " \n", "def update():\n", " number=int(raw_input(\"Enter part number: \"))\n", " i=find_part(number)\n", " if(i>=0):\n", " change=int(raw_input(\"Enter change in quantity on hand: \")) \n", " on_hand1[i]=on_hand1[i]+change\n", " else:\n", " print \"Part not found\"\n", "\n", "while(1):\n", " code=raw_input(\"Enter operation code: \")\n", " if code=='i':\n", " insert()\n", " break\n", " elif code=='s':\n", " search()\n", " break\n", " elif code=='u':\n", " update()\n", " break\n", " elif(code=='p'):\n", " print()\n", " break\n", "\n", "print \"\"" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example tabulate.c, Page 444" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import math\n", "\n", "initial=float(raw_input(\"Enter initial value: \"))\n", "final=float(raw_input(\"Enter final value: \"))\n", "increment=float(raw_input(\"Enter increment: \"))\n", "print \"\"\n", "print \" x cos(x)\"\n", "print \"-------------------------\"\n", "\n", "def tabulatec( first, last, incr):\n", " num_intervals=int(math.ceil((last-first)/incr))\n", " for i in range(num_intervals):\n", " x=first+i*incr\n", " print \"%10.5f %10.5f\"%(x,math.cos(x))\n", "def tabulates( first, last, incr):\n", " num_intervals=int(math.ceil((last-first)/incr))\n", " for i in range(num_intervals):\n", " x=first+i*incr\n", " print \"%10.5f %10.5f\"%(x,math.sin(x))\n", "def tabulatet( first, last, incr):\n", " num_intervals=int(math.ceil((last-first)/incr))\n", " for i in range(num_intervals):\n", " x=first+i*incr\n", " print \"%10.5f %10.5f\"%(x,math.tan(x))\n", "\n", " \n", "tabulatec(initial,final, increment)\n", "print \"\"\n", "print \" x sin(x)\"\n", "print \"\"\n", "print \"-------------------------\"\n", "tabulates(initial,final, increment)\n", "print \"\"\n", "print \" x tan(x)\"\n", "print \"\"\n", "print \"-------------------------\"\n", "tabulatet(initial,final, increment)\n", "\n" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter initial value: 0\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter final value: .5\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter increment: .1\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", " x cos(x)\n", "--------------------------\n", " 0.00000 1.00000\n", " 0.10000 0.99500\n", " 0.20000 0.98007\n", " 0.30000 0.95534\n", " 0.40000 0.92106\n", "\n", " x sin(x)\n", "\n", "-------------------------\n", " 0.00000 0.00000\n", " 0.10000 0.09983\n", " 0.20000 0.19867\n", " 0.30000 0.29552\n", " 0.40000 0.38942\n", "\n", " x tan(x)\n", "\n", "-------------------------\n", " 0.00000 0.00000\n", " 0.10000 0.10033\n", " 0.20000 0.20271\n", " 0.30000 0.30934\n", " 0.40000 0.42279\n" ] } ], "prompt_number": 7 }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }