7-5 表格输出
作者 乔林
单位 清华大学
本题要求编写程序,按照规定格式输出表格。
输入格式:
本题目没有输入。
输出格式:
要求严格按照给出的格式输出下列表格:
------------------------------------
Province Area(km2) Pop.(10K)
------------------------------------
Anhui 139600.00 6461.00
Beijing 16410.54 1180.70
Chongqing 82400.00 3144.23
Shanghai 6340.50 1360.26
Zhejiang 101800.00 4894.00
------------------------------------
这题居然搞了半个小时,醉了。
主要注意的是左右设置左右对齐和占位符。
我的代码
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
cout <<"------------------------------------"<<endl;
cout <<setw(14)<< left <<"Province"
<< right <<setw(9) <<"Area(km2)"
<<left<<setw(10)<<" Pop.(10K)" << endl;
cout << "------------------------------------" <<endl;
cout << setw(14) << left << "Anhui"
<< right<<setw(9) << fixed << setprecision(2)<<139600.00
<<left<< setw(10)<<" 6461.00"<<endl;
cout << setw(14) << left <<"Beijing"
<< right << setw(9) <<16410.54
<< setw(10)<<left <<" 1180.70"<<endl;
cout << setw(14) << left <<"Chongqing"
<< right << setw(9) << 82400.00
<< setw(10) <<left<<" 3144.23"<< endl;
cout << setw(14) << left <<"Shanghai"
<< right << setw(9) << 6340.50
<< left << setw(10) <<" 1360.26" << endl;
cout << setw(14) << left << "Zhejiang"
<< right << setw(9) << 101800.00
<< left << setw(10) <<" 4894.00" << endl;
cout << "------------------------------------" << endl;
return 0;
}