C语言编程高手请进

(二) 根据条件进行学生成绩排名
在函数中进行 10个学生成绩从高到低排名, 再改进函数, 进行 n个学生成绩从高到低排名, 排名方式根据函数的style参数进行,如style为‘a'按升序排,style为 ' d ' 按降序排。 ( a:ascending 升,d:descending 降)
编写多个不同函数,使之能实现不同的排序算法(3种以上),再编写一个通用输出函数,(要求用函数指针做参数)能分别打印不同排序算法的结果。
步骤、方法与要求
1. 在函数中进行10个学生成绩从高到低排名 sort(int a[10])
2. 改进第一步的函数为sort(int a[],int n),进行n个学生成绩从高到低排名,
3. 改进第二步的函数为sort(int a[],int n, char style), 将n个学生成绩从高到低排名,排名方式根据sort()函数的style参数进行,如style为‘a’按升序排,style为’d’按降序排。
(a:ascending 升,d:descending 降)
4. 编写4个排序函数,实现4种不同的排序算法(用冒泡法进行升序排序,用冒泡法进行降序排序,选择法进行升序排序,选择法进行降序排序等),函数返回指向排好序的数组的指针。
5. 编写通用输出函数 show(int *(* fun)(a[ ], int n )),其中a[]为成绩数组,n为数组元素个数。
6. 编写主函数调用以上函数。

(三)链表的操作(链表的建立,访问,删除链表指定结点,增加结点)
建立一个动态链表,将学生数据(学号,成绩)存入链表结构中,实现链表的访问(求学生成绩的平均分,找到最高分,最低分,将其数据输出)。删除指定学号的学生数据,对学生数据进行排序,分别在链表头部,中间,末尾插入学生数据。
(四) 学生成绩文件管理
定义一个结构体数组,存放10个学生的学号,姓名,三门课的成绩,输出单门课成绩最高的学生的学号、姓名、以及该门课程的成绩,输出三门课程的平均分数最高的学生的学号、姓名及其平均分,将10个学生的相关数据,存入文件中,再从文件中读出,按照平均分数从高到低进行排序,分别将结果输出到屏幕上和另一文件中,再从文件中读取第 1 ,3 , 5 ,7, 9个学生的数据。
内容、步骤、方法与要求
1. 定义一个结构体数组,存放10个学生的学号,姓名,三门课的成绩
2. 从键盘输入10个学生的以上内容
3. 输出单门课成绩最高的学生的学号、姓名、以及该门课程的成绩
4. 输出三门课程的平均分数最高的学生的学号、姓名及其平均分
5. 从键盘输入10个学生的以上内容,存入文件stud.dat,关闭文件
6. 打开stud.dat文件,将数据读出,查看是否正确写入,关闭文件。
7. 打开文件stud.dat文件,读出数据,将10个学生按照平均分数从高到低进行排序,
分别将结果输出到屏幕上和另一文件studsort.dat中。
8. 从studsort.dat 文件中读取第1,3,5,7,9个学生的数据。
请尽量依照每一个步骤帮我写出那3个程序~谢谢了

第1个回答  推荐于2016-07-23
这个是我以前编的,有些小错误,你参考一下:

#include"stdio.h"
#include"stdlib.h"
#include"malloc.h"
#include"string.h"

typedef struct stud
{
long int number;
char name[20];
int grade;
char other[30];
char sex[10];
struct stud *next;
}T;

T *creat()
{
T *p,*q,*head;
long int number;
int grade;
char name[20],other[30],sex[10];
head=(T *)malloc(sizeof(T));
q=head;

printf("Please input the data:(num,name,sex,grade,other,0:end)\n");
scanf("%ld",&number);fflush(stdin);
if(number)
{
gets(name);
gets(sex);
scanf("%d",&grade);fflush(stdin);
gets(other);
}

while(number)
{
p=(T *)malloc(sizeof(T));
q->number=number;
strcpy(q->name,name);
strcpy(q->sex,sex);
q->grade=grade;
strcpy(q->other,other);
q->next=p;
q=p;

scanf("%ld",&number);fflush(stdin);
if(number)
{
gets(name);
gets(sex);
scanf("%d",&grade);fflush(stdin);
gets(other);
}
}
q->next=NULL;

fflush(stdin);
return head;
}

void print(T *head)
{
T *p;

p=head;
if(p)
printf("Number name \tsex grade other\n");
while(p->next)
{
printf("%08ld %-15s%-6s %-8d %s\n",p->number,p->name,p->sex,p->grade,p->other);
p=p->next;
}
printf("\nEND!\n");
}

void fprint(T *head,FILE *fp)
{
T *p;

p=head;
if(p)
fprintf(fp,"Number name \tsex grade other\n");
while(p->next)
{
fprintf(fp,"%08ld %-15s%-6s %-8d %s\n",p->number,p->name,p->sex,p->grade,p->other);
p=p->next;
}
fprintf(fp,"\nEND!\n");
}

