C语言实现学生信息管理程序

让我来详细讲解“C语言实现学生信息管理程序”的攻略。

概述

学生信息管理程序是一个非常基础的程序,它主要实现如下功能:

  • 添加学生信息
  • 查询学生信息
  • 修改学生信息
  • 删除学生信息

开发学生信息管理程序可以加强我们对C语言基础知识的掌握,例如指针、结构体等等。

开发步骤

1. 创建学生信息结构体

首先,我们需要创建一个结构体来存储学生信息。结构体可以包含学生的姓名、学号、性别、出生日期、电话号码等信息。

struct Student {
    char name[20];
    int id;
    char sex[10];
    char birthday[20];
    char phone[20];
};

2. 添加学生信息

我们可以使用数组存储学生信息,每次添加学生信息时,先判断数组是否已满,如果未满则添加学生信息。

#define MAX_STUDENT_NUM 100

struct Student students[MAX_STUDENT_NUM];
int currentStudentNum = 0;

void addStudent() {
    if (currentStudentNum >= MAX_STUDENT_NUM) {
        printf("Failed to add student, the number of students has reached the highest.\n");
        return;
    }

    struct Student student;
    printf("Please input the student name: ");
    scanf("%s", student.name);
    printf("Please input the student id: ");
    scanf("%d", &student.id);
    printf("Please input the student sex: ");
    scanf("%s", student.sex);
    printf("Please input the student birthday: ");
    scanf("%s", student.birthday);
    printf("Please input the student phone: ");
    scanf("%s", student.phone);
    students[currentStudentNum] = student;
    currentStudentNum++;
    printf("Successfully added student.\n");
}

3. 查询学生信息

我们可以通过输入学生的学号或姓名来查询具体某个学生的信息,也可以查询所有学生的信息。

void searchStudent() {
    printf("Please select the search method:\n");
    printf("1. Search by student id.\n");
    printf("2. Search by student name.\n");
    printf("3. Search all students.\n");

    int option;
    scanf("%d", &option);

    switch (option) {
        case 1: {
            int id;
            printf("Please input the student id to search: ");
            scanf("%d", &id);
            for (int i = 0; i < currentStudentNum; i++) {
                if (students[i].id == id) {
                    printf("name: %s, id: %d, sex: %s, birthday: %s, phone: %s\n", students[i].name, students[i].id, students[i].sex, students[i].birthday, students[i].phone);
                    return;
                }
            }
            printf("No student found with the id of %d.\n", id);
            break;
        }
        case 2: {
            char name[20];
            printf("Please input the student name to search: ");
            scanf("%s", name);
            int foundCount = 0;
            for (int i = 0; i < currentStudentNum; i++) {
                if (strcmp(students[i].name, name) == 0) {
                    printf("name: %s, id: %d, sex: %s, birthday: %s, phone: %s\n", students[i].name, students[i].id, students[i].sex, students[i].birthday, students[i].phone);
                    foundCount++;
                }
            }
            if (foundCount == 0) {
                printf("No student found with the name of %s.\n", name);
            }
            break;
        }
        case 3: {
            if (currentStudentNum == 0) {
                printf("No students added yet.\n");
            } else {
                for (int i = 0; i < currentStudentNum; i++) {
                    printf("name: %s, id: %d, sex: %s, birthday: %s, phone: %s\n", students[i].name, students[i].id, students[i].sex, students[i].birthday, students[i].phone);
                }
            }
            break;
        }
        default: {
            printf("Invalid input.\n");
            break;
        }
    }
}

4. 修改学生信息

我们可以通过输入学生的学号或姓名来修改具体某个学生的信息。

void modifyStudent() {
    printf("Please input the student id or name to modify: ");
    char input[20];
    scanf("%s", input);
    int foundIndex = -1;
    for (int i = 0; i < currentStudentNum; i++) {
        if (strcmp(students[i].name, input) == 0 || students[i].id == atoi(input)) {
            foundIndex = i;
            break;
        }
    }
    if (foundIndex == -1) {
        printf("No student found with the name or id of %s.\n", input);
    } else {
        struct Student student;
        printf("Please input the new student name (current: %s): ", students[foundIndex].name);
        scanf("%s", student.name);
        printf("Please input the new student id (current: %d): ", students[foundIndex].id);
        scanf("%d", &student.id);
        printf("Please input the new student sex (current: %s): ", students[foundIndex].sex);
        scanf("%s", student.sex);
        printf("Please input the new student birthday (current: %s): ", students[foundIndex].birthday);
        scanf("%s", student.birthday);
        printf("Please input the new student phone (current: %s): ", students[foundIndex].phone);
        scanf("%s", student.phone);
        students[foundIndex] = student;
    }
}

5. 删除学生信息

我们可以通过输入学生的学号或姓名来删除具体某个学生的信息。

void deleteStudent() {
    printf("Please input the student id or name to delete: ");
    char input[20];
    scanf("%s", input);
    int foundIndex = -1;
    for (int i = 0; i < currentStudentNum; i++) {
        if (strcmp(students[i].name, input) == 0 || students[i].id == atoi(input)) {
            foundIndex = i;
            break;
        }
    }
    if (foundIndex == -1) {
        printf("No student found with the name or id of %s.\n", input);
    } else {
        for (int i = foundIndex; i < currentStudentNum - 1; i++) {
            students[i] = students[i + 1];
        }
        currentStudentNum--;
        printf("Successfully deleted student.\n");
    }
}

