C/C++教程

C++结构体数组

本文主要是介绍C++结构体数组,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

【问题描述】定义一个结构体数组,存放3为职工的信息,并计算最高的工资值及所有职工工资的合计值。
【输入形式】输入结构体数组中各元素的成员
【输出形式】输出三个成员的在最高工资和工资合计值,成员包括name、id和salary。
【样例输入】the first person: 

                    name:zhansan

                    ID:2001001

                    salary:14500.87

                    the second person:

                    name:lisi

                    ID:2001002

                    salary:24560.92

                    the third person: 

                    name: wangwu

                    ID:2002001

                    salary:45600.00

【样例输出】the second person: 

                    name:

                    ID:

                    salary:

                    the second person: 

                    name:

                    ID:

                    salary:

                    the second person: 

                    name:

                    ID:

                    salary:

                    the highest salary:45600.00.

                    the average salary:28220.60

//结构体数组
#include<iostream>
#include<string>
#include<iomanip>
using namespace std;
struct zhigong
{
	string num;
	string name;
	int ID;
	double salary;
 }; //定义结构体数组
double max(double a,double b,double c)
{double t;
if(a>b){t=a;a=b;b=t;
}
if(a>c){t=a;a=c;c=t;
}
if(b>c){t=b;b=c;c=t;
}//三个数排序完成 
return c;
}
int main()
{
	string name1,name2,name3;
	int ID1,ID2,ID3;
	double salary1,salary2,salary3;
	cin>>name1>>ID1>>salary1>>name2>>ID2>>salary2>>name3>>ID3>>salary3;
	struct zhigong arr[3]=
	{
		{"the first person:",name1,ID1,salary1},
		{"the second person:",name2,ID2,salary2},
		{"the third person:",name3,ID3,salary3}
	};//创建结构体数组并赋值 
	for(int i=0;i<3;i++)
	{
		cout<<arr[i].num<<endl;
		cout<<"name:"<<endl;
		cout<<"ID:"<<endl;
		cout<<"salary:"<<endl;
		}//遍历每一个数组
	cout.setf(ios::fixed);
	cout<<"the highest salary:"<<setprecision(2)<<max(salary1,salary2,salary3)<<"."<<endl;
	cout<<"the average salary:"<<(arr[0].salary+arr[1].salary+arr[2].salary)/3<<"."<<endl;
	return 0;
 }

这篇关于C++结构体数组的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!