cs201- introduction to programming- lecture 36

51
Introduction to Introduction to Programming Programming Lecture 36 Lecture 36

Upload: bilal-ahmed

Post on 18-Dec-2014

20 views

Category:

Education


0 download

DESCRIPTION

Virtual University Course CS201- Introduction to Programming Lecture No 36 Instructor's Name: Dr. Naveed A. Malik Course Email: [email protected]

TRANSCRIPT

Page 1: CS201- Introduction to Programming- Lecture 36

Introduction to Introduction to ProgrammingProgramming

Lecture 36Lecture 36

Page 2: CS201- Introduction to Programming- Lecture 36

#include #include <iostream.h><iostream.h>#include <fstream.h>#include <fstream.h>

Page 3: CS201- Introduction to Programming- Lecture 36

iomanip.iomanip.hh

Page 4: CS201- Introduction to Programming- Lecture 36

cin.eof ( ) ;cin.eof ( ) ; cin.fail ( ) ;cin.fail ( ) ; cin.bad ( ) ;cin.bad ( ) ; cin.good ( ) ;cin.good ( ) ; cin.clrear ( ) ;cin.clrear ( ) ;

Page 5: CS201- Introduction to Programming- Lecture 36

ManipulatorManipulatorss

Page 6: CS201- Introduction to Programming- Lecture 36

Stream Stream ManipulatorManipulator

ss

Page 7: CS201- Introduction to Programming- Lecture 36

float PI = 3.1415926 float PI = 3.1415926 ;;

Page 8: CS201- Introduction to Programming- Lecture 36

endlendl

Page 9: CS201- Introduction to Programming- Lecture 36

cout << endl cout << endl ;;

Page 10: CS201- Introduction to Programming- Lecture 36

cout << flush cout << flush ;;

Page 11: CS201- Introduction to Programming- Lecture 36

Manipulator Manipulator With With

ArgumentsArguments

Page 12: CS201- Introduction to Programming- Lecture 36

Inline Inline ManipulatManipulat

oror

Page 13: CS201- Introduction to Programming- Lecture 36

cout.flush cout.flush ( ) ;( ) ;

Page 14: CS201- Introduction to Programming- Lecture 36

Number SystemNumber System BinaryBinary DecimalDecimal OctalOctal HexadecimalHexadecimal

Page 15: CS201- Introduction to Programming- Lecture 36

Example Example

int i = 10 ;int i = 10 ;cout << i ;cout << i ;

Page 16: CS201- Introduction to Programming- Lecture 36

1010

Page 17: CS201- Introduction to Programming- Lecture 36

#include <iostream.h>#include <iostream.h>#include <iomanip.h>#include <iomanip.h>main ( )main ( ){{

int i = 10 ;int i = 10 ; OutputOutputcout << oct << i << endl ;cout << oct << i << endl ; 12 12cout << hex << i<< endl ;cout << hex << i<< endl ; A Acout << dec << i << endl ;cout << dec << i << endl ; 10 10

}}

ExampleExample

Page 18: CS201- Introduction to Programming- Lecture 36

White White SpaceSpace

Page 19: CS201- Introduction to Programming- Lecture 36

WS WS ManipulatManipulat

oror

Page 20: CS201- Introduction to Programming- Lecture 36

setwsetw

Page 21: CS201- Introduction to Programming- Lecture 36

#include <iostream.h>#include <iostream.h>

#include <iomanip.h>#include <iomanip.h>

main ( )main ( )

{{

int i = 5 ;int i = 5 ;

cout << “The value of i is = ” ;cout << “The value of i is = ” ;

cout << setw ( 4 ) << i << endl ;cout << setw ( 4 ) << i << endl ;

}}

ExampleExample

Page 22: CS201- Introduction to Programming- Lecture 36

setfilsetfilll

Page 23: CS201- Introduction to Programming- Lecture 36

cout << setfill ( ‘*’ ) cout << setfill ( ‘*’ ) ;;

A Character

Page 24: CS201- Introduction to Programming- Lecture 36

#include<iostream.h>#include<iostream.h>

#include<iomanip.h>#include<iomanip.h>

Main ( )Main ( )

{{

int i = 4000 ;int i = 4000 ;

cout << setfill ( ‘*’ ) << setw ( 10 ) << i << cout << setfill ( ‘*’ ) << setw ( 10 ) << i << endl ;endl ;

}}

ExampleExample

Page 25: CS201- Introduction to Programming- Lecture 36

Set Precision Set Precision ManipulatorManipulator

Page 26: CS201- Introduction to Programming- Lecture 36

#include<iostream.h>#include<iostream.h>

#include<iomanip.h>#include<iomanip.h>

main ( )main ( )

{{

float number = 6.67076632 ;float number = 6.67076632 ;cout << setprecision ( 2 ) << number cout << setprecision ( 2 ) << number

<< endl ;<< endl ;

}}

ExampleExample

Page 27: CS201- Introduction to Programming- Lecture 36

#define PI 3.1415926#define PI 3.1415926

main ( )main ( )

{{

cout << PI << endl ;cout << PI << endl ;

cout << setprecision ( 2 ) << PI << endl ;cout << setprecision ( 2 ) << PI << endl ;

}}

