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


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


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


/* 输入学生信息 */
Student *Input()
{
    int n;
    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("学号:");
        scanf("%d", &stu->Num);
        printf("姓名:");
        scanf("%s", stu->name);
        printf("性别:");
        scanf("%s", stu->sex);
        printf("年龄:");
        scanf("%d", &stu->year);
        printf("成绩:");
        scanf("%f", &stu->score);
        stu->Next = NULL;
        p->Next = stu;
        p = stu;
        ++linklen;
        system("cls");                                                         /* 跳到下一位学生信息 */
    }
   
    return link;
}

/* 查找学生信息 */
void Seek(Student *link)
{
    int flag=0;
    char name[10];
    Student *stu= link->Next;
    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("成绩:%f\n", stu->score);
            flag = 1;
            break;
            }
            else
                stu = stu->Next;
        }
        if ( !flag )
           printf("不存在该学生信息!\n");
    }
   

/* 修改学生信息 */
void Change(Student *link)
{

    int flag = 0;   
    char name[10];
    Student *stu= link->Next;
    printf("请输入学生姓名:");
    scanf("%s", name);
    while ( stu )
    {
        if ( !strcmp(stu->name,name) )
        {
            printf("查找成功!\n");
            system("cls");
            printf("新的学生信息:");
            printf("学号:");
            scanf("%d", &stu->Num);
            printf("姓名:");
            scanf("%s", stu->name);
            printf("性别:");
            scanf("%s", stu->sex);
            printf("年龄:");
            scanf("%d", &stu->year);
            printf("成绩:");
            scanf("%f", &stu->score);
            printf("修改完成!\n");
            flag = 1;
            break;
        }
            else
                stu = stu->Next;
    }
    if ( !flag )
    printf("不存在该学生信息!\n");

}

/* 添加新的学生学生信息 */
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("学号:");
    scanf("%d", &newstu->Num);
    printf("姓名:");
    scanf("%s", newstu->name);
    printf("性别:");
    scanf("%s", newstu->sex);
    printf("年龄:");
    scanf("%d", &newstu->year);
    printf("成绩:");
    scanf("%f", &newstu->score);
    system("cls");
    printf("请选择插入位置pos:");
    scanf("%d", &pos);
    if ( pos>linklen && 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");
    }
}

/* 删除学生信息 */
int Delete(Student *link)
{
    int tag = 0;
    int flag;
    int pos;
    char ch = '\0';                                             /* 用于选择 */
    Student *stu = link->Next;
    char name[10];
    printf("请输入学生姓名:");
    scanf("%s", name);
    pos = 1;                                              /* 用于记录所要删除学生的位置 */
    while ( stu )
    {
        if ( !strcmp(stu->name,name) )
        {
            printf("查找成功!\n");
            printf("学号:%d\n", stu->Num);
            printf("姓名:%s\n", stu->name);
            printf("性别:%s\n", stu->sex);
            printf("年龄:%d\n", stu->year);
            printf("成绩:%f\n", stu->score);
            system("cls");
            printf("您是否要删除该学生信息?(Y or N)\n");
            printf("          您的选择是:");
            getchar();                                  /* 吸收回车符 */
            scanf("%c", &ch);
            tag = 1;
            break;
        }
        else
        {
            stu = stu->Next;
            ++pos;
        }
        if(!tag )
        {
            printf("没有该学生信息!\n");
            return 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");
            }
        }
    }   
    return 1;
}


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("成绩:%f\n", stu->score);
        stu->Next=stu;
        printf("\n");
    }
}

/* 主函数运行学生管理系统 */
int main(void)
{
    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("\t\t请输入您的选项:");
    scanf("%d", &select);
    system("cls");
    switch ( select )
    {
    case 1:
        link = Input();
        system("pause");
        goto mark;
        break;
    case 2:
        Seek(link);
        system("pause");
        goto mark;
        break;
    case 3:        
        Change(link);
        system("pause");
        goto mark;
        break;
    case 4:
        Insert(link);
        system("pause");
        goto mark;
        break;
    case 5:
        Delete(link);
        system("pause");
        goto mark;
        break;
    case 6:
        Show(link);
        system("pause");
        goto mark;
        break;
    case 7:
        exit(0);
    default:
        printf("没有这个选项!\n");
        system("pause");
        goto mark;
        break;
    }
   
    return 0;
}
搜索更多相关主题的帖子: include 结构体 信息 记录 
2014-05-28 15:01
砖家的谎言
Rank: 12Rank: 12Rank: 12
等 级:禁止访问
威 望:30
帖 子:693
专家分:3898
注 册:2013-12-6
收藏
得分:4 
检查你用于存储数据的函数和数据结构

我不是砖家,要努力成为砖家。
2014-05-28 15:10
忘了_这一切
Rank: 3Rank: 3
来 自:星星
等 级:论坛游侠
威 望:1
帖 子:41
专家分:145
注 册:2014-4-25
收藏
得分:4 
程序代码:
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("成绩:%f\n", stu->score);
        stu=stu->Next;                          //这里你弄反了!导致输出一直无法停止
        printf("\n");
    }
}
2014-05-28 17:49
快速回复:求大神帮忙,为什么不能显示输入的信息?
数据加载中...
 
   



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

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