lalayangh 2022-11-29 13:47 采纳率: 66.7%
浏览 68
已结题

查找处理学生信息问题,含多个文件,显示问题是无法调用其中一个文件

哪位朋友能帮忙看看,为何以下代码一直运行不了?
用的编译器是dev c++

#ifndef _SCORE_H
#define _SCORE_H

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <conio.h>


#define N                10
#define STUDENT_COUNT    50        //学生数
#define LESSON_COUNT    3        //课程数

#define SUM_SORT                0         //按总分排序 
#define MATH_SORT                1        //按数学成绩排序 
#define ENGLISH_SORT            2        //按英语成绩总分排序 
#define COMPUTER_SORT            3        //按计算机成绩排序 
#define SUM_STATISTICS            4        // 统计总分 
#define MATH_STATISTICS            5        // 统计数学成绩
#define ENGLISH_STATISTICS        6        // 统计英语成绩
#define COMPUTER_STATISTICS        7        // 统计计算机成绩
#define SEARCH                    8        // 查找

typedef struct _Student
{
    int     id;                        //学号
    char     name[N];                //姓名
    int     score[LESSON_COUNT];    //成绩
    int     sum;                    //总分
}Student;


#include "ReadData.C"
#include "Menu.C" 

//按课程成绩对学生进行排序
void Sort(Student *stu , int count , int lessonId);

//求所有课程的总分
int Sum(int score[] , int count);

//按学号搜索某一名学生
int Search(Student stu[] , int count, int studentId, Student * student);

//统计某门课程的不及格人数
int Statistics(int score[] , int lessonId);

//显示信息
void ShowInfo(Student stu[] , int count);

//从文件读取数据 
//void ReadData(Student stu[] , int count);

//显示菜单 
//int Menu();
 
#endif

#include <stdio.h>
#include <stdlib.h>

#include "main.h"
void ReadData(Student stu[] , int count)
{
    FILE *fp;
    int i;
    char ch[10];
    if((fp = fopen("Score.dat","r")) == NULL)
    {
        printf("\tCan't open the file: Score.dat.\n\tPress AnyKey to Exit... ");
        getch();
        exit(0);
    }
    
    fscanf(fp,"%s%s%s%s%s",ch,ch,ch,ch,ch);  //跳过第一行
     
    for(i = 0 ; i < count ; i++)
        fscanf(fp,"%d%s%d%d%d",&stu[i].id,stu[i].name,&stu[i].score[0],&stu[i].score[1],&stu[i].score[2]);
}


#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include "main.h"

int main()
{
    Student stu[STUDENT_COUNT];
    int menuitem;
    int i ;

    //清屏
    system("cls");     
    //读入学生信息
    ReadData(stu, STUDENT_COUNT);

    //计算学生总成绩
    // add code here 
    for (i=0;i<STUDENT_COUNT;i++)
        stu[i].sum = Sum(stu,i);

    while(1)
    {
        //显示功能菜单,并获得选择的菜单项
        menuitem = Menu();

        switch(menuitem)
        {
        //按总分排序
        case SUM_SORT:
            {
                // add code here
                Sort(stu, STUDENT_COUNT, SUM_SORT);
                ShowInfo(stu,STUDENT_COUNT);
                system("PAUSE");
                break;
            }

        //按数学成绩排序
        case MATH_SORT:
            {
                // add code here
                Sort(stu,STUDENT_COUNT,MATH_SORT);
                ShowInfo(stu, STUDENT_COUNT);
                system("PAUSE");
                break;
            }

        //按英语成绩排序
        case ENGLISH_SORT:
            {
                // add code here
                Sort(stu,STUDENT_COUNT,ENGLISH_SORT);
                ShowInfo(stu, STUDENT_COUNT);
                system("PAUSE");
                break;
            }

        //按计算机成绩排序
        case COMPUTER_SORT:
            {
                // add code here
                Sort(stu,STUDENT_COUNT,COMPUTER_SORT);
                ShowInfo(stu, STUDENT_COUNT);
                system("PAUSE");
                break;
            }

        //统计数学成绩不及格人数
        case MATH_STATISTICS:
            {
                // add code here
                int array[STUDENT_COUNT];
                for(i=0;i<STUDENT_COUNT;i++)
                array[i]=stu[i].score[0];
                printf("不及格的人数为:%d\n",Statistics(array,STUDENT_COUNT));
            }

        //统计英语成绩不及格人数
        case ENGLISH_STATISTICS:
            {
                // add code here
                int array[STUDENT_COUNT];
                for(i=0;i<STUDENT_COUNT;i++)
                array[i]=stu[i].score[1];
                printf("不及格的人数为:%d\n",Statistics(array,STUDENT_COUNT));
            }

        //统计计算机成绩不及格人数
        case COMPUTER_STATISTICS:
            {
                // add code here
                int array[STUDENT_COUNT];
                for(i=0;i<STUDENT_COUNT;i++)
                array[i]=stu[i].score[2];
                printf("不及格的人数为:%d\n",Statistics(array,STUDENT_COUNT));
            }

        //按学号搜索学生,并显示学生成绩
        case SEARCH:
            {
                // add code here
                int x,student;
                printf("请输入学生学号:\n");
                scanf("%d",&x);
                if(Search(stu,STUDENT_COUNT,x, &student)==1)
                    ShowInfo(&student,1);
                else
                    printf("该学生不存在!\n");
            }

        }
    }
}


/************************************************************************************************/
//Sort function
//功能:排序
//参数:Student stu[]
//        int count:要排序的学生数目
//        int lessonId:要排序的课程编号
//                数学:MATH_SORT; 英语:ENGLISH_SORT;计算机:COMPUTER_SORT;总分:SUM_SORT
/************************************************************************************************/
void Sort(Student stu[] , int count , int lessonId)
{
    // add code here
    int i,j;
    
    if(lessonId==MATH_SORT)
    {
        for(i=0;i<count;i++)
        {
            for(j=0;j<count-i-1;j++)
            if(stu[j].score[lessonId - 1] < stu[j+1].score[lessonId - 1])
                {Student t;t=stu[j].score[lessonId - 1];stu[j].score[lessonId - 1]=stu[j+1].score[lessonId - 1];stu[j+1].score[lessonId - 1]=t;}
        }
        
    }
    
    if(lessonId==ENGLISH_SORT)
    {
        for(i=0;i<count;i++)
        {
            for(j=0;j<count-i-1;j++)
            if(stu[j].score[lessonId - 1] < stu[j+1].score[lessonId - 1])
                {Student t;t=stu[j].score[lessonId - 1];stu[j].score[lessonId - 1]=stu[j+1].score[lessonId - 1];stu[j+1].score[lessonId - 1]=t;}
        }
        
    }
    
    if(lessonId==COMPUTER_SORT)
    {
        for(i=0;i<count;i++)
        {
            for(j=0;j<count-i-1;j++)
            if(stu[j].score[lessonId - 1] < stu[j+1].score[lessonId - 1])
                {Student t;t=stu[j].score[lessonId - 1];stu[j].score[lessonId - 1]=stu[j+1].score[lessonId - 1];stu[j+1].score[lessonId - 1]=t;}
        }
        
    }
    
    if(lessonId==SUM_SORT)
    {
        for(i=0;i<count;i++)
        {
            for(j=0;j<count-i-1;j++)
            if(stu[j].sum < stu[j+1].sum)
                {Student t;t=stu[j].sum;stu[j].sum=stu[j+1].sum;stu[j+1].sum=t;}
        }
        
    }
}

/************************************************************************************************/
//功能:按学号搜索学生
//参数:Student stu[]:学生 
//        int count:学生数目
//        int studentId:要搜索的学号
//        Student * student: 搜索到的学生
//返回值:如果搜索到的学生返回1,否则返回0
/************************************************************************************************/
int Search(Student stu[] , int count, int studentId, Student * student)
{
    // add code here
    int i,flag=0;
    for(i=0;i<count;i++)
    {
        if(stu[i].id==studentId)
        {
            flag=1;
            *student=stu[i].id;
        }
    }
    
    return flag;
}

/************************************************************************************************/
//功能:计算所有课程的总分
//参数:Student stu[]
//        int count:学生数目
//返回值:总分
/************************************************************************************************/
int Sum(Student stu[] , int count)
{
    // add code here
    int sum = 0;
    sum = stu[count].score[0] + stu[count].score[1] + stu[count].score[2];

    return sum;
}


/************************************************************************************************/
//功能:统计不及格人数
//参数:int score[]:学生所有的课程成绩
//        int n: 学生人数 
//返回值:不及格人数
/************************************************************************************************/
int Statistics(int score[] , int n)
{
    // add code here
    int i,count=0;
    
    for(i=0;i<n;i++)
    if(score[i]<60) count++;
    
    return count;
}

/************************************************************************************************/
//功能:显示学生信息
//参数:Student stu[]
//        int count:学生数目
//返回值:无
/************************************************************************************************/
void ShowInfo(Student stu[] , int count)
{
    // add code here
    int i;
    for (i=0;i<count;i++)
    {
        printf("学号:%d 姓名:%s    数学:%d    英语:%d    计算机:%d 总分:%d \n",
                stu[i].id, stu[i].name, stu[i].score[0], stu[i].score[1], stu[i].score[2], stu[i].sum);
    }
}

补充错误图片

img

  • 写回答

9条回答 默认 最新

  • cyjbj 2022-11-29 21:29
    关注

    看你的信息应该是出在结构体student的指针上了,结构体类型和int之间是不能相互赋值或转换的,但是结构体指针和int之间是可以的

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

报告相同问题?

问题事件

  • 系统已结题 12月9日
  • 已采纳回答 12月1日
  • 修改了问题 11月29日
  • 创建了问题 11月29日

悬赏问题

  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器