freemandj 2020-02-20 19:42 采纳率: 100%
浏览 481
已采纳

求各位大神解答c++的一道题,希望帮忙改改,谢谢指教

设计一个程序,按表格形式输出实际生活中的多行数据信息,要求同一列数据上下对齐。

例如:有以下省市的面积和人口信息,请定义结构体类型City,包括省名、面积、人口,输入以下的数据值,再按表格形式输出所有数据。


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
using namespace std;
#include
struct City
{
char province[20];
double area;
double pop;
};
void display(struct City *);
int main()
{

struct City city[5]={
{"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}
};
display(&city);
return 0;

}
void display(struct City *C)

{
cout<<"------------------------------------"< cout cout for(int i=0;i {
coutprovince;
cout<area;
cout<<" "<pop;
*C++;
i++;
}
}

  • 写回答

1条回答 默认 最新

  • threenewbee 2020-02-20 20:14
    关注
    // Q1055336.cpp : Defines the entry point for the console application.
    //
    
    
    #include <iostream>
    #include <iomanip>
    using namespace std;
    
    struct City
    {
        char province[20];
        double area;
        double pop; 
    };
    void display(struct City *, int n);
    int main()
    {
        struct City city[5]={
            {"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}
        };
        display(city, 5);
        return 0;
    }
    void display(struct City *C, int n)
    {
        cout<<"---------------------------------------"<< endl;
        cout << setw(1) << "|" << setw(15) << "province" << "|";
        cout << setw(10) << "area" << "|";
        cout << setw(10) << "pop" << "|";
        cout << endl;
        cout<<"---------------------------------------"<< endl;
        for(int i = 0;i < n; i++) {
            cout << setw(1) << "|" << setw(15) << C[i].province << "|";
            cout << setw(10) << C[i].area << "|";
            cout << setw(10) << C[i].pop << "|";
            cout << endl;
        }
        cout<<"---------------------------------------"<< endl;
    }
    
    运行结果
    
    ---------------------------------------
    |       province|      area|       pop|
    ---------------------------------------
    |          Anhui|    139600|      6461|
    |        Beijing|   16410.5|    1180.7|
    |      Chongqing|     82400|   3144.23|
    |       Shanghai|    6340.5|   1360.26|
    |       Zhejiang|    101800|      4894|
    ---------------------------------------
    Press any key to continue . . .
    
    

    问题解决的话,请点采纳

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误