งานนำเสนอกำลังจะดาวน์โหลด โปรดรอ

งานนำเสนอกำลังจะดาวน์โหลด โปรดรอ

Streams. Formatted output cout << right << setw(2) << i << setw(8) << raiseToPower(2, i) << endl; // Insertion operator << This program lists powers of.

งานนำเสนอที่คล้ายกัน


งานนำเสนอเรื่อง: "Streams. Formatted output cout << right << setw(2) << i << setw(8) << raiseToPower(2, i) << endl; // Insertion operator << This program lists powers of."— ใบสำเนางานนำเสนอ:

1 Streams

2 Formatted output cout << right << setw(2) << i << setw(8) << raiseToPower(2, i) << endl; // Insertion operator << This program lists powers of two. Enter exponent limit: 10 0 1 1 2 2 4 3 8 4 16 5 32 6 64 7 128 8 256 9 512 10 1024

3 Output manipulator The manipulators that take parameters, you need to include setw(n) Set the width of the next field. Transient property (it affects only the next value). setprecision(digits) Set the precision for the stream to digits. Persistent property (it remains in effect until it is explicitly changed.) setfill(ch) Set the fill character for the stream to ch. By default, space are added to.. Persistent property.

4 Output manipulator (Persistent property) left right fixed (3.14) scientific (3.14E+00) showpoint noshowpoint showpos (+) noshowpos uppercase (E) nouppercase (e) boolalpha (true) noboolalpha

5 Formatted output #include using namespace std; const double PI = 3.14159265358979323846; const double SPEED_OF_LIGHT = 2.99792458E+8; const double FINE_STRUCTURE = 7.2573525E-3;

