| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 712 人关注过本帖
标题:程序有个错误我不知道怎么修改 请高手帮帮忙指点指点
只看楼主 加入收藏
hhmmark
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2008-6-28
收藏
 问题点数:0 回复次数:1 
程序有个错误我不知道怎么修改 请高手帮帮忙指点指点
/***************Score.h******************/

#ifndef _SCORE_H
#define _SCORE_H

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

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

#define SUM_SORT 9
#define MATH_SORT 0
#define ENGLISH_SORT 1
#define COMPUTER_SORT 2
#define SUM_STATISTICS 4
#define MATH_STATISTICS 5
#define ENGLISH_STATISTICS 6
#define COMPUTER_STATISTICS 7
#define SEARCH 8

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

void swap(Student *stu1, Student *stu2);
int cmp(char a[N], char b[N]);
void Sort(Student stu[] , int count , int lessonId);
int Search(Student stu[] , int count, char studentId[10], Student * student);
int Sum(int score[] , int count);
int Menu();
void ReadData(Student stu[] , int count);

#endif

/****************Main.c*********************/

#include "Score.h"

void main()
{
Student stu[STUDENT_COUNT];
Student s;
int menuitem;
int i ;
int k = 0;
char n[10]={'\0'};

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

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

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

switch(menuitem)
{
//按总分排序
case SUM_SORT:
{
Sort(stu, STUDENT_COUNT, SUM_SORT);
printf ("Id\t\tName\tMath\tEnglish\tComputer Sum\n");
for (i=0; i<STUDENT_COUNT; i++)
printf ("%s\t%s\t%d\t%d\t%d\t %d\n",stu[i].id,stu[i].name ,stu[i].score[0],stu[i].score[1],stu[i].score[2],stu[i].sum);
getchar();
break;
}

//按数学成绩排序
case MATH_SORT:
{
Sort(stu, STUDENT_COUNT, MATH_SORT);
printf ("Id\t\tName\tMath\tEnglish\tComputer Sum\n");
for (i=0; i<STUDENT_COUNT; i++)
printf ("%s\t%s\t%d\t%d\t%d\t %d\n",stu[i].id,stu[i].name ,stu[i].score[0],stu[i].score[1],stu[i].score[2],stu[i].sum);
getchar();
break;
break;
}

//按英语成绩排序
case ENGLISH_SORT:
{
Sort(stu, STUDENT_COUNT, ENGLISH_SORT);
printf ("Id\t\tName\tMath\tEnglish\tComputer Sum\n");
for (i=0; i<STUDENT_COUNT; i++)
printf ("%s\t%s\t%d\t%d\t%d\t %d\n",stu[i].id,stu[i].name ,stu[i].score[0],stu[i].score[1],stu[i].score[2],stu[i].sum);
getchar();
break;
break;
}

//按计算机成绩排序
case COMPUTER_SORT:
{
Sort(stu, STUDENT_COUNT, COMPUTER_SORT);
printf ("Id\t\tName\tMath\tEnglish\tComputer Sum\n");
for (i=0; i<STUDENT_COUNT; i++)
printf ("%s\t%s\t%d\t%d\t%d\t %d\n",stu[i].id,stu[i].name ,stu[i].score[0],stu[i].score[1],stu[i].score[2],stu[i].sum);
getchar();
break;
break;
}

//统计数学成绩不及格人数
case MATH_STATISTICS:
{
k=0;
Sort(stu, STUDENT_COUNT, MATH_SORT);
for (i=0; i<STUDENT_COUNT; i++)
if (stu[i].score[0]<60)
break;
for (;i<STUDENT_COUNT; i++)
printf ("%d\t%s\t%s\t%d\n",k++,stu[i].id,stu[i].name,stu[i].score[0]);
getchar();
break;
}

//按英语成绩统计
case ENGLISH_STATISTICS:
{
k=0;
Sort(stu, STUDENT_COUNT, ENGLISH_SORT);
for (i=0; i<STUDENT_COUNT; i++)
if (stu[i].score[1]<60)
break;
for (;i<STUDENT_COUNT; i++)
printf ("%d\t%s\t%s\t%d\n",k++,stu[i].id ,stu[i].name,stu[i].score[1]);
getchar();
break;
}

//按计算机成绩统计
case COMPUTER_STATISTICS:
{
k=0;
Sort(stu, STUDENT_COUNT, COMPUTER_SORT);
for (i=0; i<STUDENT_COUNT; i++)
if (stu[i].score[2]<60)
break;
for (;i<STUDENT_COUNT; i++)
printf ("%d\t%s\t%s\t%d\n",k++,stu[i].id,stu[i].name,stu[i].score[2]);
getchar();
break;
}

//按学号搜索学生,并显示学生成绩
case SEARCH:
{
printf ("Please enter an ID:");
scanf ("%s",n);
if (Search(stu,STUDENT_COUNT,n,&s))
{
printf ("Id\t Name\tMath\tEnglish\tComputer Sum\n");
printf ("%s %s\t%d\t%d\t%d\t %d",s.id,s.name,s.score [0],s.score [1],s.score [2],s.sum);
getchar();
}
else
{
printf ("No such student! %s\n",n);
getchar();
}
break;
}

}
printf("\nPress AnyKey to Continue... ");
getchar();
}
}


/*****************Menu.c******************/

#include "Score.h"

//显示功能菜单,返回选择的菜单项
int Menu()
{
int menuitem ;
int item;
while(1)
{
menuitem = -1;
item = -1;
while ( menuitem != 0 && menuitem != 1 && menuitem != 2 && menuitem != 3 )
{
system("cls");
printf("\n\t\t Main Menu\n") ;
printf("\n\t\t1. Sort\n");
printf("\t\t2. Statistics\n");
printf("\t\t3. Search\n");
printf("\t\t0. Exit\n\n");
printf("\tPlease choose an item(0-3):");
scanf("%d",&menuitem);
if (menuitem == 0 || menuitem == -1) exit(0);
}
switch(menuitem)
{
case 1:
{
while(item != 0 && item != 1 && item != 2 && item != 3 && item != 4)
{
system("cls");
printf("\n\t\t Sort Menu\n");
printf("\n\t\t1. Sort by Math score\n");
printf("\t\t2. Sort by English score\n");
printf("\t\t3. Sort by Computer score\n");
printf("\t\t4. Sort by Sum score\n");
printf("\t\t0. return\n\n");
printf("\tPlease choose an item(0-4):");
scanf("%d",&item);
if (item == 0 || item == -1) break;
}

switch(item)
{
case 1:
return MATH_SORT;
case 2:
return ENGLISH_SORT;
case 3:
return COMPUTER_SORT;
case 4:
return SUM_SORT;
}
break;
}
case 2:
{
while(item != 0 && item != 1 && item != 2 && item != 3 && item != 4)
{
system("cls");
printf("\n\t\t Statitics Menu\n");
printf("\n\t\t1. Statitics by Math score\n");
printf("\t\t2. Statitics by English score\n");
printf("\t\t3. Statitics by Computer score\n");
printf("\t\t0. return\n\n");
printf("\tPlease choose an item(0-4):");
scanf("%d",&item);
if (item == 0) break;
}

switch(item)
{
case 1:
return MATH_STATISTICS;
case 2:
return ENGLISH_STATISTICS;
case 3:
return COMPUTER_STATISTICS;
case 4:
return SUM_STATISTICS;
}
break;
}
case 3:
{
return SEARCH;
}
}
}
}

/**************ReadData.c******************/

#include "Score.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... ");
getchar();
exit(0);
}
fscanf(fp,"%s%s%s%s%s",ch,ch,ch,ch,ch);
for(i = 0 ; i < count ; i++)
fscanf(fp,"%s %s%d%d%d",stu[i].id,stu[i].name,&stu[i].score[0],&stu[i].score[1],&stu[i].score[2]);
}

/***************code.c****************/

#include "Score.h"
void swap(Student *stu1, Student *stu2)
{
int temp;
char s[N];

strcpy(s,(*stu1).id);
strcpy((*stu1).id , (*stu2).id);
strcpy((*stu2).id,s);

strcpy(s,(*stu1).name );
strcpy((*stu1).name,(*stu2).name );
strcpy((*stu2).name, s);

temp = (*stu1).score [0];
(*stu1).score [0] = (*stu2).score[0];
(*stu2).score[0] = temp;

temp = (*stu1).score [1];
(*stu1).score [1] = (*stu2).score[1];
(*stu2).score[1] = temp;

temp = (*stu1).score [2];
(*stu1).score [2] = (*stu2).score[2];
(*stu2).score[2] = temp;

temp = (*stu1).sum ;
(*stu1).sum = (*stu2).sum;
(*stu2).sum = temp;
}
int cmp(char a[N], char b[N])
{
int i = 0;
for (i=0; i<9; i++)
if (a[i]!=b[i])
return 0;
return 1;
}


/************************************************************************************************/
//Sort function
//功能:排序
//参数:Student stu[]
// int count:要排序的学生数目
// int lessonId:要排序的课程编号
// 数学:MATH_SORT; 英语:ENGLISH_SORT;计算机:COMPUTER_SORT;总分:SUM_SORT
/************************************************************************************************/
void Sort(Student stu[] , int count , int lessonId)
{
int i,k;

if (lessonId == SUM_SORT)//sort by sum
{
for (i=0; i<count-1; i++)
for (k = i+1; k<count; k++)
{
if (stu[i].sum < stu[k].sum )
{
swap(&stu[i],&stu[k]);//swap
}
}
}

else
{
for (i=0; i<count-1; i++)
for (k = i+1; k<count; k++)
{
if (stu[i].score[lessonId] < stu[k].score[lessonId] )
{
swap(&stu[i],&stu[k]);//sweet
}
}
}
}

/************************************************************************************************/
//功能:按学号搜索学生
//参数:Student stu[]
// int count:学生数目
// int lessonId:要搜索的学号
// Student * student: 搜索到的学生
//返回值:如果搜索到的学生返回1,否则返回0
/************************************************************************************************/
int Search(Student stu[] , int count, char studentId[10], Student * student)
{
int i;
int state = 0;

for (i=0; i<STUDENT_COUNT; i++)
if (cmp(stu[i].id,studentId))
{
strcpy((*student).id,stu[i].id);
strcpy((*student).name,stu[i].name );
(*student).score[0] = stu[i].score [0];
(*student).score[1] = stu[i].score [1];
(*student).score[2] = stu[i].score [2];
(*student).sum = stu[i].sum;
return 1;
}
return 0;
}

/************************************************************************************************/
//功能:计算所有课程的总分
//参数:Student stu[]
// int count:学科数目
//返回值:总分
/************************************************************************************************/
int Sum(int score[] , int count)
{
int sum = 0;
while (count >0)
sum += score[--count];
return sum;
}
搜索更多相关主题的帖子: define STATISTICS SORT include MATH 
2008-06-28 19:41
flyue
Rank: 10Rank: 10Rank: 10
来 自:江南西道
等 级:贵宾
威 望:19
帖 子:3465
专家分:1563
注 册:2006-6-20
收藏
得分:0 
抄到的代码吧?

天之道,损有余而补不足.人之道则不然,损不足以奉有余.孰能有余以奉天下,唯有道者.
2008-06-28 19:53
快速回复:程序有个错误我不知道怎么修改 请高手帮帮忙指点指点
数据加载中...
 
   



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

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