问题及代码:
/*
*Copyright (c) 2014,烟台大学计算机学院
*All rights reserved.
*文件名称:lily.cpp
*作者:李莉
*完成日期:2015年6月3日
*版本号:v1.0
*问题描述:控制输出的格式
程序输入:输入若干正数
程序输出:运行结果
*/
#include <iostream>
using namespace std;
int main()
{
int a=21;
cout.setf(ios::showbase);//设置技术符号
cout<<"dec:"<<a<<endl;//设置以10进制输出
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;//八进制输出
char *pt="China";
cout.width(10);//设置输出的字段宽度是10位
cout<<pt<<endl;//输出字段宽度为10的字符串
cout.width(10);//设置字段宽度为10
cout.fill('*');//空白处以*填充
cout<<pt<<endl;
double pi=22.0/7.0;
cout.setf(ios::scientific);//用指定的科学计数法输出
cout<<"pi=";
cout.width(14);//设置字段宽度为14
cout<<pi<<endl;
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;
return 0;
}
运行结果:
心得体会:
键代码注释