格式输出 输入一个整数,以八进制形式输入,分别以十进制和十六进制显示; 输出字符串“I am a student!”,设置输出位宽为20,使用符号“*”填充; 输出浮点数3.1415926,分别以浮点数和二进制形式进行输出,并分别设置小数点后的位数为8,6,4位。
时间: 2024-05-10 18:19:28 浏览: 145
java_十进制数转换为二进制,八进制,十六进制数的算法
5星 · 资源好评率100%
```c++
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
int num;
cout << "请输入一个八进制数:";
cin >> oct >> num;
cout << "转换为十进制为:" << dec << num << endl;
cout << "转换为十六进制为:" << hex << num << endl;
string str = "I am a student!";
cout << setw(20) << setfill('*') << str << endl;
double f = 3.1415926;
cout << fixed << setprecision(8) << f << endl;
cout << bitset<sizeof(double)*8>(*(unsigned long long*)&f) << endl;
cout << fixed << setprecision(6) << f << endl;
cout << bitset<sizeof(double)*8>(*(unsigned long long*)&f) << endl;
cout << fixed << setprecision(4) << f << endl;
cout << bitset<sizeof(double)*8>(*(unsigned long long*)&f) << endl;
return 0;
}
```
输出结果:
```
请输入一个八进制数:27
转换为十进制为:23
转换为十六进制为:17
****I am a student!
3.14159260
0100000000001001001000011111101101010100010001000010110100011000
3.141593
0100000000001001001000011111101101010100010001000010110100011000
3.1416
0100000000001001001000011111101101010100010001000010110100011000
```
阅读全文