Chapter 12: the Standard Library in C++

Program Source Code 12.1, page no: 368

In [1]:
import sys
FormatString=[]    #for taking input
a=10
b=20
c=a+b
print 'ok...I have added them: ', a,'+' ,b,'=',c
print 'Now you tell me your format string',
print '(Max 40 characters)'
print 'I will use that to show input numbers',
print 'and their sum'
sys.stdout.flush()                 #fflush stdin 
FormatString='The sum of two numbers'
print FormatString,a,'and',b,'is',c               #displaying the numbers and their sum
ok...I have added them:  10 + 20 = 30
Now you tell me your format string (Max 40 characters)
I will use that to show input numbers and their sum
The sum of two numbers 10 and 20 is 30

Program Source Code 12.2, page no: 375

In [16]:
#variable declaration
value=100
print 'The decimal value is: ',value
print 'The octal value is: ',oct(value)
print 'The hexadecimal value is: ',hex(value)
The decimal value is:  100
The octal value is:  0144
The hexadecimal value is:  0x64

Program Source Code 12.3, page no: 378

In [1]:
import math
PI=22.0/7.0
print '123456789012345678901234567890'
print ''
f=0.0

while f<6.28571428:
   print '{0:>8.3f}'.format(f),   #width(8)
   print '{0:>13.4}'.format(math.sin(f))   #width(13)
   f+=0.52380952
print ''
print 'sin(2.0*PI)=',math.sin(2.0*PI)
123456789012345678901234567890

   0.000           0.0
   0.524        0.5002
   1.048        0.8662
   1.571           1.0
   2.095        0.8656
   2.619        0.4991
   3.143     -0.001264
   3.667       -0.5013
   4.190       -0.8669
   4.714          -1.0
   5.238        -0.865
   5.762        -0.498
   6.286      0.002529

sin(2.0*PI)= 0.00252897583892

Program Source Code 12.4, page no: 379

In [4]:
import string 
print '1234567890123456789012345678901234567890'
print '{:*>10}'.format(100)
print ''

print '1234567890123456789012345678901234567890'
if(-12.34567>0):
   i='{0:>15.3e}'.format(-12.34567) #for ios::scientific
   i='+'+i
   print i
else :
   print '{0:e}'.format(123.23)
i='{0:*>15.3f}'.format(-12.34567) #for ios::scientific

print i
print ''

print '1234567890123456789012345678901234567890'
print '{0:*>+10.3f}'.format(275.5) #for ios::showpos
print ''

print '1234567890123456789012345678901234567890'
print '{:.>5}'.format('n'), #for ios::fill
print '{:.>15}'.format("inverse_of_n"),
print '{:.>15}'.format("sum_of_terms")

sum=0
n=1
for n in xrange(1,11):
    term=1.0/float(n)
    sum+=term
    print '+{:.>5}'.format(n),
    print '{:.>14.4f}+'.format(term),
    i='{0:.>13.4f}'.format(sum) #for ios::scientific
   # i='+'+i
    print i
1234567890123456789012345678901234567890
*******100

1234567890123456789012345678901234567890
1.232300e+02
********-12.346

1234567890123456789012345678901234567890
**+275.500

1234567890123456789012345678901234567890
....n ...inverse_of_n ...sum_of_terms
+....1 ........1.0000+ .......1.0000
+....2 ........0.5000+ .......1.5000
+....3 ........0.3333+ .......1.8333
+....4 ........0.2500+ .......2.0833
+....5 ........0.2000+ .......2.2833
+....6 ........0.1667+ .......2.4500
+....7 ........0.1429+ .......2.5929
+....8 ........0.1250+ .......2.7179
+....9 ........0.1111+ .......2.8290
+...10 ........0.1000+ .......2.9290

Program Source Code 12.5, page no: 380

In [2]:
def currency(s):
    i='Rs.'
    return i

def form(s):
    i= '{:.>+10.2f}'.format(s)            #showpoint,fill,precision
   
    return i

print currency(1234.5),form(1234.5)
Rs. ..+1234.50

Program Source Code 12.6, page no: 382

In [11]:
str = raw_input('please input a line of message: ')              
print str                   #display
please input a line of message: hello, do you like C++ I/O
hello, do you like C++ I/O

Program Source Code 12.7, page no: 383

In [11]:
SIZE=40
line=[]*SIZE
str = raw_input('please input a line terminated by .: ')
if str!='.':
    print str                   #display
please input a line terminated by .: I am line
I am line

Program Source Code 12.8, page no: 383

In [3]:
infile=open("TEST.txt","r")
#open file for reading

ch=infile.read(1)

while ch != '':    #reading file
    print ch,
    ch=infile.read(1)
    
infile.close()
1 0   2 0 
3 0   4 0 
5 0

Program Source Code 12.9, page no: 386

In [2]:
data1 = input("Please input a number: ")     #user input 
data2 = input("Please input another number: ")  #user input 

print 'Now I will add your numbers:',data1,'and',data2

data3 = data1 + data2

print 'And the result of this addition is:',data3

print 'now I will store all these numbers (comma seperated) \nIn file TEST.txt'

outfile = open('TEST.txt','w')

outfile.write(str(data1))              #writing in file
outfile.write(',')
outfile.write(str(data2))
outfile.write(',')
outfile.write(str(data3))

print 'Now I will close the file TEST.txt'
outfile.close()


print 'Now I will reopen the file TEST.txt'
infile = open('TEST.txt','r')

a = infile.read(len(str(data1)))           #reading from file
b = infile.read(1)
c = infile.read(len(str(data2)))
d = infile.read(1)
e = infile.read(len(str(data3)))

print 'value retrieved in first line is',
print a,b,c,d,e

infile.close()                    #closing the file

print 'And the retrieved data are:'
print a
print c
print e
Please input a number: 40
Please input another number: 50
Now I will add your numbers: 40 and 50
And the result of this addition is: 90
now I will store all these numbers (comma seperated) 
In file TEST.txt
Now I will close the file TEST.txt
Now I will reopen the file TEST.txt
value retrieved in first line is 40 , 50 , 90
And the retrieved data are:
40
50
90

Program Source Code 12.10, page no: 387

In [1]:
data1 = input("Please input a number: ")     #user input 
data2 = input("Please input another number: ")  #user input 

print 'Now I will add your numbers:',data1,'and',data2

data3 = data1 + data2

print 'And the result of this addition is:',data3

print 'now I will store all these numbers (comma seperated) \nIn file TEST.txt'

outfile = open('TEST.txt','w')

outfile.write(str(data1))              #writing in file
outfile.write(',')
outfile.write(str(data2))
outfile.write(',')
outfile.write(str(data3))

print 'Now I will close the file TEST.txt'
outfile.close()


print 'Now I will reopen the file TEST.txt'
infile = open('TEST.txt','r')

a = infile.read(len(str(data1)))           #reading from file
b = infile.read(1)
c = infile.read(len(str(data2)))
d = infile.read(1)
e = infile.read(len(str(data3)))

print 'value retrieved in first line is',
print a,b,c,d,e

infile.close()                    #closing the file

print 'And the retrieved data are:'
print a
print c
print e
Please input a number: 40
Please input another number: 50
Now I will add your numbers: 40 and 50
And the result of this addition is: 90
now I will store all these numbers (comma seperated) 
In file TEST.txt
Now I will close the file TEST.txt
Now I will reopen the file TEST.txt
value retrieved in first line is 40 , 50 , 90
And the retrieved data are:
40
50
90
In [ ]: