#include <iostream>
#include <iomanip>
using namespace std;
void main()
{
char bookname[100];double price;int counts;
cout << "请输入书名:"<< endl;
cin>>bookname;
cout << "请输入单价:"<< endl;
cin>>price;
cout << "请输入销售数量:"<< endl;
cin>>counts;
cout<<"使用状态标志和成员函数进行格式化输出"<<endl;
cout<<"《"<<bookname<<"》:";
cout.width(5); //单价显示宽度为5个字符
cout.fill('*'); //宽度不满用*字符填充
cout<<price<<"(单价) "; //按照上面的格式设置输出
#ifdef scientific
cout.setf(ios::scientific); //用科学记数法输出销售额
cout.precision(3); //保留3位精度
#else
cout<<std::fixed; //不使用科学计数法输出
cout<<price*counts<<"(销售额) ";
#endif
cout.setf(ios::showpos|ios::left); //显示销售数量,带有正负号、左对齐
cout<<counts<<"(销售数量)"<<endl;
cout<<"使用流操作符进行格式输出"<<endl;
cout<<"《"<<bookname<<"》:"
<<setw(5)<<setfill('*')<<price<<"(单价) "
<<scientific<<setprecision(3)<<fixed<<price*counts<<"(销售额) "
<<showpos<<left<<counts<<"(销售数量)"<<endl;
cin>>bookname;
}《VC++6.0示例程序光盘》实例代码

本文演示了如何使用C++编程语言通过控制台输入获取图书名称、单价和销售数量,然后采用不同的输出格式(包括状态标志、成员函数、流操作符等)展示图书销售信息,包括单价、销售额和销售数量的输出。
785

被折叠的 条评论
为什么被折叠?