T *range(T *head)
{
T *p,*q,*s;
int key;

printf("Please input the key you want to range:\n1.number,2.name,3.sex,4.grade,5.other.\nYour choice:[ ]\b\b");
scanf("%d",&key);fflush(stdin);

s=q=head;
p=q->next;

switch(key)
{
case 1:
while(p)
{
if(s->number>p->number) s=p;
p=p->next;
}

if(s!=head)
{
s->next=head;
head=s;
}

for(s=q=head->next;s;s=q=s->next)
{
for(p=q->next;p;p=p->next)
if(s->grade>p->grade) s=p;

if(s!=q)
{
s->next=q;
q=s;
}
}

break;

case 2:
while(p)
{
if(strcmp(s->name,p->name)>0) s=p;
p=p->next;
}

if(s!=head)
{
s->next=head;
head=s;
}

for(s=q=head->next;s;s=q=s->next)
{
for(p=q->next;p;p=p->next)
if(s->grade>p->grade) s=p;

if(s!=q)
{
s->next=q;
q=s;
}
}

break;

case 3:

while(p)
{
if(strcmp(s->sex,p->sex)>0) s=p;
p=p->next;
}

if(s!=head)
{
s->next=head;
head=s;
}

for(s=q=head->next;s;s=q=s->next)
{
for(p=q->next;p;p=p->next)
if(s->grade>p->grade) s=p;

if(s!=q)
{
s->next=q;
q=s;
}
}

break;

case 4:
while(p)
{
if(s->grade>p->grade) s=p;
p=p->next;
}

if(s!=head)
{
s->next=head;
head=s;
}

for(s=q=head->next;s;s=q=s->next)
{
for(p=q->next;p;p=p->next)
if(s->grade>p->grade) s=p;

if(s!=q)
{
s->next=q;
q=s;
}
}

break;

case 5:
while(p)
{
if(strcmp(s->other,p->other)>0) s=p;
p=p->next;
}

if(s!=head)
{
s->next=head;
head=s;
}

for(s=q=head->next;s;s=q=s->next)
{
for(p=q->next;p;p=p->next)
if(s->grade>p->grade) s=p;

if(s!=q)
{
s->next=q;
q=s;
}
}

break;
}

return head;
}

T *range_1(T *head)
{
T *p,*q,*s;

s=q=head;
p=q->next;
while(p)
{
if(s->number>p->number) s=p;
p=p->next;
}

if(s!=head)
{
s->next=head;
head=s;
}

for(s=q=head->next;s;s=q=s->next)
{
for(p=q->next;p;p=p->next)
if(s->grade>p->grade) s=p;

if(s!=q)
{
s->next=q;
q=s;
}
}

return head;
}

T *insert(T *head)
{
T *p,*q,*s;

s=(T *)malloc(sizeof(T));
printf("Please input the insert data:(num,name,sex,grade,other)\n");
scanf("%ld",&s->number);fflush(stdin);
if(s->number==0)
{
printf("ERROR!\nPlease input number again.\n");
scanf("%ld",&s->number);fflush(stdin);
}
gets(s->name);
gets(s->sex);
scanf("%d",&s->grade);fflush(stdin);
gets(s->other);

q=head;
p=q->next;

if(q->number>s->number)
{
s->next=head;
head=s;
}

while(q->number<s->number&&p)
{
q=p;
p=q->next;
}

/*if(!q)
{
q->next=s;
s->next=NULL;
}
else
{
s->next=p;
q->next=s;
}*/

s->next=p;
q->next=s;

return head;
}

T *del(T *head)
{
T *p,*q;
long int x;
int key=2;

print(head);
printf("Please input the data you want to delete:(num)\n");
scanf("%ld",&x);fflush(stdin);
for(;key==2;)
{
printf("Are you sure?\n1.OK.\n2.Input new.\n3.Exit delete.\nYour choice:[ ]\b\b");
scanf("%d",&key);fflush(stdin);
if(key==3) return head;
if(key==2) scanf("%ld",&x);
}
q=head;
p=q->next;
if(q->number==x)
{head=p;return(head);}
while(q&&p->number!=x)
{
q=p;
p=q->next;
}

if(q)
{
q->next=p->next;
free(p);
}

else printf("NOT Found!!!\n");

return head;
}

void prt()
{
printf(" welcome to use!!! \t\t========================\n");
printf(" \t\t* Linklist Student *\n");
printf(" \t\t* by czj *\n");
printf(" \t\t* 2008 10 4 *\n");
printf(" \t\t========================\n");
}

void fprt(FILE *p)
{
fprintf(p," welcome to use!!! \t\t========================\n");
fprintf(p," \t\t* Linklist Student *\n");
fprintf(p," \t\t* by czj *\n");
fprintf(p," \t\t* 2008 10 4 *\n");
fprintf(p," \t\t========================\n");
}

void main()
{
T *head=NULL;
int key=1;
FILE *p;

prt();
head=creat();
print(head);

printf("1.INSERT.\n2.DLETE.\n3.CORRECT.\n4.RANGE.\n0.EXIT.\nYour choice[ ]\b\b");
scanf("%d",&key);fflush(stdin);
for(;key;)
{
system("cls");

prt();
if(key==1) insert(head);
if(key==2||key==3) del(head);
if(key==3) insert(head);
if(key==4) range(head);
print(head);
printf("1.INSERT.\n2.DELETE.\n3.CORRECT.\n4.RANGE.\n0.EXIT.\nYour choice[ ]\b\b");
scanf("%d",&key);fflush(stdin);
}

if((p=fopen("d:\\linklist1.dat","w"))==NULL)
{
printf("Can't open the file.\n");
exit(0);
}

fprt(p);
fprint(head,p);

fclose(p);
}

/*
最后一个节点不用。
fflush(stdin);清除缓存。
insert中输入有问题,range。
*/本回答被提问者采纳
相似回答