标题:关于如何查询嵌套数组中元素。
只看楼主
moonchild
Rank: 1
等 级:新手上路
帖 子:12
专家分:0
注 册:2011-10-25
结帖率:100%
 问题点数:0 回复次数:1 
关于如何查询嵌套数组中元素。
#include<stdio.h>  
#include<stdlib.h>
#include<string.h>
struct student//学生成绩结构数组
{
    int no;//学号
    struct course//嵌套科目名称数组
    {
        char coursename[6];//科目名称
        char courseno[6];//科目编号
        float credit;//科目学分
        float score[3];//平时成绩,实验成绩,考试成绩组成的数组
        float result[2];//综合成绩,实际学分组成的数组
    }course[2];//两项科目
};
struct inform//学生基本信息结构数组
{  
    char id[10];
    char name[10];
    char sex[20];
    char room[10];
    char tel[15];//定义学生基本信息中的学号,宿舍号,电话,姓名,性别
};


void saveStu(struct student stu[],int count,int flag)
{
    FILE *fp;//定义文件指针
    int i;
    if((fp=fopen("score.dat","wb"))==NULL)//以写的方式建立二进制文件
    {
        printf("不能打开文件!!\n");
        exit(1);
    }
    for(i=0;i<count;i++)
        if(fwrite(&stu[i],sizeof(struct student),1,fp)!=1)
            printf("文件写错误,请检查是否正确录入!\n");//如果当前写入的数据为空,则提示文件写错误
        fclose(fp);
}


void ReadStu(struct student stu[],int *stu_number)
{
    FILE *fp;
    int i=0;
    if((fp=fopen("score.dat","rb"))==NULL)
    {
        printf("不能打开文件,请检查文件是否已破坏或者删除!\n");//如果读取的数据为空或者文件不存在,提示报错.
        exit(1);
    }
    while(fread(&stu[i],sizeof(struct student),1,fp)==1&&i<100)
        i++;//在要求范围内读取学生成绩内容
    *stu_number=i;//记录文件中的学生信息条数
    if(feof(fp))
        fclose(fp);
    else
    {
        printf("文件读取错误,请检查文件完整性!");
        fclose(fp);//遇到文件结束符时,关闭文件;否则提示文件读取错误
    }
}

void Menu()
{
    printf("\n\n");
    printf("   xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n");
    printf("   x                  欢迎使用学生信息管理系统                      x\n");
    printf("   x                                        Disigned By B10010518   x\n");
    printf("   x****************************************************************x\n");
    printf("   x `````````````````````①录入学生成绩````````````````````````````x\n");
    printf("   x                                                                x\n");
    printf("   x``````````````````````②查看成绩排序````````````````````````````x\n");
    printf("   x                                                                x\n");
    printf("   x``````````````````````③查询学生成绩````````````````````````````x\n");
    printf("   x                                                                x\n");
    printf("   x``````````````````````④按学号删除成绩``````````````````````````x\n");
    printf("   x                                                                x\n");
    printf("   x``````````````````````⑤查找基本信息````````````````````````````x\n");
    printf("   x                                                                x\n");
    printf("   x``````````````````````⑥删除基本信息````````````````````````````x\n");
    printf("   x                                                                x\n");
    printf("   x``````````````````````0,退        出````````````````````````````x\n");
    printf("   xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n");
}//菜单函数

