DREAM IDEA PLAN IMPLEMENTATION 1 Present to Amirkabir
DREAM IDEA PLAN IMPLEMENTATION 1
Present to: Amirkabir University of Technology (Tehran Polytechnic) & Semnan University Dr. Kourosh Kiani 2 Email: kkiani 2004@yahoo. com Email: Kourosh. kiani@aut. ac. ir Web: aut. ac. com
3
C++ Stream Input/Output 4
Integral Stream Base: dec, oct, hex and setbase • Integers normally base 10 (decimal) – Stream manipulators to change base • hex (base 16) • oct (base 8) • dec (resets to base 10) • cout << hex << my. Integer – setbase(new. Base) • One of 8, 10, or 16 – Base remains same until explicitly changed • Parameterized stream manipulators – Use header <iomanip. h> 5
// Using stream manipulators hex, oct, dec and setbase. #include <iomanip. h> #include <iostream. h> int main() { int number; cout << "Enter a decimal number: "; cin >> number; // input number // use hex stream manipulator to show hexadecimal number cout << number << " in hexadecimal is: " << hex<< number << endl; // use oct stream manipulator to show octal number cout << dec << number << " in octal is: " << oct << number << endl; return 0; } // end main 6
7
8
Floating-Point Precision (precision, setprecision) • Set precision of floating point numbers – Number of digits to right of decimal – setprecision stream manipulator • Pass number of decimal points • cout << setprecision(5) – precision member function • cout. precision(new. Precision) • With no arguments, returns current precision – Settings remain until changed explicitly 9
10
11
312. 0 x More examples float x = 312. 0 ; float y = 4. 827 ; cout. setf ( ios: : fixed , ios: : floatfield ) ; cout. setf ( ios: : showpoint ) ; 4. 827 y OUTPUT cout << setprecision ( 2 ) << setw ( 10 ) << x << endl << setw ( 10 ) << y << endl ; ’’’’ 3 1 2. 00 ’’’’’’ 4. 83 cout << setprecision ( 1 ) << setw ( 10 ) << x << endl << setw ( 10 ) << y << endl ; ’’’’’ 3 1 2. 0 ’’’’’’’ 4. 8 cout << setprecision ( 5 ) << setw ( 10 ) << x << endl 12 << setw ( 10 ) << y << endl ; 3 1 2. 00000 4. 82700
13
File I/O with C++ #include <fstream> using namespace std; int main ( ) { int a, b, c ; ifstream fin ; //Create file input stream object fin. open ( "my_input. dat") ; //Open input file fin >> a >> b ; //Read two values from input file c=a+b; ofstream fout ; //Create file output stream object fout. open ( "my_output. dat"); //Open output file //Write result to output file fout <<"a =" <<a<<" b = "<< b<<" a+b= "<< c << endl ; fin. close ( ) ; //Close input file fout. close ( ) ; //Close output file } 14
15
16
17
18
19
20
21
22
23
File Open Mode Name Description ios: : in Open file to read ios: : out Open file to write ios: : app All the date you write, is put at the end of the file. It calls ios: : out ios: : ate All the date you write, is put at the end of the file. It does not call ios: : out ios: : trunc Deletes all previous content in the file. (empties the file) ios: : nocreate If the file does not exists, opening it with the open() function gets impossible. ios: : noreplace If the file exists, trying to open it with the open() function, returns an error. ios: : binary Opens the file in binary mode. 24
25
26
27
28
//Demonstrating left justification and right justification. # include <iostream> # include <iomanip> using namespace std; int main(void) { int x = 12345; // display x right justified (default) cout << "Default is right justified: " << endl<<endl << setw( 10 ) << x<<endl; // use left manipulator to display x left justified cout << "Use std: : left to left justify x: " << endl<< left << setw( 10 ) << x<<endl; cout << "Use std: : right to right justify x: "<< endl<< right << setw( 10 ) << x << endl<<endl; return 0; } 29
30
31
// Demonstrating left justification and right justification. #include <iostream> #include <iomanip> using namespace std; int main(void) { cout << showpos << setw( 10 ) << 123 << endl; // display value with internal spacing and plus sign cout << internal << showpos << setw( 10 ) << 123 << endl; return 0; } // end main 32
33
34
35
Floating-Point Numbers; Scientific and Fixed Notation (scientific, fixed) • Stream manipulator scientific – Forces scientific notation • 1. 946000 e+009 • Stream manipulator fixed – Forces fixed point format – Prints number of decimals specified by precision • 1946000000 • If no manipulator specified – Format of number determines how it appears 36
37
38
Uppercase/Lowercase Control (uppercase) • Stream manipulator uppercase – Uppercase E in scientific notation • 1 E 10 – Uppercase X in hex notation and uppercase hex letters • 0 XABCD – By default, lowercase – Reset with nouppercase 39
40
41
Questions? Discussion? Suggestions ?
43
- Slides: 43