有序的结构体数组

/*
* 程序的版权和版本声明部分
* Copyright (c)2013, 烟台大学计算机学院学生
* All rightsreserved.
* 文件名称:shuzu .cpp
* 作    者: 袁静
* 完成日期:2013年03月13日
* 版本号: v1.0
* 输入描述: 已经在程序中初始化
* 问题描述: 将学生的成绩信息储存在结构体数组中,对结构体排序并输出
*/
#include <iostream>
#include <string.h>
using namespace std;
struct Score
{
	char num[14];
	int cpp                                                                                                                                         ;
	int math;
	int english;
};
void sort1(Score score[],int n);  //要自定义的函数
void sort2(Score score [],int n);
void output(Score score[],int n);
int main()
{
	Score score[]={
		{"201152501104",65,69 ,68 },
		{"201152501114",94 ,89 ,63 },
		{"201152501138",67 ,62 ,84 },
		{"201152501204",100 ,65 ,91 },
		{"201152501202",59 ,80 ,55 },
		{"201152501115",92 ,84 ,60 },
		{"201152501201",80 ,92 ,71 },
		{"201152501145",88 ,56 ,67 },
		{"201152501203",62 ,62 ,95 },
		{"201152501140",80 ,60 ,86 },
		{"201152501205",73 ,90 ,94}
	};
	int stuNum=sizeof(score)/sizeof(score[0]);
	//将所有同学按C++降序排序后输出
	sort1(score,stuNum);
	cout<<"按C++降序排序后:"<<endl;
	output(score,stuNum);
	//将所有同学按学号升序排序后输出
	sort2(score,stuNum);
	cout<<"按学号升序排序后:"<<endl;
	output(score,stuNum);
	return 0;
}

void sort1(Score score[],int n)
{
	int i,j;
	Score t;
	for(j=1;j<n;j++)
		for(i=0;i<=n-j-1;i++)
			if(score[i].cpp<score[i+1].cpp)
			{
				t=score[i];
				score[i]=score[i+1];
				score[i+1]=t;
			}
}
void sort2(Score score[],int n)
{
	int i,j;
	Score t;
	for(j=1;j<=n;j++)
		for(i=0;i<=n-j-1;i++)
            if(strcmp(score[i].num,score[i+1].num)>0)
			{
				t=score[i];
				score[i]=score[i+1];
				score[i+1]=t;
			}
}
void output(Score score[],int n)
{
	int i;
	for(i=0;i<n;i++)
		cout<<score[i].num<<"   "<<score[i].cpp<<"   "<<score[i].math<<"   "<<score[i].english<<endl;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章