2023.4.25编程一小时打卡

发布时间 2023-04-25 20:31:16作者: 信2211-8李欣垚

一、问题描述:

格式输出:

输入一个整数以八进制形式输入分别以十进制和十六进制显示

输出字符串I am a student!”,设置输出位宽为20,使用符号“*”填充;

输出浮点数3.1415926,分别以浮点数和二进制形式进行输出,并分别设置小数点后的位数为8,6,4位。

 

二、解题思路:

首先,根据题意定义一个整数,再用#include<iomanip>头文件中的输入输出格式进行打印输出,八进制oct、十进制dec、十六进制hex;位宽函数为setw();填充为setfill(‘’);最后,在主函数中根据题目要求进行打印输出。验证代码的运行。

三、代码实现:

 1 #include<iostream>
 2 #include<string>
 3 #include<vector>
 4 #include<iomanip>
 5 #include<bitset>
 6 using namespace std;
 7 int main()
 8 {
 9     int n;
10     cin>>oct>>n;
11     cout<<dec<<n<<" "<<hex<<n<<endl;
12     string s="I am a student!";
13     cout<<setfill('*')<<setw(20)<<s<<endl;
14     double x=3.1415926;
15     cout<<fixed<<setprecision(8)<<x<<" ";
16     cout<<fixed<<setprecision(6)<<x<<" ";
17     cout<<fixed<<setprecision(4)<<x<<endl;
18     long y=3.1415926;
19     cout<<bitset<8>(y)<<endl;
20     cout<<bitset<6>(y)<<endl;
21     cout<<bitset<4>(y)<<endl;
22     return 0;
23 }