| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 4431 人关注过本帖
标题:求大神帮忙添加一功能···感激不尽!
只看楼主 加入收藏
阶段丶成长
Rank: 2
来 自:中国
等 级:论坛游民
帖 子:41
专家分:32
注 册:2013-11-11
结帖率:87.5%
收藏
 问题点数:0 回复次数:2 
求大神帮忙添加一功能···感激不尽!
# include <stdio.h>
# include <malloc.h>
# include <stdlib.h>
# include <conio.h>
# include <string.h>

int linklen = 0;                             /* 全局变量,用于记录链表长度 */
int n;


typedef struct Node                                  /* 学生结构体 */
{
    int Num;                                  /* 学号格式为:2013XXXX */        
    char name[10];
    char sex[10];
    int  year;
    float score[6];
    struct Node *Next;                       /* 保存下一位学生信息 */
}Student;


/* 输入学生信息 */
Student *Input()
{
    int i;   
    Student *stu;
    Student *p;
    Student *link = (Student *)malloc(sizeof(Student));
    if ( !link ){ printf("Failed to init !\n"); exit(0); }
    link->Next = NULL;
    p = link;
    printf("请输入所需存储学生信息的人数:");
    scanf("%d", &n);
    getchar();                                                                       //吸收回车符
    system("cls");
    for ( i=1; i<=n; i++ )
    {
        stu = (Student *)malloc(sizeof(Student));
        if ( !stu ){ printf("Failed to new!\n"); exit(0); }
        printf("第%d位学生信息:\n", i);
        printf("学号(学号格式为:2013XXXX):");
        scanf("%d", &stu->Num);
        printf("姓名:");
        scanf("%s", stu->name);
        printf("性别(男/女):");
        scanf("%s", stu->sex);
        printf("年龄(年龄限制为:16-24):");
        scanf("%d", &stu->year);
        printf("成绩==>\n");
        printf("学科一(成绩分数为:0-100): ");
        scanf("%f", &stu->score[0]);
        printf("学科二(成绩分数为:0-100): ");
        scanf("%f", &stu->score[1]);
        printf("学科三(成绩分数为:0-100): ");
        scanf("%f", &stu->score[2]);
        printf("学科四(成绩分数为:0-100): ");
        scanf("%f", &stu->score[3]);
        printf("学科五(成绩分数为:0-100): ");
        scanf("%f", &stu->score[4]);
        printf("学科六(成绩分数为:0-100): ");
        scanf("%f", &stu->score[5]);
        p->Next = stu;
        p = stu;
        ++linklen;
        system("cls");
    }
    return link;
}

/*向文件输入信息*/
void *save_data()
{
    FILE *fp;
    int i;
    Student *stu;
    Student *p;
    Student *link = (Student *)malloc(sizeof(Student));
    if ( !link ){ printf("Failed to init!\n"); exit(0); }
    link->Next = NULL;
    p = link;
    if ( !( fp = fopen("Student.txt","wb") ) )
    { return link; }
    fscanf(fp,"%d", &n);
    for ( i=0; i< n; i++ )
    {
        stu = (Student *)malloc(sizeof(Student));
        if ( !stu ){ printf("Failed to new!\n"); exit(0); }
        fread(stu,sizeof(Student),1,fp);
        stu->Next = NULL;
        p->Next = stu;
        p = stu;
    }
    fclose(fp);
    return link;
}


/* 保存当前学生信息 */

void Save(Student *link)
{
    FILE *fp;
    Student *stu = link->Next;
    if ( !( fp = fopen("student.txt","rb+") ) )
    { printf("文件无法打开!\n"); exit(0); }
    fprintf(fp,"%3d", n);
    while ( stu )
    {
        fwrite(stu,sizeof(Student),1,fp);
        stu = stu->Next;
    }
    fclose(fp);
}



/*读取文本信息 */
int  read_data(Student *link)
{
    FILE *fp;
    int i;
    Student *pre = NULL;
    Student *stu = NULL;
    link = (Student *)malloc(sizeof(Student));
    if ( !link ) { printf("Failed to new!\n"); exit(0); }
    pre = link;
    if ( !( fp = fopen("Student.txt","rb") ) )
    { return 0; }
    fscanf(fp, "%d", &n);
    for ( i=0; i< n; i++ )
    {
        stu = (Student *)malloc(sizeof(Student));
        if ( !stu ) { printf("Failed to new!\n"); exit(0); }
        fread(stu, sizeof(Student), 1, fp);
        pre->Next = stu;
        pre = stu;
    }
    fclose(fp);
    return 0;
}


/* 查找学生信息 */
void Rearch(Student *link)
{
    int select;
    int flag=0;
    Student *stu= link->Next;
    printf("请选择查询学生信息的方式:\n");
    printf("\t\t1.按学号查找\n");
    printf("\t\t2.按姓名查找\n");
    printf("\t\t请选择(1 or 2):");
    scanf("%d", &select);
    system("cls");
    if ( select == 1 )
    {
        int Num;
        printf("请输入学生学号:");
        scanf("%d", &Num);
        while ( stu )
        {
            if ( stu->Num == Num )
            {
                printf("查找成功!\n");
                system("cls");
                printf("学号:%d\n", stu->Num);
                printf("姓名:%s\n", stu->name);
                printf("性别:%s\n", stu->sex);
                printf("年龄:%d\n", stu->year);
                printf("成绩==>\n");
                printf("学科一: %f\n",stu->score[0]);
                printf("学科二: %f\n",stu->score[1]);
                printf("学科三: %f\n",stu->score[2]);
                printf("学科四: %f\n",stu->score[3]);
                printf("学科五: %f\n",stu->score[4]);
                printf("学科六: %f\n",stu->score[5]);
                // stu = stu->Next;
                flag+=1;
                break;
            }
            stu = stu->Next;
        }
        if ( !flag )
            printf("不存在该学生信息!\n");
    }
    else if ( select == 2 )
    {
        char name[10];
        printf("请输入需要查询的姓名:");
        scanf("%s",name);
        while( stu )
        {
            
            if ( !strcmp(stu->name,name) )
            {
                printf("查找成功!\n");
                system("cls");
                printf("学号:%d\n", stu->Num);
                printf("姓名:%s\n", stu->name);
                printf("性别:%s\n", stu->sex);
                printf("年龄:%d\n", stu->year);
                printf("成绩==>\n");
                printf("学科一: %f\n",stu->score[0]);
                printf("学科二: %f\n",stu->score[1]);
                printf("学科三: %f\n",stu->score[2]);
                printf("学科四: %f\n",stu->score[3]);
                printf("学科五: %f\n",stu->score[4]);
                printf("学科六: %f\n",stu->score[5]);
                //stu = stu->Next;
                flag+=1;
                break;
            }
            stu = stu->Next;
        }
        if ( !flag )
            printf("没有该学生信息!\n");
    }
    else
    {
        printf("输入错误,请重新输入!\n");
        getchar();
    }
}




/* 修改学生信息 */
void Change(Student *link)
{
    int select;
    int flag=0;
    Student *stu= link->Next;
    printf("请选择修改学生信息的方式:\n");
    printf("\t\t1.按学号查找\n");
    printf("\t\t2.按姓名查找\n");
    printf("\t\t请选择(1 or 2):");
    scanf("%d", &select);
    system("cls");
    if ( select == 1 )
    {
        int Num;
        printf("请输入要修改的学生学号:");
        scanf("%d", &Num);
        while ( stu )
        {
            if ( stu->Num == Num )
            {
                printf("查找成功!\n");
                getchar();
                system("cls");
                printf( "请输入新同学的信息!\n" );
                printf("学号(学号格式为:2013XXXX):");
                scanf("%d", &stu->Num);
                printf("姓名:");
                scanf("%s", stu->name);
                printf("性别:");
                scanf("%s", stu->sex);
                printf("年龄(年龄限制为:16-24):");
                scanf("%d", &stu->year);
                printf("成绩==>\n");
                printf("学科一(成绩分数为:0-100): ");
                scanf("%f", &stu->score[0]);
                printf("学科二(成绩分数为:0-100): ");
                scanf("%f", &stu->score[1]);
                printf("学科三(成绩分数为:0-100): ");
                scanf("%f", &stu->score[2]);
                printf("学科四(成绩分数为:0-100): ");
                scanf("%f", &stu->score[3]);
                printf("学科五(成绩分数为:0-100): ");
                scanf("%f", &stu->score[4]);
                printf("学科六(成绩分数为:0-100): ");
                scanf("%f", &stu->score[5]);
                system("cls");
                stu = stu->Next;
                flag += 1;
                break;
            }
        }
        if ( !flag )
            printf("没有该学生信息!\n");
    }
    else if ( select == 2 )
    {
        char name[10];
        printf("请输入需要修改的姓名:");
        scanf("%s",name);
        while( stu )
        {
            if ( !strcmp(stu->name,name) )
            {
                printf("查找成功!\n");
                getchar();
                system("cls");
                printf("请填入新学生的信息:\n");
                printf("学号:");
                scanf("%d", &stu->Num);
                printf("姓名:");
                scanf("%s", stu->name);
                printf("性别:");
                scanf("%s", stu->sex);
                printf("年龄:");
                scanf("%d", &stu->year);
                printf("成绩==>\n");
                printf("学科一: ");
                scanf("%f", &stu->score[0]);
                printf("学科二: ");
                scanf("%f", &stu->score[1]);
                printf("学科三: ");
                scanf("%f", &stu->score[2]);
                printf("学科四: ");
                scanf("%f", &stu->score[3]);
                printf("学科五: ");
                scanf("%f", &stu->score[4]);
                printf("学科六: ");
                scanf("%f", &stu->score[5]);
                system("cls");
                stu = stu->Next;
                flag+=1;
                break;
            }
            if ( !flag )
                printf("没有该学生信息!\n");
        }
    }
    else
    {
        printf("输入错误,请重新输入!\n");
        getchar();
    }
   
}

/* 插入新的学生学生信息 */
void Insert(Student *link)
{
    int pos, flag;
    Student *p = link;                                              /* 首位插入 */
    Student *stu = link->Next;
    Student *newstu = (Student *)malloc(sizeof(Student));
    if ( !newstu ){ printf("Failed to new!\n"); exit(0); }
    newstu->Next = NULL;
    printf("请填入新学生的信息:\n");
    printf("学号(学号格式为:2013XXXX):");
    scanf("%d", &stu->Num);
    printf("姓名:");
    scanf("%s", stu->name);
    printf("性别(男/女):");
    scanf("%s", stu->sex);
    printf("年龄(年龄限制为:16-24):");
    scanf("%d", &stu->year);
    printf("成绩==>\n");
    printf("学科一(成绩分数为:0-100): ");
    scanf("%f", &stu->score[0]);
    printf("学科二(成绩分数为:0-100): ");
    scanf("%f", &stu->score[1]);
    printf("学科三(成绩分数为:0-100): ");
    scanf("%f", &stu->score[2]);
    printf("学科四(成绩分数为:0-100): ");
    scanf("%f", &stu->score[3]);
    printf("学科五(成绩分数为:0-100): ");
    scanf("%f", &stu->score[4]);
    printf("学科六(成绩分数为:0-100): ");
    scanf("%f", &stu->score[5]);
    system("cls");
    printf("已存在%d学生的信息!\n\n\n",linklen);
    printf("请选择插入位置: ");
    scanf("%d", &pos);
    if ( pos>linklen+1 && pos<1 )
    {
        printf("越界!\n");
        exit(0);
    }
    else if ( pos == 1 )
    {
        newstu->Next = p->Next;
        p->Next = newstu;
        printf("插入完成!\n");
    }
    else
    {
        flag = 1;                                                 /* 起始位置 */
        while ( stu && flag<pos-1 )
        {
            stu = stu->Next;
            ++flag;
        }
        if ( !stu && flag>pos-1 )
        {
            printf("Data exception!\n");
            exit(0);
        }
        newstu->Next = stu->Next;
        stu->Next = newstu;
        ++linklen;                                               /* 链表长度增加1 */
        printf("插入完成!\n");
    }
}


