这是帮别人做的一个题目,好久没有接触过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("\n");
printf("*依据学生姓名排序后的学生情况:\n\n"); for(int i=0;i<N;i++)
{
printf("第 %d 个学生信息:\n学号:%ld\t姓名:%s\t身份证:%s\t语文:%.2f\t数学:%.2f\t英语:%.2f\n\n",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("\n");
printf("*依据学生身份证排序后的学生情况:\n\n"); for(int i=0;i<N;i++)
{
printf("第 %d 个学生信息:\n学号:%ld\t姓名:%s\t身份证:%s\t语文:%.2f\t数学:%.2f\t英语:%.2f\n\n",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("\n");
printf("*依据学生学号排序后的学生情况:\n\n"); for(int i=0;i<N;i++)
{
printf("第 %d 个学生信息:\n学号:%ld\t姓名:%s\t身份证:%s\t语文:%.2f\t数学:%.2f\t英语:%.2f\n\n",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("请依次输入学生的学号,姓名,身份证,三门成绩(空格分开)\n");
for(int i=0;i<N;i++)
{
printf("输入第 %d 个学生的信息\n",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("\n*** 输入1 依照学生学号排序 ***\n*** 输入2 依照学生姓名排序 ***\n*** 输入3 依照学生身份证排序 ***\n*** 输入0 退出 ***\n\n"); 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("\n请输入:");
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("\n");
// printf("*依据学生姓名排序后的学生情况:\n\n");
//
// for(int i=0;i<N;i++)
// {
// printf("第 %d 个学生信息:\n学号:%ld\t姓名:%s\t身份证:%s\t语文:%.2f\t数学:%.2f\t英语:%.2f\n\n",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("\n");
// printf("*依据学生身份证排序后的学生情况:\n\n");
//
// for(int i=0;i<N;i++)
// {
// printf("第 %d 个学生信息:\n学号:%ld\t姓名:%s\t身份证:%s\t语文:%.2f\t数学:%.2f\t英语:%.2f\n\n",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("\n");
// printf("*依据学生学号排序后的学生情况:\n\n");
//
// for(int i=0;i<N;i++)
// {
// printf("第 %d 个学生信息:\n学号:%ld\t姓名:%s\t身份证:%s\t语文:%.2f\t数学:%.2f\t英语:%.2f\n\n",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("请依次输入学生的学号,姓名,身份证,三门成绩(空格分开)\n");
// for(int i=0;i<N;i++)
// {
// printf("输入第 %d 个学生的信息\n",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("*你所输入的学生信息情况:\n");
// //for(i=0;i<N;i++)
// //{
// // printf("第 %d 个学生信息:\n学号:%ld\t姓名:%s\t身份证:%s\t语文:%.2f\t数学:%.2f\t英语:%.2f\n",i+1,stu[i].stuNum,stu[i].name,stu[i].idCard,stu[i].score[0],stu[i].score[1],stu[i].score[2]);
// //
// //}
//
//
// printf("\n*** 输入1 依照学生学号排序 ***\n*** 输入2 依照学生姓名排序 ***\n*** 输入3 依照学生身份证排序 ***\n*** 输入0 退出 ***\n\n");
//
// 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("\n请输入:");
// scanf("%d",&t);
//
// }while(t!=0);
//
//
// return 1;
//}

首页效果图:

资源下载:

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

C语言-对一个结构体中的字段进行排序的更多相关文章

  1. C结构体中数据的内存对齐问题

    转自:http://www.cnblogs.com/qwcbeyond/archive/2012/05/08/2490897.html 32位机一般默认4字节对齐(32位机机器字长4字节),64位机一 ...

  2. Go语言基础之结构体

    Go语言基础之结构体 Go语言中没有“类”的概念,也不支持“类”的继承等面向对象的概念.Go语言中通过结构体的内嵌再配合接口比面向对象具有更高的扩展性和灵活性. 类型别名和自定义类型 自定义类型 在G ...

  3. Go语言教程之结构体

    Hello,大家好,我是小栈君,最近因为工作的事情延误了一点分享的进度,但是我会尽量抽时间分享关于IT干货知识,还希望大家能够持续关注"IT干货栈"哦. 闲话不多说,今天给大家继续 ...

  4. GO学习-(13) Go语言基础之结构体

    Go语言基础之结构体 Go语言中没有"类"的概念,也不支持"类"的继承等面向对象的概念.Go语言中通过结构体的内嵌再配合接口比面向对象具有更高的扩展性和灵活性. ...

  5. matlab学习笔记12_2创建结构体数组,访问标量结构体,访问非标量结构体数组的属性,访问嵌套结构体中的数据,访问非标量结构体数组中多个元素的字段

    一起来学matlab-matlab学习笔记12 12_2 结构体 创建结构体数组,访问标量结构体,访问非标量结构体数组的属性,访问嵌套结构体中的数据,访问非标量结构体数组中多个元素的字段 觉得有用的话 ...

  6. c语言结构体中的一个char数组怎么赋值?

    目录 前景提示 这里的结构体处理的步骤 一.char数组类型的处理 二.char数组指针类型的处理 三.全部代码 1. char数组 2. char数组指针 结语 前景提示 定义一个结构体,结构体中有 ...

  7. C语言 结构体中的成员域偏移量

    //C语言中结构体中的成员域偏移量 #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> # ...

  8. C语言结构体中的函数指针

      这篇文章简单的叙述一下函数指针在结构体中的应用,为后面的一系列文章打下基础 本文地址:http://www.cnblogs.com/archimedes/p/function-pointer-in ...

  9. c语言结构体中的冒号的用法

    结构体中常见的冒号的用法是表示位域. 有些信息在存储时,并不需要占用一个完整的字节,   而只需占几个或一个二进制位.例如在存放一个开关量时,只有0和1   两种状态,   用一位二进位即可.为了节省 ...

随机推荐

  1. Dubbo 用户手册学习笔记 —— Dubbo架构

    Dubbo的架构 节点角色说明 节点 角色说明 Provider 服务提供方 Consumer 服务消费方 Registry 服务注册与发现的注册中心 Monitor 统计服务的调用次数和调用时间的监 ...

  2. Set Matrix Zeroes——常数空间内完成

    Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. Did yo ...

  3. laravel中新增路由文件

    随着业务的发展,前后台和不同平台的代码都写在一个路由文件里面会非常庞杂,这时候就诞生了拆分路由文件的需求,好在Lavravel给我们提供了支持: 1.在routes文件夹中添加新的路由文件如:admi ...

  4. Vue结合原生js实现自定义组件自动生成

    就目前三大前端主流数据驱动框架(vue,ng,react)而言,均具有创建自定义组件的api,但都是必须先做到事先写好挂载点,这个挂载点可以是原有静态元素标签也可以是自定义模板:对于多种组件通过同一数 ...

  5. IllegalStateException: Unable to find a @SpringBootConfiguration

    此处需要改掉包名和类名

  6. 将xml文件转为c#对像

    读取xml文件数据,通过序列化反序列化转为List<T>对象后,对对象进行操作.

  7. destoon 部署到服务器如何修改密码使网站能正常访问登录

    1.根目录的config.inc.php  修改 db_pass 2.缓存文件 根目录/file/cache/module-2.php   修改 uc_dbpwd PS:不一定都在module-2.p ...

  8. go chapter 8 - 初始化对象

    http://blog.haohtml.com/archives/14239 struct定义的属性如果是小写开头的,那么该属性不是public的,不能跨包调用 (implicit assignmen ...

  9. Flask实战第60天:帖子分页技术实现

    编辑manage.py,添加测试帖子 @manager.command def create_test_post(): for x in range(1, 100): title = '标题{}'.f ...

  10. 「LGR-049」洛谷7月月赛 D.Beautiful Pair

    「LGR-049」洛谷7月月赛 D.Beautiful Pair 题目大意 : 给出长度为 \(n\) 的序列,求满足 \(i \leq j\) 且 $a_i \times a_j \leq \max ...