当前位置: 代码迷 >> C语言 >> 帮我看看哪里错了啊...输入学号后就显示错误...
  详细解决方案

帮我看看哪里错了啊...输入学号后就显示错误...

热度:537   发布时间:2008-04-28 22:34:29.0
帮我看看哪里错了啊...输入学号后就显示错误...
程序代码:

#include "conio.h"
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#define NUMLEN 5        //定义学号最大位数
#define NAMELEN 10     //定义名字长度
#define NUMBER  10    //定义学生个数
#define SCOREMAX 5    //定义成绩的最大位数
#define ESC 0x1b      //ESC的十六进制键码
struct stu
{
    int num;                 //学号
    char name[NAMELEN];            //名字
    float score[3];            //三门课程的成绩                    
    float average;                //记录平均分
    struct stu *next;
};
//查看所有学生的记录
Display_LinkList(struct stu *);
//检查学输入的学号是否合法
int Find_Num(char *);
//检查学号是否重复
int Check_Num(int ,struct stu *);
//检查姓名是否合法
int Find_Name(char *);
//检查姓名是否重复
int Check_Name(char *,struct stu *);
//检查成绩输入是否合法
int Check_Score(char *);
//求出平均成绩
int _average(struct * t);
//输出最高分
int high_score(struct *o);
//建立动态链表

//插入链表
struct stu * Insert_LinkList(struct stu * h)
{
    struct stu *p,*p1,*l;    
    char n[NUMLEN],_score[SCOREMAX],key;
    int flag,flag1,i,j;
    p=h;
    if(p == NULL)
    {
        printf("\n\t\t\t没有学生记录,是否创建y/n");        
        if(key=getch(),key != 'y')        
            return h;        
    }    
    do{
             system("cls");                            
            l=(struct stu *)malloc(sizeof(struct stu)); //调试时显示l的值居然是空的..这个不明白
            //    {
                //    printf("内存不足");
            //        return h;
            //    }
                    //检查学号输入的是不是数字,学号只能是数字,合法做if括号里面的语句
                    printf("请输入学号:");
                    do
                        {    
                            for(i=0;n[i] != '\0';i++)
                                n[i]='\0';
                            scanf("%s",n);                            
                            if( flag=Find_Num(n) )
                                {                                    
                                    l->num=0;            
                                    for(i=0;n[i] != '\0';i++)                
                                        l->num=l->num*10+(n[i]-'0');                        
                                        flag1=Check_Num(l->num ,h);                                                                                
                                }
                              
                        }while( !(flag && flag1) );
                    system("cls");                        
                //输入姓名
                    printf("请输入%d号学生的姓名",l->num);    
            do
            {                
                scanf("%s",l->name);
                flag=Find_Name(l->name);  //检查姓名是包含数字..和其他字符..名字只能是字母,有不合法的字符重新输入
                flag1=Check_Name(l->name,h);
            }while( !(flag && flag1) );
            system("cls");            
            //输入成绩
                printf("请输入%d号学生的三门成绩",l->num);
                for(j=0;j<3;j++)
                    {
                        do
                        {
                            for(i=0;n[i] != '\0';i++)
                            _score[i]='\0';
                            scanf("%s",_score);
                            if( flag=Check_Score(_score) )
                            {
                                l->score[j]=0;
                                for(i=0;_score[i] != '\0';i++)
                                    l->score[j]=l->score[j]*10+(_score[i]-'0');                                
                                printf("在在在%f\n",l->score[j]);
                                if(l->score[j] > 100 || l->score[j] < 0)
                                {
                                    printf("错误!!成绩只能是0-100之间,请重新输入");
                                        flag=0;
                                }
                            }
                        }while( !flag  );
                    }                    
            l->next=NULL;
            if(p == NULL)
                return l;
            //插入链表 插入学生记录
            while( (l->num > p->num) && (p->next != NULL) )
            {
                p1=p;
                p=p->next;
            }
                if(l->num <= p->num)
                {
                    if(p == h)
                       h=l;
                    else p1->next=l;
                         l->next=p;
                }
                else {
                        p->next=l;
                        l->next=NULL;
                    }
        Display_LinkList(h);
        printf("\t\t\t是否继续添加y/n\n");
        printf("\t\t\t请选择:");
        key=getch();
    }while(key != 'y');

    return h;
}

//删除链表
struct stu * Delete_LinkList(struct stu * h)
{
    struct stu *pl,*pb;
    pl=h;
    char key,n[NUMLEN];
    int del,flag,i;    
    if(h == NULL)            
    {
        printf("\n\t\t\t没有学生记录,删除失败\n");        
        if(key=getch(),key != 'y')
            return h;
                  
    }
    do
    {
        scanf("%s",n);
        if(flag=Find_Num(n))    //检查学号输入的是不是数字,学号只能是数字,合法做if括号里面的语句
        {                        //把字符串转换成学号
            del=0;            
            for(i=0;n[i] != 0;i++)                
                del*=10+n[i];
        }
    }while( !flag );
    if(pl->num == del)    
        return pl->next;
    else
    {
        while( (del != pl->num) && (pl->next != NULL) )
        {
            pb=pl;
            pl=pl->next;
        }
        if( (del == pl->num) && (pl !=NULL) )        
            pb->next=pl->next;
        else {
              printf("\n\t\t\t\t无法找到该学号,删除失败");
              getch();
            }
    }
    return h;
}
//输出链表
int Display_LinkList(struct stu * a)
{
    int i;
    struct stu *p;
    p=a;
    if(p==NULL)
    {
        printf("\n\t\t\t\t没有学生记录");
        getch();
        return 0;
    }
    printf("学号\t姓名\t\t成绩1\t成绩2\t成绩3\n");
    while(p != NULL)
    {        
        printf("%d\t%s\t",p->num,p->name);
        i=0;
        while(i<3)    
        {
            printf("%.2f\t",p->score[i]);
            i++;
        }
        p=p->next;
        printf("\n");
    }
    return 0;
}
//打印每个的平均成绩
int _average(struct stu *p)
{
    struct stu *pw;
    int i;    
    float t;
    if( p == NULL)
    {
        printf("\n\t\t\t\t没有学生记录");
        getch();
        return 0;
    }
    pw=p;
    while(pw != NULL)
    {
        i=0;
        t=0;
        for(i=0;i<3;i++)        
            t+=pw->score[i];
        pw->average=t/3;
        pw=pw->next;
    }
    pw=p;
    printf("学号    姓名    平均成绩\n");
    while(pw != NULL)
    {
        printf("%.2f",pw->average);
        pw=pw->next;
    }
    return 0;
}


//输出最高分的所有成绩
int High_score(struct stu * k)
{
    struct stu *b,*max;
    int i;
    max=b=k;
    if(k == NULL)
    {
        printf("\n\t\t\t\t没有学生记录");
        getch();
        return 0;
    }    
    else
    {

        while(b != NULL)
        {
                
            if(max->average < b->average)        
                max=b;
            b=b->next;
        }
        printf("平均分最高的是:\n");
        printf("%d\t%s\t",max->num,max->name);
        for(i=0;i<3;i++)
            printf("%.2f\t",max->score[i]);
        printf("\n");
    }
    return 0;
}

//检查学输入的学号是否合法
int Find_Num(char *c)
{
    while(*c != 0)
    {
        if( !((*c>='0') && (*c <= '9')) )
        {
            printf("你输入的学号不合法,学号要求是纯数字组成,请重新输入:");
            return 0;
        }
        c++;
    }
    return 1;
}

//检查学号是否重复
int Check_Num(int s,struct stu * sy)
{
    struct stu *d;
    d=sy;
    while( d != NULL )
    {
        if( s == d->num )
        {
            printf("学号有重复,请重新输入学号:");
                return 0;
        }
        d=d->next;
    }
    return 1;
}

//检查姓名是否合法
int Find_Name(char * ch)
{
    while(*ch != 0)
    {
        if(  !(((*ch >= 'a') && (*ch <= 'z')) || ((*ch >= 'a') && (*ch <= 'z')))  )
        {
            printf("输入的名字不合法,只能由字母组成!!请重新输入一个名字:");
            return 0;
        }
        ch++;
    }
    return 1;
}

//检查姓名是否重复
int Check_Name(char *c,struct stu *d)
{
    while( (strcmp(c,d->name)) && (d->next != NULL) )
    {
        if( strcmp(c,d->name) == 0 )
        {
            printf("姓名有重复!!请重新输入:");
            return 0;
        }
        d=d->next;
    }
    return 1;
}
//检查成绩输入是否合法
int Check_Score(char *ck)
{
    if(*ck == '0')
    {
        printf("第一个数字不能为0");
        return 0;
    }
    while(*ck != 0)
    {
        if( !( ((*ck>='0') && (*ck <= '9')) || (*ck == '.') ) )
        {
            printf("成绩输入有错!!请重新输入:");
            return 0;
        }
        ck++;
    }
    return 1;
}
//主目录
int display()
{
    int key;
    struct stu *head=NULL;
    do
    {
        system("cls");
        printf("\t\t\t\t学生管理系统\n\n");
        printf("-------------------------------------------------------------------------------\n");
        printf("|                                          |\n");
        printf("|                                          |\n");
        printf("|\t\t\t1.添加学生记录\t\t\t\t\t      |\n");
        printf("|                                          |\n");
        printf("|                                              |\n");
        printf("|\t\t\t2.删除学生记录\t\t\t\t\t      |\n");
        printf("|                                          |\n");
        printf("|                                          |\n");
        printf("|\t\t\t3.查看所有记录\t\t\t\t\t      |\n");
        printf("|                                          |\n");
        printf("|                                          |\n");
        printf("|\t\t\t4.查看所有学生的平均分数\t\t\t      |\n");
        printf("|                                          |\n");
        printf("|                                          |\n");
        printf("|\t\t\t5.查看最高分的学生信息\t\t\t\t      |\n");
        printf("|                                          |\n");
        printf("|                                          |\n");
        printf("|\t\t\t按ESC键退出本软件\t\t\t\t      |\n");
        printf("-------------------------------------------------------------------------------\n");
        printf("请选择:");
        key=getch();
        switch(key)
        {
        case '1':
                head=Insert_LinkList(head);
                break;
        case '2':               
                head=Delete_LinkList(head);
                break;
        case '3':
                Display_LinkList(head);
                break;
        case '4':
                _average(head);
                break;

        case '5':
                High_score(head);
                break;
        }
    }while(key != ESC);
    return 0;
}

int main()
{
    display();
    system("cls");
    printf("\n\n\n\n\n\n\n\n\n\t\t\t\t谢谢使用");
    _sleep(1000);
    return 0;
}
搜索更多相关的解决方案: 学号  define  include  定义  

----------------解决方案--------------------------------------------------------
o(∩_∩)o...
我也刚学到这。看的很迷糊...
----------------解决方案--------------------------------------------------------
搞定了..做了二天了..
----------------解决方案--------------------------------------------------------
  相关解决方案