/* 删除学生信息 */
void Delete(Student *link)
{
    int tag = 0;
    int flag;
    int select;
    int pos;
    char ch;                                                         /* 用于选择 */
    Student *stu = link->Next;
    printf("请选择删除学生信息的方式:\n");
    printf("\t\t1.按姓名查找\n");
    printf("\t\t2.按学号查找\n");
    printf("\t\t请选择(1 or 2):");
    scanf("%d", &select);
    system("cls");
    if ( select == 1 )
    {
        int Num;
        printf("请输入学生学号:");
        scanf("%d", &Num);
        pos = 1;                                               /* 用于记录所要删除学生的位置 */
        while ( stu )
        {
            if ( stu->Num == Num )
            {
                printf("查找成功!\n");
                system("cls");
                printf("学号:%d\n", stu->Num);
                printf("姓名:%s\n", stu->name);
                printf("性别:%s\n", stu->sex);
                printf("年龄:%d\n", stu->year);
                printf("学号:%d\n", stu->Num);
                printf("成绩==>\n");
                printf("学科一: %f\n",stu->score[0]);
                printf("学科一: %f\n",stu->score[1]);
                printf("学科一: %f\n",stu->score[2]);
                printf("学科一: %f\n",stu->score[3]);
                printf("学科一: %f\n",stu->score[4]);
                printf("学科一: %f\n",stu->score[5]);
                system("cls");
                printf("您是否要删除该学生信息?(Y or N)\n");
                printf("您的选择是:");
                getchar();                                      /* 吸收回车符 */
                scanf("%c", &ch);
                tag = 1;
                break;
            }
            else
            {
                stu = stu->Next;
                ++pos;
            }
        }
        if ( !tag )
        {
            printf("没有该学生信息!\n");
            exit (0);
        }
        if ( 'Y' == ch )
        {
            if ( pos == 1 )
            {
                Student *Q = link->Next;
                link->Next = Q->Next;
                free(Q);
                Q = NULL;
            }
            else
            {
                Student *q = NULL;
                Student *p = link->Next;
                flag = 1;                                     /* 查找结点位置 */
                while ( p && flag<pos-1 )
                {
                    p = p->Next;
                    ++flag;
                }
                if ( !p && flag>pos-1 )
                {
                    printf("Data exception!\n");
                    exit(0);
                }
                q = p->Next;
                p->Next = q->Next;
                free(q);
                q = NULL;
                printf("删除成功!\n");
            }
        }
    }
    else if ( select == 2 )
    {
        char name[10];
        printf("请输入学生姓名:");
        scanf(" %s ",name);
        pos = 1;                                            /* 用于记录所要删除学生的位置 */
        while ( stu )
        {
            if ( !strcmp(stu->name,name) )
            {
                printf("查找成功!\n");
                system("cls");
                printf("学号:%d\n", stu->Num);
                printf("姓名:%s\n", stu->name);
                printf("性别:%s\n", stu->sex);
                printf("年龄:%d\n", stu->year);
                printf("成绩==>\n");
                printf("学科一: %f\n",stu->score[0]);
                printf("学科一: %f\n",stu->score[1]);
                printf("学科一: %f\n",stu->score[2]);
                printf("学科一: %f\n",stu->score[3]);
                printf("学科一: %f\n",stu->score[4]);
                printf("学科一: %f\n",stu->score[5]);
                system("cls");
                printf("您是否要删除该学生信息?(Y or N)\n");
                printf("您的选择是:");
                getchar();                                  /* 吸收回车符 */
                scanf("%c", &ch);
                tag = 1;
                break;
            }
            else
            {
                stu = stu->Next;
                ++pos;                                       /* 查找需要删除学生位置 */
            }
        }
        if ( !tag )
        {
            printf("没有该学生信息!\n");
            exit (0);
        }
        if ( 'Y' == ch )
        {
            if ( pos == 1 )
            {
                Student *Q = link->Next;
                link->Next = Q->Next;
                free(Q);
                Q = NULL;
            }
            else
            {
                Student *q = NULL;
                Student *p = link->Next;
                flag = 1;                                   /* 查找结点位置 */
                while ( p && flag<pos-1 )
                {
                    p = p->Next;
                    ++flag;
                }
                if ( !p && flag>pos-1 )
                {
                    printf("Data exception!\n");
                    exit(0);
                }
                q = p->Next;
                p->Next = q->Next;
                free(q);
                q = NULL;
                printf("删除成功!\n");
            }
        }
    }
    else
    {
        printf("输入错误,请重新输入!\n");
        getchar();
    }
   
}



