'''
Example 6.1 and example 6.2 are same for python.
'''
class wp_char:
wp_cval = ''
wp_font = 0
wp_psize = 0
def __init__(self):
wp_cval = ''
wp_font = 0
wp_psize = 0
def infun():
w = wp_char()
#w.wp_cval = raw_input("Enter any Character.. :")
w.wp_font = 2
w.wp_psize = 10
return w
ARSIZE = 10
icount=0
lo_indx=0
hi_indx=0
ar = []
for icount in range(ARSIZE):
ar.append(infun())
if(ar[icount].wp_cval == '\n'):
''' /*
* Leave the loop.
* not incrementing icount means that the
* '\n' is ignored in the sort
*/
'''
break;
#/* now a simple exchange sort */
for lo_indx in range(icount-1):
for hi_indx in range(icount):
if (ar[lo_indx].wp_cval > ar[hi_indx].wp_cval):
#Swap the two structures.
wp_tmp = ar[lo_indx]
ar[lo_indx] = ar[hi_indx]
ar[hi_indx] = wp_tmp
for lo_indx in range(icount+1):
print "%s %d %d\n" % (ar[lo_indx].wp_cval,ar[lo_indx].wp_font,ar[lo_indx].wp_psize)
'''
This wont give any output.
Note: while compiling this program. comment either of the s_1 class structure.
'''
class s_1: #incomplete type
pass
class s_2:
something = 0
s = s_1()
class s_1:
something = 0.0
s = s_2()
'''
This wont give any output.
'''
class x:
f = 0.0
def func():
a = x()
a.f = 0
return a
def f2():
o = func()
'''
this wont give any output.
'''
class list_ele:
def __init__(self,d=0,p=None):
data=d
pointer = p
ar=[]
ar.append(list_ele())
ar.append(list_ele())
ar.append(list_ele())
ar[0].data = 5;
ar[0].pointer = ar[2]
ar[1].data = 99
ar[1].pointer = ar[2]
ar[2].data = -7
ar[2].pointer = None
class list_ele:
def __init__(self,d=0,p=None):
data=d
pointer = p
ar=[]
ar.append(list_ele())
ar.append(list_ele())
ar.append(list_ele())
ar[0].data = 5;
ar[0].pointer = ar[2]
ar[1].data = 99
ar[1].pointer = ar[2]
ar[2].data = -7
ar[2].pointer = None
for i in ar:
print "contents %d\n" %(i.data)
'''
This wont compile as we do not have list_ele class here.
'''
def sortfun(list ):
exchange = True
nextp = []
thisp = None
dummy = list_ele()
'''
* Algorithm is this:
* Repeatedly scan list.
* If two list items are out of order,
* link them in the other way round.
* Stop if a full pass is made and no
* exchanges are required.
* The whole business is confused by
* working one element behind the
* first one of interest.
* This is because of the simple mechanics of
* linking and unlinking elements.
*/ '''
dummy.pointer = list;
while(exchange):
exchange = False
thisp=dummy
while( (nextp == thisp.pointer) and nextp.pointer):
if(nextp.data < nextp.pointer.data):
#/* exchange */
exchange = 1
thisp.pointer = nextp.pointer
nextp.pointer =thisp.pointer.pointer
thisp.pointer.pointer = nextp
thisp = thisp.pointer;
return(dummy.pointer);
'''
Examples 6.8 and Example 6.9
Both are same for Python as we do not have any concept of pointer.
Python will go same for both.
'''
class Tree:
def __init__(self, cargo, left=None, right=None):
self.cargo = cargo
self.left = left
self.right = right
def __str__(self):
return str(self.cargo)
'''
/*
* Tree search algorithm.
* Searches for value 'v' in tree,
* returns pointer to first node found containing
* the value otherwise 0.
*/
'''
def t_search(root,v):
while(root):
if(root.cargo == v):
return(root)
if(root.cargo > v):
root = root.left
else:
root = root.right
#/* construct tree by hand */
tp = None
root_p = None
tree=[]
for i in range(7):
j = 0
j = i+1
tree.append(Tree(j))
if(j == 2 or j == 6):
tree[i].left = tree[i-1]
tree[i-1].right = tree[i]
root_p = tree[3]
root_p.left = tree[1]
root_p.right = tree[5]
# /* try the search */
tp = t_search(root_p, 9)
if(tp!=None):
print "found at position %d\n" %(tp-tree)
else:
print "value not found\n"
'''
Note : this wont give any output.
'''
def t_walk(r):
if(root_p == 0):
return
t_walk(root_p.left)
print "%d\n" %(root_p.data)
t_walk(root_p.right)
class A:
def __init__(self):
u_f = 0.0
u_i = 0
var = A()
var.u_f = 23.5
print "value is %f\n" %(var.u_f)
var.u_i = 5
print "value is %d\n" %(var.u_i)
FLOAT_TYPE = 1
CHAR_TYPE = 2
INT_TYPE = 3
class var_type:
def __init__(self):
type_in_union = 0
un_float = 0.0
un_char = '0'
un_int = 0
v = var_type()
def print_vt():
if(v.type_in_union == FLOAT_TYPE):
print "%f\n" %(v.un_float)
else:
if(v.type_in_union == CHAR_TYPE):
print "%s\n" %(v.un_char)
else:
if(v.type_in_union == INT_TYPE):
print "%d\n" %(v.un_int)
else:
print "Unknown type in union\n"
v.type_in_union = FLOAT_TYPE
v.un_float = 3.5
print_vt()
v.type_in_union = CHAR_TYPE
v.un_char = 'a'
print_vt()
'''
This wont give any output.
'''
class Abc:
field1 = 4
a = 3
field2 = 1
igned = 0
field3 = 6
NMONTHS=12
month = 0
month_days =[31,28,31,30,31,30,31,31,30,31,30,31]
mnames=["January", "February","March", "April","May", "June","July", "August","September", "October","November", "December"]
day_count = month;
for day_count in range(NMONTHS):
print "%d days in %s\n" %(month_days[day_count],mnames[day_count])
class s:
a = 0
b = '0'
cp = '0'
def set(self,aa,bb,c):
a = aa
b = bb
cp = c
ex_s = s()
ex_s.set(1, 'a', "hello")
first = ex_s
second = s()
second.set(2, 'b', "byebye")
print first
print second
class s:
a = 0
class ss:
c = 0
d = '0'
e = s()
e.a = 1
e.c = 2
e.d ='a'
print e
y = [[1, 3, 5],[2, 4, 6],[3, 5, 7]]
print y