• C语言-对一个结构体中的字段进行排序


    这是帮别人做的一个题目,好久没有接触过C语言了。有点发怵,只是似乎找回点当时学C语言,做课程设计的感觉。

    题目:定义一个数组(学生结构体数组),里面包括学号、姓名、身份证和三科学生成绩。要求写一个函数,依据学生不论什么一个字段(如学号、姓名、身份证),进行排序。


    源代码:

    //// stu.cpp : Defines the entry point for the console application.
    ////
    //
    #include "stdafx.h"
    //------------------------------------------指针排序-------------------------------------------------------------------------------
    
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    
    #define N 3
    
    //学生结构体
    struct student{
    	long stuNum; //学号
    	char name[20];//姓名
    	char idCard[18];//身份证
    	float score[3];//三门成绩
    };
    
    //依据学生姓名排序
    void name_sort(student *stu,int n)
    {
    	student temp;
    	for(int i=0;i<n-1;i++)
    	{
    		for(int j=0;j<n-1-i;j++)
    		{
    			if(strcmp(stu[j].name,stu[j+1].name)>0)
    			{
    				temp =stu[j+1];
    				stu[j+1]=stu[j];
    				stu[j]=temp;
    
    			}
    		}
    	}
    
    	printf("
    ");
    	printf("*依据学生姓名排序后的学生情况:
    
    ");
    
    	for(int i=0;i<N;i++)
    	{
    		printf("第 %d 个学生信息:
    学号:%ld	姓名:%s	身份证:%s	语文:%.2f	数学:%.2f	英语:%.2f
    
    ",i+1,stu[i].stuNum,stu[i].name,stu[i].idCard,stu[i].score[0],stu[i].score[1],stu[i].score[2]);
    
    	}
    }
    //依据身份证进行排序
    void idCard_sort(student *stu,int n)
    {
    	student temp;
    	for(int i=0;i<n-1;i++)
    	{
    		for(int j=0;j<n-1-i;j++)
    		{
    			if(strcmp(stu[j].idCard,stu[j+1].idCard)>0)
    			{
    				temp =stu[j+1];
    				stu[j+1]=stu[j];
    				stu[j]=temp;
    
    			}
    		}
    	}
    
    
    	printf("
    ");
    	printf("*依据学生身份证排序后的学生情况:
    
    ");
    
    	for(int i=0;i<N;i++)
    	{
    		printf("第 %d 个学生信息:
    学号:%ld	姓名:%s	身份证:%s	语文:%.2f	数学:%.2f	英语:%.2f
    
    ",i+1,stu[i].stuNum,stu[i].name,stu[i].idCard,stu[i].score[0],stu[i].score[1],stu[i].score[2]);
    
    	}
    
    
    }
    
    //依据学号进行排序
    
    void stuNum_sort(student *stu,int n)
    {
    	
    	student temp;
    	for(int i=0;i<n-1;i++)
    	{
    		for(int j=0;j<n-1-i;j++)
    		{
    			if(stu[j].stuNum>stu[j+1].stuNum)
    			{
    				temp =stu[j+1];
    				stu[j+1]=stu[j];
    				stu[j]=temp;
    
    			}
    		}
    	}
    
    
    	printf("
    ");
    	printf("*依据学生学号排序后的学生情况:
    
    ");
    
    	for(int i=0;i<N;i++)
    	{
    		printf("第 %d 个学生信息:
    学号:%ld	姓名:%s	身份证:%s	语文:%.2f	数学:%.2f	英语:%.2f
    
    ",i+1,stu[i].stuNum,stu[i].name,stu[i].idCard,stu[i].score[0],stu[i].score[1],stu[i].score[2]);
    		
    	}
    
    
    }
    
    //main函数
    
    int main()
    {
    
    	struct student stu[N],*pStu;
    
    	//控制台屏幕变为蓝色背景
    	system("color 1f");
    
    	printf("请依次输入学生的学号,姓名,身份证,三门成绩(空格分开)
    ");
    	for(int i=0;i<N;i++)
    	{
    		printf("输入第 %d 个学生的信息
    ",i+1);
    		scanf("%ld%s%s%f%f%f",&stu[i].stuNum,stu[i].name,stu[i].idCard,&stu[i].score[0],&stu[i].score[1],&stu[i].score[2]);
    		
    	}
    	
    	pStu=stu;
    
    
    
    	//清屏
    	system("cls");
    
    
    	printf("
    *** 输入1 依照学生学号排序 ***
    *** 输入2 依照学生姓名排序 ***
    *** 输入3 依照学生身份证排序 ***
    *** 输入0 退出 ***
    
    ");
    
    	printf("请输入:");
    	int t;
    	scanf("%d",&t);
    
    	//循环
    	do{
    		//依据用户输入的值选择排序的字段
    		switch (t)
    		{
    		case 1:
    			stuNum_sort(pStu,N);//学号排序
    			break;
    		case 2:
    			name_sort(pStu,N);//姓名排序
    			break;
    
    		case 3:
    			idCard_sort(pStu,N);//身份证排序
    			break;
    
    		default:
    			name_sort(pStu,N);
    		}
    
    
    		printf("
    请输入:");
    		scanf("%d",&t);
    
    	}while(t!=0);
    
    	return 1;
    }
    
    
    
    
    
    
    //------------------没有指针--------------------------------------------------------------------------------------------------
    
    //
    //#include<stdio.h>
    //#include<stdlib.h>
    //#include<string.h>
    //
    //#define N 5
    //
    ////学生结构体
    //struct student{
    //	long stuNum; //学号
    //	char name[20];//姓名
    //	char idCard[18];//身份证
    //	float score[3];//三门成绩
    //};
    //
    //
    ////依据学生姓名排序
    //void name_sort(student stu[],int n)
    //{
    //	student temp;
    //	for(int i=0;i<n-1;i++)
    //	{
    //		for(int j=0;j<n-1-i;j++)
    //		{
    //			if(strcmp(stu[j].name,stu[j+1].name)>0)
    //			{
    //				temp =stu[j+1];
    //				stu[j+1]=stu[j];
    //				stu[j]=temp;
    //
    //			}
    //		}
    //	}
    //
    //
    //	printf("
    ");
    //	printf("*依据学生姓名排序后的学生情况:
    
    ");
    //	
    //	for(int i=0;i<N;i++)
    //	{
    //		printf("第 %d 个学生信息:
    学号:%ld	姓名:%s	身份证:%s	语文:%.2f	数学:%.2f	英语:%.2f
    
    ",i+1,stu[i].stuNum,stu[i].name,stu[i].idCard,stu[i].score[0],stu[i].score[1],stu[i].score[2]);
    //		
    //	}
    //
    //
    //}
    ////依据身份证进行排序
    //void idCard_sort(student stu[],int n)
    //{
    //	student temp;
    //	for(int i=0;i<n-1;i++)
    //	{
    //		for(int j=0;j<n-1-i;j++)
    //		{
    //			if(strcmp(stu[j].idCard,stu[j+1].idCard)>0)
    //			{
    //				temp =stu[j+1];
    //				stu[j+1]=stu[j];
    //				stu[j]=temp;
    //
    //			}
    //		}
    //	}
    //
    //
    //	printf("
    ");
    //	printf("*依据学生身份证排序后的学生情况:
    
    ");
    //
    //	for(int i=0;i<N;i++)
    //	{
    //		printf("第 %d 个学生信息:
    学号:%ld	姓名:%s	身份证:%s	语文:%.2f	数学:%.2f	英语:%.2f
    
    ",i+1,stu[i].stuNum,stu[i].name,stu[i].idCard,stu[i].score[0],stu[i].score[1],stu[i].score[2]);
    //
    //	}
    //
    //
    //}
    //
    ////依据学号进行排序
    //
    //void stuNum_sort(student stu[],int n)
    //{
    //	student temp;
    //
    //	for(int i=0;i<n-1;i++)
    //	{
    //		for(int j=0;j<n-1-i;j++)
    //		{
    //			if(stu[j].stuNum>stu[j+1].stuNum)
    //			{
    //				temp =stu[j+1];
    //				stu[j+1]=stu[j];
    //				stu[j]=temp;
    //
    //			}
    //		}
    //	}
    //
    //
    //	printf("
    ");
    //	printf("*依据学生学号排序后的学生情况:
    
    ");
    //
    //	for(int i=0;i<N;i++)
    //	{
    //		printf("第 %d 个学生信息:
    学号:%ld	姓名:%s	身份证:%s	语文:%.2f	数学:%.2f	英语:%.2f
    
    ",i+1,stu[i].stuNum,stu[i].name,stu[i].idCard,stu[i].score[0],stu[i].score[1],stu[i].score[2]);
    //
    //	}
    //
    //
    //}
    //
    ////main函数
    //
    //int main()
    //{
    //
    //	struct student stu[N];
    //	
    //	//控制台屏幕变为蓝色背景
    //	system("color 1f");
    //
    //	printf("请依次输入学生的学号,姓名,身份证,三门成绩(空格分开)
    ");
    //	for(int i=0;i<N;i++)
    //	{
    //		printf("输入第 %d 个学生的信息
    ",i+1);
    //		scanf("%ld%s%s%f%f%f",&stu[i].stuNum,stu[i].name,stu[i].idCard,&stu[i].score[0],&stu[i].score[1],&stu[i].score[2]);
    //
    //	}
    //
    //	//清屏
    //	system("cls");
    //	
    //	
    //	//printf("*你所输入的学生信息情况:
    ");
    //	//for(i=0;i<N;i++)
    //	//{
    //	//	printf("第 %d 个学生信息:
    学号:%ld	姓名:%s	身份证:%s	语文:%.2f	数学:%.2f	英语:%.2f
    ",i+1,stu[i].stuNum,stu[i].name,stu[i].idCard,stu[i].score[0],stu[i].score[1],stu[i].score[2]);
    //	//
    //	//}
    //	
    //
    //	printf("
    *** 输入1 依照学生学号排序 ***
    *** 输入2 依照学生姓名排序 ***
    *** 输入3 依照学生身份证排序 ***
    *** 输入0 退出 ***
    
    ");
    //	
    //	printf("请输入:");
    //	int t;
    //	scanf("%d",&t);
    //	
    //	//循环
    //	do{
    //		//依据用户输入的值选择排序的字段
    //		switch (t)
    //		{
    //		case 1:
    //				stuNum_sort(stu,N);//学号排序
    //				break;
    //		case 2:
    //				name_sort(stu,N);//姓名排序
    //				break;
    //				
    //		case 3:
    //				idCard_sort(stu,N);//身份证排序
    //				break;
    //			
    //		default:
    //			name_sort(stu,N);
    //		}
    //
    //
    //		printf("
    请输入:");
    //		scanf("%d",&t);
    //
    //	}while(t!=0);
    //
    //
    //	return 1;
    //}
    
    首页效果图:

    资源下载:

    http://download.csdn.net/my/uploads

  • 相关阅读:
    网管的自我修养-网络系统
    网管的自我修养-电脑维护
    iOS继承与类别
    iOS支付宝集成
    HTTP HTTPS TCP/IP UDP
    AFNetworking新版本3.0的迁移
    GCD使用 并行串行队列同步异步执行组合情况
    使用vim遇到的问题
    mac取色
    网络解析
  • 原文地址:https://www.cnblogs.com/cynchanpin/p/6753460.html
Copyright © 2020-2023  润新知