ExampleExample

Page 28: CS201- Introduction to Programming- Lecture 36

setbassetbasee

Page 29: CS201- Introduction to Programming- Lecture 36

#include <iostream.h>#include <iostream.h>#include <iomanip.h>#include <iomanip.h>main ( )main ( ){{

int x = 10 ;int x = 10 ;cout << setbase ( 8 ) << x <<endl cout << setbase ( 8 ) << x <<endl

;;cout << setbase ( 16 ) << x <<endl cout << setbase ( 16 ) << x <<endl

;; cout << setbase ( 10 ) << x <<endl ;cout << setbase ( 10 ) << x <<endl ; cout << setbase ( 0 ) << x <<endl ;cout << setbase ( 0 ) << x <<endl ;

}}

ExampleExample

Same as setbase (10)

Page 30: CS201- Introduction to Programming- Lecture 36

Input Output state Input Output state flagsflags

IOS FlagsIOS Flags

Page 31: CS201- Introduction to Programming- Lecture 36

width width ( ) ;( ) ;

Page 32: CS201- Introduction to Programming- Lecture 36

cin.width ( 7 ) ;cin.width ( 7 ) ;cout.width ( 10 ) cout.width ( 10 ) ;;

Page 33: CS201- Introduction to Programming- Lecture 36

cout.precision cout.precision ( 2 ) ;( 2 ) ;

Page 34: CS201- Introduction to Programming- Lecture 36

#include <iostream.h>#include <iostream.h>

#include <iomanip.h>#include <iomanip.h>

main ( )main ( )

{{

int i = 10 , j = 20 ;int i = 10 , j = 20 ;

cout << setw ( 7 ) << i cout << setw ( 7 ) << i <<endl ;<<endl ;

cout << j ;cout << j ;

}}

ExampleExample

Page 35: CS201- Introduction to Programming- Lecture 36

ios :: adjustfieldios :: adjustfield ios :: leftios :: left ios :: rightios :: right ios :: left | ios :: right , ios :: ios :: left | ios :: right , ios ::

adjustfieldadjustfield

Formatting Manipulation

Page 36: CS201- Introduction to Programming- Lecture 36

cout.setf ( ios :: left , ios :: cout.setf ( ios :: left , ios :: adjustfield ) ;adjustfield ) ;

Set FlagSet Flag

Page 37: CS201- Introduction to Programming- Lecture 36

cout.fill ( ‘*’ ) cout.fill ( ‘*’ ) ;;

Page 38: CS201- Introduction to Programming- Lecture 36

cout.fill ( '0' ) ;cout.fill ( '0' ) ;

Page 39: CS201- Introduction to Programming- Lecture 36

Formatting Formatting ManipulationManipulation

cout.fill ( '0' ) ;cout.fill ( '0' ) ;

cout << setw ( 10 ) << number << endl ;cout << setw ( 10 ) << number << endl ;

Page 40: CS201- Introduction to Programming- Lecture 36

cout.setf ( ios :: cout.setf ( ios :: hex ) ;hex ) ;

Page 41: CS201- Introduction to Programming- Lecture 36

7ff7ff0111 1111 1111

Page 42: CS201- Introduction to Programming- Lecture 36

showbasshowbasee

Page 43: CS201- Introduction to Programming- Lecture 36

showbaseshowbase

cout.setf ( ios :: showbase ) ; cout.setf ( ios :: showbase ) ;

Page 44: CS201- Introduction to Programming- Lecture 36

cout.setf ( ios :: showbase ) ; cout.setf ( ios :: showbase ) ;

cout.setf ( ios::dec , ios :: basefield ) ; cout.setf ( ios::dec , ios :: basefield ) ;

cout << x << '\n' ; // Outputs 77cout << x << '\n' ; // Outputs 77

cout.setf ( ios :: oct , ios :: basefield ) ;cout.setf ( ios :: oct , ios :: basefield ) ;

cout << x << '\n' ; // Outputs 077 cout << x << '\n' ; // Outputs 077

cout.setf ( ios :: hex , ios :: basefield ) ; cout.setf ( ios :: hex , ios :: basefield ) ;

cout << x << '\n' ; // Outputs 0x77cout << x << '\n' ; // Outputs 0x77

showbaseshowbase

Page 45: CS201- Introduction to Programming- Lecture 36

ios :: ios :: scientificscientific

Page 46: CS201- Introduction to Programming- Lecture 36

cout.setf ( ios :: scientific ) cout.setf ( ios :: scientific ) ;;

Page 47: CS201- Introduction to Programming- Lecture 36

Scientific Scientific NotationNotation

1.2334e1.2334e+09+09

+/-+/-

Page 48: CS201- Introduction to Programming- Lecture 36

Fixed Point Fixed Point NotationNotation

Page 49: CS201- Introduction to Programming- Lecture 36

ios :: fixedios :: fixed

Page 50: CS201- Introduction to Programming- Lecture 36

ios :: ios :: uppercaseuppercase

Page 51: CS201- Introduction to Programming- Lecture 36

What we learnt so far..What we learnt so far..

Input Output Input Output StreamStream

And their And their Manipulations in C+Manipulations in C+++