C++格式化输出(详解版)

C++格式化输出(详解版),第1张

概述前面已经介绍过可用于流对象的 I/O *** 作符,特别是 setw(n)、fixed、showpoint、setprecision(n)、left 和 right *** 作符。这些 *** 作符都可以在 fstream、ofstream 和 ostringstream 对象上使用。 为了这些 *** 前面已经介绍过可用于流对象的 I/O *** 作符,特别是 setw(n)、fixed、showpoint、setprecision(n)、left 和 right *** 作符。这些 *** 作符都可以在 fstream、ofstream 和 ostringstream 对象上使用。

为了这些 *** 作符的用法,可以假设需要一个函数,该函数釆用一个 double 类型的实参,表示一个项目的价格(以美元为单位),并返回一个以美元符号 $ 开头的字符串,将价格的数值精确到小数点后两位。例如,如果将 12.5 作为金额参数传递给函数,则函数将返回字符串 $12.50。使用 ostringstream 即可轻松编写该函数:
string dollarFormat(double amount){    //创建 ostringstream 对象    ostringstream outStr;    //设置信息格式并写入outStr    outStr << showpoint << fixed << setprecision(2);    outStr << '$' << amount;    //提取并返回outStr中的字符串    return outStr.str ();}
下面的程序使用 dollarFormat 函数来编写一个整齐格式化的价格表。价格是以二维数组给出的。该程序设置了每个价格的格式并打印了一个包含所有价格的表格,每个价格均右对齐,列宽为 10:
//This program demonstrates the use of an ostringstream object to do sophisticated formatting.#include <iostream>#include <iomanip>#include <sstream>using namespace std;string dollarFormat(double); // Function Prototypeint main(){    const int ROWS = 3,ColS = 2;    double amount[ROWS][ColS] = {184.45,7,59.13,64.32,7.29,1289 };    // Format table of dollar amounts right justfIEd in columns of wIDth 10    cout << right;    for (int row = 0; row< ROWS; row++)    {        for (int column = 0; column < ColS; column++)        {            cout << setw(10) << dollarFormat(amount[row][column]);        }        cout << endl;    }    return 0;}string dollarFormat(double amount){    // Create ostringstream object    ostringstream outStr;    // Set up format information and write to outStr.    outStr << showpoint << fixed << setprecision(2);    outStr << '$' << amount;    // Extract and return the string insIDe outStr.    return outStr.str();}
程序输出结果:
   4.45     .00    .13    .32     .29  89.00
表 1 列出了可以与 C++ 流对象一起使用的 I/O *** 作符列表,并给出了简要含义描述。

表 1 C++流对象
*** 作符描 述
Dec以十进制格式显示后续数字
endl写入新行并冲刷输出流
fixed使用固定点表示法表示浮点数
flush冲刷输出流
hex以十六进制输入或输出
left左对齐输出
oct以八进制输入或输出
right右对齐输出
scIEntific使用科学表示法表示浮点数
setfill(ch)使用 ch 填充字符
setprecision(n)将浮点精度设置为n
setw(n)将输出字段的宽度设置为n
showbase打印数字时显示基数(进制)
noshowbase打印数字时不要显示基数(进制)
showpoint强制显示小数点和尾随零
noshowpoint如果可能的话,不打印结尾零和小数点
showpos在非负数前面打印一个加号
noshowpos在非负数前面不打印加号

前面我们已经介绍过表 1 中的部分 *** 作符。oct、dec 和 hex *** 作符可以用于输入和输出流。它们允许使用八进制、十进制或十六进制数字系统读取或写入数字。

下面的程序演示了如何使用 cin 和 cout 读取和写入十进制、十六进制和八进制值。
//This program demonstrates @R_404_5983@ and output of numbers//using the octal,decimal,and hexadecimal number systems.#include <iostream>#include <iomanip>using namespace std;int main(){    int a,b;    //Read two decimals and print hex and octal equivalents    cout << "Enter two decimal numbers: ";    cin >> a >> b;    cout << "The numbers in decimal: " << a << '\t1' << b << endl;    cout << "The numbers in hexadecimal: " << hex << showbase << a << '\t' << b << endl;    cout << "The numbers in octal: " << oct << a << '\t' << b << endl;    // Read some hexadecimals and print their decimal equivalents    cout << "Enter two hexadecimal numbers:";    cin >> hex >> a >> b;    cout << "You entered decimal " << dec << a << '\t' << b << endl;    // Read some octals and print their decimal equivalents    cout << "Enter two octal numbers:";    cin >> oct >> a >> b;    cout << "You entered decimal " << dec << a << '\t' << b << endl;    return 0;}
程序输出结果为:

Enter two decimal numbers: 23 45
The numbers in decimal: 23      45
The numbers in hexadecimal: 0x17        0x2d
The numbers in octal: 027       055
Enter two hexadecimal numbers:17 2d
You entered decimal 23  45
Enter two octal numbers:27 55
You entered decimal 23 45

前面已经介绍过,当一个程序将数据写入一个打开的文件时,数据不会直接进入文件。相反,数据存储在与文件相关的输出缓冲区中,稍后在一个冲刷缓冲区的过程中才会传送到文件。通常情况下,如果缓冲区已满或文件关闭时,缓冲区才会被冲刷。endl 和 flush *** 作符允许程序员随时冲刷缓冲区,从而强制将缓冲的数据传输到文件。

例如,以下语句将冲刷输出流的缓冲区:

outfile << flush;

scIEntific *** 作符使得浮点数以科学计数法表示,即形式为 d.dddEdd。填充字符是当打印的数字未填满要打印的整个字段时写入的字符。默认情况下,填充字符为空。程序员可以使用 setfill *** 作符来指定不同的填充字符。示例如下:

outfile << setfill ('%');

该语句将使用百分比字符(%)作为填充字符。
总结

以上是内存溢出为你收集整理的C++格式化输出(详解版)全部内容,希望文章能够帮你解决C++格式化输出(详解版)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/1231096.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-06
下一篇2022-06-06

发表评论

登录后才能评论

评论列表(0条)

    保存