利用结构体录入学生的所有数据(学号,姓名,三门课程成绩,生日),选择性输出 包括(所有学生平均分,查看排名,生日,查询学生信息)四个选项

#include<stdio.h>
#define N 5  //定义5个学生

struct Student
{
    
    
	int num;//学号
	char name[20];//姓名
	double score[3];//三门成绩
	double aver;//平均数
	int year;//年
	int month;//月
	int day;//日
};

int main()
{
    
    
	void input(struct Student stu[]);   //输入数据 
	void average(struct Student stu[]); //平均值
	void sort(struct Student stu[]);//降序输出
	int days(struct Student student);//计算
	struct Student stu[N], * p = stu;
	input(p);

	int choice;
    int m;
    
     printf("Input each students's information\n");	
	/*显示主菜单*/
	while (1)
	{
    
    
		printf("===============the Score Processing System============================\n");
		printf("1,print each student's average\n");
		printf("2,order the students by student's average decreasingly\n");
		printf("3,printf brithday of student\n");
		printf("======================================================================\n");
		printf("Please choose (1~3):\n");
		scanf("%d",&choice);//输入数字
		switch (choice)
		{
    
    
	     	case 1:
			       average(p); break;
		    case 2:
		    	   sort(p); break;  
			case 3:
			       printf("请输入第几名同学的生日\t");
		           scanf("%d", &m);
			       printf("该同学的学号:%d ,名字:%s 生日 :%d年%d月%d日,本年第%d天\n", stu[m-1].num, stu[m-1].name, stu[m-1].year, stu[m-1].month, stu[m-1].day, days(stu[m-1]));	                  
		    default:
			        break;

		}

	}
	return 0;
}
void input(struct Student stu[])
{
    
    
	int i;
	printf("请输入各学生的信息:\n");
	for (i = 0; i < N; i++)
	{
    
    
		printf("学号 姓名 3门课成绩  生日\t");
		scanf("%d %s %lf %lf %lf\n %d %d %d", &stu[i].num, stu[i].name, &stu[i].score[0], &stu[i].score[1], &stu[i].score[2], &stu[i].year, &stu[i].month, &stu[i].day);
		stu[i].aver = (stu[i].score[0] + stu[i].score[1] + stu[i].score[2]) / 3.0;
		printf("\n");
	}

}

void average(struct Student stu[])//计算平均数
{
    
    
	for (int i = 0; i < N; i++)
	{
    
    
		stu[i].aver = (stu[i].score[0] + stu[i].score[1] + stu[i].score[2]) / 3.0;
		printf("学号:%d   姓名: %s   平均成绩:%4.2lf", stu[i].num, stu[i].name, stu[i].aver);
	}
}

void sort(struct Student stu[])//降序输出
{
    
    
	struct Student temp;
	int i, j, k;
	for (i = 0; i < N - 1; i++)//选择排序法
	{
    
    
		k = i;
		for (j = i + 1; j < N; j++)
		{
    
    
			if (stu[i].aver < stu[j].aver)
				k = j;
			temp = stu[k];
			stu[k] = stu[i];
			stu[i] = temp;
		}

	}
	for (i = 0; i < N; i++)
	{
    
    
		printf("学号:%d   姓名: %s   三门课的成绩分别为: %4.2lf %4.2lf %4.2lf  平均成绩:%4.2lf\n", stu[i].num, stu[i].name, stu[i].score[0], stu[i].score[1], stu[i].score[2], stu[i].aver);
	}
	printf("\n");
}
int days(struct Student student)
{
    
    
	         
	  if((student.year%4==0&&student.year%100!=0)||(student.year%400==0))//闰年 
      {
    
    
                                       if(student.month == 1)
	                                    {
    
    
					                         return student.day;
			                            }
			                           else if(student.month == 2)
										{
    
    
		                                    return student.day+31;									
										}
										else if(student.month == 3)
										{
    
    
	                                       return student.day+60;										
										} 
										else if(student.month == 4)
										{
    
    
						                  return student.day+91;
										}
										else if(student.month == 5)
										{
    
    
					                      return student.day+121;	 
										}			
										else if(student.month == 6)
										{
    
    
											     return student.day+152;
										}
										else if(student.month == 7)
										{
    
    
				                          return student.day+182;
										}
										else if(student.month == 8)
										{
    
    
                                          return student.day+213;
										}
										else if(student.month == 9)
										{
    
    
		                                      return student.day+244;
										}
										else if(student.month == 10)
										{
    
    
											      return student.day+274;
										}
										else if(student.month == 11)
										{
    
    
			                                     return student.day+305;
										}
										else if(student.month == 12)
										{
    
    
                                                   return student.day+335;							
										}
			}
			else//平年
			{
    
    
          
	                                    if(student.month == 1)
										{
    
    
											   return student.day;
										}
										else if(student.month == 2)
										{
    
    
		                                      return student.day+30;									
										}
										else if(student.month == 3)
										{
    
    
	                                          return student.day+59;										
										} 
										else if(student.month == 4)
										{
    
    
						                  return student.day+90;
										}
										else if(student.month == 5)
										{
    
    
					                      return student.day+120;	 
										}			
										else if(student.month == 6)
										{
    
    
											     return student.day+151;
										}
										else if(student.month == 7)
										{
    
    
				                                   return student.day+181;
										}
										else if(student.month == 8)
										{
    
    
                                                   return student.day+212;
										}
										else if(student.month == 9)
										{
    
    
		                                           return student.day+243;
										}
										else if(student.month == 10)
										{
    
    
											      return student.day+273;
										}
										else if(student.month == 11)
										{
    
    
			                                    return student.day+304;
										}
										else if(student.month == 12)
										{
    
    
                                             return student.day+334;							
										}
			} 
}


