C++基础复习之一 I/O控制

题目:
[quote]1、定义常整数123456
①分别用10/16/8进制输出三行
②位宽均为10,填充字符*。
③最后一行左对齐输出,并显示正数前面的正号


2、定义常量12.3456789
①输出8位有效位数的浮点数
②定点方式+4位小数表示的数
③指数形式+4位小数位表示的数。


3、以大写方式显示" abcdefg",小写方式显示"ABCDEFG",并忽略其忽略前导空白。[/quote]


提示:
[quote]1、dec hex oct setw() setfill()
setiosflags(ios::left) setiosflags(ios:showpos)

2、setprecision() setiosflags(ios::fixed) setprecision(ios::scientific)

3、setiosflags(ios::uppercase) setiosflags(ios::lowercase)
setiosflags(ios::skipws)[/quote]


复习代码:

#include <iostream>
#include <iomanip> //要用到格式控制符

using namespace std;

int main()
{
//10/16/8 进制
cout << "---10/16/8进制---" << endl;
int number = 1001;
cout << "Decimal:" << dec << number << endl
<< "Hexadecimal:" << hex << number << endl
<< "Octal:" << oct << number << endl << endl;

//---填充字符 位宽
cout << "---填充字符/位宽设置---" << endl;
cout << setfill('*')
<< setw(2) << 21 << endl
<< setw(3) << 21 << endl
<< setw(4) << 21 << endl;

cout << setfill(' ') << endl << endl; //恢复默认设置

//---设置小数精度
double amount = 6.5487455;

cout << amount << endl;
cout << setprecision(0) << "setprecision(0) " << amount << endl
<< setprecision(1) << "setprecision(1) " << amount << endl
<< setprecision(2) << "setprecision(2) " << amount << endl
<< setprecision(3) << "setprecision(3) " << amount << endl
<< setprecision(4) << "setprecision(4) " << amount << endl;

cout << setiosflags(ios::fixed);
cout << setprecision(6) << amount << endl;

//---
cout << setiosflags(ios::scientific) << amount << endl;
cout << setprecision(6) << endl << endl; //重新设置成原默认设置

//---左右对齐
cout << setiosflags(ios::left)
<< setw(5) << 1
<< setw(5) << 2
<< setw(5) << 3 << endl << endl;

//---显示小数点
cout << 10.0/5 << endl;

cout << setiosflags(ios::showpoint)
<< 10.0/5 << endl << endl;

//---显示+
cout << 10 << " " << -20 << endl;

cout << setiosflags(ios::showpos)
<< 10 <<" " << -20 << endl;

setiosflags(ios::skipws);
cout<<" hello,my boy~";

return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值