求高手帮忙啊……6、统计成绩信息时一直报错

作者在 2011-04-30 23:00:00 发布以下内容
head.cpp
typedef struct grade
{
    char num[10];//学号
    char name[8];
    int chinese;
    int english;
    int math;
    int computer;
    struct grade *next;
}GRADE;

int count;//全局变量,总学生人数传递
char password[10];//全局变量,密码消息传递
GRADE *head,*tail;//头指针,尾指针
char first;//为0时,表明不是第一次登陆系统
queue.cpp
typedef struct node
{
    int data;
    struct node *next;
}qnode;
typedef struct
{
    qnode *front;
    qnode *rear;
}linkqueue;

void init(linkqueue *L)
{
    L->front=L->rear=(qnode *)malloc(sizeof(qnode));
    if(L->front==NULL)
    {
        printf("内存分配失败,按任意键退出!\n");
        exit(1);
    }
    L->front->next=NULL;
}//初始化一个链式队

void in(linkqueue *L,int elem)
{
    qnode *s;
    s=(qnode *)malloc(sizeof(qnode));
    if(s==NULL)
    {
        printf("内存分配失败,按任意键退出!\n");
        exit(1);
    }
    s->data=elem;
    s->next=NULL;
    L->rear->next=s;
    L->rear=s;
}//入队