以下是改良版
效果图在这里插入图片描述

#include<stdio.h>
#define N 5  //定义5个学生

struct Student
{
    
    
	int num;//学号
	char name[20];//姓名
	double score[3];//三门成绩
	double aver;//平均数
	int year;//年
	int month;//月
	int day;//日
};

int main()
{
    
    
	void input(struct Student stu[]);   //输入数据 
	void average(struct Student stu[]); //输出各个平均分 
	void sort(struct Student stu[]);    //输出名次
	void search(struct Student stu[], int n);  //寻找学生 
	int searchStudentnum(struct Student stu[], int m);//寻找对应编号的学生 
	int days(struct Student student);   //计算
	struct Student stu[N], * p = stu;
	input(p);

	int choice, m, n, c;
    
     printf("Input each students's information\n");
	 	
	/*显示主菜单*/
	printf("===============the Score Processing System============================\n");
	printf("1,print each student's average\n");
	printf("2,order the students by student's average decreasingly\n");
	printf("3,printf brithday of student\n");
	printf("4,search student according to your searched num\n");
	printf("======================================================================\n");
	while (1)
	{
    
    
	    printf("\n");
		printf("Please choose (1~4):\n");
		scanf("%d",&choice);//输入数字
		switch (choice)
		{
    
    
	     	case 1:
			       average(p); break;
		    case 2:
		    	   sort(p); break;  
			case 3:
			       printf("请输入查询编号多少的同学的生日:\t");
		           scanf("%d", &m);//输入编号 
	               c = searchStudentnum(p,m);
			       printf("该同学的学号:%d   名字:%s     生日 :%d年%d月%d日,本年第%d天\n", stu[c].num, stu[c].name, stu[c].year, stu[c].month, stu[c].day, days(stu[c]));break;
			case 4:
			       printf("请输入您要寻找的同学编号:\t");
				   scanf("%d", &n);
				   search(p,n);	   	                  
		    default:
			        break;

		}

	}
	return 0;
}
void input(struct Student stu[])//记录学生信息 
{
    
    
	int i;
	printf("请输入各学生的信息:\n");
	for (i = 0; i < N; i++)
	{
    
    
		printf("学号 姓名 3门课成绩  生日\t");
		scanf("%d %s %lf %lf %lf\n %d %d %d", &stu[i].num, stu[i].name, &stu[i].score[0], &stu[i].score[1], &stu[i].score[2], &stu[i].year, &stu[i].month, &stu[i].day);
		stu[i].aver = (stu[i].score[0] + stu[i].score[1] + stu[i].score[2]) / 3.0;
		printf("\n");
	}

}

void average(struct Student stu[])//计算平均数
{
    
    
	for (int i = 0; i < N; i++)
	{
    
    
		stu[i].aver = (stu[i].score[0] + stu[i].score[1] + stu[i].score[2]) / 3.0;
		printf("学号:%d   姓名: %s   平均成绩:%4.2lf", stu[i].num, stu[i].name, stu[i].aver);
	    printf("\n"); 
	}
}

