有三个重要的对象
cin cout cerr
以及一些manipulator有的带参数,有的不带参数。
/*
* This program demonstrates various options for floating-point output
* by displaying three different constants (pi, the speed of light in meters/second,
* and the fine-structure constant). These constants
* are chosen because they illustrate a range of exponent scales.
*/
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
const double PI = 3.1415926535897;
const double SPEED_OF_LIGHT = 2.99792458e+8;
const double FINE_STRUCTURE = 7.2573525e-3;
void printPrecisionTable();
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;
}
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;
}
}
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
Fixed format: