有戏

 找回密码
 立即注册
简历下载
搜索
热搜: 活动 交友 discuz
查看: 868|回复: 0

学生信息管理系统

[复制链接]
万事皆有可能 该用户已被删除
发表于 2015-12-31 10:50:50 | 显示全部楼层 |阅读模式
#include <stdio.h>
typedef struct
{
        int year;      //年
        int month;    //月
        int day;      //日
} Date;

typedef struct
{
        char name[40];        //学生姓名
        char sno[20];          //学号
        char sid[20];          //身份证号码
        Date birth;           //出生日期
        char address[200];     //家庭地址
} Student;

Student *stu = NULL;
int current = 0;
int total = 0;

int insert()
{

    while(total <= 0)
    {
        printf("请输入要录入的学生总数:");
        scanf("%d", &total);
    }

    if (stu == NULL)
        stu = (Student *)malloc(total*sizeof(Student));
    if (stu == NULL)
    {
        printf("分配失败\n");
        return 1;
    }
  
        if (current >= total)
        {
   
            Student *tmp = (Student *)malloc(current*sizeof(Student));

            memmove(tmp,stu,current*sizeof(Student));

                total += 10;
                realloc(stu,total*sizeof(Student));

        memmove(stu,tmp,current*sizeof(Student));
        free(tmp);
               
        }
  
        printf("请输入学生姓名:");
        scanf("%s", stu[current].name);
        printf("请输入学生学号:");
        scanf("%s", stu[current].sno);
        printf("请输入学生身份证号码:");
        scanf("%s", stu[current].sid);

        printf("请输入学生出生日期,格式(年-月-日):");
        scanf("%d-%d-%d", &stu[current].birth.year,
                          &stu[current].birth.month, &stu[current].birth.day);
        printf("请输入学生家庭地址:");
        scanf("%s", stu[current].address);

        current++;

        return 0;
}

int query()
{
        char name[40];
        int i;

        printf("请输入要查询的学生的姓名:");
        scanf("%s", name);

        for (i=0; i<current; i++)
        {
                if (strcmp(stu[i].name, name) == 0)
                {
                        printf("学生信息如下\n");
                        printf("姓名:%s\n", stu[i].name);
                        printf("学号:%s\n", stu[i].sno);
                        printf("身份证号:%s\n", stu[i].sid);
                        printf("出生日期:%d-%d-%d\n", stu[i].birth.year,
                                                  stu[i].birth.month, stu[i].birth.day);
                        printf("地址:%s\n", stu[i].address);

                        return 0;
                }
        }

        printf("该学生不存在!\n\n");
        return 1;
}

int update()
{
        char name[40];
        int i;

        printf("请输入要修改的学生的姓名:");
        scanf("%s", name);

        for (i=0; i<current; i++)
        {
                if (strcmp(stu[i].name, name) == 0)
                {
                        printf("请输入学生姓名:");
                        scanf("%s", stu[i].name);
                        printf("请输入学生学号:");
                        scanf("%s", stu[i].sno);
                        printf("请输入学生身份证号码:");
                        scanf("%s", stu[i].sid);

            printf("请输入学生出生日期,格式(年-月-日):");
            scanf("%d-%d-%d", &stu[i].birth.year,
                          &stu[i].birth.month, &stu[i].birth.day);
                        printf("请输入学生家庭地址:");
                        scanf("%s", stu[i].address);

                        return 0;
                }
        }
        printf("该学生不存在\n");

        return 1;
}

int delete()
{
        char name[40];
        int i;

        printf("请输入要删除的学生的姓名:");
        scanf("%s", name);

        for (i=0; i<current; i++)
        {
                if (strcmp(stu[i].name, name) == 0)
                {
                        if (i<(current-1))
                                memmove(&stu[i], &stu[i+1], (current-1-i)*sizeof(Student));
                        current--;

                        return 0;
                }
        }
        printf("该学生不存在\n");

        return 1;
}

int list()
{
        int i=0;

        printf("共有%d个学生信息\n", current);
        printf("学生信息列表\n");
        for (i=0; i<current; i++)
        {
                printf("第%d个学生信息\n", i+1);
                printf("学生信息如下\n");
                printf("姓名:%s\n", stu[i].name);
                printf("学号:%s\n", stu[i].sno);
                printf("身份证号:%s\n", stu[i].sid);
                printf("出生日期:%d-%d-%d\n", stu[i].birth.year,
                                          stu[i].birth.month, stu[i].birth.day);
                printf("地址:%s\n", stu[i].address);

                printf("\n");
        }

        return 0;
}

int save()
{

        int count;
        FILE *fs;

        fs = fopen("StudentData.dat", "wb");

        if (fs == NULL)
        {
                printf("不能打开文件\n");
                return 1;
        }

        count = fwrite(stu, sizeof(Student), current, fs);

        fclose(fs);

        if (count != current)
        {
                printf("保存失败\n");
                return 1;
        }
        printf("保存成功\n");

        return 0;

}

int load()
{

        FILE *fs;
        int count;
        int succ;

        fs = fopen("StudentData.dat", "rb");
        if (fs == NULL)
        {
                printf("不能打开文件\n");
                return 1;
        }


        succ = fseek(fs, 0, SEEK_END);//为了测量文件有多大,把文件指针移动回文件尾部,
        if (succ != 0)
        {
                printf("文件操作失败\n");
                fclose(fs);
                return 1;
        }

        count = ftell(fs);   //文件有多大?count字节
        total = current = count/sizeof(Student);  //求文件中保存多少个学生 ?
        if (stu == NULL)
        stu = (Student *)malloc(count);  //为stu  分配内存
    if (stu == NULL)
        return 1;


        fseek(fs, 0, SEEK_SET);  //把文件指针移动回文件头部


        count = fread(stu, sizeof(Student), current, fs);
        fclose(fs);
        if (count != current)
        {
                printf("读文件失败\n");
                return 1;
        }
        printf("读文件成功\n");

        return 0;
}


int main()
{
        char choice;

        //显示程序基本信息
        printf("        学生信息管理系统,版本1.0,版权所有2010-2020\n");

        while(1)
        {
                printf("\n");
                printf("                   程序功能\n");
                printf("                1:录入学生信息\n");
                printf("                2:查询学生信息\n");
                printf("                3:修改学生信息\n");
                printf("                4:删除学生信息\n");
                printf("                5:列出所有学生信息\n");
                printf("                6:保存学生信息\n");
                printf("                7:加载学生信息\n");
                printf("                0:退出程序\n");
                printf("\n");

                printf("        请选择:");
                choice = getch();
                printf("\n\n");

                switch(choice)
                {
                        case '1':
                                insert();
                                break;

                        case '2':
                                 query();
                                 break;

                        case '3':
                                update();
                                break;

                        case '4':
                                delete();
                                break;

                        case '5':
                                list();
                                break;

                        case '6':
                                save();
                                break;

                        case '7':
                                load();
                                break;

                        case '0':
                if (stu != NULL)
                {
                    free(stu);
                }
                                return 0;

                        default:
                                printf("非法选择,请重新选择\n");
                }
        }

        return 0;    //程序返回
}

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|有戏 粤ICP备2020111303号

GMT+8, 2025-12-6 13:48 , Processed in 0.090195 second(s), 20 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表