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("\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语言-对一个结构体中的字段进行排序的更多相关文章
- C结构体中数据的内存对齐问题
转自:http://www.cnblogs.com/qwcbeyond/archive/2012/05/08/2490897.html 32位机一般默认4字节对齐(32位机机器字长4字节),64位机一 ...
- Go语言基础之结构体
Go语言基础之结构体 Go语言中没有“类”的概念,也不支持“类”的继承等面向对象的概念.Go语言中通过结构体的内嵌再配合接口比面向对象具有更高的扩展性和灵活性. 类型别名和自定义类型 自定义类型 在G ...
- Go语言教程之结构体
Hello,大家好,我是小栈君,最近因为工作的事情延误了一点分享的进度,但是我会尽量抽时间分享关于IT干货知识,还希望大家能够持续关注"IT干货栈"哦. 闲话不多说,今天给大家继续 ...
- GO学习-(13) Go语言基础之结构体
Go语言基础之结构体 Go语言中没有"类"的概念,也不支持"类"的继承等面向对象的概念.Go语言中通过结构体的内嵌再配合接口比面向对象具有更高的扩展性和灵活性. ...
- matlab学习笔记12_2创建结构体数组,访问标量结构体,访问非标量结构体数组的属性,访问嵌套结构体中的数据,访问非标量结构体数组中多个元素的字段
一起来学matlab-matlab学习笔记12 12_2 结构体 创建结构体数组,访问标量结构体,访问非标量结构体数组的属性,访问嵌套结构体中的数据,访问非标量结构体数组中多个元素的字段 觉得有用的话 ...
- c语言结构体中的一个char数组怎么赋值?
目录 前景提示 这里的结构体处理的步骤 一.char数组类型的处理 二.char数组指针类型的处理 三.全部代码 1. char数组 2. char数组指针 结语 前景提示 定义一个结构体,结构体中有 ...
- C语言 结构体中的成员域偏移量
//C语言中结构体中的成员域偏移量 #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> # ...
- C语言结构体中的函数指针
这篇文章简单的叙述一下函数指针在结构体中的应用,为后面的一系列文章打下基础 本文地址:http://www.cnblogs.com/archimedes/p/function-pointer-in ...
- c语言结构体中的冒号的用法
结构体中常见的冒号的用法是表示位域. 有些信息在存储时,并不需要占用一个完整的字节, 而只需占几个或一个二进制位.例如在存放一个开关量时,只有0和1 两种状态, 用一位二进位即可.为了节省 ...
随机推荐
- java - 线程1打印1-10,当线程打印到5后,线程2打印“hello”,然后线程1继续打印
public class T { private static int a =1;//1代表线程1 2线程2 public static void main(String[] args) { fina ...
- Linux 基础——开山篇
为什么要开始学习Linux命令? 首先当然是因为工作需要了,现在的工作是负责银行调度的系统的源系统接入的工作,经常要到生产部署版本.所以……买了一本<Linux命令行与shell脚本编程大全&g ...
- python资源合集
Python 官网: https://www.python.org/ Python2.7 doc: https://docs.python.org/2/ Python Package User Gui ...
- 走进 Cake for .NET
一.什么是 Cake Cake(C# Make) 是一个使用 C# DSL 面向 Task 的跨平台构建自动化系统,像编译代码,复制文件和文件夹,运行单元测试,压缩文件和构建 NuGet 包. 更多 ...
- 360杀毒导致的 VS 报扩展错误,请查看 ActiveLog.xml
360杀毒将 TypeScript的 tsserver.js 列为木马,结果导致VS2017启动时,总是报错,将其加为信任即可解决.
- thinkphp+dwz完成的一个号码查询小系统
基于网友的例子(http://www.thinkphp.cn/extend/450.html),改进完成一个电话号码查询管理系统.基于thinkphp+dwz完成的电话号码查询小系统,主要改进与功能如 ...
- es6解构赋值总结
数组的解构赋值 1.简单的赋值方式 2.多维数组解构赋值 3.默认值,只有当右边对应位置为undefined时候才会选择默认(null不属于undefined) 4.左右不对等,会相应的对号入座,没有 ...
- react native项目增加devtools工具
第一步:安装react devtools工具 在当前项目中打开命令行,添加react devtools工具,因为运行的工具有九十几M,下载时需要时间,请耐心等待 yarn add react-devt ...
- bWAPP练习--injection篇之HTML Injection - Reflected (GET)
什么是Injection? injection,中文意思就是注入的意思,常见的注入漏洞就是SQL注入啦,是现在应用最广泛,杀伤力很大的漏洞. 什么是HTML injection? 有交互才会产生漏洞, ...
- 【POJ 3177】Redundant Paths
http://poj.org/problem?id=3177 又写了一遍手动栈... 把边双都缩点,缩成一棵树,答案就是树中度数为1的点的个数除2上取整. 为什么呢?因为1个度数为1的点的树需要多连0 ...