当前位置: 代码迷 >> C语言 >> 谁能帮我看一下..输入成绩时老是错误
  详细解决方案

谁能帮我看一下..输入成绩时老是错误

热度:407   发布时间:2008-04-23 21:39:06.0
谁能帮我看一下..输入成绩时老是错误
程序代码:

#include "stdio.h"
#include "conio.h"
#include "stdlib.h"
#define NUM 5        //控制学号的位数
#define SCO 8    //控制成绩的位数

struct student
{
    int num;
    char name[20];
    float score;
    struct student * next;
};
int Find_num(char *);
int letter(char *);
int Find_number(char *);
//建立链表
struct student * Creat_LinkList()
{
    struct student *t,*d,*h;
    int i=0,j,flag;
    char c[NUM],n[SCO],temp,*c1,*n1;
    float temp1;
    if((t=d=(struct student *)malloc(sizeof(struct student))) == NULL)
    {
        printf("内存不足");
        return NULL;
    }
    h=NULL;
    printf("请输入学号,姓名和成绩\n");
    printf("------------------------------------------------------\n");
    //输入学号
    do
        {
                for(j=0;j<NUM;j++)
                        n[j]='\0';
                printf("请输入学号:");
                scanf("%s",n);            
                if(!(flag=Find_num(n)))
                {
                    n1=n;
                    t->num=0;
                    while(*n1 != '\0')
                    {
                        temp=*n1-'0';
                        t->num=t->num*10+temp;
                        n1++;
                    }
                }
        }while(flag);  //如果输入有重复的学号重复输入

        //输入的名字    
    do
        {
                printf("%d号同学的名字:",t->num);
                scanf("%s",t->name);
        }while(letter(t->name));   //判断输入的名字是否字母        
        //输入成绩            
    do
        {
                for(j=0;j<SCO;j++)
                        c[j]='\0';
                printf("%d号同学的成绩:",t->num);
                scanf("%s",c);
                if( !(flag=Find_number(c)) )
                {                    
                    t->score=0;
                    c1=c;
                    while(c1 != '\0')
                    {
                        temp1=float(*c1-'0');
                        t->score=t->score*10+temp1;
                        c1++;
                    }
                }
        }while(flag);
                t->next=NULL;          //给第一个开辟的结点next赋上空值

    while(t->num != 0)
    {
        i++;
        if(i == 1)
            h = t;    //开辟的第一个结点的地址给h
        else d->next=t;   //以前d->next的值是null,现在指向新开辟的空间
        d= t;
        if((t = (struct student *)malloc(sizeof(struct student))) == NULL)
        {
            printf("内存不足");
            return h;
        }
            t->next=NULL;
            //输入学号
    do
        {
                for(j=0;j<NUM;j++)
                        n[j]='\0';
                printf("请输入学号:");
                scanf("%s",n);            
                if(!(flag=Find_num(n)))
                {
                    n1=n;
                    t->num=0;
                    while(*n1 != '\0')
                    {
                        temp=*n1-'0';
                        t->num=t->num*10+temp;
                        n1++;
                    }
                }
        }while(flag);  //如果输入有重复的学号重复输入

        //判断输入的名字是否字母    
    do
        {
                printf("%d号同学的名字:",t->num);
                scanf("%s",t->name);
        }while(letter(t->name));   //判断输入的名字是否字母        
    //输入成绩            
    do
        {
                for(j=0;j<SCO;j++)
                        c[j]='\0';
                printf("%d号同学的成绩:",t->num);
                scanf("%s",c);
                if( !(flag=Find_number(c)) )
                {                    
                    t->score=0;
                    c1=c;
                    while(c1 != '\0')
                    {
                        temp1=float(*c1-'0');        
                        t->score=t->score*10+temp1;
                        c1++;
                    }
                }
        }while(flag);
    }
    d->next = NULL;
    return h;
}

//删除链表
struct student * Del_LinkList(struct student * s , int n)
{
    struct student *j,*k;
    j=s;
    if(j == NULL)
    {
        printf("空表");
        return s;
    }
    while((n != j->num) && (j->next != NULL))
    {
        k=j;
        j= j->next;
    }
    if(n == j->num)
    {
        if(j == s)        
            return s->next;
        else k->next = j->next;
    }
    else printf("无法找到该学号,删除失败");
    return s;
}

//插入链表

struct student * Insert_LinkTable(struct student * u, struct student * i)
{
    struct student *l,*m;
    l=u;
    if(u == NULL)
    {
        i->next =NULL;
        return i;
    }
    else
    {
        while((i->num > l->num) && (l->num != NULL))
        {
            m=l;
            l=l->next;
        }
        if(i->num <= l->num)
            {
                if(u == l)
                    u=i;
                else m->next =i;
                i->next=l;
            }
        else
            {
                l->next=i;
                i->next=NULL;
            }    
    }
    return u;
}

void display(struct student * p)
{
    struct student *p1;
    p1=p;
    printf("学号    姓名    成绩\n");
    while(p1 != NULL)
    {
        printf("%d\t%s\t%.2f",p1->num, p1->name, p1->score);
        p1=p1->next;
    }
}

//判断输入的学号是否合法
int Find_num(char *p)
{
    char *t;
    t=p;
    if(*p == '0')
    {
        printf("学号不能以0开始\n");
        return 1;
    }
    while(*t)
    {
        if(!(*t>='0'&&*t<='9'))
        {
            printf("你输入的学号不合法请重新输入\n");
            return 1;
        }
        t++;
    }    
    return 0;
}

//判断输入的名字是否字母
int letter(char *ch)
{
    int i=0;        
    while(*ch != '\0')
    {
        if(!(*ch>='a'&&*ch<='z') || (*ch>='A'&&*ch<='Z'))
        {
            printf("输入的名字有非法字符请重新输入\n");
            return 1;
        }
        ch++;
    }
    return 0;
}
//判断输入的成绩是否合法
Find_number(char *c)
{
    char *b=c;
    if(*b == 0)
        {
            printf("不一个数字不能为0");
            return 1;
        }
    while(*b !='\0')
    {
        if( !((*b>='0') && (*b<='9')) )
        {
            printf("输入的成绩不合法,请重新输入\n");
            return 1;
        }
        b++;
    }
    return 0;
}
            


int main()
{  
    struct student *head,*f;
    int num;
    if((head = Creat_LinkList()) == NULL)
    {
        printf("内存不足");
        exit(1);
    }
    display(head);
    while(scanf("%d",
          &num),num!=0)
    {
        head=Del_LinkList(head,num);
        printf("删除后\n");
        display(head);
    }
    while(printf("输入你要删除的学号"),
          scanf("%d",&num),        
          num != 0)
    {
        head=Insert_LinkTable(head, f=Creat_LinkList());
        printf("删除后\n");
        display(head);
    }
    getch();
    return 0;
}


[[it] 本帖最后由 bianfeng 于 2008-4-23 21:49 编辑 [/it]]
搜索更多相关的解决方案: student  SCO  int  struct  char  

----------------解决方案--------------------------------------------------------
顶啊
----------------解决方案--------------------------------------------------------
  相关解决方案