2023-04-06 21:51 采纳率: 100%
浏览 59
已结题

学生信息管理系统(指针 链表)

为什么程序会显示 head和p未定义
代码:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAX_STUDENTS 100
int s1;
typedef struct stu
{
    char stuhao[20];
    char stuname[20];
    char sturiqi[4];
    int score[6];
    float pin;
    int stuzong;
    double sum;
    double ave;
    struct stu *next;
}stu;
stu student[100];

void daoru(stu *head,stu *p); 
void shuchu(stu *head,stu *p);
void shuru(stu *head,stu *p);
void baocun(stu *head,stu *p);
void shanchu(stu *head,stu *p);
void paixu(stu *head,stu *p);
void tongji(stu *head,stu *p);
void menu2(stu *head,stu *p);
void xh(stu *head,stu *p);
void name(stu *head,stu *p);
void xuan1(stu *head,stu *p);
void xuan2(stu *head,stu *p);
void xuan3(stu *head,stu *p);
void xuan4(stu *head,stu *p);
void xuan5(stu *head,stu *p);
void xuan6(stu *head,stu *p);
void xueke(stu *head,stu *p);
void chaxun(stu *head,stu *p);
void denglu();
void xuanze(stu *head,stu *p);
void meun();

int main()
{
        int i;
    stu *temp=(stu *)malloc(sizeof(stu));
    strcpy(temp->stuhao,"\0");
    strcpy(temp->stuname,"\0");
    strcpy(temp->sturiqi,"\0");
    temp->score[1]=0;
    temp->score[2]=0;
    temp->score[3]=0;
    temp->score[4]=0;
    temp->score[5]=0;
    temp->score[6]=0;
    temp->sum=0;
    temp->ave=0;
    temp->next=NULL;
    stu *head=temp;

        stu *a=(stu *)malloc(sizeof(stu));
        strcpy(a->stuhao,"\0");
        strcpy(a->stuname,"\0");
        strcpy(a->sturiqi,"\0");
        a->score[1]=0;
        a->score[2]=0;
        a->score[3]=0;
        a->score[4]=0;
        a->score[5]=0;
        a->score[6]=0;
        a->sum=0;
        a->ave=0;
        a->next=NULL;
        temp->next=a;
        temp=temp->next;
     
    stu *p=head;
    
    int x=0,flag=1;
    denglu();
    if(x) flag=0;//判断是否进入界面 
    if(flag)
    {
        meun();
    } 
            
}


