zoukankan      html  css  js  c++  java
  • 指向结构体的指针作业

     1 #include <stdio.h>
     2 #define N 5
     3 struct Student {
     4     char name[20];
     5     int num;
     6     char sex;
     7     int age;
     8     char department[20];
     9 };
    10 void inputStudent(struct Student *p, int n);
    11 void sortStudent(struct Student *p, int n);
    12 void swap(struct Student *p, struct Student *q);
    13 void outputStudent(struct Student *p, int n);
    14 int main(void)
    15 {
    16     struct Student student[N];
    17     
    18     inputStudent(student, N);
    19     sortStudent(student, N);
    20     outputStudent(student, N);
    21     
    22     return 0;
    23 }
    24 void inputStudent(struct Student *p, int n)
    25 {
    26     int i;
    27     for (i = 0; i < n; i++, p++) {
    28         printf("请输入第%d个学生姓名:", i + 1);
    29         scanf("%s", p->name);
    30         printf("请输入第%d个学生学号:", i + 1);
    31         scanf("%d", &(p->num));
    32         getchar();
    33         printf("请输入第%d个学生性别:", i + 1);
    34         scanf("%c", &(p->sex));
    35         printf("请输入第%d个学生院系:", i + 1);
    36         scanf("%s", p->department);
    37         getchar();
    38     }
    39 }
    40 void sortStudent(struct Student *p, int n)
    41 {
    42     struct Student *end_p, *index_p, *temp_p;
    43     
    44     //指向最后一个学生的指针
    45     end_p = p + n -1;
    46     //遍历结构体数组
    47     for (; p < end_p; p++) {
    48         index_p = p;
    49         //找出最小值
    50         for (temp_p = index_p + 1; temp_p <= end_p; temp_p++) {
    51             if (temp_p->num < index_p->num) {
    52                 index_p = temp_p;
    53             }
    54         }
    55         if (index_p != p) {
    56             swap(p, index_p);
    57         }
    58     }
    59 }
    60 void swap(struct Student *p, struct Student *q)
    61 {
    62     struct Student temp;
    63     
    64     temp = *p;
    65     *p = *q;
    66     *q = temp;
    67 }
    68 void outputStudent(struct Student *p, int n)
    69 {
    70     struct Student *end_p;
    71     
    72     end_p = p + n - 1;
    73     for (; p <= end_p; p++) {
    74         printf("%s	%d	%c	%s
    ", p->name, p->num, p->sex ,p->department);
    75     }
    76 }

  • 相关阅读:
    read、readline、readlines和linecache的使用
    无法启用internet连接共享,为LAN连接配置的IP地址需要使用自动IP寻址
    虚拟机pycharm
    Ubuntu安装谷歌浏览器
    pandas dataframe重复数据查看.判断.去重
    git 删除误上传的.idea文件
    python logger日志通用配置文件
    pyinstaller打包python文件成exe(原理.安装.问题)
    SSH 免密登录服务器
    homebrew安装和解决brew安装速度慢的问题
  • 原文地址:https://www.cnblogs.com/2018jason/p/12929782.html
Copyright © 2011-2022 走看看