C语言结构体之学生教师管理系统简单实现,结构体。

题目:设有若干人员的数据,其中有老师和学生.学生的数据中主要包括姓名、号码、性别、职业、班级。老师的数据主要包括姓名、号码、性别、职业、职务。要求可以输入人员的数据并能输出它们的资料,把资料放在同一个表格中(也就是只能用一个结构体,根据职业的不同,在选择是班级还是职务

主菜单效果图:

添加人员:

搜索人员:

 

C语言程序代码:

#include<stdio.h>
#include<windows.h>
#include<string.h>

struct DATA
{
    char name[10];   // 姓名
    char number[15]; // 号码
    char sex[3];     // 性别
    char career[5];  // 职业
    char class[10];  // 班级
    char duties[10]; // 职务
};


void displayMenu() {
    system("cls");
    printf("\t学生教师管理系统\n\n");
    printf("\t1.查看人员\n");
    printf("\t2.搜索人员\n");
    printf("\t3.添加人员\n");
    printf("\t4.删除人员\n");
    printf("\n\n\n");
}


// 显示所有员工信息
void display(struct DATA *data, int count) {
    
    system("cls");
    if (count <= 0) {
        printf("没有任何数据\n\n");
        printf("1.返回\n");
    }else{
        for (int i = 0; i < count; ++i) {
            printf("---------------------------------------\n");
            printf("姓名: %s\n", data[i].name);
            printf("手机号码: %s\n", data[i].number);
            printf("性别: %s\n", data[i].sex);
            printf("职业: %s\n", data[i].career);

            if(strcmp(data[i].career,"教师") == 0 || strcmp(data[i].career,"老师") == 0) {
                printf("职务: %s\n", data[i].duties);
            }else if(strcmp(data[i].career,"学生") == 0) {
                printf("班级: %s\n", data[i].class);
            }else{
                printf("班级: %s\n", data[i].class);
            }
        }
        printf("\n");
        printf("1.返回\n");
    }
}



void add(struct DATA *data, int *count) {

    system("cls");
    if (*count >= 100) {
        printf("没有任何数据\n\n");
        printf("1.返回\n");
    }else{
        struct DATA newData;
        printf("请输入姓名: ");
        scanf("%s", newData.name);

        printf("请输入号码: ");
        scanf("%s", newData.number);

        printf("请输入性别: ");
        scanf("%s", &newData.sex);

        printf("请输入职业: ");
        scanf("%s", &newData.career);

        if(strcmp(newData.career,"教师") == 0 || strcmp(newData.career,"老师") == 0) {
            printf("请输入职务: ");
            scanf("%s", &newData.duties);
        }else if(strcmp(newData.career,"学生") == 0) {
            printf("请输入班级: ");
            scanf("%s", &newData.class);
        }else{
            printf("请输入班级: ");
            scanf("%s", &newData.class);
        }

        data[*count] = newData;
        (*count)++;

        printf("添加人员信息成功\n\n");
        printf("\n1.返回");
        printf("\n2.继续\n");
    }
}


void search(struct DATA *data, int count) {

    system("cls");
    printf("输入要搜索的姓名:");
    char word[10];
    scanf("%s", word);

    if (count <= 0) {
        printf("没有任何数据\n\n");
        printf("1.返回\n");
    }else{
        for (int i = 0; i < count; ++i) {
            char *result = strstr(data[i].name, word);
            if (result != NULL) {
                printf("---------------------------------------\n");
                printf("姓名: %s\n", data[i].name);
                printf("手机号码: %s\n", data[i].number);
                printf("性别: %s\n", data[i].sex);
                printf("职业: %s\n", data[i].career);

                if(strcmp(data[i].career,"教师") == 0 || strcmp(data[i].career,"老师") == 0) {
                    printf("职务: %s\n", data[i].duties);
                }else if(strcmp(data[i].career,"学生") == 0) {
                    printf("班级: %s\n", data[i].class);
                }else{
                    printf("班级: %s\n", data[i].class);
                }
            }
        }
        printf("\n");
        printf("1.返回\n");
    }
}


int main(void) {

    struct DATA data[100]; // 设置存储最大限制 100个
    int count = 0;
    int key = 0;
    

    while (1) {
        displayMenu();
        scanf("%d",&key);
        if(key == 1) {    // 显示所有人员
            display(data, count);
            scanf("%d",&key);
            while (key != 1) {
                display(data, count);
                scanf("%d",&key);
            }
        }else if(key ==2) {
            search(data,count);
            scanf("%d",&key);
            while (key != 1) {
                search(data,count);
                scanf("%d",&key);
            }
        }else if(key == 3) {  // 添加人员
            add(data,&count);
            scanf("%d",&key);

            while (key == 2) {
                add(data,&count);
                scanf("%d",&key);
            }
        }
    }
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

百院小韦

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

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

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

打赏作者

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

抵扣说明:

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

余额充值