结构体程序题

目录

1.统计结构体数组中性别(sex)为'M'的变量的个数。

2.通过函数调用实现:讲结构体数组中的三个元素按num成员进行升序排列。

写法1:在函数外部 定义 结构体变量并且赋值,参数值为空。

写法2:在主函数中定义变量,通过参数传递变量值

3.计算n名学生成绩的平均分并通过函数返回。

4.输入三个学生的学号,姓名,分数,输出分最高同学的所有信息。

4.2用函数



1.统计结构体数组中性别(sex)为'M'的变量的个数。

要求:

①数组元素依次赋初值为:{1 , "Andy" , 'M' } 、 {2 , "Mike" , 'F' } 、 {3 , "Rose" , 'M'}。

②结构体定义如下:struct Student{int num;char name[30];char sex;};


#include
 
struct Student{
	int num;
	char name[30];
	char sex;
};
 
int Fun(struct Student *s , int n)
{
	int num=0; 
	int i;
	
	for(i=0; i

2.通过函数调用实现:讲结构体数组中的三个元素按num成员进行升序排列。

要求:

①数组元素依次赋初值为:{12,"sunny",89.1}、{8,"henry",73.5}、{21,"lucy",91.7}。

②结构体定义如下:struct s{int num;char name[30];float score;};

写法1:在函数外部 定义 结构体变量并且赋值,参数值为空。

#include

struct s{
	int num;
	char name[30];
	float score;
}a[3] = {{12,"sunny",89.1} , {8,"henry",73.5} , {21,"lucy",91.7}}; 

void fun()
{
	int i,j;
	struct s t;
	for(i=0; i<2; i++)
	{
		for(j=0; j<2-i; j++)
		{
			if(a[j].num > a[j+1].num)
			{
				t = a[j];
				a[j] = a[j+1];
				a[j+1] = t;
			}
		}
	}
	
}

int main()
{
	int i;
	fun();
	for(i=0; i<3; i++)
	{
		printf("%d,%s,%f\n",a[i].num , a[i].name , a[i].score);
	}
	
	return 0;
}

8,henry,73.500000
12,sunny,89.099998
21,lucy,91.699997 

写法2:在主函数中定义变量,通过参数传递变量值

#include

struct s{
	int num;
	char name[30];
	float score;
};

void fun(struct s *a , int n)
{
	int i,j;
	struct s t;
	for(i=0; i a[j+1].num)
			{
				t = a[j];
				a[j] = a[j+1];
				a[j+1] = t;
			}
		}
	}
	
}

int main()
{
	struct s a[3] = {{12,"sunny",89.1} , {8,"henry",73.5} , {21,"lucy",91.7}}; 
	int i;
	fun(a,3);
	for(i=0; i<3; i++)
	{
		printf("%d,%s,%f\n",a[i].num , a[i].name , a[i].score);
	}
	
	return 0;
}

8,henry,73.500000
12,sunny,89.099998
21,lucy,91.699997 

3.计算n名学生成绩的平均分并通过函数返回。

要求:

结构体代码如下:struct StudentScore{double score;};

#include

struct StudentScore{
	double score;
};

double Fun(struct StudentScore *a , int n)
{
	int i;
	double sum = 0.0;
	
	for(i=0; i

请输入学生数量:3
请输入第1个学生的成绩:90
请输入第2个学生的成绩:100.0
请输入第3个学生的成绩:80
90.000000

4.输入三个学生的学号,姓名,分数,输出分最高同学的所有信息。

#include
 
int main()
{
	struct stu{
		int num;
		char name[20];
		float score;
	};
	
	int i;
	
	struct stu a[3] , max;
	
	for(i=0; i<3; i++)
	{
		printf("第%d个:",i+1);
		scanf("%d%s%f",&a[i].num,&a[i].name,&a[i].score);
	}
	
	max = a[0];		//结构体变量整体赋值 
	
	for(i=1; i<3; i++)
	{
		if(max.score < a[i].score)
			max = a[i];		//结构体变量整体赋值 
	}
	
		printf("成绩最高的是%d号,%s,%f分",max.num,max.name,max.score);
}

第1个:1 亚历山大 80
第2个:2 牛顿 100
第3个:3 李白 59
成绩最高的是2号,牛顿,100.000000分 

4.2用函数

#include
 
struct stu{				//struct stu定义 应该放全局。如果在主函数内,那么定义函数会提示没这个类型。 
	int num;
	char name[20];
	float score;
};
	
struct stu Fun(struct stu *a,int n)
{
	struct stu max = a[0];
	int i;
	
	for(i=1; i<3; i++)
	{
		if(max.score < a[i].score)
			max = a[i];		//结构体变量整体赋值 
	}
	
	return max;
}
 
int main()
{
	int i;
	
	struct stu a[3] = {
		1,"li",70.1,2,"wang",70.5,3,"zhai",70.3
	};
	
	
	
	struct stu max = Fun(a,3);
	
	printf("成绩最高的是%d号,%s,%f分",max.num,max.name,max.score);
}

成绩最高的是2号,wang,70.500000分 

 

你可能感兴趣的:(蓝桥杯,职场和发展)