示例说明

示例1:添加/student

在程序中输入1,然后输入学生的姓名、学号、性别、出生日期和电话号码,程序会将这个学生添加到列表中。

Please select the operation:
1. Add student.
2. Search student.
3. Modify student.
4. Delete student.
5. Exit.
1
Please input the student name: Tom
Please input the student id: 123
Please input the student sex: Male
Please input the student birthday: 1990/1/1
Please input the student phone: 18600000000
Successfully added student.

示例2:查询学生信息

在程序中输入2,然后根据提示选择查询方式,可以根据学号、姓名或者全部查询学生信息。

Please select the operation:
1. Add student.
2. Search student.
3. Modify student.
4. Delete student.
5. Exit.
2
Please select the search method:
1. Search by student id.
2. Search by student name.
3. Search all students.
3
name: Tom, id: 123, sex: Male, birthday: 1990/1/1, phone: 18600000000

这就是“C语言实现学生信息管理程序”的完整攻略,希望对你有所帮助。

本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:C语言实现学生信息管理程序 - Python技术站

(0)
上一篇 2023年5月22日
下一篇 2023年5月22日

相关文章

  • c++如何实现Base64算法

    C++如何实现Base64算法 Base64简介 Base64是一种将二进制数据编码成ASCII字符的方法,常用于在HTTP协议等网络协议中将二进制数据进行传输。 Base64将三个8位的字节转换为四个6位的字节,产生出来的输出结果最后可能会出现1~2个”=”号,这是为了补齐长度用的。 例如,“Man”这个单词被编码后为“TWFu”,解码后为”Man”。 C…

    C 2023年5月22日
    00
  • C++面向对象实现五子棋小游戏

    C++面向对象实现五子棋小游戏攻略 A. 概述 本文将介绍如何通过C++面向对象的方式实现五子棋小游戏。本文的重点是通过面向对象的分析和设计,呈现出一个完整的OOP编程流程。具体的实现代码在这里不赘述,通过项目开发过程中的分析和设计,读者可以获得更为重要的启发。 B. 项目分析 1. 确定项目需求 我们首先需要确定实现五子棋小游戏(Gobang)需要满足的核…

    C 2023年5月22日
    00
  • C语言中字符串和数字的相互转换实现代码

    C语言中字符串和数字的相互转换是常见的编程操作。下面是一些实现代码,以便帮助你进行相应的转换。 将字符串转换为数字 C语言中,字符串可以使用标准库函数 atoi() 转换为整数。由于 atoi() 是标准库函数,因此需要包含头文件 <stdlib.h>。 #include <stdio.h> #include <stdlib.h…

    C 2023年5月24日
    00
  • C++静态成员变量和静态成员函数的使用方法总结

    C++静态成员变量和静态成员函数的使用方法总结 C++中的静态成员变量和静态成员函数是相对于类而言的,它们不是属于对象的,而是属于类的。静态成员变量和静态成员函数的使用可以方便地实现一些数据的共享和对这些数据的操作。在本文中,我将对C++中的静态成员变量和静态成员函数的使用进行总结,并给出示例说明。 静态成员变量 静态成员变量的定义 在类的定义外部定义静态成…

    C 2023年5月23日
    00
  • 一起来了解c语言的str函数

    一起来了解C语言的str函数 str函数简介 在C语言中,字符串是以字符数组的形式存在的,而str函数就是C语言中对字符串的处理函数之一。str函数常用于字符串的复制,连接,比较和查找等操作。 str函数的常用类型 str函数有多个类型,其中最常用的函数类型如下: strcpy: 字符串拷贝函数,用于将源字符串复制到目标字符串中。 strcat: 字符串连接…

    C 2023年5月23日
    00
  • C语言实现链队列

    接下来我将详细讲解“C语言实现链队列”的完整攻略。 什么是链队列 链队列是一种基于链表的队列实现,其底层数据结构为一个链表。相比于数组实现的队列,链队列具有动态分配内存空间的优势。链队列的队首与队尾分别指向链表的首尾节点,数据元素按顺序排列,后进先出。 实现链队列的步骤 1. 定义队列结构体 首先,需要定义队列结构体,包括队列的基本属性和操作方法: // 定…

    C 2023年5月23日
    00
  • 面试题积累_01

    1 如何判断一个数是否为奇数? //常规方法 bool isOdd_Method1(int n) { if (n % 2) return true; else return false; } //高效方法 bool isOdd_Method2(int n) { //奇数的二进制形式最后一位一定是1 return n & 0x1; } 注:二进制除了最…

    C语言 2023年4月18日
    00
  • C++游戏编程之模拟实现键盘打字程序

    C++游戏编程之模拟实现键盘打字程序 简介 键盘打字游戏是目前非常流行的游戏之一。本文将介绍如何使用C++编写一个小型的键盘打字游戏,用于锻炼玩家的打字能力。本文将通过模拟实现的方式来介绍如何编写这个小型游戏程序。 过程 1.首先,我们需要设计游戏界面。游戏界面应该包括一个文本框、一个文本输入框和一个“开始”按钮。用户需要在文本输入框中输入键盘上的单词,按下…

    C 2023年5月23日
    00
合作推广
合作推广
分享本页
返回顶部