一看就懂的 学生学籍管理系统(C语言实现)

草率了 之前那个把long 和long long的输出输入整错了 %lld 和%ld改错了 ,谢谢这位小兄弟

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
const int maxn = 100; // 最多存储maxn条学籍记录
struct Student {
    long long  id; // 学号
    char name[200]; // 姓名
    char sex[200]; // 性别,
    int score; // 分数
}stu[100];
int n;
void show(int i)
{
    // 显示一条记录
    printf("%lld\t%s\t%s\t%d", stu[i].id, stu[i].name, stu[i].sex, stu[i].score);
}

bool add()
{
    // 新增记录
    if (n >= maxn - 5)
        return false;
    printf("请输入学号: ");
    scanf("%lld", &stu[n].id);//输入学号的代码
    printf("请输入姓名: ");
    scanf("%s", stu[n].name);// 输入姓名的代码
    printf("请输入性别: ");
    scanf("%s", stu[n].sex); // 请勿改动此句
    printf("请输入分数: ");
    scanf("%d", &stu[n].score);
    n++;
    return true;
}

void show_all()
{
    // 显示所有记录
    for (int i = 0; i < n; i++)
    {
        show(i);
        printf("\n");
    }
}

int search1(long long id)
{
    // 按学号id查找记录
    for (int i = 0; i < n; i++)
        if (id == stu[i].id)
            return i;
    return -1;
}

int search(char name[])
{
    // 按姓名name查找记录
    for (int i = 0; i < n; i++)
        if (strcmp(name, stu[i].name) == 0)
            return i;
    return -1;
}

bool modify_by_id()
{
    // 按学号修改记录
    long long id;
    printf("请输入学号:");
    scanf("%lld", &id);
    int position = search1(id);
    if (position < 0)
    {
        return false;
    }
    else {
        printf("请输入学号: ");
        scanf("%lld", &stu[position].id);//输入学号的代码
        printf("请输入姓名: ");
        scanf("%s", stu[position].name);// 输入姓名的代码
        printf("请输入性别: ");
        scanf("%s", stu[position].sex); // 请勿改动此句
        printf("请输入分数: ");
        scanf("%d", &stu[position].score);
    }
    return true;
}

bool del_by_id()
{
    // 删除指定学号的记录
    long long id;
    printf("请输入学号:");
    scanf("%lld", &id);
    int position = search1(id);
    if (position < 0)
    {
        return false; // 记录不存在,返回false
    }
    else {
        for (int i = position + 1; i < n; i++) {
            stu[i - 1] = stu[i];
        }
        n--;
        return true;
    }// 删除position位置的元素
    // 删除成功,返回true 
}
void sort()
{
    // 按成绩从高到低排序
    for (int i = 0; i < n - 1; i++)
        for (int j = i + 1; j < n; j++)
            if (stu[i].score < stu[j].score)
            {
                struct Student tmp;

                tmp.id = stu[i].id;
                tmp.score = stu[i].score;
                strcpy(tmp.name, stu[i].name);
                strcpy(tmp.sex, stu[i].sex);

               stu[i].id = stu[j].id;
                stu[i].score = stu[j].score;
                strcpy(stu[i].name, stu[j].name);
                strcpy(stu[i].sex, stu[j].sex);

                 stu[j].id= tmp.id;
                stu[j].score= tmp.score;
                strcpy( stu[j].name, tmp.name);
                strcpy(stu[j].sex, tmp.sex);
            }
}

void menu()
{
    printf("1. 新增一条学籍记录\n");
    printf("2. 显示所有学籍记录\n");
    printf("3. 按学号查找学籍记录\n");
    printf("4. 按姓名查找学籍记录\n");
    printf("5. 按学号修改学籍记录\n");
    printf("6. 按学号删除学籍记录\n");
    printf("7. 按分数排序排序\n");
    printf("8. 退出\n");
}

int main()
{
    int choice;
    do
    {
        system("cls");
        menu();

        scanf("%d", &choice);
        if (choice == 1)
        {
            if (add())
            {
                printf("新增成功!\n");
            }
            else
            {
                printf("数组已满,新增失败\n");
            }
        }
        else if (choice == 2)
        {
            show_all();// ______补充代码,调用show_all函数显示所有记录
        }
        else if (choice == 3)
        {
            long long id;
            printf("请输入学号:");
            scanf("%lld", &id);
            int position = search1(id);// int position = ______ 补充代码,调用searh按学号查找记录在数组中的位置
            if (position < 0)
            {
                printf("记录不存在!\n");
            }
            else
            {
                show(position);//______ 补充代码,调用show显示记录
            }
        }
        else if (choice == 4)
        {
            char name[20];
            printf("请输入姓名:");
            scanf("%s", name);// ____ 补充代码输入姓名name
            int position = search(name);// int position = ______ 补充代码,调用searh按姓名查找记录在数组中的位置
            if (position < 0)
            {
                printf("记录不存在!\n");
            }
            else
            {
                show(position);//______ 补充代码,调用show显示记录
            }
        }
        else if (choice == 5)
        {
            if (modify_by_id())
            {
                printf("修改成功!\n");
            }
            else
            {
                printf("记录不存在\n");
            }
        }
        else if (choice == 6)
        {
            if (del_by_id())
            {
                printf("删除成功\n");
            }
            else
            {
                printf("记录不存在\n");
            }
        }
        else if (choice == 7)
        {
            sort(); // 调用sort函数排序
            show_all();// 调用show_all显示排序后的结果
        }
        else
        {
            break; // 退出系统
        }
        system("pause");
    } while (true);
    printf("bye!\n");
    return 0;
}

  • 0
    点赞
  • 103
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 14
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 14
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

DongGu.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值