C++结构体相关习题

题目一:

代码如下:

需要注意几个点:1.将结构体与向量结合使用2.要记得计算人均gdp

#include<iostream>
#include<iomanip>
#include<cmath>
#include<fstream>
#include<sstream>
#include<vector>
using namespace std;

struct GDP{
	string name;
	bool iscity;
	double gdp;
	double num;
	//人均gdp
	double gdp1;
};
int main(){
	vector<GDP> gg;//创建一个结构体矢量存储数据结果 
	string line;
	ifstream file("C:\\Users\\35959\\Desktop\\data.txt");
	while(getline(file,line)){
		int tmp;
		GDP aa;//创建一个名称为aa的结构体实例 
		stringstream ss(line);
		ss>>tmp>>aa.name>>aa.iscity>>aa.gdp>>aa.num;
		aa.gdp1 = aa.gdp /aa.num ;
		gg.push_back(aa);//将aa填入到gg中 
	}
	file.close();
	//开始计算题目
	double TGDP = 0,Tnum = 0;
	double BGDP = 0,Bnum = 0;//这个是计算九个城市的GDP和人口

	GDP maxcity = gg[0];
	GDP mincity = gg[0];
	for(vector<GDP>::iterator it = gg.begin();it!=gg.end();++it) 
	{
		const  GDP& city = *it;
		TGDP += city.gdp;
		Tnum += city.num;
		if (city.iscity){
			BGDP += city.gdp;
			Bnum += city.num;
		}
		if(city.gdp1>maxcity.gdp1){
			maxcity = city;
		}
		if(city.gdp1<mincity.gdp1){
			mincity = city;
		}
	}
	cout << "全省GDP总数: " << TGDP << "亿元" << endl;
    cout << "全省人口总数: " << Tnum << "万人" << endl;
    cout << "粤港澳大湾区9个城市的GDP总数占广东省比例: " << BGDP/TGDP  << endl;
    cout << "粤港澳大湾区9个城市的人口总数占广东省比例: " << Bnum/TGDP << endl;
    for(vector<GDP>::iterator it = gg.begin();it!=gg.end();++it) 
	{
		const  GDP& city = *it;
		cout << "城市: " << city.name << ",人均GDP: " << city.gdp1 << "万元/人" << endl;
	}
    cout << "最高人均GDP的城市: " << maxcity.name << ",人均GDP: " << maxcity.gdp1 << "万元/人" << endl;
    cout << "最低人均GDP的城市: " << mincity.name << ",人均GDP: " << mincity.gdp1 << "万元/人" << endl;
    return 0;
}

运行结果画面: 

题目二:

代码如下:

#include <iostream>
#include <cmath>

using namespace std;

class DotPos {
private:
    double x, y, z; 
public:
    // 构造函数
    DotPos(double xVal = 0.0, double yVal = 0.0, double zVal = 0.0) : x(xVal), y(yVal), z(zVal) {}

    // 计算与原点的欧几里得距离
    double EuclideanDist() const {
        return sqrt(x * x + y * y + z * z);
    }

    // 计算与另一个点的欧几里得距离
    double EuclideanDist(const DotPos& other) const {
        return sqrt((x - other.x) * (x - other.x) + (y - other.y) * (y - other.y) + (z - other.z) * (z - other.z));
    }

    // 计算与原点的曼哈顿距离
    double ManhattanDist() const {
        return fabs(x) + fabs(y) + fabs(z);
    }

    // 计算与另一个点的曼哈顿距离
    double ManhattanDist(const DotPos& other) const {
        return fabs(x - other.x) + fabs(y - other.y) + fabs(z - other.z);
    }

    // 计算 Lp 范数
    double LpNorm(double p) const {
        return pow(pow(fabs(x), p) + pow(fabs(y), p) + pow(fabs(z), p), 1.0 / p);
    }

    // 输出点的位置
    void OutputDotPos() const {
        cout << "(" << x << ", " << y << ", " << z << ")" << endl;
    }

    // 允许通过点对象访问私有成员变量
    double GetX() const { return x; }
    double GetY() const { return y; }
    double GetZ() const { return z; }
};

int main() {
    // 创建对象
    DotPos d1(3, 4, 5);
    DotPos d2(-2, 1, -2);

    // 结果 
    d1.OutputDotPos();
    d2.OutputDotPos();
    cout << "d1和d2到原点的欧几里得距离: " << d1.EuclideanDist() <<","<<d2.EuclideanDist()<< endl;
    cout << "d1和d2的欧几里得距离:        " << d1.EuclideanDist(d2) << endl;
    cout << "d1和d2到原点的曼哈顿距离:    " << d1.ManhattanDist() <<","<<d1.ManhattanDist()<< endl;
    cout << "d1和d2的曼哈顿距离:          " << d1.ManhattanDist(d2) << endl;
    cout << "d1的lp范数p=2:   " << d1.LpNorm(2) << endl;
    cout << "d1的lp范数p=1.5: " << d1.LpNorm(1.5) << endl;
    cout << "d1的lp范数p=1:   " << d1.LpNorm(1) << endl;
    cout << "d2的lp范数p=2:   " << d2.LpNorm(2) << endl;
    cout << "d2的lp范数p=1.5: " << d2.LpNorm(1.5) << endl;
    cout << "d2的lp范数p=1:   " << d2.LpNorm(1) << endl;

    return 0;
}

运行结果画面:

  • 10
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值