c语言控制台 学生管理系统完整代码 保存在数组中

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
//定义结构体
struct student
{
    int id;      //学号
    char name[20]; //姓名
    int age;   //年龄
    char address[100]; //地址
};
typedef struct student Stu;
int main()
{
    printf("******************学生管理系统******************\n");
    Stu stus[10] = { 0 };
    Stu* p_stus_start = stus;
    Stu* p_stus_end = stus;
    while (1)
    {
        printf("------------------------------------------------\n");
        printf("学号\t姓名\t年龄\t地址\n");
        Stu* stu = p_stus_start;
        while (stu->id != 0) 
        {
            printf("%d\t%s\t%d\t%s\n", stu->id, stu->name, stu->age, stu->address);
            stu++;
        }
        printf("------------------------------------------------\n");
        printf("请选择:1 添加  2 修改  3 删除  4 查询  0 退出\n");
        int input;
        scanf("%d", &input);
        //添加
        if (1 == input)
        {
            Stu* stu = p_stus_end;
            int id;
            char name[20];
            int age;
            char address[20];
            printf("请按顺序添加(学号 姓名 年龄 地址)\n");
            scanf("%d %s %d %s", &id, name, &age, address);
            stu->id = id;
            strcpy(stu->name, name);
            stu->age = age;
            strcpy(stu->address, address);
            p_stus_end++;
            continue;
        }
        //修改
        else if (2 == input)
        {
            printf("请输入修改的ID:\n");
            int a;
            scanf("%d", &a);
            Stu* stu = p_stus_start;
            Stu* stu_search = p_stus_start;
            while (stu->id != 0)
            {
                if (stu->id == a)
                {
                    stu_search = stu;
                    break;
                }
                stu++;
            }
            printf("请输入修改的数据(姓名\t年龄\t地址):\n");
            char name[20];
            int age;
            char address[20];
            scanf("%s %d %s", name, &age, address);
            strcpy(stu_search->name, name);
            stu_search->age = age;
            strcpy(stu_search->address, address);
            continue;
        }
        //删除
        else if (3 == input)
        {
            printf("请输入删除的ID:\n");
            int a;
            scanf("%d", &a);
            Stu* stu = p_stus_start;
            Stu* stu_search = p_stus_start;
            while (stu->id != 0)
            {
                if (stu->id == a)
                {
                    stu_search = stu;
                }
                stu++;
            }
            Stu* stu_next = stu_search + 1;
            while (1) 
            {
                if (stu_next->id == 0) 
                {
                    stu_search->id = 0;
                    strcpy(stu_search->name, "");
                    stu_search->age = 0;
                    strcpy(stu_search->address, "");
                    p_stus_end--;
                    break;
                }
                *stu_search = *stu_next;
                stu_search++;
                stu_next++;
            }
        }
        //查询
        else if (4 == input)
        {
            printf("请输入查询的ID:\n");
            int a;
            scanf("%d", &a);
            Stu* stu = p_stus_start;
            Stu* stu_search = p_stus_start;
            while (stu->id != 0)
            {
                if (stu->id == a)
                {
                    stu_search = stu;
                    break;
                }
                stu++;
            }
            printf("------------------------------------------------\n");
            printf("学号\t姓名\t年龄\t地址\n");
            printf("%d\t%s\t%d\t%s\n", stu_search->id,stu_search->name,stu_search->age,stu_search->address);
            printf("------------------------------------------------\n");
            printf("0 退出\n");
            int b;
            scanf("%d", &b);
            if (0 == b)
            {
                continue;
            }
        }
        //退出
        else if (0 == input)
        {
            break;
        }
    }
    printf("已退出\n");
    getchar();
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

浮生若梦01

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

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

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

打赏作者

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

抵扣说明:

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

余额充值