6 void printPrecisionTable() { cout << " prec | pi | speed of light | fine-structure" << endl; cout << "------+--------------+------------------+----------------" << endl; for (int prec = 0; prec <= 6; prec += 2) { cout << setw(4) << prec << " |"; cout << " " << setw(12) << setprecision(prec) << PI << " |"; cout << " " << setw(16) << setprecision(prec) << SPEED_OF_LIGHT << " |"; cout << " " << setw(14) << setprecision(prec) << FINE_STRUCTURE << endl; }

7 int main() { cout << uppercase << right; cout << "Default format:" << endl << endl; printPrecisionTable(); cout << endl << "Fixed format:" << fixed << endl << endl; printPrecisionTable(); cout << endl << "Scientific format:" << scientific << endl << endl; printPrecisionTable(); return 0; }

8 Default format: prec | pi | speed of light | fine-structure ------+---------------------+-------------------------+------------------------ 0 | 3 | 3E+08 | 0.007 2 | 3.1 | 3E+08 | 0.0073 4 | 3.142 | 2.998E+08 | 0.007257 6 | 3.14159 | 2.99792E+08 | 0.00725735

9 Fixed format: prec | pi | speed of light | fine-structure ------+---------------------+----------------------------+-------------------- 0 | 3 | 299792458 | 0 2 | 3.14 | 299792458.00 | 0.01 4 | 3.1416 | 299792458.0000 | 0.0073 6 | 3.141593 | 299792458.000000 | 0.007257

10 Scientific format: prec | pi | speed of light | fine-structure ------+---------------------+-----------------------+--------------------- 0 | 3E+00 | 3E+08 | 7E-03 2 | 3.14E+00 | 3.00E+08 | 7.26E-03 4 | 3.1416E+00 | 2.9979E+08 | 7.2574E-03 6 | 3.141593E+00 | 2.997925E+08 | 7.257352E-03

11 Formatted input int limit; cout << "Enter exponent limit: "; cin >> limit; // extraction operator >> char ch; cout << "Enter a single character: "; cin >> noskipws >> ch; skipwsPersistent property noskipwsPersistent property

12 Data files ไฟล์ข้อมูลถูกสร้างขึ้นเพื่อเก็บรวบรวมข้อมูลต่างๆเข้าไว้ ด้วยกัน ไฟล์ข้อมูลมักจะถูกเก็บอยู่ในหน่วยความจำภายนอก เช่น Hard Disk, CD หรือ DVD เป็นต้น เมื่อโปรแกรมทำการอ่าน (read) ข้อมูล ข้อมูลจะถูก สำเนาจากอุปกรณ์ภายนอกเข้ามายังหน่วยความจำ ภายใน เมื่อโปรแกรมทำการบันทึก (write) ข้อมูล ข้อมูลจะถูก สำเนาจากหน่วยความจำภายในไปยังอุปกรณ์ภายนอก ในการเคลื่อนย้ายข้อมูลนี้จะมีการใช้พื้นที่หน่วยความจำ ที่กันเอาไว้เป็นพิเศษสำหรับพักข้อมูล ซึ่งเราเรียกว่า buffer การอ่านข้อมูลจากอุปกรณ์ภายนอกนั้นปกติจะอ่าน เป็น block (512 byte) ข้อมูลที่อ่านได้จะถูกเก็บไว้ใน buffer ก่อนที่โปรแกรมจะอ่านข้อมูลจาก buffer แล้ว นำไปประมวลผลต่อไป

13 Data files ในทางกลับกันเมื่อโปรแกรมต้องการบันทึกข้อมูลลง ฮาร์ดดิสค์ โปรแกรมจะบันทึกข้อมูลลง buffer ซึ่ง buffer จะทำหน้าที่ในการพักข้อมูลไว้จนกว่าข้อมูลจะเต็ม block แล้วจึงมีการนำข้อมูลจาก buffer บันทึกลงฮาร์ดดิสค์ จริงๆ การทำงานที่เกี่ยวข้องกับ buffer จะดูแลโดยโปรแกรม พิเศษที่เราเรียกกันว่า device drivers ซึ่งโปรแกรมนี้จะให้ มาโดยผู้สร้างอุปกรณ์ต่างๆ หรือมาพร้อมกับ Operating System โปรแกรมจะมองข้อมูลที่ส่งมาจากอุปกรณ์ (e.g. keyboard) หรือไฟล์ เสมือนว่าเป็นสายข้อมูล (data stream) และเมื่อโปรแกรมต้องการส่งข้อมูลให้กับ อุปกรณ์ (e.g. CRT) หรือไฟล์ก็จะส่งเป็นสายข้อมูล (data stream) ไปให้ ในระหว่างการรับส่งข้อมูลกับ buffer หากมีข้อผิดพลาด เกิดขึ้นก็อาจจะทำให้มีข้อมูลบางส่วนตกค้างอยู่ใน buffer ได้

14 Stream is an abstract representation of an input or output device that is a source of, or destination for, data.

15

16 Using file streams Reading or Writing a file in C++ – Declare a stream variable to refer to the file ifstream infile; ofstream outfile; – Open the file infile.open(“student.txt”); infile.open(filename.c_str()); // if the name of file stored in a // string variable named filename – Transfer the data – Close the file Infile.close();

17

18 Single character I/O Input stream – ใช้คำสั่ง get() ในการอ่านข้อมูล 1 ตัวอักษร เช่น ch = infile.get(); – คำสั่ง get จะ return ข้อมูลเป็น int เนื่องจาก ต้องการ return ข้อมูลบางตัวที่อยู่นอกขอบเขต ตัวอักษร เช่น EOF (End Of File) Output stream ใช้คำสั่ง put(ch) ในการบันทึก ข้อมูล ch – ใช้คำสั่ง put(ch) ในการบันทึกข้อมูล 1 ตัวอักษร เช่น outfile.put(ch);

19 Single character I/O int ch; while ((ch = infile.get()) != EOF) { cout.put(ch); }

20 Single character I/O #include using namespace std;

21 string promptUserForFile(ifstream & infile, string prompt = ""); int main() { ifstream infile; promptUserForFile(infile, "Input file: "); while (true) { int ch = infile.get(); if (ch == EOF) break; cout.put(ch); } infile.close(); return 0; }

22 string promptUserForFile(ifstream & infile, string prompt) { while (true) { cout << prompt; string filename; getline(cin, filename); infile.open(filename.c_str()); if (!infile.fail()) return filename; infile.clear(); cout << "Unable to open file. Try again." << endl; if (prompt == "") prompt = "Input file: "; }

23 Single character I/O How to read an integer represented as a string of decimal digits? The library implementation cannot know that the number is finished until it reads character that is not a digit. That character may be part of subsequent input. infile.unget();

24 Line oriented I/O while (true) { string line; getline(infile, line); if (infile.fail()) break; cout << line << endl; }

25 Formatted I/O int main() { ifstream infile; promptUserForFile(infile, "Input file: "); int total = 0; while (true) { int value; infile >> value; if (infile.fail()) break; // 1. reaching EOF 2. wrong char total += value; } infile.close(); cout << "The sum is " << total << endl; return 0; } // The program give no indication that an error has occurred.

26 Formatted I/O if (!infile.eof()) { error("Data error in file"); }

27 int main() { ifstream infile; promptUserForFile(infile, "Input file: "); int total = 0; while (true) { string line; getline(infile, line); if (infile.fail()) break; total += stringToInteger(line); } infile.close(); cout << "The sum is " << total << endl; return 0; }

28 String streams int stringToInteger(string str) { istringstream stream(str); int value; stream >> value >> ws; if (stream.fail() || !stream.eof()) { error("stringToInteger: Illegal integer format"); } return value; }

29 String streams string integerToString(int n) { ostringstream stream; stream << n; return stream.str(); }

30 Console input int limit; cout << "Enter exponent limit: "; cin >> limit; What happens if the user tries to enter the value 16 but types the letter t instead of the digit 6?

31 int getInteger(string prompt) { int value; string line; while (true) { cout << prompt; getline(cin, line); istringstream stream(line); stream >> value >> ws; if (!stream.fail() && stream.eof()) break; cout << "Illegal integer format. Try again." << endl; } return value; }


ดาวน์โหลด ppt Streams. Formatted output cout << right << setw(2) << i << setw(8) << raiseToPower(2, i) << endl; // Insertion operator << This program lists powers of.

งานนำเสนอที่คล้ายกัน


Ads by Google