void daoru()
{
    FILE *fp;
    int i=1;
    fp=fopen("数据.txt","r");
    if(!fp)
    {
        printf("打开文件失败\n");
        return;
    }
    i=0;
    while(!feof(fp))
    {
        fscanf(fp,"%s %s %d %d &d &d &d &d &d &d\n",student[i].stuhao,student[i].stuname,
                           student[i].sturiqi[1],student[i].sturiqi[2],student[i].sturiqi[3],
                           student[i].score[1],student[i].score[2],student[i].score[3],student[i].score[4],student[i].score[5],student[i].score[6]);
                                 i++;
    }
    fclose(fp);
    s1=i-1;
}
void shuchu()
{
    int i;
    printf("学号\t    姓名       出生年月    成绩1 成绩2 成绩3 成绩4 成绩5 成绩6\n");
    for(i=1;i<=s1;i++)
    {
        printf("%s %s %d年%d月%d日    %d    %d    %d    %d    %d    %d\n",student[i].stuhao,student[i].stuname,
                           student[i].sturiqi[1],student[i].sturiqi[2],student[i].sturiqi[3],
                           student[i].score[1],student[i].score[2],student[i].score[3],student[i].score[4],student[i].score[5],student[i].score[6]);
    }
}
void shuru()
{
    int i,j,flag=1,n,t=0,x=1;
    stu *d=p,*h=head; 
    printf("请输入本次输入的学生数量\n");
    scanf("%d",&n);
     if(p==head) {p=p->next;n=n-2;t=1;}
    for(i=1;i<=n;i++)
    {
        stu *a=(stu *)malloc(sizeof(stu));
        strcpy(a->stuhao,"\0");
        strcpy(a->stuname,"\0");
        strcpy(a->sturiqi,"\0");
        a->score[1]=0;
        a->score[2]=0;
        a->score[3]=0;
        a->score[4]=0;
        a->score[5]=0;
        a->score[6]=0;
        a->sum=0;
        a->ave=0;
        a->next=NULL;
        p->next=a;
        p=p->next;
    }
    p=d->next;//回到最初位置 
    if(t) {n+=2;p=head;}
    for(i=1;i<=n;i++)
    {
        x=1;
        while(x) 
        {
            fflush(stdin);
            printf("请输入学生学号\n");
            gets(p->stuhao);
            for(head=h;head!=p;head=head->next)
            {
                if(strcmp(head->stuhao,p->stuhao)==0){printf("学号重复!!!\n");break;}
            }
            if(head==p)x=0;
        }
        printf("请输入学生姓名\n");
        gets(p->stuname);
        printf("请输入学生生日\n"); 
        gets(p->sturiqi);
        fflush(stdin);
        printf("请输入学生科目一成绩\n");
        scanf("%lf",&p->score[1]);
        printf("请输入学生科目二成绩\n");
        scanf("%lf",&p->score[2]);
        printf("请输入学生科目三成绩\n");
        scanf("%lf",&p->score[3]);
        printf("请输入学生科目四成绩\n");
        scanf("%lf",&p->score[4]);
        printf("请输入学生科目五成绩\n");
        scanf("%lf",&p->score[5]);
        printf("请输入学生科目六成绩\n");
        scanf("%lf",&p->score[6]);
        p->sum=p->score[1]+p->score[2]+p->score[3]+p->score[4]+p->score[5]+p->score[6]; 
        p->ave=p->sum/6;
        if(i!=n)p=p->next;//p最后指向链表最末节点的头上 
    }
    return p;
}
void baocun()
{
    for(;head!=(p->next);head=head->next)
    {
        printf("   学号       姓名       生日      科目一成绩 科目二成绩 科目三成绩 科目四成绩 科目五成绩 科目六成绩 总成绩   平均成绩\n");
        printf("%10s   %5s   %10s   %6.2lf   %6.2lf   %6.2lf   %6.2lf   %6.2lf   %6.2lf   %6.2lf   %6.2lf\n",head->stuhao,head->stuname,head->sturiqi,head->score[1],head->score[2],head->score[3],head->score[4],head->score[5],head->score[6],head->sum,head->ave);
    }
}
void shanchu()
{
    char a[20];
    printf("请输入要删除的学生学号:");
    scanf("%s",&a);
    for(int i=1;i<=s1;i++)
    {
        if(strcmp(student[i].stuhao,a)==0)
        {
            break;
        }
    } 
    for(int i=1;i<=1;i++)
    {
        student[i]=student[i+1];
    }
    s1--;
    printf("已经删除成功!\n");
}
void paixu()
{
    stu *hd=head,*t=head,*ed=p,*td=NULL;
    int i=0,chance=0;
    char tnum[20],tname[20],tbirth[20];
    double tscore1,tscore2,tscore3,tscore4,tscore5,tscore6,tsum,tave;
    printf("请选择排序方式\n");
    printf("(1)学号(2)年龄(3)总分\n");
    scanf("%d",&chance);
    switch(chance)
    {
        case 1:
        for(t=head;t!=p;t=t->next)
        {
            for(head=hd;head!=p;head=head->next)
            {
                td=head->next;
                if(strcmp(head->stuhao,td->stuhao)>0)
                {
                    strcpy(tnum,td->stuhao);
                    strcpy(td->stuhao,head->stuhao);
                    strcpy(head->stuhao,tnum);
                    
                    strcpy(tname,td->stuname);
                    strcpy(td->stuname,head->stuname);
                    strcpy(head->stuname,tname);
                
                    strcpy(tbirth,td->sturiqi);
                    strcpy(td->sturiqi,head->sturiqi);
                    strcpy(head->sturiqi,tbirth);
                
                    tscore1=td->score[1];
                    td->score[1]=head->score[1];
                    head->score[1]=tscore1;
                    
                    tscore2=td->score[2];
                    td->score[2]=head->score[2];
                    head->score[2]=tscore2;
                
                    tscore3=td->score[3];
                    td->score[3]=head->score[3];
                    head->score[3]=tscore3;
                    
                    tscore4=td->score[4];
                    td->score[4]=head->score[4];
                    head->score[4]=tscore4;
                    
                    tscore5=td->score[5];
                    td->score[5]=head->score[5];
                    head->score[5]=tscore5;
                    
                    tscore6=td->score[6];
                    td->score[6]=head->score[6];
                    head->score[6]=tscore6;
                
                    tsum=td->sum;
                    td->sum=head->sum;
                    head->sum=tsum;
                    
                    tave=td->ave;
                    td->ave=head->ave;
                    head->ave=tave;
                }
            }
        }break;
    case 2:
    for(t=head;t!=p;t=t->next)
        {
            for(head=hd;head!=p;head=head->next)
            {
                td=head->next;
                if(strcmp(head->sturiqi,td->sturiqi)>0)
                {
                    strcpy(tnum,td->stuhao);
                    strcpy(td->stuhao,head->stuhao);
                    strcpy(head->stuhao,tnum);
                    
                    strcpy(tname,td->stuname);
                    strcpy(td->stuname,head->stuname);
                    strcpy(head->stuname,tname);
                
                    strcpy(tbirth,td->sturiqi);
                    strcpy(td->sturiqi,head->sturiqi);
                    strcpy(head->sturiqi,tbirth);
                
                    tscore1=td->score[1];
                    td->score[1]=head->score[1];
                    head->score[1]=tscore1;
                    
                    tscore2=td->score[2];
                    td->score[2]=head->score[2];
                    head->score[2]=tscore2;
                
                    tscore3=td->score[3];
                    td->score[3]=head->score[3];
                    head->score[3]=tscore3;
                    
                    tscore4=td->score[4];
                    td->score[4]=head->score[4];
                    head->score[4]=tscore4;
                    
                    tscore5=td->score[5];
                    td->score[5]=head->score[5];
                    head->score[5]=tscore5;
                    
                    tscore6=td->score[6];
                    td->score[6]=head->score[6];
                    head->score[6]=tscore6;
                
                    tsum=td->sum;
                    td->sum=head->sum;
                    head->sum=tsum;
                    
                    tave=td->ave;
                    td->ave=head->ave;
                    head->ave=tave;
                }
            }
        }break;
        case 3:
        for(t=head;t!=p;t=t->next)
        {
            for(head=hd;head!=p;head=head->next)
            {
                td=head->next;
                if(head->sum<td->sum)
                {
                    strcpy(tnum,td->stuhao);
                    strcpy(td->stuhao,head->stuhao);
                    strcpy(head->stuhao,tnum);
                    
                    strcpy(tname,td->stuname);
                    strcpy(td->stuname,head->stuname);
                    strcpy(head->stuname,tname);
                
                    strcpy(tbirth,td->sturiqi);
                    strcpy(td->sturiqi,head->sturiqi);
                    strcpy(head->sturiqi,tbirth);
                
                    tscore1=td->score[1];
                    td->score[1]=head->score[1];
                    head->score[1]=tscore1;
                
                    tscore2=td->score[2];
                    td->score[2]=head->score[2];
                    head->score[2]=tscore2;
                
                    tscore3=td->score[3];
                    td->score[3]=head->score[3];
                    head->score[3]=tscore3;
                    
                    tscore4=td->score[4];
                    td->score[4]=head->score[4];
                    head->score[4]=tscore4;
                    
                    tscore5=td->score[5];
                    td->score[5]=head->score[5];
                    head->score[5]=tscore5;
                    
                    tscore6=td->score[6];
                    td->score[6]=head->score[6];
                    head->score[6]=tscore6;
                
                    tsum=td->sum;
                    td->sum=head->sum;
                    head->sum=tsum;
                    
                    tave=td->ave;
                    td->ave=head->ave;
                    head->ave=tave;
                }
            }
        }break;
    }
}
void tongji()
{
        stu *d=head;//记录head 
    double max1=head->score[1],min1=head->score[1],max2=head->score[2],min2=head->score[2],max3=head->score[3],min3=head->score[3],max4=head->score[4],min4=head->score[4],max5=head->score[5],min5=head->score[5],max6=head->score[6],min6=head->score[6];
    int los1=0,los2=0,los3=0,los4=0,los5=0,los6=0;    
    printf("   学号       姓名    总成绩   平均成绩\n"); 
    for(;head!=(p->next);head=head->next)
    {
        if(head->score[1]>max1)max1=head->score[1];
        if(head->score[1]<min1)min1=head->score[1];
        if(head->score[2]>max2)max2=head->score[2];
        if(head->score[2]<min2)min2=head->score[2];
        if(head->score[3]>max3)max3=head->score[3];
        if(head->score[3]<min3)min3=head->score[3];
        if(head->score[4]>max4)max4=head->score[4];
        if(head->score[4]<min4)min4=head->score[4];
        if(head->score[5]>max5)max5=head->score[5];
        if(head->score[5]<min5)min5=head->score[5];
        if(head->score[6]>max6)max6=head->score[6];
        if(head->score[6]<min6)min6=head->score[6];
        if(head->score[1]<60)los1++;
        if(head->score[2]<60)los2++;
        if(head->score[3]<60)los3++;
        if(head->score[4]<60)los4++;
        if(head->score[5]<60)los5++;
        if(head->score[6]<60)los6++;
        printf("%10s   %5s   %6.2lf   %6.2lf\n",head->stuhao,head->stuname,head->sturiqi,head->score);
    }
    printf("科目一最高分 科目一最低分 科目二最高分 科目二最低分 科目三最高分 科目三最低分 科目四最高分 科目四最低分 科目五最高分 科目五最低分 科目六最高分 科目六最低分  科目一不及格 科目二不及格 科目三不及格 科目四不及格 科目五不及格 科目六不及格\n");
    printf("%9.2lf  %9.2lf  %9.2lf  %9.2lf  %9.2lf  %9.2lf  %9.2lf  %9.2lf  %9.2lf  %9.2lf  %9.2lf  %9.2lf  %9d  %9d  %9d  %9d  %9d\n",max1,min1,max2,min2,max3,min3,max4,min4,max5,min5,max6,min6,los1,los2,los3,los4,los5,los6);

}
void menu2()
{
    printf("*****************************************\n");
    printf("***         欢迎进入查找系统          ***\n");
    printf("***         1.学号查询法              ***\n");
    printf("***         2.姓名查询法              ***\n");
    printf("***         3.科目不及格查询法        ***\n");
    printf("*****************************************\n");
}
void xh()
{
    char a[20];
    int i=1;
    printf("请输入要找的同学的学号:");
    scanf("%s",a);
    for(i;i<=s1;i++)
    {
        if(strcmp(student[i].stuhao,a)==0)
        {
            printf("%s %s %d %d &d &d &d &d &d &d\n",student[i].stuhao,student[i].stuname,
                           student[i].sturiqi[1],&student[i].sturiqi[2],&student[i].sturiqi[3],
                           student[i].score[1],&student[i].score[2],&student[i].score[3],student[i].score[4],student[i].score[5],student[i].score[6]);
                           break;
        }
    } 
    if(i==(s1+1))
    printf("找不到此学号的同学\n");
}
void name()
{
    char a[20];
    int i=1;
    printf("请输入要找的同学的名字:");
    scanf("%s",a);
    for(i;i<=s1;i++)
    {
        if(strcmp(student[i].stuname,a)==0)
        {
            printf("%s %s %d %d &d &d &d &d &d &d\n",student[i].stuhao,student[i].stuname,
                           student[i].sturiqi[1],student[i].sturiqi[2],student[i].sturiqi[3],
                           student[i].score[1],student[i].score[2],student[i].score[3],student[i].score[4],student[i].score[5],student[i].score[6]);
                           break;
        }
    } 
    if(i==(s1+1))
    printf("找不到此名字的同学\n");
}
void xuan1()
{
    int i;
    printf("科目1不及格的名单:\n");
    for(i=1;i<s1;i++)
    {
        if(student[i].score[1]<60)
        {
            printf("%s %s %d %d &d &d &d &d &d &d\n",student[i].stuhao,student[i].stuname,
                           student[i].sturiqi[1],student[i].sturiqi[2],student[i].sturiqi[3],
                           student[i].score[1],student[i].score[2],student[i].score[3],student[i].score[4],student[i].score[5],student[i].score[6]);
        }
    }
}
void xuan2()
{
    int i;
    printf("科目2不及格的名单:\n");
    for(i=1;i<s1;i++)
    {
        if(student[i].score[2]<60)
        {
            printf("%s %s %d %d &d &d &d &d &d &d\n",student[i].stuhao,student[i].stuname,
                           student[i].sturiqi[1],student[i].sturiqi[2],student[i].sturiqi[3],
                           student[i].score[1],student[i].score[2],student[i].score[3],student[i].score[4],student[i].score[5],student[i].score[6]);
        }
    }
}
void xuan3()
{
    int i;
    printf("科目3不及格的名单:\n");
    for(i=1;i<s1;i++)
    {
        if(student[i].score[3]<60)
        {
            printf("%s %s %d %d &d &d &d &d &d &d\n",student[i].stuhao,student[i].stuname,
                           student[i].sturiqi[1],student[i].sturiqi[2],student[i].sturiqi[3],
                           student[i].score[1],student[i].score[2],student[i].score[3],student[i].score[4],student[i].score[5],student[i].score[6]);
        }
    }
}void xuan4()
{
    int i;
    printf("科目4不及格的名单:\n");
    for(i=1;i<s1;i++)
    {
        if(student[i].score[4]<60)
        {
            printf("%s %s %d %d &d &d &d &d &d &d\n",student[i].stuhao,student[i].stuname,
                           student[i].sturiqi[1],student[i].sturiqi[2],student[i].sturiqi[3],
                           student[i].score[1],student[i].score[2],student[i].score[3],student[i].score[4],student[i].score[5],student[i].score[6]);
        }
    }
}
void xuan5()
{
    int i;
    printf("科目5不及格的名单:\n");
    for(i=1;i<s1;i++)
    {
        if(student[i].score[5]<60)
        {
            printf("%s %s %d %d &d &d &d &d &d &d\n",student[i].stuhao,student[i].stuname,
                           student[i].sturiqi[1],student[i].sturiqi[2],student[i].sturiqi[3],
                           student[i].score[1],student[i].score[2],student[i].score[3],student[i].score[4],student[i].score[5],student[i].score[6]);
        }
    }
}
void xuan6()
{
    int i;
    printf("科目6不及格的名单:\n");
    for(i=1;i<s1;i++)
    {
        if(student[i].score[6]<60)
        {
            printf("%s %s %d %d &d &d &d &d &d &d\n",student[i].stuhao,student[i].stuname,
                           student[i].sturiqi[1],student[i].sturiqi[2],student[i].sturiqi[3],
                           student[i].score[1],student[i].score[2],student[i].score[3],student[i].score[4],student[i].score[5],student[i].score[6]);
        }
    }
}
void xueke()
{
    int a;
    printf("选择查询不及格的科目:"); 
    scanf("%d,&a");
    switch(a)
    {
        case 1:
        xuan1();
        break;
        
        case 2:
        xuan2();
        break;
        
        case 3:
        xuan3();
        break;
        
        case 4:
        xuan4();
        break;
        
        case 5:
        xuan5();
        break;
        
        case 6:
        xuan6();
        break;        
    } 
}
void chaxun()
{
    int a;
    menu2();
    printf("请选择查询方式:"); 
    scanf("%d",&a);
    switch(a)
    {
        case 1:
        xh();
        break;
        case 2:
        name();
        break;
        case 3:
        xueke();
        break;
    }
}
void denglu()
{
    int i,flag=0;
    char a[10],b[10],c[10],d[10];
    FILE *fp;
    printf("---------------登录-----------------\n");
    for(i=1;i<=3;i++)
    {
        printf("请输入您的姓名:");
        gets(a);
        printf("请输入您的密码:");
        gets(b);
        fp=fopen("账号信息.txt","r");
        if(fp==NULL)
           {
          perror("can not open file!");
          exit(1);                    
        }
        while(fgets(c,sizeof(c),fp))  //逐行循环读取文件,直到文件结束或找到相同姓名
        {
            if(strstr(c,a))
            {
                fscanf(fp,"%s",d);
                break;
            }
        }
        if(strcmp(b,d)==0)
        {
            printf("---------------登录成功,进入系统----------\n");
            flag=1;
            break;
        }
        else printf("-------登录失败,请重新输入,您还有%d次机会--------\n",3-i);
        fclose(fp);
    }
    if(flag==0)
        exit(0);
}
void xuanze()
{
    int a,b=3;
    printf("请输入模式:");
    scanf("%d",&a);
    switch(a)
    {
        case 1:
        shuru();
        break;
        
        case 2:
        tongji();
        break;
        
        case 3:
        shuchu();
        break;
        
        case 4: 
        chaxun();
        break;
        
        case 5:
        paixu();
        break;
        
        case 6:
        shanchu();
        break;
        
        case 7:
        baocun();
        break;
    } 
    
}
void meun()
{
        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("************************************************************\n");
}

  • 写回答

5条回答 默认 最新

  • 前网易架构师-高司机 游戏服务器领域优质创作者 2023-04-06 22:46
    关注

    指出你的两个问题。
    1.你的函数比如baocun,paixu声明和最后的定义逻辑实现部分的入参不一样,声明有参数,但实现的时候却没参数,虽然你调用的时候没有穿参数没有问题,但是这不是一个良好的编程习惯
    2.报错和我刚才说的1有关,因为你的baocun,paixu等函数内部直接使用了head,但head并不是一个全局变量或者函数内定义的局部变量(包含栈空间内的入参),因此就会报错

    如何解决呢,保持你的函数声明和定义的类型一样,也就是给你的baocun,paixu等函数实现部分加入像声明一样的参数

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

问题事件

  • 系统已结题 4月15日
  • 已采纳回答 4月7日
  • 创建了问题 4月6日

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。