void InputStu(struct student stu[],int *stu_number)
{
    char ch='y';
    int count=0,i;
    while((ch=='y')||(ch=='Y'))
    {
        system("cls");
        printf("\n    ******************* 录入学生成绩 *******************\n");
        printf("\n\n  请输入学生信息(为了学生的权益,请您认真谨慎填写!)\n");
        printf("\n\n\t\t    学    号:");scanf("%d",&stu[count].no);
        for(i=0;i<2;i++)
        {
            printf("\n\n\t\t    课程名称:");scanf("%s",&stu[count].course[i].coursename);
            printf("\n\n\t\t    课程编号:");scanf("%s",&stu[count].course[i].courseno);
            printf("\n\n\t\t    学    分:");scanf("%f",&stu[count].course[i].credit);
            printf("\n\n\t\t    平时成绩:");scanf("%f",&stu[count].course[i].score[0]);
            printf("\n\n\t\t    实验成绩:");scanf("%f",&stu[count].course[i].score[1]);
            printf("\n\n\t\t    卷面成绩:");scanf("%f",&stu[count].course[i].score[2]);
            if(stu[count].course[i].score[1]==-1)
                stu[count].course[i].result[0]=stu[count].course[i].score[0]*3/10+stu[count].course[i].score[2]*7/10;
            else
                stu[count].course[i].result[0]=stu[count].course[i].score[0]*15/100+stu[count].course[i].score[2]*7/10+\
                stu[count].course[i].score[1]*15/100;
            
            if(stu[count].course[i].result[0]>=90&&stu[count].course[i].result[0]<=100)
                stu[count].course[i].result[1]=stu[count].course[i].credit;
            else if(stu[count].course[i].result[0]>=80)
                stu[count].course[i].result[1]=(stu[count].course[i].credit)*8/10;
            else if(stu[count].course[i].result[0]>=70)
                stu[count].course[i].result[1]=(stu[count].course[i].credit)*75/100;
            else if(stu[count].course[i].result[0]>=60)
                stu[count].course[i].result[1]=(stu[count].course[i].credit)*6/10;
            else
                stu[count].course[i].result[1]=0;
        }
        printf("\n\n\t\t 是否输入下一个学生信息?(y/n)");
        scanf("\n%c",&ch);
        count++;
    }
    *stu_number+=count;
    saveStu(stu,count,1);
}//学生成绩录入函数,并计算综合成绩和学分,调用保存函数把输入内容和结果保存到score.dat文件中


void BrowseStu(struct student stu[] ,int *stu_number)
{
    system("cls");
    ReadStu(stu,stu_number);                                                                   //读取socore.dat文件记录
    if(*stu_number==0)
        printf("\n没有学生记录!\n");
    else
    {
        struct student temp;
        int i=0,a,b,c,k;
        printf(" \n```````````  返回主菜单请输入:  0  ```````````````` \n");
        printf("方式选择: 1.按科目A综合成绩排名    2.按科目B综合成绩排名\n ");
        printf("          3.按科目A实得学分排名    4.按科目B实得学分排名\n");                //按照用户选择进行排名
        scanf("%d",&k);
        printf("\n\n\n     学号  课程编号  课程名称  平时成绩  实验成绩  卷面成绩  综合成绩  实得学分\n");
        printf("-----------------------------------------------------------------------------\n");
        if(k==1)
        {
            for(a=0; a<(*stu_number-1); a++)
            {
                c=a;
                for(b=a+1; b<*stu_number; b++)
                    if(stu[b].course[0].result[0]>stu[c].course[0].result[0])
                        c=b;
                    if (c!=a)                 
                    {
                        temp=stu[a];
                        stu[a]=stu[c];
                        stu[c]=temp;
                    }
            }
            while(i<*stu_number)
            {
                printf("\n   %-10d%-10s%-10s%-10f%-10f%-10f%-10.2f%-10.2f\n",stu[i].no,stu[i].course[0].courseno,stu[i].course[0].coursename,\
                stu[i].course[0].score[0],stu[i].course[0].score[1],stu[i].course[0].score[2],stu[i].course[0].result[0],stu[i].course[0].result[1]);
               
                i++;
            }
            getchar();
        }                                                                                        //按科目a综合成绩排名
        
        else if(k==2)
        {
            for(a=0; a<(*stu_number-1); a++)
            {
                c=a;
                for(b=a+1; b<*stu_number; b++)
                    if(stu[b].course[1].result[0]>stu[c].course[1].result[0])
                        c=b;
                    if (c!=a)                 
                    {
                        temp=stu[a];
                        stu[a]=stu[c];
                        stu[c]=temp;
                    }
            }
            while(i<*stu_number)
            {
                printf("\n   %-10d%-10s%-10s%-10f%-10f%-10f%-10.2f%-10.2f\n",stu[i].no,stu[i].course[1].courseno,stu[i].course[1].coursename,\
                stu[i].course[1].score[0],stu[i].course[1].score[1],stu[i].course[1].score[2],stu[i].course[1].result[0],stu[i].course[1].result[1]);
               
                i++;
            }
            getchar();
        }                                                                                   //按照科目b综合成绩排名
        else if(k==3)
        {
            for(a=0; a<(*stu_number-1); a++)
            {
                c=a;
                for(b=a+1; b<*stu_number; b++)
                    if(stu[b].course[0].result[1]>stu[c].course[0].result[1])
                        c=b;
                    if (c!=a)                 
                    {
                        temp=stu[a];
                        stu[a]=stu[c];
                        stu[c]=temp;
                    }
            }
            while(i<*stu_number)   
            {
                printf("\n   %-10d%-10s%-10s%-10f%-10f%-10f%-10.2f%-10.2f\n",stu[i].no,stu[i].course[0].courseno,stu[i].course[0].coursename,\
                stu[i].course[0].score[0],stu[i].course[0].score[1],stu[i].course[0].score[2],stu[i].course[0].result[0],\
                stu[i].course[0].result[1]);
               
                i++;
            }
            getchar();
        }//按照科目a实得学分排名
        else if(k==4)
        {
            for(a=0; a<(*stu_number-1); a++)
            {
                c=a;
                for(b=a+1; b<*stu_number; b++)
                    if(stu[b].course[1].result[1]>stu[c].course[1].result[1])
                        c=b;
                    if (c!=a)                 
                    {
                        temp=stu[a];
                        stu[a]=stu[c];
                        stu[c]=temp;
                    }
            }
            while(i<*stu_number)
            {
                printf("\n   %-10d%-10s%-10s%-10d%-10d%-10d%-10.2f%-10.2f\n",stu[i].no,stu[i].course[1].courseno,stu[i].course[1].coursename,\
                stu[i].course[1].score[0],stu[i].course[1].score[1],stu[i].course[1].score[2],stu[i].course[1].result[0],\
                stu[i].course[1].result[1]);
               
                i++;
            }
            getchar();
        }
    }
 getchar();
 getchar();
}

