这是帮别人做的一个题目,好久没有接触过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. 简单理解Hash算法的作用

    什么是Hash Hash算法,简称散列算法,也成哈希算法(英译),是将一个大文件映射成一个小串字符.与指纹一样,就是以较短的信息来保证文件的唯一性的标志,这种标志与文件的每一个字节都相关,而且难以找到 ...

  2. mysql数据库隔离级别

    # 原创,转载请留言联系 事务的隔离级别 (由高到低)1.串行化(serializable):一个事务一个事务的执行2.可重复读(Repeatable-Read) 可重复读,无论其他事务是否修改并提交 ...

  3. python_day6学习笔记

    一.Logger模块 logging.basicConfig函数 可通过具体参数来更改logging模块默认行为,可用参数有 filename: 用指定的文件名创建FiledHandler(后边会具体 ...

  4. Container With Most Water——双指针

    Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai).  ...

  5. [Android] 按钮单击事件的五种写法

    在平时学习安卓的过程中,不论是看视频还是看博客,我发现每个人对代码的写法都有不同的偏好,比较明显的就是对控件响应事件的写法的不同.所以我想把这些写法总结一下,比较下各种写法的优劣,希望可以让自己可以灵 ...

  6. EF6 Working with Proxies ProxyCreationEnabled

    When creating instances of POCO entity types, the Entity Framework often creates instances of a dyna ...

  7. cookie笔记(二)

    小荔枝 增 删 查 改 <form action="javascript:void(0)" method="get" accept-charset=&qu ...

  8. python 编码处理

    # -*- coding: utf-8 -*-import easygui as gimport sysreload(sys)sys.setdefaultencoding('utf-8')

  9. nginx应用场景,特性,目录结构,常用模块,内置变量,URL和URI,http状态码,配置文件详解

    1.nginx介绍 1丶俄罗斯人开发的,开源www服务软件 2丶软件一共780K 3丶nginx本身是一款静态(html,js,css,jpg等)www软件 4丶静态小文件高并发,同时占用的资源很少, ...

  10. 【剑指offer】面试题 11. 旋转数组的最小数字

    面试题 11. 旋转数组的最小数字 题目描述 题目:把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转. 输入一个非递减排序的数组的一个旋转,输出旋转数组的最小元素. 例如数组{3,4, ...