void Show(Student *link)
{
    Student *stu=link->Next;
    while(stu!=NULL)
    {
        printf("学号:%d\n", stu->Num);
        printf("姓名:%s\n", stu->name);
        printf("性别:%s\n", stu->sex);
        printf("年龄:%d\n", stu->year);
        printf("成绩==>\n");
        printf("学科一: %f\n",stu->score[0]);
        printf("学科一: %f\n",stu->score[1]);
        printf("学科一: %f\n",stu->score[2]);
        printf("学科一: %f\n",stu->score[3]);
        printf("学科一: %f\n",stu->score[4]);
        printf("学科一: %f\n",stu->score[5]);
        stu=stu->Next;
        printf("\n");
    }
}



/* 主函数运行学生管理系统 */
int main()
{
    Student *link = NULL;
    int select;   
    int flag=0;
mark:
    system("cls");
    printf("\t\t\t----------------------------------\n");
    printf("\t\t\t*   欢迎进入学生信息管理系统     *\n");
    printf("\t\t\t----------------------------------\n");
    printf("\n\n");
    printf("\t\t****************************************************\n");
    printf("\t\t*                 1.输入(Input)                    *\n");
    printf("\t\t*                 2.查询(Search)                   *\n");
    printf("\t\t*                 3.修改(Change)                   *\n");
    printf("\t\t*                 4.插入(Insert)                   *\n");
    printf("\t\t*                 5.删除(Delete)                   *\n");
    printf("\t\t*                 6.显示(Show)                     *\n");
    printf("\t\t*                 7.退出(Exit)                     *\n");
    printf("\t\t****************************************************\n");
    printf("\n\t\t请输入一个数(0--7):");
    scanf("%d", &select);
    system("cls");
    switch ( select )
    {
    case 1:
        link = Input();
        //save_data(link);
        system("pause");
        goto mark;
        break;
    case 2:
        //read_data(link);
        Rearch(link);
        system("pause");
        goto mark;
        break;
    case 3:
        //read_data(link);
        Change(link);
        system("pause");
        goto mark;
        break;
    case 4:
        //read_data(link);
        Insert(link);
        system("pause");
        goto mark;
        break;
    case 5:
        //read_data(link);
        Delete(link);
        system("pause");
        goto mark;
        break;
    case 6:
        //read_data(link);
        Show(link);
        system("pause");
        goto mark;
        break;
    case 7:
        //Save(link);
        exit(0);
    default:
        printf("输入错误,请重新输入!\n");
        system("pause");
        goto mark;
        break;
    }
   
    return 0;
}

二次执行可以从文本文件中读取第一次执行的信息···
搜索更多相关主题的帖子: 结构体 include 记录 信息 include 结构体 记录 信息 
2014-06-05 15:50
wp231957
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:神界
等 级:贵宾
威 望:423
帖 子:13688
专家分:53332
注 册:2012-10-18
收藏
得分:0 
不短的代码 啊

DO IT YOURSELF !
2014-06-05 15:51
Artless
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
等 级:贵宾
威 望:103
帖 子:4211
专家分:28888
注 册:2009-4-8
收藏
得分:0 
不短

无知
2014-06-11 12:10
快速回复:求大神帮忙添加一功能···感激不尽!
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.024952 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved