使用格式操作符设置数值的输出格式
以cout为输出目的的数值可以使用格式操作符来设置数值的格式。一般而言,数值的格式包括4个特征:
1、靠左或靠右对齐2、以一般浮点数表示法或者科学记数法显示3、占多少字段4、小数点以下的有效位数数目
下表列出了常用的格式操作符:
例程:
#include "stdafx.h"
#include <iostream>
#include <iomanip>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
//------------------
cout << '|' << setw(2) << 3 << '|' << endl;
cout << '|' << setw(5) << 158 << '|' << endl;
cout << '|' << setw(5) << 69.72 << '|' << endl;
cout << setiosflags(ios::left)
<< '|' << setw(5) << 876 << '|' << endl;//左对齐
cout << setiosflags(ios::fixed)
<< setprecision(2)
<< '|' << setw(6) << 133.456 << '|' << endl;//四舍五入处理
cout << "------------------"<<endl;
//------------------
//------------------
//以十六进制法表示
cout << hex << '|' << 14 << '|' << endl;//十六进制
//scientific使用科学表示法
/*
setprecision(n)将浮点精度设置为n位数,不管是一般表示法或科学表示法,实数的小数点后
的后的位数以四舍五入处理,,如果数字不够则以0补足。对于整数则没有作用。
*/
cout << scientific
<< setprecision(2) << '|' << setw(5)
<< 46218.542 << '|' << endl;
/*
一般浮点数表示
*/
cout << setprecision(4)
<< fixed << showpoint << '|' << setw(13)
<< 64.7766 << '|' << endl;
cout << "------------------" << endl;
//------------------
//------------------
double t1 = 253.0;
double t2 = 0.123456789;
cout << "(1)The value of t1 is:"
<< setprecision(2) << t1 << endl;
cout << "(2)The value of t1 is:"
<< setprecision(3)
<<showpoint<< t1 << endl;
cout << "(3)The value of t1 is:"
<< setprecision(5)<<showpoint
<<fixed<< t1 << endl;
cout << "(4)The value of t1 is:"
<< setprecision(5)<<showpoint
<<scientific<< t1 << endl;
cout << "------------------" << endl;
//------------------
//------------------
cout << "(1)The value of t2 is:"
<< setprecision(2) << t2 << endl;
cout << "(2)The value of t2 is:"
<< setprecision(3)
<< showpoint << t2 << endl;
cout << "(3)The value of t2 is:"
<< setprecision(5) << showpoint
<< fixed << t2 << endl;
cout << "(4)The value of t2 is:"
<< setprecision(5) << showpoint
<< scientific << t2 << endl;
return 0;
}
输出:
文件存储格式的设置
文件的存储与cout的用法相同,都是使用数据流的概念,只是把cout以输出文件数据流取代而已。
以下程序以矩阵的存储为例来说明存储大量数据的输出格式问题:
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
const int M = 4;
const int N = 5;
void RecMatrix(char*, double[][N], int, int);
int _tmain(int argc, _TCHAR* argv[])
{
double Matrix[M][N];
char *Filename = "Record.txt";
for (int i=0; i < M;i++)
{
for (int j = 0; j < N;j++)
{
Matrix[i][j] = (i + j*j + 0.5) / (i + j + 2);
}
}
char Ch;
cout << "你要将矩阵存在" << Filename
<< "中吗?(Y/N)" << endl;
cin >> Ch;
if (Ch == 'Y' || Ch == 'y')
{
RecMatrix(Filename, Matrix, M, N);
}
else
cout << "没有存档" << endl;
return 0;
}
void RecMatrix(char *FileNameOut,
double A[][N], int M, int N)
{
ofstream FileOutput;
FileOutput.open(FileNameOut, ios::out);
if (!FileOutput)
{
cout << "文件:" << FileNameOut
<< "存档失败!" << endl;
exit(1);
}
FileOutput << setprecision(4) << right
<< showpoint << fixed;
for (int i = 0; i < M;i++)
{
FileOutput << "第" << i + 1 << "行:";
for (int j = 0; j < N;j++)
{
FileOutput << setw(8) << A[i][j] << " ";
}
FileOutput << endl;
}
FileOutput.close();
cout << "成功存于文件"
<< FileNameOut << "内。" << endl;
}
运行后命令窗口输入n,结果:
运行后命令窗口输入Y或y,结果:
打开工程所在文件夹可看到: