使用cout格式化输出字符串

 

/********************************************************************

    created:    2006/04/29

    created:    29:4:2006   23:33

    filename:   e:\work\C++\输入输出和文件\CoutFormatSample.cpp

    file path:  e:\work\C++\输入输出和文件

    file base:  CoutFormatSample

    file ext:   cpp

    author:     xuzhong

   

    purpose:   

*********************************************************************/

 

#include "StdAfx.h"

#include ".\coutformatsample.h"

 

using namespace std;

 

// public

CCoutFormatSample::CCoutFormatSample(void)

{

}

 

CCoutFormatSample::~CCoutFormatSample(void)

{

}

 

 

void CCoutFormatSample::Run(void)

{

    this->P_17_3();

    this->P_17_4();

    this->P_17_5();

    this->P_17_6();

    this->P_17_7();

    this->P_17_8();

}

 

 

 

// private

 

// 例子在 P615 。修改显示时使用的计数系统

void CCoutFormatSample::P_17_3(void)

{

    cout << _T(" 17.3 修改显示时使用的计数系统") << endl << endl;

    cout << "Enter an integer : ";

    int n;

    // cin >> n;

    n = 255;

 

    dec(cout);              // cout << dec;

    cout << "dec : " << n << endl;

 

    oct(cout);              // cout << oct;

    cout << "oct : " << n << endl;

 

    hex(cout);              // cout << hex ;

    cout << "hex : " << n << endl;

 

    dec(cout);              // 不要影响其它人

 

    cout << endl << endl;

}

 

 

void CCoutFormatSample::P_17_4(void)

{

    cout << _T(" 17.4 调整字段宽度") << endl << endl;

 

    int w = cout.width(30);

    cout << "default field width = " << w << ":" << endl;

    cout.width(5);

    cout << "N" << ": ";

    cout.width(8);

    cout << "N * N" << ":" << endl;

 

    for(long i=1; i<=100; i*=10)

    {

        cout.width(5);

        cout << i << ": ";

        cout.width(8);

        cout << i * i << ":" << endl;

    }

 

    cout.width();

 

    cout << endl << endl;

}

 

 

// cout.fill的用法

void CCoutFormatSample::P_17_5(void)

{

    cout << _T(" 17.5 填充字符") << endl << endl;

    cout.fill('*');

 

    int w = cout.width(30);

    cout.width(5);

    cout << "N" << ": ";

    cout.width(8);

    cout << "N * N" << ":" << endl;

 

    for(long i=1; i<=100; i*=10)

    {

        cout.width(5);

        cout << i << ": ";

        cout.width(8);

        cout << i * i << ":" << endl;

    }

 

    cout.width();

 

 

    cout << endl << endl;

}

 

 

// cout.precision

void CCoutFormatSample::P_17_6(void)

{

    cout << _T(" 17.6 设置浮点数的显示精度") << endl << endl;

    float f1 = 23.3232;

    float f2 = 1.9 + 8.0 / 9.0;

 

    cout << "f1 = " << f1 << endl;

    cout << "f2 = " << f2 << endl;

 

    // 设置精度为2

    cout.precision(2);

 

    cout << "f1 = " << f1 << endl;

    cout << "f2 = " << f2 << endl;

 

    // 解除设定

    cout.precision();

    cout << endl << endl;

}

 

// cout.setf

void CCoutFormatSample::P_17_7(void)

{

    cout << _T(" 17.7 设置精度为2打印末位的0和小数点") << endl << endl;

    float f1 = 23.3232;

    float f2 = 1.9 + 8.0 / 9.0;

 

    cout << "f1 = " << f1 << endl;

    cout << "f2 = " << f2 << endl;

 

    // 设置精度为2打印末位的0和小数点

    cout.setf(ios_base::showpoint);

 

    cout << "f1 = " << f1 << endl;

    cout << "f2 = " << f2 << endl;

 

    // 解除设定

    cout.setf(ios_base::unitbuf);

    cout << endl << endl;

}

 

 

// cout.setf 高级用法

void CCoutFormatSample::P_17_8()

{

 

    cout << _T(" 17.7 setf 高级用法") << endl << endl;

 

    cout.setf(ios_base::showpos);

    cout << 63 << endl;

    cout.setf(ios_base::uppercase);

    cout << "A string" << endl;

    cout.setf(ios_base::showbase);

    cout << 437 << endl;

    cout.setf(ios_base::boolalpha);

    cout << true << endl;

    cout << false << endl;

 

    cout << endl << endl;

 

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
cout是C++中的标准输出流对象,可以用于格式化输出。为了按照一定的格式进行输出,可以使用流操作算子或者成员函数进行控制。使用流操作算子时,可以使用一系列的格式控制符来指定输出的格式。例如,%X可以用于按十六进制输出整数,%.2f可以用于输出浮点数时保留小数点后面两位,setw()可以用于指定输出的宽度等。 需要注意的是,每次使用setw()设置输出宽度时,只会影响下一次的输出。如果需要多次指定输出宽度,需要在每次输出前使用setw()来设置。此外,setw()也可以影响cin在读入字符串时的行为。 以下是一个示例程序,演示了如何使用setw()和流操作算子进行格式化输出: #include <iostream> #include <iomanip> using namespace std; int main() { int num = 123; float f = 3.14159; cout << "Formatted output:" << endl; cout << "Number in hexadecimal: " << hex << num << endl; cout << "Float with 2 decimal places: " << fixed << setprecision(2) << f << endl; cout << "Number with width of 6, left-padded with 0: " << setw(6) << setfill('0') << num << endl; return 0; } 运行这段代码将会输出以下结果: Formatted output: Number in hexadecimal: 7b Float with 2 decimal places: 3.14 Number with width of 6, left-padded with 0: 000123 在上述示例中,我们先使用hex设置输出为十六进制形式,然后使用fixed和setprecision(2)设置浮点数的小数点位数,最后使用setw(6)和setfill('0')设置输出宽度为6,左侧用0填充。通过这些控制符,我们可以实现各种格式化的输出。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值