int out(linkqueue *L)
{
    qnode *p;
    int elem;
    p=(qnode *)malloc(sizeof(qnode));
    if(p==NULL)
    {
        printf("内存分配失败,按任意键退出!\n");
        exit(1);
    }
    if(L->front==L->rear)
    {
        printf("队空\n");
        elem=0;
    }
    else
    {
        p=L->front->next;
        L->front->next=p->next;
        if(p->next==NULL)
        {
            L->rear=L->front;
        }
        elem=p->data;
        free(p);
    }
    return elem;
}//出队
学生成绩管理系统.cpp
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include "head.cpp"
#include "queue.cpp"
void main()
{
    head=tail=NULL;
    save=first=0;
    checkfirst();
    login();
    read();
    menu();
}
void checkfirst()
{
    FILE *fp,*fp1;
    char pwd[10],temp[10];
    char ch;
    int i;
    if(NULL==(fp=fopen("config.dat","rb")))
    {
        printf("\n首次登陆学生成绩管理系统,请单击任意键开始初始化\n");
        bound('*',50);
        getch();
        do
        {
            printf("\n请设置登录密码(最多8位):\n");
            for(i=0;i<8&&(pwd[i]=getch())!=13;i++)
            {
                putch('*');
            }
            printf("\n请再次输入确认密码:\n");
            for(i=0;i<8&&(temp[i]=getch())!=13;i++)
            {
                putch('*');
            }
            pwd[i]='\0';
            temp[i]='\0';
            if(strcmp(pwd,temp)!=0)
            {
                printf("\n两次输入的密码不同,请重新输入:\n");
            }
            else break;
        }while(1);
        if(NULL==(fp1=fopen("config.dat","wb")))
        {
            printf("\n配置文件失败,按任意键退出\n");
            getch();
            exit(1);
        }
        i=0;
        while(pwd[i])
        {
            putw(pwd[i],fp1);
            i++;
        }
        fclose(fp1);
        printf("\n系统配置成功,请重新登陆\n");
        getch();
        exit(1);
    }
    else
    {
        i=0;
        while(!feof(fp)&&i<8)
        {
            pwd[i++]=getw(fp);
        }
        pwd[i]='\0';
        
        if(i>8) i--;
        while(pwd[i]!=-1&&i>=0)
        {
            i--;
        }
        pwd[i]='\0';
        
        strcpy(password,pwd);
    }
}
void login()
{
    int i;
    int n=3;
    char pwd[10];
    do
    {
        printf("请输入密码:\n");
        for(i=0;i<8&&(pwd[i]=getch())!=13;i++)
        {
            putch('*');
        }
        pwd[i]='\0';
        if(strcmp(pwd,password))
        {
            printf("密码错误,请重新输入\n");
            n--;
        }
        else
        {
            system("cls");
            break;
        }
    }while(n>0);
    if(!n)
    {
        printf("非法登陆!按任意键退出\n");
        getch();
        exit(1);
    }
}
void read()
{
    FILE *fp;
    GRADE *gad;

    if(NULL==(fp=fopen("grade.dat","rb")))
    {
        first=1;
        return;
    }
    while(!feof(fp))
    {
        gad=(GRADE *)malloc(sizeof(GRADE));
        if(gad==NULL)
        {
            printf("内存分配失败\n");
            getch();
            return;
        }
        fread(gad,sizeof(GRADE),1,fp);
        if(feof(fp)) break;
        if(head==NULL)
        {
            head=gad;
            tail=gad;
        }
        else
        {
            tail->next=gad;
            tail=gad;
        }
        tail->next=NULL;
    }
    first=0;
    fclose(fp);
}
void menu()
{
    char choice;
    system("cls");
    do
    {
        printf("\t成绩管理系统\n");
        bound('*',50);
        printf("\t1、添加成绩信息\n");
        printf("\t2、查询成绩信息\n");
        printf("\t3、显示成绩信息\n");
        printf("\t4、修改成绩信息\n");
        printf("\t5、删除成绩信息\n");
        printf("\t6、统计成绩信息\n");
        printf("\t8、更换系统密码\n");
        printf("\t0、退出\n");
        bound('*',50);
        printf("\n请选择菜单\n");
        do
        {
            fflush(stdin);
            choice=getchar();
            system("cls");
            switch(choice)
            {
                case '1':add();break;
                case '2':
                    {
                        if(first)
                        {
                            printf("新系统,暂时没有信息\n");
                            getch();
                            break;
                        }
                        find();break;
                    }
                case '3':
                    {
                        if(first)
                        {
                            printf("新系统,暂时没有信息\n");
                            getch();
                            break;
                        }
                        dis();break;
                    }
                case '4':
                    {
                        if(first)
                        {
                            printf("新系统,暂时没有信息\n");
                            getch();
                            break;
                        }
                        alt();break;
                    }
                case '5':
                    {
                        if(first)
                        {
                            printf("新系统,暂时没有信息\n");
                            getch();
                            break;
                        }
                        del();break;
                    }
                case '6':
                    {
                        if(first)
                        {
                            printf("新系统,暂时没有信息\n");
                            getch();
                            break;
                        }
                        staall();break;
                    }
                case '8':rst();break;
                case '0':sav();exit(0);
            }
        }while(choice<='8'&&choice>='0');
    }while(1);
}
void bound(char ch,int n)
{
    while(n--)
    {
        putch(ch);
    }
    printf("\n");
    return;
}
void sav()
{
    FILE *fp;
    GRADE *gad1;
    if(save==0) return;
    if(NULL==(fp=fopen("grade.dat","wb")))
    {
        printf("打开文件出错\n");
        getch();
        return;
    }
    gad1=head;
    while(gad1)
    {
        fwrite(gad1,sizeof(GRADE),1,fp);
        gad1=gad1->next;
    }
    save=0;
    fclose(fp);
}
void add()
{
    FILE *fp;
    GRADE *gad2;
    int i=0;
    char choice='y';
    if(NULL==(fp=fopen("grade.dat","ab")))
    {
        printf("打开文件出错\n");
        getch();
        return;
    }
    do
    {
        i++;
        gad2=(GRADE *)malloc(sizeof(GRADE));
        if(gad2==NULL)
        {
            printf("内存分配失败\n");
            getch();
            return;
        }
        printf("输入第%d个学生的成绩信息:\n",i);
        bound('*',20);
        printf("学号:\t");
        scanf("%s",gad2->num);
        printf("姓名:\t");
        scanf("%s",gad2->name);
        printf("语文:\t");
        scanf("%d",&gad2->chinese);
        printf("英语:\t");
        scanf("%d",&gad2->english);    
        printf("数学:\t");
        scanf("%d",&gad2->math);
        printf("计算机:\t");
        scanf("%d",&gad2->computer);
        gad2->next=NULL;
        if(head==NULL)
        {
            head=gad2;
            tail=gad2;
        }
        else
        {
            tail->next=gad2;
            tail=gad2;
        }
        fwrite(tail,sizeof(GRADE),1,fp);
        first=0;
        printf("\n");
        bound('*',20);
        printf("\n是否继续?(y/n)\n");
        fflush(stdin);
        choice=getchar();
        if(toupper(choice)!='Y')
        {
            fclose(fp);
            printf("\n输入完毕,按任意键返回\n");
            getch();
            return;
        }
        system("cls");
    }while(1);
}
void find()
{
    char choice;
    int ret=0;
    char strnum[10],strname[8];
    GRADE *gad3;
    system("cls");
    do
    {
        printf("\n查询信息\n");
        bound('*',25);
        printf("\t1、按学号查找\n");
        printf("\t2、按姓名查找\n");
        printf("\t0、返回主菜单\n");
        bound('*',25);
        printf("请选择菜单\n");
        do
        {
            fflush(stdin);
            choice=getchar();
            system("cls");
            switch(choice)
            {
                case '1':
                    {
                        printf("请输入学号:\n");
                        scanf("%s",strnum);
                        gad3=findnum(strnum);
                        disgad(gad3,"学号",strnum);
                        getch();
                        break;
                    }
                case '2':
                    {
                        printf("请输入姓名:\n");
                        scanf("%s",strname);
                        gad3=findname(strname);
                        disgad(gad3,"姓名",strname);
                        getch();
                        break;
                    }
                case '0':
                    {
                        ret=1;
                        break;
                    }
            }
        }while(choice>='0'&&choice<='2');
        system("cls");
        if(ret) break;
    }while(1);
}
GRADE *findname(char *name)
{
    GRADE *gad;
    gad=head;
    
    while(gad)
    {
        if(strcmp(name,gad->name)==0) return gad;
        gad=gad->next;
    }
    return NULL;
}
GRADE *findnum(char *num)
{
    GRADE *gad;
    gad=head;
    while(gad)
    {
        if(strcmp(num,gad->num)==0) return gad;
        gad=gad->next;
    }
    return NULL;
}
void disgad(GRADE *gad,char *field,char *name)
{
    if(gad)
    {
        printf("\n%s:%s信息如下:\n",field,name);
        bound('*',20);
        printf("学号:%s\n",gad->num);
        printf("姓名:%s\n",gad->name);
        printf("语文:%d\n",gad->chinese);
        printf("外语:%d\n",gad->english);
        printf("数学:%d\n",gad->math);
        printf("计算机:%d\n",gad->computer);
        bound('*',20);
    }
    else
    {
        bound('*',40);
        printf("系统中没有%s为%s的信息!\n",field,name);
    }
}
void dis()
{
    GRADE *gad;
    printf("\n成绩列表:\n");
    bound('*',25);
    gad=head;
    while(gad)
    {
        printf("学号:%s\n",gad->num);
        printf("姓名:%s\n",gad->name);
        printf("语文:%d\n",gad->chinese);
        printf("外语:%d\n",gad->english);
        printf("数学:%d\n",gad->math);
        printf("计算机:%d\n",gad->computer);
        bound('*',25);
        gad=gad->next;
    }
    printf("\n按任意键返回\n");
    getch();
    return;
}
void alt()
{
    GRADE *gad;
    char name[8],*str;
    char choice;
    printf("\n输入要修改的姓名信息\n");
    scanf("%s",name);
    gad=findname(name);
    disgad(gad,"姓名",name);
    if(gad)
    {
        printf("\n选择要修改的项目:\n");
        bound('*',35);
        printf("\t1、修改学号\t\t2、修改姓名\n");
        printf("\t3、修改语文成绩\t\t4、修改英语成绩\n");
        printf("\t5、修改数学成绩\t\t6、修改计算机成绩\n");
        printf("\t0、返回\n");
        bound('*',35);
        printf("请选择:\n");
        do
        {
            fflush(stdin);
            choice=getchar();
            switch(choice)
            {
                case '1':
                    {
                        str=modi_field("学号",gad->num,10);
                        if(str!=NULL)
                        {
                            strcpy(gad->num,str);
                            free(str);
                        }
                        break;
             &nbs
求助 | 阅读 723 次
文章评论,共0条
游客请输入验证码
文章归档
最新评论