这是帮别人做的一个题目,好久没有接触过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. leetcode 之Remove Duplicates from Sorted List(17)

    很简单的一题,需要注意的是如果某结点重复了记得将其删除. ListNode *deleteDuplicates(ListNode *head) { if (head == nullptr) retur ...

  2. NOIP 2012 Day2

    tags: 扩展欧几里得 二分答案 查分 倍增 二分答案 贪心 NOIP categories: 信息学竞赛 总结 同余方程 借教室 疫情控制 同余方程 Solution 首先同余式可以转化为等式. ...

  3. 几个例子理解对称加密与非对称加密、公钥与私钥、签名与验签、数字证书、HTTPS加密方式

    # 原创,转载请留言联系 为什么会出现这么多加密啊,公钥私钥啊,签名啊这些东西呢?说到底还是保证双方通信的安全性与完整性.例如小明发一封表白邮件给小红,他总不希望给别人看见吧.而各种各样的技术就是为了 ...

  4. LightOJ - 1370

    Bi-shoe and Phi-shoe Time Limit: 2000MS   Memory Limit: 32768KB   64bit IO Format: %lld & %llu S ...

  5. hdu 1130,hdu 1131(卡特兰数,大数)

    How Many Trees? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  6. python IDE的配置

    本人使用过的两款,系统环境ubuntukylin 15.04 jupyter 主要参考:ref1 和 ref2 遇到问题: error: [I 21:48:41.947 NotebookApp] Wr ...

  7. Jmeter----读取excel表中的数据

    Jmeter 读取excel数据使用的方法是使用CSV Data Set Config参数化,之后使用BeanShell Sampler来读取excel表中的数据 第一步.查看所需的接口都要哪些字段和 ...

  8. JavaScript备忘录-闭包(2)

    闭包的定义 闭包是指函数有自由独立的变量.换句话说,定义在闭包中的函数可以“记忆”它创建时候的环境. 闭包的浅显理解 function makeFunc() { var name = "Mo ...

  9. 【javascript】基于javascript的小时钟

    计时事件:通过JavaScript,我们可以设置在一段时间间隔后执行一段代码,而不仅仅是在函数调用后立即执行. 在JavaScript中,使用计时事件是很容易的,主要有两个事件供我们使用 setTim ...

  10. nio案例一:个简单的客户-服务的案例

    <Java NIO文档>非阻塞式服务器 原文连接 原文作者:Jakob Jenkov 译者:higher 即使你知道Java NIO 非阻塞的工作特性(如Selector,Channel, ...