void SearchStu(struct student stu[] ,int *stu_number)
{
   int i=0,x;
   ReadStu(stu,stu_number); //读取socore.dat文件记录
   if(*stu_number==0)
        printf("\n没有学生记录!\n");

    system("cls");
    printf("\n   请输入要查询学生的学号:");scanf("%d",&x);
   while(i<*stu_number)
    {
        if(stu[i].no==x)
        {
            printf("\n   学号  课程编号  课程名称  平时成绩  实验成绩  卷面成绩  综合成绩  实得学分\n");
            printf("-----------------------------------------------------------------------------\n");
            printf("\n   %-10d%-10s%-10s%-10d%-10d%-10d%-10.2f%-10.2f\n",stu[i].no,stu[i].course[0].courseno,stu[i].course[0].coursename,\
                stu[i].course[0].score[0],stu[i].course[0].score[1],stu[i].course[0].score[2],stu[i].course[0].result[0],\
                stu[i].course[0].result[1]);
            printf("\n   %-10d%-10s%-10s%-10d%-10d%-10d%-10.2f%-10.2f\n",stu[i].no,stu[i].course[1].courseno,stu[i].course[1].coursename,\
                stu[i].course[1].score[0],stu[i].course[1].score[1],stu[i].course[1].score[2],stu[i].course[1].result[0],\
                stu[i].course[1].result[1]);
            getchar();
        }   
        i++;
    }
    if(i>*stu_number)
        printf("没有所找学生信息\n");
    getchar();
}//学生成绩查询函数*/