void sort(struct Student stu[])//排序名次 
{
    
    
    int index[N] = {
    
    0}; //初始化索引数组 
	int i, j, k, temp;
	for(i = 0; i < N; i++)
	{
    
    
		index[i] = i;  //放入对应的序号 
	}
	for (i = 0; i < N - 1; i++)//选择排序法
	{
    
    
		k = i;
		for (j = i + 1; j < N; j++)
		{
    
    
		       if (stu[index[k]].aver < stu[index[j]].aver)
		       {
    
    
		            k = j;   
		       }
		}
		temp = index[k];
		index[k] = index[i];
		index[i] = temp;
	}
	for (i = 0; i < N; i++)
	{
    
    
		printf("学号:%d   姓名: %s   三门课的成绩分别为: %4.2lf %4.2lf %4.2lf  平均成绩:%4.2lf\n", stu[index[i]].num, stu[index[i]].name, stu[index[i]].score[0], stu[index[i]].score[1], stu[index[i]].score[2], stu[index[i]].aver);
	}
	printf("\n");
}


int days(struct Student student)//生日 
{
    
    
	         
	  if((student.year%4==0&&student.year%100!=0)||(student.year%400==0))//闰年 
      {
    
    
                                       if(student.month == 1)
	                                    {
    
    
					                         return student.day;
			                            }
			                           else if(student.month == 2)
										{
    
    
		                                    return student.day+31;									
										}
										else if(student.month == 3)
										{
    
    
	                                       return student.day+60;										
										} 
										else if(student.month == 4)
										{
    
    
						                  return student.day+91;
										}
										else if(student.month == 5)
										{
    
    
					                      return student.day+121;	 
										}			
										else if(student.month == 6)
										{
    
    
											     return student.day+152;
										}
										else if(student.month == 7)
										{
    
    
				                          return student.day+182;
										}
										else if(student.month == 8)
										{
    
    
                                          return student.day+213;
										}
										else if(student.month == 9)
										{
    
    
		                                      return student.day+244;
										}
										else if(student.month == 10)
										{
    
    
											      return student.day+274;
										}
										else if(student.month == 11)
										{
    
    
			                                     return student.day+305;
										}
										else if(student.month == 12)
										{
    
    
                                                   return student.day+335;							
										}
			}
			else//平年
			{
    
    
          
	                                    if(student.month == 1)
										{
    
    
											   return student.day;
										}
										else if(student.month == 2)
										{
    
    
		                                      return student.day+30;									
										}
										else if(student.month == 3)
										{
    
    
	                                          return student.day+59;										
										} 
										else if(student.month == 4)
										{
    
    
						                  return student.day+90;
										}
										else if(student.month == 5)
										{
    
    
					                      return student.day+120;	 
										}			
										else if(student.month == 6)
										{
    
    
											     return student.day+151;
										}
										else if(student.month == 7)
										{
    
    
				                                   return student.day+181;
										}
										else if(student.month == 8)
										{
    
    
                                                   return student.day+212;
										}
										else if(student.month == 9)
										{
    
    
		                                           return student.day+243;
										}
										else if(student.month == 10)
										{
    
    
											      return student.day+273;
										}
										else if(student.month == 11)
										{
    
    
			                                    return student.day+304;
										}
										else if(student.month == 12)
										{
    
    
                                             return student.day+334;							
										}
			} 
}

void search(struct Student stu[], int n)//寻找学生 
{
    
    
		   for(int i = 0; i < N; i++)
		   {
    
    
                 if(stu[i].num == n)
				 {
    
    
 					 printf("学号:%d   姓名: %s   三门课的成绩分别为: %4.2lf %4.2lf %4.2lf  平均成绩:%4.2lf\n", stu[i].num, stu[i].name, stu[i].score[0], stu[i].score[1], stu[i].score[2], stu[i].aver);
 					 printf("\n");
				 }
   		   }
} 

int searchStudentnum(struct Student stu[],int m)
{
    
    
        for(int i = 0; i < N; i++)
		{
    
    
			if(stu[i].num == m)
			{
    
    
			     return i;	
			}
		}   
} 

猜你喜欢

转载自blog.csdn.net/qq_52001969/article/details/111713497