您的位置:首页 > 编程语言 > C语言/C++

《C++第十五周实验报告3-1、2》---对照运行的结果和教材中关于格式控制的说明

2012-05-29 21:02 351 查看
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
	int a;
	cout<<"input a:";//在控制台上打印字符串
	cin>>a;
	cout<<"dec:"<<dec<<a<<endl; //数据以十进制格式输出  
	cout<<"hex:"<<hex<<a<<endl; //以十六进制输出  
	cout<<"oct:"<<setbase(8)<<a<<endl;//以八进制输出 
	char *pt="Jerryson";      
	cout<<setw(10)<<pt<<endl; //输出的字符占十个字符的长度,默认靠左 
	cout<<setfill('*')<<setw(10)<<pt<<endl; //输出的字符占十个字符的长度默认靠左不够的用*号补
	double pi=22.0/7.0;  
	cout<<setiosflags(ios::scientific)<<setprecision(8);//浮点数以科学计数法输出,精确位数为八位
	cout<<"pi="<<pi<<endl;  //输出字符串及pi的值  
	cout<<"pi="<<setprecision(4)<<pi<<endl; //输出精确位数为四位pi的值 
	cout<<"pi="<<setiosflags(ios::fixed)<<pi<<endl;//浮点数以定点的格式输出
	system("pause");
	return 0;
}
#include <iostream>
using namespace std;
int main( )
{
	int a=54;
	cout.setf(ios::showbase);//设置输出时的基数符号
	cout<<"dec:"<<a<<endl; // 默认输出a的值是以十进制
	cout.unsetf(ios::dec); // 终止十进制格式的设置
	cout.setf(ios::hex);   // 设置输出格式为十六进制
	cout<<"hex:"<<a<<endl; // 以十六进制输出a
	cout.unsetf(ios::hex); //终止十六进制格式的设置
	cout.setf(ios::oct);   //设置输出格式为八进制
	cout<<"oct:"<<a<<endl; //以八进制输出a
	cout.unsetf(ios::oct); //终止八进制格式的设置
	char *pt="Jerryson";   
	cout.width(10);    //指定宽度为十
	cout<<pt<<endl;    //输出pt
	cout.width(10);    //指定宽度为十
	cout.fill('*');    //指定空白用 '*'填充
	cout<<pt<<endl;    //输出pt
	double pi=22.0/7.0;   
	cout.setf(ios::scientific); // 设置输出格式为科学计数法
	cout<<"pi=";       //输出字符串"pi="
	cout.width(14);    //指定宽度为十四
	cout<<pi<<endl;    //输出pi
	cout.unsetf(ios::scientific); //终止科学计数法
	cout.setf(ios::fixed);        //指定用定点形式输出
	cout.width(12);               //指定宽度为十二
	cout.setf(ios::showpos);      //输出时显示“+”号
	cout.setf(ios::internal);     //符号在左侧
	cout.precision(6);            //保留六位小数
	cout<<pi<<endl;               //输出pi
	system("pause");
	return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