第2题
#include <stdio.h>
#include <string.h>
#define MAXNUM 35 /* 最大记录数*/
#define MAXCLASS 3 /* 课程数目 */
struct Student
{
char id[16];
char name[64];
int score[MAXCLASS];
float avg;
int sum;
};
void EnterData(void);
void PrepareData(void);
void DisplayData(void);
void SortData(void);
void BubbleSort(int which);
void SearchData(void);
int SearchByID(char *p);
void InsertData(void);
void InsertRecord(struct Student value);
void DeleteData(void);
void DelRecord(char *p);
void StatisticsData(void);
struct Student stu[MAXNUM]; /*结构体数组*/
int nCurrent = 0; /*当前的记录数目*/
void main()
{
int chose;
menu:
printf ("主菜单\n");
printf ("=================\n");
printf ("1,数据输入\n");
printf ("2,数据输出\n");
printf ("3,数据排序(按平均分)\n");
printf ("4,数据查找(按学号)\n");
printf ("5,数据插入\n");
printf ("6,数据删除\n");
printf ("7,数据统计(按平均分)\n");
printf ("0,退出\n");
printf ("=================\n");
printf ("请选择(0--7)\n");
scanf ("%d", &chose );
do
{
switch ( chose )
{
case 0:
return;
case 1:
EnterData();
break;
case 2:
DisplayData();
break;
case 3:
SortData();
break;
case 4:
SearchData();
break;
case 5:
InsertData();
break;
case 6:
DeleteData();
break;
case 7:
StatisticsData();
break;
}
goto menu;
}while (1);
}
void StatisticsData(void)
{
BubbleSort(6);/* 按总分排序*/
int avg;
int each[5];
int i;
for ( i=0; i<5; i++ )
each[i] = 0;
for ( i=0; i<nCurrent; i++ )
{
avg = int(stu[i].avg);
switch ( avg / 10 )
{
case 10:
case 9: /* 90-100 */
each[4] ++;
break;
case 8: /* 80-89 */
each[3] ++;
break;
case 7: /* 70-79 */
each[2] ++;
break;
case 6: /* 60-69 */
each[1] ++;
break;
default: /* 0-59 */
each[0] ++;
}
}
printf (" 按平均分统计各分数段学生人数 \n ");
printf ("=========================================== \n " );
printf ("分数段 0-59 60-69 70-79 80-89 90-100 \n" );
printf ("人数:");
for ( i=0; i<5; i++ )
printf ("%8d",each[i] );
printf ("\n ======================================== \n " );
}
void DeleteData(void)
{
char id[16];
char delnext;
printf ("====== 删除数据=====\n");
do
{
printf ("请输入学号:\n");
scanf("%s",id);
DelRecord(id);
getchar();
printf ("是否继续删除?(y/n):y)");
scanf ("%c",&delnext );
if ( delnext == 'n' || delnext == 'N' )
break;
else
continue;
}
while ( nCurrent > 0 );
}
void DelRecord(char *p)
{
int index = SearchByID(p);
if ( index == - 1 )
{
printf ("未找到要删除学号!");
return;
}
int i;
for ( i=index; i<nCurrent-1; i++ )
stu[i] = stu[i+1];
nCurrent--;
printf ("删除学号为:%s 的数据成功!\n",p);
}
void InsertData(void)
{
struct Student temp;
char insertnext;
printf ("====插入数据=====\n");
do
{
if ( nCurrent == MAXNUM )
{
printf (" full ! ");
return;
}
printf ("学号=");
scanf ("%s",temp.id);
printf ("姓名=");
scanf ("%s",temp.name );
printf ("英语=");
scanf ("%d",&temp.score[0] );
printf ("数学=");
scanf ("%d",&temp.score[1] );
printf ("计算机=");
scanf ("%d",&temp.score[2] );
InsertRecord(temp);
getchar();
printf ("是否继续插入?(y/n):y)");
scanf ("%c",&insertnext );
if ( insertnext == 'n' || insertnext == 'N' )
break;
else
continue;
}
while ( 1 );
}
void InsertRecord(struct Student value)
{
int right = nCurrent - 1;
int i;
value.sum = 0;
value.avg = 0;
BubbleSort(6); /* 按总分排序*/
for ( i=0; i<MAXCLASS; i++ )
value.sum += value.score[i];
value.avg = (float) (value.sum / MAXCLASS );
while ( value.sum < stu[align=right].sum && right >= 0 ) // from last move the next
{
stu[right+1] = stu[align=right];
right -- ;
}
stu[right+1] = value;
nCurrent ++ ;
printf ("插入数据成功!\n");
}
void SearchData()
{
char id[16];
char searchnext;
int index;
printf ("====查找数据===\n");
do
{
printf ("请输入学号=");
scanf ("%s",id);
index = SearchByID(id);
if ( index != - 1 )
{
printf (" 找到了:数组下标为: %d \n ", index );
printf (" 学号 =%s \n ", stu[index].id );
printf (" 姓名 = %s \n ", stu[index].name );
printf (" 英语 = %d \n ", stu[index].score[0] );
printf (" 数学 = %d \n ", stu[index].score[1] );
printf (" 计算机= %d \n ", stu[index].score[2] );
printf (" 总分 = %4.1f \n ", stu[index].avg );
printf (" 平均分= %d \n ", stu[index].sum );
}
else
printf (" 没有找到改学号的学生数据! \n " );
getchar();
printf ( "是否继续查找?(y/n)\n" );
scanf ("%c",&searchnext );
if ( searchnext == 'n' || searchnext == 'N' )
break;
else
continue;
}
while ( 1 );
}
int SearchByID(char *p) /* -1:未找到; 0..nCurrent-1: 找到.其值为数组下标 */
{
int left = 0;
int right = nCurrent - 1;
int mid;
int find = -1;
int result;
do
{
mid = ( left + right ) / 2;
result = strcmp ( p, stu[mid].id );
if ( result == 0 )
find = mid;
else if ( result < 0 )
right = mid - 1;
else if ( result > 0 )
left = mid + 1;
}
while ( ( left <= right ) && ( find == -1 ) );
return find;
}
void SortData(void)
{
PrepareData();
BubbleSort(6);
}
void BubbleSort(int which) /* which是排序字段,1:学号 2:姓名 3:英语 4:数学 5:计算机 6:总分 */
{
struct Student temp;
int result;
for ( int i=0; i<nCurrent-1; i++ )
for ( int j=i+1; j<nCurrent; j++ )
{
switch ( which )
{
case 1:
result = strcmp ( stu[i].id , stu[j].id );
break;
case 2:
result = strcmp ( stu[i].name, stu[j].name );
break;
case 3:
result = stu[i].score[0] - stu[j].score[0];
break;
case 4:
result = stu[i].score[1] - stu[j].score[1];
break;
case 5:
result = stu[i].score[2] - stu[j].score[2];
break;
case 6:
result = stu[i].sum - stu[j].sum;
break;
}
if ( result > 0 )
{
temp = stu[i];
stu[i] = stu[j];
stu[j] = temp;
}
}
}
void PrepareData(void)
{
int i;
for ( i=0; i<nCurrent; i++ )
{
stu[i].sum = 0;
stu[i].avg = 0;
for ( int j=0; j<MAXCLASS; j++ )
stu[i].sum += stu[i].score[j];
stu[i].avg = (float) (stu[i].sum / MAXCLASS);
}
}
void DisplayData(void)
{
printf ("===========================================================\n");
printf ("学号 姓名 英语 数学 计算机 总成绩 平均成绩\n");
for ( int num=0; num<nCurrent; num++ )
printf ("%s %s %d %d %d %d %4.1f\n",stu[num].id, stu[num].name,
stu[num].score[0], stu[num].score[1], stu[num].score[2], stu[num].sum,stu[num].avg);
printf ("===========================================================\n");
}
void EnterData(void)
{
char inputnext ;
printf ("=========输入数据=======\n");
while ( nCurrent < 35 )
{
printf ("==已经输入的数据条数= %d======\n",nCurrent);
printf ("学号=");
scanf ("%s",stu[nCurrent].id);
printf ("姓名=");
scanf ("%s",stu[nCurrent].name );
printf ("英语=");
scanf ("%d",&stu[nCurrent].score[0] );
printf ("数学=");
scanf ("%d",&stu[nCurrent].score[1] );
printf ("计算机=");
scanf ("%d",&stu[nCurrent].score[2] );
nCurrent ++;
getchar();
printf ("是否输入下一条数据?(y/n):y)");
scanf ("%c",&inputnext );
if ( inputnext == 'n' || inputnext == 'N' )
break;
else
continue;
}
PrepareData();
}
给你个可以参考的。。。