void DeleteStu(struct student stu[],int *stu_number)
{
    int no,i,j;
    char ch='y';
    while((ch=='y')||(ch=='Y'))
    {
        system("cls");
        printf("    请输入要删除学生的学号:\n\n");
        scanf("%d",&no);
        for(i=0;i<*stu_number;i++)
        {
            if(no==stu[i].no)
            {
                printf("\n   要删除学生的信息如下:\n");
                printf("\n   学号  课程编号  课程名称  平时成绩  实验成绩  卷面成绩  综合成绩  实得学分\n");
                printf("-----------------------------------------------------------------------------\n");
                printf("\n   %-10d%-10s%-10s%-10d%-10d%-10d%-10.2%-10.2f\n",stu[i].no,stu[i].course[0].courseno,stu[i].course[0].coursename,\
                    stu[i].course[0].score[0],stu[i].course[0].score[1],stu[i].course[0].score[2],stu[i].course[0].result[0],\
                    stu[i].course[0].result[1]);
                printf("\n   %-10d%-10s%-10s%-10d%-10d%-10d%-10.2f%-10.2f\n",stu[i].no,stu[i].course[1].courseno,stu[i].course[1].coursename,\
                    stu[i].course[1].score[0],stu[i].course[1].score[1],stu[i].course[1].score[2],stu[i].course[1].result[0],\
                    stu[i].course[1].result[1]);
                break;
            }
        }
        if(i>=*stu_number)
            printf("\n\t 要删除学生的成绩不存在!\n");
        else
        {
            printf("\n\t 确定要删除学生信息么(y/n)?:");
            scanf("\n%c",&ch);
            if(ch=='y'||ch=='Y')
            {
                for(j=i+1;j<=*stu_number;j++)
                    stu[j-1]=stu[j];
                printf("\n\t已删除!");
                (*stu_number)--;
            }
            
        }
        printf("\n\n\t是否继续删除其他学生(y/n)?");
        scanf("\n%c",&ch);
    }
    saveStu(stu,*stu_number,0);
    return ;
}//学生成绩删除函数

void SearchInf()
{
    struct inform inform;
    int x;
    char Name[10],no[10],room[10],ch='y';
    FILE *fp;
SEARCH:
    fp=fopen("student.txt","r");
    if(!fp)
    {
        printf("file cannot be opened.\n");
        exit(1);
    }
    while((ch=='y')||(ch=='Y'))
    {
        system("cls");
        printf("查询方式:\n 1.按学号查询学生基本信息 \n 2.按姓名查询基本信息\n 3.以宿舍为单位查询基本信息\n");
        scanf("%d",&x);
        switch(x)
        {
        case 1:
            printf("输入要查询的学号:\n");
            scanf("%s",no);
            fscanf(fp,"%s%s%s%s%s\n",&inform.id,&inform.name,&inform.sex,&inform.room,&inform.tel);
            while(!feof(fp))
            {
                if(strcmp(inform.id,no)==0)
                {
                    printf("学号  姓名  性别   宿舍号码  电话号码\n");
                    printf("%-5s%-8s%-6s%-8s%-15s\n",inform.id,inform.name,inform.sex,inform.room,inform.tel);
                }
                fscanf(fp,"%s%s%s%s%s\n",&inform.id,&inform.name,&inform.sex,&inform.room,&inform.tel);
            }
            fclose(fp);
            break;
        case 2:printf("输入要查询的姓名:\n");
            scanf("%s",Name);
            fscanf(fp,"%s%s%s%s%s\n",&inform.id,&inform.name,&inform.sex,&inform.room,&inform.tel);
            while(!feof(fp))
            {
                if(strcmp(Name,inform.name)==0)
                {
                    printf("学号  姓名  性别   宿舍号码  电话号码\n");
                    printf("%-5s%-8s%-6s%-8s%-15s\n",inform.id,inform.name,inform.sex,inform.room,inform.tel);
                }
                fscanf(fp,"%s%s%s%s%s\n",&inform.id,&inform.name,&inform.sex,&inform.room,&inform.tel);
            }
            fclose(fp);
            break;
        case 3:
            printf("输入要查询的宿舍号码:\n");
            scanf("%s",room);
            fscanf(fp,"%s%s%s%s%s\n",&inform.id,&inform.name,&inform.sex,&inform.room,&inform.tel);
            while(!feof(fp))
            {
                if(strcmp(room,inform.room)==0)
                {   
                    printf("学号  姓名  性别   宿舍号码  电话号码\n");
                    printf("%-5s%-8s%-6s%-8s%-15s\n",inform.id,inform.name,inform.sex,inform.room,inform.tel);
                }
                fscanf(fp,"%s%s%s%s%s\n",&inform.id,&inform.name,&inform.sex,&inform.room,&inform.tel);
            }
            fclose(fp);
            break;
        }
        printf("\n\t\t是否继续查找(y/n)?:");
        scanf("\n%c",&ch);
        goto SEARCH;
    }
}//学生基本信息查询函数

void DeleteInf()
{
    int i=0,j,k=0;
    char no[10];
    char ch='y';
    struct inform inform[100];
    struct inform inform_0[100];
    FILE *fp;
    fp=fopen("student.txt","r");
    if(!fp)
    {
        printf("文件无法打开!!请检查是否已受到破坏!!\n");
        exit(1);
    }
    while((ch=='y')||(ch=='Y'))
    {
        system("cls");
        printf("    请输入要删除学生的学号:\n\n");
        scanf("%s",no);
        fscanf(fp,"%s%s%s%s%s\n",inform[0].id,inform[0].name,inform[0].sex,inform[0].room,inform[0].tel);
        do
        {
            i++;
            fscanf(fp,"%s%s%s%s%s\n",inform[i].id,inform[i].name,inform[i].sex,inform[i].room,inform[i].tel);
        }while(!feof(fp));
        for(i=0;i<100;i++)
        {
            if(strcmp(inform[i].id,no)==0)
            {   
                printf("需要删除的学生信息如下:\n");
                printf("学号  姓名  性别   宿舍号码  电话号码\n");
                printf("%-5s%-8s%-6s%-8s%-15s\n",inform[i].id,inform[i].name,inform[i].sex,inform[i].room,inform[i].tel);
                printf("确认删除?(y/n)");
                scanf("\n%c",&ch);
                if(ch=='y'||ch=='Y')
                {
                    for(j=i;j<100;j++)
                    {
                        inform[j]=inform[j+1];
                    }
                }
            }
        }
        fp=fopen("student.txt","w+");
        if(!fp)
        {
            printf("文件无法打开!!请检查是否已受到破坏!!\n");
            exit(1);
        }
    for(;k<100;k++)
        {
            fprintf(fp,"%s%s%s%s%s",inform[k].id,inform[k].name,inform[k].sex,inform[k].room,inform[k].tel);
        
        }

    printf("\n\n\t是否继续删除其他学生(y/n)?:");
        scanf("\n%c",&ch);
    }

}//学生基本信息删除函数
void Menu();                                                
void saveStu(struct student stu[],int count,int flag);//保存成绩
void ReadStu(struct student stu[],int *stu_number);//读取成绩
void InputStu(struct student stu[],int *stu_number);//输入成绩      
void BrowseStu(struct student stu[],int *stu_number);//成绩排名      
void SearchStu(struct student stu[],int *stu_number);//查询成绩
void DeleteStu(struct student stu[],int *stu_number);//删除成绩
void SearchInf();                                  //查找基本信息
void DeleteInf();                                  //删除基本信息

void main()
{
    struct student stu[100];
   
    int stu_number=0;
    int choose,flag=1;
    while(flag)
    {
        system("cls");
        Menu();
        printf("\t\t 请按照序号选择相应功能:");
        scanf("%d",&choose);
        switch(choose)
        {
        case 1:InputStu(stu,&stu_number);break;
        case 2:BrowseStu(stu,&stu_number);break;
        case 3:SearchStu(stu,&stu_number);break;
        case 4:DeleteStu(stu,&stu_number);break;
        case 5:SearchInf();break;
        case 6:DeleteInf();break;
        case 0:flag=0;
        }
    }
}


搜索更多相关主题的帖子: 如何 信息 考试成绩 include course 
2011-11-02 08:39
moonchild
Rank: 1
等 级:新手上路
帖 子:12
专家分:0
注 册:2011-10-25
得分:0 
输入学生成绩后无法查询学生正确成绩,但是成绩排名可以正确输出。应该是查询函数SearchStu的问题。求专家指导。
2011-11-02 08:41



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




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

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