Chapter 16: Structures, Unions and Enumerations

Example inventory.c, Page 391

In [9]:
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: Disk Drive
Enter quality on hand: 10

Example readline.c, Page 395

In [3]:
def read_line(str,n):
    ch=0
    i=0
    EOF='\n'
    ch=raw_input()
    while(isspace(ch)):
        donothing=0
    while(ch!='\n' and ch!=EOF):
        if(i<n):
            str[i+1]=ch
        ch=raw_input()
    str[i]='\0'
    return i
In [ ]: