2023.5.4编程一小时打卡
一、问题描述:
输入一个整数,以八进制形式输入,分别以十进制和十六进制显示;
输出字符串“I am a student!”,设置输出位宽为20,使用符号“*”填充;
输出浮点数3.1415926,分别以浮点数和二进制形式进行输出,并分别设置小数点后的位数为8,6,4位。
使用流对象的成员函数实现
二、解题思路:
在主函数中,定义一个数,分别用dec,hex,setbase(8)输出,输出字符串,输出setw()为10,填充字符setfill()为‘*’,输出浮点数,输出保留小数setprecision为8,4位的科学计数法scientific的方式。以上全部用流对象的成员函数去实现。
三、代码实现:
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 }