結構體綜合訓練

iamzxf發表於2014-12-22

有10個學生,每個學生的資料包括學號、姓名、3門課的成績,編寫如下函式:

    ① 輸入10個學生的基本資訊(Input);

    ② 求出每個學生的平均分(Average);

    ③ 求出最高分的學生資訊(High_Score);

    ④ 輸出每個學生的資訊(Output);

    ⑤ 按平均分從高到低的順序排序(Sort)。


參考程式碼如下:

#include <stdio.h>
#include <math.h>

struct Student{
	int num;
	char name[10];
	int score[3];
	float average;
};

int input(struct Student person[], int n)
{
	int i,j,k;
	for(i=0;i<n;i++)
	{
		scanf("%d",&person[i].num);
		getchar();
		gets(person[i].name);
		for(j=0;j<3;j++)
			scanf("%d",&person[i].score[j]);
	}

	return 0;
}

int output(struct Student person[], int n)
{
	int i,j,k;
	printf("=======================\n");
	for(i=0;i<3;i++)
	{
		printf("%5d",person[i].num);
		printf("%10s",person[i].name);
		for(j=0;j<3;j++)
			printf("%5d",person[i].score[j]);

		putchar('\n');
	}

	return 0;
}

int average(struct Student person[], int n)
{
	int i,j,k=0;
	float average,maxaverage=0.;
	for(i=0;i<3;i++)
	{
		average=0.0;
		for(j=0;j<3;j++)
			average+=person[i].score[j];
		
		person[i].average=average/3;
		printf("%6.2f",person[i].average);	

		if(average/3>maxaverage)
		{
			maxaverage=average/3;
			k=i;
		}
	}
	putchar('\n');

	printf("the best student is %s.\n",person[k].name);
	return 0;

}


int sort(struct Student *ps[], int n)
{
	int i,j,k;
	struct Student *temp;

	for(i=0;i<n;i++)
	{
		k=i;
		for(j=i+1;j<n;j++)
		{
			if(ps[j]->average>ps[k]->average)
				k=j;
		}

		if(k!=i)
		{
			temp=ps[i];
			ps[i]=ps[k];
			ps[k]=temp;
		}
	}

	for(i=0;i<n;i++)
	{
		printf("%5d %10s %6.2f\n",ps[i]->num,ps[i]->name, ps[i]->average);
	}
	
	return 0;

}

int main()
{
	struct Student stu[10]; //&stu[0], &stu[1], &stu[2]  stu, stu+1, stu+2
	struct Student *ps[10]; //ps[0], ps[1], ps[2]
	int i,j,k;

	input(stu,10);
	output(stu,10);

	average(stu,10);
	
	for(i=0;i<10;i++)
		ps[i]=stu+i;

	sort(ps,10);

	return 0;
}

 

 

 



 

#include <stdio.h>
#define M 3

typedef struct stu{
	int num;
	char name[10];
	int score[3];
	float average;	
}Student;

int input(Student person[],int n);
int output(Student person[],int n);
int best(Student person[],int n);
int sort(Student person[],int n);


int main()
{

	int in;
	Student person[M];

	while(1)
	{
		printf("\t1. input\n");
		printf("\t2. display\n");
		printf("\t3. find the best\n");
		printf("\t4. sort\n");
		printf("\t5. quit\n");
		printf("\t please select:");
		scanf("%d",&in);


		switch(in)
		{
		case 1: input(person,3); break;
		case 2: output(person,3); break;
		case 3: best(person,3); break;
		case 4: sort(person, 3); break;
		case 5: return 0;
		}
	}

	return 0;
}


int input(Student person[],int n)
{
	int i,j;
	float average;

	for(i=0;i<n;i++)
	{
		scanf("%d",&person[i].num);
		getchar();
		gets(person[i].name);
		average=0.;
		for(j=0;j<3;j++){
			scanf("%d",&person[i].score[j]);
			average+=person[i].score[j];
		}

		person[i].average=average/3;
	}

	return 0;
	
}

int output(Student person[],int n)
{
	
	int i,j;
	printf("number\t name\t Chinese\t Math \t English\t average\n");

	for(i=0;i<n;i++)
	{
		printf("%8d",person[i].num);
		printf("%8s",person[i].name);
	
		for(j=0;j<3;j++){
			printf("%8d",person[i].score[j]);
		}
		printf("%8.2f\n",person[i].average);				
	}

	return 0;
}

int best(Student person[],int n)
{	
	int i,j;
	
	j=0;

	for(i=1;i<n;i++)
	{
		if(person[i].average>person[j].average)	
			j=i;
	}
	
	printf("the best student is:\n");
	i=j;
	printf("%8d",person[i].num);
	printf("%8s",person[i].name);
	
	for(j=0;j<3;j++){
		printf("%8d",person[i].score[j]);
	}
	printf("%8.2f\n",person[i].average);				

	return 0;
}

int sort(Student person[],int n)
{
	Student *ps[M],*temp;
	int i,j;

	for(i=0;i<n;i++)
		ps[i]=person+i;

	for(i=0;i<n;i++)
	{
		for(j=i+1;j<n;j++)
		{
			if((*(ps+i))->average<(*(*(ps+j))).average)
				//if (ps[i]->average<ps[j]->average)
			{
				temp=ps[i];
				ps[i]=*(ps+j);
				ps[j]=temp;
			}
		}
	}

	for(i=0;i<n;i++)
	{
		printf("%5d%10s%5d%5d%5d%8.2f\n",ps[i]->num, ps[i]->name,ps[i]->score[0],ps[i]->score[1],ps[i]->score[2],ps[i]->average);
	}

	return 0;

}


 



相關文章