当前位置:网站首页>C语言日记 7 输入/输出格式控制
C语言日记 7 输入/输出格式控制
2022-08-02 14:03:00 【宇 -Yu】
例 2-10:
#include <iostream>
#include <iomanip>
//io:输入/输出 manip:manipulator(操纵器)的缩写
//(在c++上只能通过输入缩写才有效)只要对I/O进行格式控制,就必须要写
//具体详情,见注释链接1
using namespace std;
int main()
{
int a;
cin >> a;
cout << dec << a << endl;// decimalism 十进制
cout << oct << a << endl;// octonary 八进制
cout << hex << a << endl;// hexadecimal 十六进制
return 0;
}
注释链接1:iomanip解释_码里奥特慢的博客-CSDN博客_iomanip
另外,如果后面不重新定义对输出进制的规定,那么后面语句将一直遵循上一个对输出进制的规定的定义进行输出。例:
#include <iostream>
#include <iomanip>
//io:输入/输出 manip:manipulator(操纵器)的缩写
//(在c++上只能通过输入缩写才有效)只要对I/O进行格式控制,就必须要写
//具体详情,见:iomanip解释_码里奥特慢的博客-CSDN博客_iomanip
using namespace std;
int main()
{
int a;
cin >> a;
cout << oct << a << endl;// octonary 八进制
cout << a << endl;
return 0;
}
随便输入一个数,例如12,则:
例 2-11:
#include <iostream>
#include <iomanip>
//io:输入输出 manip:manipulator(操纵器)的缩写
//(在c++上只能通过输入缩写才有效)只要对I/O进行格式控制,就必须要写
//具体详情,见:iomanip解释_码里奥特慢的博客-CSDN博客_iomanip
using namespace std;
int main()
{
int a = 15;
cout << showbase;//指定输出计数制的前缀
cout << dec << a << endl;//输出 a的十进制形式 15
cout << oct << a << endl; //前缀0
cout << hex << a << endl; //前缀0x
cout << noshowbase;//指定不输出计数制的前缀,此为默认方式
cout << dec << a << endl;
cout << oct << a << endl;
cout << hex << a << endl;
return 0;
}
没有前缀操作符默认没有前缀,十进制没有前缀。
例 2-12:
#include <iostream>
#include <iomanip>
//io:输入输出 manip:manipulator(操纵器)的缩写
//(在c++上只能通过输入缩写才有效)只要对I/O进行格式控制,就必须要写
//具体详情,见:iomanip解释_码里奥特慢的博客-CSDN博客_iomanip
using namespace std;
int main()
{
int a = 15;
cout << showbase << hex;
cout<< a << endl;
cout << uppercase << a << endl;
cout << nouppercase << a << endl;
return 0;
}
例 2-13:
#include <iostream>
#include <iomanip>
//io:输入输出 manip:manipulator(操纵器)的缩写
//(在c++上只能通过输入缩写才有效)只要对I/O进行格式控制,就必须要写
//具体详情,见:iomanip解释_码里奥特慢的博客-CSDN博客_iomanip
using namespace std;
int main()
{
double x = 1.23456789;
cout << x << endl;
//从这里我们可以发现,默认的有效数字个数为6位
//并且,该操作符运算遵循四舍五入法则
//[改成(float x = 1.23456789;)运行结果也一样]
cout << setprecision(3) << x << endl;
//该操作符运算遵循四舍五入法则。代入:
//cout << setprecision(4) << x << endl;
//即可验证
cout << setprecision(12) << x << endl;
//从这里我们可以发现
// 如果操作符规定的有效数字个数大于给出实数最大精度的话
// 就输出最大精度的实数,不用后面加零,也不用四舍五入。
return 0;
}
有效数字:
在一个数中,从该数的第一个非零数字起,直到末尾数字止的数字称为有效数字。
precise:准确的;精确的;
precision:精确(度);
另外,如果后面不重新定义对有效数字个数的规定,那么后面语句将一直遵循上一个定义有效数字个数的标准。例:
#include <iostream>
#include <iomanip>
//io:输入输出 manip:manipulator(操纵器)的缩写
//(在c++上只能通过输入缩写才有效)只要对I/O进行格式控制,就必须要写
//具体详情,见:iomanip解释_码里奥特慢的博客-CSDN博客_iomanip
using namespace std;
int main()
{
double x = 1.23456789, y = 4.5647687;
cout<< x << endl;
cout << setprecision(3) << x << endl;
cout << y<< endl;
return 0;
}
结合前面C语言日记 5 运算符和表达式(含类型转换(例2-6))中:
#include <iostream>
#include<iomanip>
using namespace std;
void main()
{
int a, b = 322;//整型
float x, y = 8.88;//浮点型,即实(数)型
char cl = 'k', c2;//字符型
a = y; //实型转整型,a=8
x = b;//整型转实型,x=322.0
a = cl;//字符型转整型,把字符k对应的ASCII码数值ASC(k)=107赋给a,a=107
c2 = b;//整型转字符型
//把数字322对应二进制数(101000010)低八位(01000010)作为ASCII码数值(66)
//把ASCII码数值(66)对应的字符Char(66)=B赋给c2
cout << a << "," << x << "," << a << "," << c2 << endl;
}
实数后面的小数点不显示。
利用我们这里学的setprecision()进行修改:
#include <iostream>
#include<iomanip>
using namespace std;
void main()
{
int a, b = 322;//整型
float x, y = 8.88;//浮点型,即实(数)型
char cl = 'k', c2;//字符型
a = y; //实型转整型,a=8
x = b;//整型转实型,x=322.0
a = cl;//频率字符型转整型,把字符k对应的ASCII码数值ASC(k)=107赋给a,a=107
c2 = b;//整型转字符型,把数字322对应二进制数(101000010)低八位(01000010)作为ASCII码数值(66),把ASCII码数值(66)对应的字符Char(66)=B赋给c2
cout << a << ",";
cout << setprecision(5) << x;
cout << "," << c2 << endl;
}
实数输出形式依然无改变,不是322.00
即使把两句都加上setprecision()也没用:
#include <iostream>
#include<iomanip>
using namespace std;
void main()
{
int a, b = 322;//整型
float x, y = 8.88;//浮点型,即实(数)型
char cl = 'k', c2;//字符型
a = y; //实型转整型,a=8
x = b;//整型转实型,x=322.0
a = cl;//频率字符型转整型,把字符k对应的ASCII码数值ASC(k)=107赋给a,a=107
c2 = b;//整型转字符型,把数字322对应二进制数(101000010)低八位(01000010)作为ASCII码数值(66),把ASCII码数值(66)对应的字符Char(66)=B赋给c2
cout << setprecision(5) << a << ",";
cout << setprecision(5) << x;
cout << "," << c2 << endl;
}
你奶奶的,怎么办?
例 2-14:
#include <iostream>
#include <iomanip>
//io:输入输出 manip:manipulator(操纵器)的缩写
//(在c++上只能通过输入缩写才有效)只要对I/O进行格式控制,就必须要写
//具体详情,见:iomanip解释_码里奥特慢的博客-CSDN博客_iomanip
using namespace std;
int main()
{
double x = 1.23456789;
cout << x << endl;
//默认的有效数字个数为6位
cout << fixed << x << endl;
//定点数(fixed-point number):
// 小数点位置固定的数(小数点后面的位数是固定的),详见注释链接2
//定点数格式输出的有效数字规定标准网上查不到
//所以暂时就按照书P34写的(作为标准):
//"不论是使用 fixed 还是使用 scientific,小数部分都是6位小数"
cout << scientific << x << endl;//同理
cout << scientific << setprecision(8) << x << endl;
//fixed()函数与setprecision(int n )函数结合使用是保留小数点后的位数
//小数点的保留采用四舍五入
return 0;
}
注释链接2:定点数(fixed-point number)_chaoguo1234的博客-CSDN博客_什么是定点数
书P34:
不论是使用 fixed 还是使用 scientific,小数部分都是6位小数.
此(使用 fixed /scientific)时,
使用setprecision(n)不再用于设置有效数字的个数,而是设置小数位数为n位。
详细可参考:
数据格式控制函数:setprecision() 、fixed()和setw()函数的使用_周一写bug,bug改一周的博客-CSDN博客
科学计数法中的e+000:e(+/- n)等价于10^(+/- n)
例 2-15:
#include <iostream>
#include <iomanip>
//io:输入输出 manip:manipulator(操纵器)的缩写
//(在c++上只能通过输入缩写才有效)只要对I/O进行格式控制,就必须要写
//具体详情,见:iomanip解释_码里奥特慢的博客-CSDN博客_iomanip
using namespace std;
int main()
{
int a = 123456;
cout << setw(3) << a << endl;
//(若字符长度大于3,则全部直接输出)
cout << setw(12) << a << endl;
return 0;
}
setw:预设宽度
=set width(广度、宽度)
例 2-16:
#include <iostream>
#include <iomanip>
//io:输入输出 manip:manipulator(操纵器)的缩写
//(在c++上只能通过输入缩写才有效)只要对I/O进行格式控制,就必须要写
//具体详情,见:iomanip解释_码里奥特慢的博客-CSDN博客_iomanip
using namespace std;
int main()
{
cout << setiosflags(ios::left) << setw(5) << 100 << endl;
cout << setiosflags(ios::right) << setw(5) << 100 << endl;
}
setiosflags(ios::left) :控制输出标志(输入输出流::左侧)
=set I/O stream flags
flag:旗帜; 标志;标示;
边栏推荐
- jwt(json web token)
- Flask framework
- [ROS](05)ROS通信 —— 节点,Nodes & Master
- 第三单元 视图层
- drf路由组件Routers
- Unit 7 ORM table relationships and operations
- Steps to connect the virtual machine with xshell_establish a network connection between the host and the vm virtual machine
- window10下半自动标注
- 宏定义问题记录day2
- verilog学习|《Verilog数字系统设计教程》夏宇闻 第三版思考题答案(第七章)
猜你喜欢
【ROS】工控机的软件包不编译
Building and getting started with the Flask framework
C语言sizeof和strlen的区别
How does Apache, the world's largest open source foundation, work?
[ROS](01)创建ROS工作空间
window10下半自动标注
【c】大学生在校学习c语言常见代码
Unit 5 Hold Status
MarkDown syntax summary
Deep learning framework pytorch rapid development and actual combat chapter3
随机推荐
Introduction and use of Haystack
Implementation of redis distributed lock and watchdog
瑞吉外卖笔记——第08讲读写分离
Verilog学习 系列
【c】大学生在校学习c语言常见代码
第十一单元 序列化器
Caused by: org.gradle.api.internal.plugins.PluginApplicationException: Failed to apply plugin [id ‘c
8576 顺序线性表的基本操作
verilog学习|《Verilog数字系统设计教程》夏宇闻 第三版思考题答案(第十章)
【c】小游戏---扫雷雏形
drf视图组件
run yolov5
宏定义问题记录day2
Paddle window10 environment using conda installation
【ROS】编译软件包packages遇到进度缓慢或卡死,使用swap
[ROS]ROS常用工具介绍(待续)
二级指针,数组指针,指针数组和函数指针
Building and getting started with the Flask framework
网络剪枝(1)
跑跑yolov5吧