Chapter 17: Advanced Uses of Pointers

Example remind2.c, Page 418

In [2]:
MAX_REMIND=50
MSG_LEN=60
reminders = [['' for i in xrange(MSG_LEN+3)] for i in xrange(MAX_REMIND)]
num_remind=0
while(1):
    if(num_remind==MAX_REMIND):
        print "--No space left --"
        break
    li=raw_input( "Enter day and reminder: ")
    li1=li.split()
    day_str=li1[0]
    if(day_str=='0'):
        break
    msg_str=li1[1:]
    i=0
    for i in range(num_remind):
        if(day_str<reminders[i]):
            break
    j=num_remind
    while(j>i):
        reminders[j]=reminders[j-1]
        j=j-1
    reminders[i]=day_str
    reminders[i]=str(reminders[i])+str(msg_str)
    
    num_remind=num_remind+1
print ""
print "Day Reminder"
for i in range(num_remind):
    print "%s"%reminders[i]
Enter day and reminder: 24 Susan's Birthday
Enter day and reminder: 5 6:00 - Dinner with Marge and Russ
Enter day and reminder: 26 Movie - "Chinatown"
Enter day and reminder: 7 10:30 - Dental appointment
Enter day and reminder: 12 Movie - "Dazed and Confused"
Enter day and reminder: 5 Saturday class
Enter day and reminder: 12 Saturday class
Enter day and reminder: 0

Day Reminder
12['Saturday', 'class']
12['Movie', '-', '"Dazed', 'and', 'Confused"']
26['Movie', '-', '"Chinatown"']
5['Saturday', 'class']
5['6:00', '-', 'Dinner', 'with', 'Marge', 'and', 'Russ']
7['10:30', '-', 'Dental', 'appointment']
24["Susan's", 'Birthday']

Example inventory2.c, Page 434

In [3]:
NAME_LEN=25
MAX_PARTS=100
num_parts=0
number1=[0]*100
on_hand1=[0]*100
name1=[0]*100
def find_part(number):
    i=0
    for i in range (i,num_parts):
        if(number1[i]==number):
            return i
    return -1
def insert():
    num_parts=0
    if(num_parts==MAX_PARTS):
        print "Database is full, cant add more parts"
        return
    part_number=int(raw_input("Enter part number: "))
    if(find_part(part_number)>=0):
        print "Part already exists"
        return
    number1[num_parts]=part_number
    name1[num_parts]=raw_input("Enter part name: ")
    on_hand1[num_parts]=raw_input("Enter quality on hand: ")
    num_parts=num_parts+1
    
def search():
    number=int(raw_input("Enter part number: "))
    i=find_part(number)
    if(i>=0):
        print "Part name: %s" %name1[i]
        print "Quantity on hand: %d"%on_hand1[i]
    else:
        print "Part not found"
        
def update():
    number=int(raw_input("Enter part number: "))
    i=find_part(number)
    if(i>=0):
        change=int(raw_input("Enter change in quantity on hand: ")) 
        on_hand1[i]=on_hand1[i]+change
    else:
        print "Part not found"

while(1):
    code=raw_input("Enter operation code: ")
    if code=='i':
        insert()
        break
    elif code=='s':
        search()
        break
    elif code=='u':
        update()
        break
    elif(code=='p'):
        print()
        break

print ""
Enter operation code: i
Enter part number: 528
Enter part name: Disc Drive
Enter quality on hand: 10

Example tabulate.c, Page 444

In [7]:
import math

initial=float(raw_input("Enter initial value: "))
final=float(raw_input("Enter final value: "))
increment=float(raw_input("Enter increment: "))
print ""
print "    x    cos(x)"
print "-------------------------"

def tabulatec( first, last, incr):
    num_intervals=int(math.ceil((last-first)/incr))
    for i in range(num_intervals):
        x=first+i*incr
        print "%10.5f %10.5f"%(x,math.cos(x))
def tabulates( first, last, incr):
    num_intervals=int(math.ceil((last-first)/incr))
    for i in range(num_intervals):
        x=first+i*incr
        print "%10.5f %10.5f"%(x,math.sin(x))
def tabulatet( first, last, incr):
    num_intervals=int(math.ceil((last-first)/incr))
    for i in range(num_intervals):
        x=first+i*incr
        print "%10.5f %10.5f"%(x,math.tan(x))

    
tabulatec(initial,final, increment)
print ""
print "    x    sin(x)"
print ""
print "-------------------------"
tabulates(initial,final, increment)
print ""
print "    x    tan(x)"
print ""
print "-------------------------"
tabulatet(initial,final, increment)
Enter initial value: 0
Enter final value: .5
Enter increment: .1

    x    cos(x)
--------------------------
   0.00000    1.00000
   0.10000    0.99500
   0.20000    0.98007
   0.30000    0.95534
   0.40000    0.92106

    x    sin(x)

-------------------------
   0.00000    0.00000
   0.10000    0.09983
   0.20000    0.19867
   0.30000    0.29552
   0.40000    0.38942

    x    tan(x)

-------------------------
   0.00000    0.00000
   0.10000    0.10033
   0.20000    0.20271
   0.30000    0.30934
   0.40000    0.42279
In [ ]: