标题:求教,为什么这个程序显示能成功读入文件,但查询课程时却一个都没有呢?
只看楼主
pororo8
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2013-5-3
 问题点数:0 回复次数:0 
求教,为什么这个程序显示能成功读入文件,但查询课程时却一个都没有呢?
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<string.h>
typedef struct
{
    char num[20];//学号
    char name[20];//姓名
    char course1[20];//课程1
    char course2[20];//课程2
}student;
student stu[50];
typedef struct
{
    char name[20];//课程名称
    char character[200];//课程性质
    char score[5];//课程学分
}course;
course cou[50];
int menu(char *p)
{
    for(;*p!='m'&&*p!='s'&&*p!='M'&&*p!='S';)
    {
        system("cls");        //运行前清空
        printf("\t\t\t<<<<<学生选课系统>>>>>\n");
        printf("管理员还是学生?(m/s)");
        *p=getchar();
        getchar();                    //吸收回车键
    }
    if(*p=='m'||*p=='M')
        return('m');
    else
        return('s');
}
char menum()                //管理员菜单
{
    char c;
    do
    {
    system("cls");
    printf("\t\t\t<<<<<管理员你好!>>>>>\n");
    printf("\t1.添加课程\n");
    printf("\t2.删除课程\n");
    printf("\t3.查询课程\n");
    printf("\t4.浏览课程\n");
    printf("\t5.读入文件\n");
    printf("\t6.导出文件\n");
    printf("\t7.回到主菜单\n");
    printf("\t0.结束\n");
    printf("输入你的选择:");
    c=getchar();
    }while(c<'0'||c>'7');
    return(c);
}
char menus()                //学生菜单
{
    char c;
    do
    {
    system("cls");
    printf("\t\t\t<<<<<学生你好!>>>>>\n");
    printf("\ta.浏览所有课程\n");
    printf("\tb.查询课程信息\n");
    printf("\tc.选择课程\n");
    printf("\td.浏览选修的课程\n");
    printf("\te.重选课程\n");
    printf("\tf.读入资料\n");
    printf("\tg.保存\n");
    printf("\th.回到主菜单\n");
    printf("\ti.结束\n");
    printf("输入你的选择:");
    c=getchar();
    }while(c<'a'||c>'i');
    return (c);
}
int madd(course coud[],int n)                //管理员添加课程
{
    char c;
    while(c!='n'&&'N')
    {
        printf("\t课程名称:");
        scanf("%s",coud[n].name);
        printf("\t课程学分:");
        scanf("%s",coud[n].score);
        printf("\t课程性质:");
        scanf("%s",coud[n].character);
        for(;;)
        {
            printf("\t继续?(Y/N)");
            getchar();                                         //吸收多余字符
            c=getchar();
            if(c=='y'||c=='Y'||c=='n'||c=='N')
                break;
            else
                printf("错误!请重新输入选择\n");
        }
        n++;
    }
    return (n);
}
int mdelete(course coud[],int n)                            //管理员删除课程
{
    char s[20];
    int i=0;
    printf("输入要删除的课程名称:");
    getchar();                                                    //吸收多余字符
    gets(s);
    while(strcmp(coud[i].name,s)!=0&&i<n)
        i++;
    if(i==n)
    {
        printf("没有此课程\n");
        system("pause");
        return(n);
    }
    else
    {
        for(;i<n-1;i++)
        {
            strcpy(coud[i].name,cou[i+1].name);
            strcpy(coud[i].score,cou[i+1].score);
            strcpy(coud[i].character,cou[i+1].character);
        }
        printf("\t已删除\n");
        system("pause");
        return (n-1);
    }
}
void msearch(course coud[],int n)                                            //管理员查询课程
{
    int i=0;
    char s[20];
    getchar();                                                    //吸收多余字符
    printf("\t输入你要寻找的课程:");
    gets(s);
    while (strcmp(coud[i].name,s)!=0&&i<n)
        i++;
    if(i==n)
    {
        printf("没有此课程\n");
        system("pause");
    }
    else
    {
        printf("\t课程名称\t课程学分\t课程性质\n");
        printf("\t%s\t",coud[i].name);
        printf("\t%s\t",coud[i].score);
        printf("\t%s\t\n",coud[i].character);
        system("pause");
    }
}
void mdisplay(course coud[],int n)                                        //管理员浏览课程
{
    int i;
    getchar();                                                    //吸收多余字符
    printf("\t课程名称\t课程学分\t课程性质\n");
    for(i=0;i<n;i++)
    {
        printf("\t%s\t",coud[i].name);
        printf("\t%s\t",coud[i].score);
        printf("\t%s\t\n",coud[i].character);
    }
    system("pause");
}
void sdisplay(course coud[],int n)                                        //学生浏览课程名称
{
    int i;
    getchar();                                                    //吸收多余字符
    printf("\t课程名称\n");
    for(i=0;i<n;i++)
    {
        printf("\t%d.%s\n",i+1,coud[i].name);
    }
    system("pause");
}
void ssearch(course coud[],int n)                                    //学生查询课程信息
{
    int i=0;
    char s[20];
    getchar();                                                    //吸收多余字符
    printf("\t输入你要寻找的课程:");
    gets(s);
    while (strcmp(coud[i].name,s)!=0&&i<n)
        i++;
    if(i==n)
    {
        printf("没有此课程\n");
        system("pause");
    }
    else
    {   printf("\t课程学分\t课程性质\n");
        printf("\t%s\t",coud[i].score);
        printf("\t%s\t\n",coud[i].character);
        system("pause");
    }
}
int sselect(student stud[],int n)                                        //学生选课
{
    char c;
    getchar();                                                    //吸收多余字符
    while(c!='n'&&c!='N')
    {
        printf("\t输入你的学号:");
        scanf("%s",stud[n].num);
        printf("\t输入你的姓名:");
        scanf("%s",stud[n].name);
        printf("\t选择你的课程1:");
        scanf("%s",stud[n].course1);
        printf("\t选择你的课程2:");
        scanf("%s",stud[n].course2);
        for(;;)
        {
            printf("\t继续?(Y/N)");
            getchar();                                         //吸收多余字符
            c=getchar();
            if(c=='y'||c=='Y'||c=='n'||c=='N')
                break;
            else
                printf("错误!请重新输入选择\n");
        }
        n++;
    }
    return (n);
}
void sscan(student stud[],int n)                                            //学生浏览已选课程
{
    int i=0;
    char s[20];
    getchar();                                                    //吸收多余字符
    printf("\t输入你的姓名:");
    gets(s);
    while (strcmp(stud[i].name,s)!=0&&i<n)
        i++;
    if(i==n)
    {
        printf("查无此人\n");
        system("pause");
    }
    else
    {
        printf("\t学号\t姓名\t课程1\t课程2\n");
        printf("\t%s",stud[i].num);
        printf("\t%s",stud[i].name);
        printf("\t%s",stud[i].course1);
        printf("\t%s\n",stud[i].course2);
        system("pause");
    }
}
void schange(student stud[],int n)                                    //学生重选错选课程
{
    int i=0;
    char s[20];
    printf("\t输入你的姓名:");
    getchar();
    gets(s);
    while (strcmp(stud[i].name,s)!=0&&i<n)
        i++;
    if(i==n)
    {
        printf("查无此人\n");
        system("pause");
    }
    else
    {
        printf("课程1:");
        scanf("%s",stud[i].course1);
        printf("课程2:");
        scanf("%s",stud[i].course2);
    }
}
int saddfile(student stud[],int n)       //读入学生文件
{
    int i=0,num;
    FILE *fp;
    char filename[20];
    printf("\t输入文件名:");
    scanf("%s",filename);
    if((fp=fopen(filename,"rb"))==NULL)
    {
        printf("打开失败\n");
        system("pause");
        return(n);
    }
    fscanf(fp,"%d",&num);
    while(i<num)
    {
        fscanf(fp,"%s%s%s%s",stud[n+i].num,stud[n+i].name,stud[n+i].course1,stud[n+i].course2);
        i++;
    }
    n+=num;
    fclose(fp);
    printf("\t成功!\n");
    system("pause");
    return(n);
}
int maddfile(course coud[],int n)       //读入课程文件
{
    int i=0,num;
    FILE *fp;
    char filename[20];
    printf("\t输入文件名:");
    scanf("%s",filename);
    if((fp=fopen(filename,"rb"))==NULL)
    {
        printf("打开失败\n");
        system("pause");
        return(n);
    }
    fscanf(fp,"%d",&num);
    while(i<num)
    {
        fscanf(fp,"%s%s%s%s",coud[n+i].name,coud[n+i].character,coud[n+i].score);
        i++;
    }
    n+=num;
    fclose(fp);
    printf("\t成功!\n");
    system("pause");
    return(n);
}
void mwritefile(course coud[],int n1)    //创建文件或保存文件(课程)
{
    int j=0;
    FILE *fp;
    char filename[20];
    printf("\t保存到一个文本文件\n");
    printf("\t输入文件名:");
    scanf("%s",filename);
    if((fp=fopen(filename,"w"))==NULL)
    {
        printf("\t打开失败\n");
        system("pause");
        return;
    }
    fprintf(fp,"%d\n",n1);
    while(j<n1)
    {
        fprintf(fp,"%-16s%-16s%-16s\n",coud[j].name,coud[j].character,coud[j].score);
        j++;
    }
    fclose(fp);
    printf("\t\t\t成功!\n");
    system("pause");
}
void swritefile(student stud[],int n2)    //创建文件或保存文件(课程)
{
    int i=0;
    FILE *fp;
    char filename[20];
    printf("\t保存到一个文本文件\n");
    printf("\t输入文件名:");
    scanf("%s",filename);
    if((fp=fopen(filename,"w"))==NULL)
    {
        printf("\t打开失败\n");
        system("pause");
        return;
    }
    fprintf(fp,"%d\n",n2);
    while(i<n2)
    {
        fprintf(fp,"%-16s%-16s%-16s%-16s\n",stud[i].num,stud[i].name,stud[i].course1,stud[i].course2);
        i++;
    }
    fclose(fp);
    printf("\t\t\t成功!\n");
    system("pause");
}
int main()                                                            //主函数
{
    char p2[10],p1[]="123";    //密码
    char c;
    char *p=&c;
    int n1=0,n2=0;
    for(;;)
    {
        c=menu(p);
        if(c=='m')
        {
            for(;strcmp(p1,p2)!=0;)
            {
                printf("password:");
                gets(p2);
            }
            switch(menum())
            {
            case '1':printf("\t\t添加课程\n");
                    n1=madd(cou,n1);break;
            case '2':printf("\t\t删除课程\n");
                    n1=mdelete(cou,n1);break;
            case '3':printf("\t\t查询课程\n");
                    msearch(cou,n1);break;
            case '4':printf("\t\t浏览全部课程\n");
                    mdisplay(cou,n1);break;
            case '5':printf("\t\t读入文件\n");
                     saddfile(stu,n2);maddfile(cou,n1);break;
            case '6':printf("\t\t导出文件\n");
                     mwritefile(cou,n1);break;
            case '7':*p='q';gets(p2);break;                //重置menu按钮和管理员密码数组
            case '0':printf("\t\t感谢使用");exit(1);
            }
        }
        else
            if(c=='s')
        {
            switch(menus())
            {
            case 'a':printf("\t\t浏览课程\n");
                    sdisplay(cou,n1);break;
            case 'b':printf("\t\t查询课程信息\n");
                    ssearch(cou,n1);break;
            case 'c':printf("\t\t选择课程\n");
                    n2=sselect(stu,n2);break;
            case 'd':printf("\t\t查询已选课程\n");
                    sscan(stu,n2);break;
            case 'e':printf("\t\t重选课程\n");
                    schange(stu,n2);break;
            case 'f':printf("\t\t读入文件\n");
                     saddfile(stu,n2);maddfile(cou,n1);break;
            case 'g':printf("\t\t保存\n");
                     swritefile(stu,n2);break;
            case 'h':*p='q';getchar();break;                    //重置menu按钮
            case 'i':printf("\t\t\t<<<<<感谢使用>>>>>\n");exit(1);
            }
        }
    }
    return 0;
}
搜索更多相关主题的帖子: 课程 name character include course 
2013-05-03 22:20



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-406149-1-1.html




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

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