4 楼mousemice(荣)回复于 2003-01-03 11:12:59 得分 40分给我吧
程序中有下列功能:
1)建立学生数据结构,学生数据结构有学生姓名,学号,20门成绩
2)输入数据功能模块,完成数据输入,添加。数据用文件保存。
3)查询功能模块,可以按姓名,学号查询学生纪录
4)排序输出模块,可以按姓名,学号,平均成绩输出学生纪录
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<malloc.h>
struct stud{
char name[10];
int num;
float score[21];
struct stud *next;
};
//void save (struct stud *head);
int add () ;
void search_name (void);
void search_num (void) ;
int comp_name ( struct stud *left, struct stud *right );
int comp_num ( struct stud *left, struct stud *right );
int comp_score ( struct stud *left, struct stud *right );
void print_list ( struct stud *head );
void print_file ( FILE *fp ) ;
struct stud *sort( int ( *compare )( struct stud*, struct stud* ) );
struct stud *creat();
int clear ( struct stud *head );
/*-----------------------------------------------------------------*/
/*-----------------------------------------------------------------*/
int clear ( struct stud *head ) {
struct stud *p, *q;
p = head->next;
while ( p ){
q = p->next;
free ( p );
p = q;
}
free ( head );
return 0;
}
/*-----------------------------------------------------------------*/
/*-----------------------------------------------------------------*/
int add ()
{
FILE *fp;
struct stud t;
int i;
char goon = 'y';
float temp, sum = 0.0;
fp = fopen( "output.txt","a" );
if ( !fp ){
printf( "Error!!!" );
return 0;
}
while( goon != 'N' && goon != 'n' ){
printf( "\nEnter the student's name : " );
scanf ( "%s", &t.name );
printf( "\nEnter number: " );
scanf ( "%d", &t.num );
printf( "\nEnter the student's marks: " );
for( i = 0; i < 20; ++i ){
scanf( "%f", &temp );
t.score[i] = temp;
sum += temp;
}
t.score[20] = sum/20;
fwrite( &t, sizeof( struct stud ), 1, fp );
printf( "\nGo on input?(Y/N)?" );
do{
goon = getchar();
}while( goon == '\n' );
}
fclose( fp );
return 1;
}
/*-----------------------------------------------------------------*/
int comp_name ( struct stud *left, struct stud *right )
{
if ( strcmp ( left->next->name, right->name ) < 0 )
return 1;
else return 0;
}
/*-----------------------------------------------------------------*/
int comp_num ( struct stud *left, struct stud *right )
{
return (left->next->num) < (right->num);
}
/*-----------------------------------------------------------------*/
int comp_score ( struct stud *left, struct stud *right )
{
return (left->next->score[20]) < (right->score[20]);
}
/*-----------------------------------------------------------------*/
struct stud *sort( int ( *compare )( struct stud*, struct stud* ) )
{
struct stud *p, *q, *head, temp;
int i, j = -1;
FILE *fp;
fp = fopen( "output.txt", "r" );
if( !fp ){
printf("Error");
return 0;
}
head = ( struct stud * )malloc( sizeof( struct stud ) );
head->next = 0;
while( !feof( fp ) ){
q = ( struct stud * )malloc( sizeof( struct stud ) );
q->next = 0;
fread( &temp, sizeof( struct stud ), 1, fp );
if ( temp.num == j)
break;
strcpy( q->name, temp.name );
j = q->num = temp.num;
for( i = 0; i < 20; ++i )
q->score[i] = temp.score[i];
p = head;
while( p->next && ( ( *compare )( p, q ) ) )
p = p->next;
q->next = p->next;
p->next = q;
}
return head;
}
/*-----------------------------------------------------------------*/
void print_list ( struct stud *head ) /* Print according to LinkList */
{
struct stud *p;
int i;
p = head->next;
if( p )
{
printf( "\nThe Records are:" );
while( p )
{
printf( "\n%s\t%d\n", p->name, p->num );
for( i = 0; i < 20; ++i )
printf( "\t%5.2f", p->score[i] );
p = p->next;
}
}
else printf("All records print!");
}
/*-----------------------------------------------------------------*/
void print_file ( FILE *fp ) /* Print according to File */
{
struct stud p;
int i,t = -1;
printf( "\nThe Records are:" );
while ( !feof( fp ) ){
fread( &p, sizeof( struct stud ), 1, fp );
if ( t == p.num )
continue;
printf( "\n%s\t%d\n" , p.name ,p.num );
for( i = 0; i < 20; ++i )
printf( "\t%5.2f", p.score[i] );
t = p.num;
}
}
/*-----------------------------------------------------------------*/
void search_name (void) /* Search according to name */
{
int find = 0;
FILE *fp;
struct stud p;
int i;
char search_name[10];
fp = fopen( "output.txt", "r" );
if( !fp ){
printf( "Error!!!" );
return;
}
printf( "\nEnter the name of the student you want to search: " );
scanf ( "%s", search_name );
while( !feof( fp ) )
{
fread( &p, sizeof( struct stud ), 1, fp );
if( strcmp( p.name, search_name ) == 0 ){
printf( "\n%s", p.name );
printf( "\t%d\n", p.num );
for(i=0;i< 20;++i)
printf(
没有合适的资源?快使用搜索试试~ 我知道了~
数据结构试验选课系统
共14个文件
txt:2个
pdb:2个
ncb:1个
需积分: 10 11 下载量 68 浏览量
2008-12-25
23:49:18
上传
评论
收藏 241KB RAR 举报
温馨提示
数据结构上机实验,选课系统。用c语言编写的直接就能使用,要是怕到时交作业的时候有一样的,请自己再改一下
资源详情
资源评论
资源推荐
收起资源包目录
.rar (14个子文件)
选课系统
选课系统.ncb 57KB
student_inf.txt 72B
选课系统.opt 48KB
选课系统.plg 758B
选课系统.cpp 14KB
Debug
vc60.pdb 84KB
选课系统.pdb 545KB
选课系统.ilk 240KB
vc60.idb 49KB
选课系统.exe 208KB
选课系统.obj 44KB
选课系统.dsp 3KB
选课系统.txt 109KB
选课系统.dsw 524B
共 14 条
- 1
tiantangyijiu
- 粉丝: 5
- 资源: 44
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0