题目:
[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]
复习代码:
[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;
}