用鏈表給學生成績排序
Ⅰ 建立2個班學生成績的無需鏈表,其中每個班包含10個節點數據,將每個班的成績鏈表按成績高低排序後輸出。將
這個是我自己做的,你是想修改改你的還是。。?
#include<iostream>
#include<cstring>
using namespace std;
struct student
{
long num;
int score;
char name[10];
student *next;
};
student *CreatClass(int n) //創建班級
{
student *head,*p1,*p2;
head=p1=p2=NULL;
for(int i=0;i<n;i++)
{
p1=new student;
if(i==0)
{
head=p1;
cout<<"姓名:";cin>>p1->name;
cout<<"學號:";cin>>p1->num;
cout<<"分數:";cin>>p1->score;
}
else
{
p2->next=p1;
cout<<"姓名:";cin>>p1->name;
cout<<"學號:";cin>>p1->num;
while(p2->num==p1->num){cout<<"學號重復!請重新輸入:"<<endl;
cout<<"學號:";cin>>p1->num;}
cout<<"分數:";cin>>p1->score;
}
p2=p1;
}
p1->next=NULL;
return head;
}
student *SortScore(student *head,int n) //班級成績排序
{
student *p1=head,*p2;
for(int i=1;i<n;i++)
{
p1=head;
for(int j=0;j<n-i;j++)
{
p2=p1;
p1=p1->next;
if(p2->score<p1->score)
{
int temp1=p2->score;p2->score=p1->score;p1->score=temp1;
long temp2=p2->num;p2->num=p1->num;p1->num=temp2;
char temp3[10];strcpy(temp3,p2->name);strcpy(p2->name,p1->name);strcpy(p1->name,temp3);
}
}
}
return head;
}
student *CombineClass(student *head1,student *head2) //合並班級
{
student *p1=head1,*p2=head2;
cout<<"兩班合並成績排序輸出:"<<endl;
while(p1->next!=NULL)
{
p1=p1->next;
}
p1->next=p2;
return head1;
}
void FindStudent(student *head,int n) //查找學生
{
student *p1=head;
int number;
cout<<endl<<"輸入要查找的學生學號:";cin>>number;
for(int i=0;i<n;i++)
{
if(p1->num==number){cout<<"該學生信息為:"<<endl<<"姓名:"<<p1->name<<" 學號"<<p1->num<<" 分數:"<<p1->score<<endl;break;}
else {p1=p1->next;}
}
if(!p1){cout<<"無此學生!"<<endl;}
}
void PrintClass(student *head,int n) //輸出班級
{
student *p1=head;
for(int i=0;i<n;i++)
{
cout<<"姓名:"<<p1->name<<" 學號:"<<p1->num<<" 分數:"<<p1->score<<endl;
p1=p1->next;
}
}
int main()
{
student *ptr1,*ptr2,*ptr3;
int m,n;
cout<<"輸入1班人數:";cin>>m;
cout<<"輸入1班"<<m<<"名學生信息:"<<endl;
ptr1=CreatClass(m);
cout<<endl<<"輸入2班人數:";cin>>n;
cout<<"輸入2班"<<n<<"名學生信息:"<<endl;
ptr2=CreatClass(n);
cout<<endl<<"1班成績排序輸出:"<<endl;
ptr1=SortScore(ptr1,m);
PrintClass(ptr1,m);
cout<<endl<<"2班成績排序輸出:"<<endl;
ptr2=SortScore(ptr2,n);
PrintClass(ptr2,n);
cout<<endl;
ptr3=CombineClass(ptr1,ptr2);
int s=m+n;
ptr3=SortScore(ptr3,s);
PrintClass(ptr3,s);
FindStudent(ptr1,s);
return 0;
}
Ⅱ c語言!!!程序設計:建立一個學生信息鏈表,包括學號,姓名,成績.(實現添加,刪除,查詢,排序,平均)
#include<iostream>
using namespace std;
struct stu{
char name[20];
int num;
int age;
char sex;
int grade;
struct stu *next;
};
struct stu *mythis,*mynew;
void newrecord(struct stu *head)
{
mythis=head->next;
while(mythis!=NULL)
mythis=mythis->next;
mynew=(struct stu *)malloc(sizeof(struct stu));
cin>>mynew->name>>mynew->num>>mynew->age>>mynew->sex>>mynew->grade;
mynew->next=NULL;
if(mythis==NULL)
{
mythis=(struct stu *)malloc(sizeof(struct stu));
mythis=mynew;
}
}
void listall(stu *head)
{
mythis=head->next;
while(mythis!=NULL)
{
cout<<mythis->name<<mythis->num<<mythis->age<<mythis->sex<<mythis->grade;
mythis=mythis->next;
}
}
int main()
{
char decide;
struct stu *head;
head=(struct stu *)malloc(sizeof(struct stu));
head->next=NULL;
while(1)
{
cout<<"Please input decide:"<<endl;
cin>>decide;
if(decide=='n')
newrecord(head);
else
if(decide=='1')
listall(head);
else
return 0;
}
}
拓展資料
C語言是一門通用計算機編程語言,應用廣泛。C語言的設計目標是提供一種能以簡易的方式編譯、處理低級存儲器、產生少量的機器碼以及不需要任何運行環境支持便能運行的編程語言。
盡管C語言提供了許多低級處理的功能,但仍然保持著良好跨平台的特性,以一個標准規格寫出的C語言程序可在許多電腦平台上進行編譯,甚至包含一些嵌入式處理器(單片機或稱MCU)以及超級電腦等作業平台。
二十世紀八十年代,為了避免各開發廠商用的C語言語法產生差異,由美國國家標准局為C語言制定了一套完整的美國國家標准語法,稱為ANSI C,作為C語言最初的標准。目前2011年12月8日,國際標准化組織(ISO)和國際電工委員會(IEC)發布的C11標準是C語言的第三個官方標准,也是C語言的最新標准,該標准更好的支持了漢字函數名和漢字標識符,一定程度上實現了漢字編程。
Ⅲ 設計一程序將文件中的學生信息讀出,按先後順序建立鏈表,並對鏈表中的學生記錄按總分降序排序,要排名。
其他的都給你寫出來了 排序很簡單的 你自己就可以寫 我沒有時間給你寫了
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
typedef struct student
{char num[6];
char name[8];
int tax; /*名次*/
int english;
int math;
int score;
struct student *next;
}STUDENT;
STUDENT *head = NULL,*tel=NULL;
void Score_Sort();//排序
void Student_Information_show();//顯示所有學生信息
void Stident_Read_from_File();
void Student_Add(STUDENT stu);
void Score_Sort()
{
}
void Student_Information_show()
{
STUDENT *temp;
temp=head;
for(temp;temp!=NULL;temp=temp->next)
{
printf("%s %s %d %d %d \n",temp->num,temp->name,temp->english,temp->math,temp->score);
}
}
void Stident_Read_from_File()
{
FILE *file1;
char str[255];
char num[6],name[8];
int english, math;
STUDENT STU;
file1=fopen("Student.txt","r+");
if(file1!=NULL)
{
while((fgets(str,255,file1))!=NULL)
{
sscanf(str,"%s %s %d %d",num,name,&english,&math);
strcpy(STU.num,num);
strcpy(STU.name,name);
STU.english=english;
STU.math=math;
STU.score=english+math;
STU.tax=0;
Student_Add(STU);
}
}
}
void Student_Add(STUDENT stu)
{
STUDENT *temp;//創建一個新的節點並申請空間
temp=(STUDENT *)malloc(sizeof(STUDENT));
strcpy(temp->num,stu.num);
strcpy(temp->name,stu.name);
temp->english=stu.english;
temp->math=stu.math;
temp->tax=stu.tax;
temp->score=stu.score;
temp->next =NULL;
if(head==NULL)
{
head=temp;
}
else
{
tel->next = temp;
}
tel=temp;
}
int main()
{
Stident_Read_from_File();
Student_Information_show();
return 0;
}
Ⅳ C語言學生成績管理系統中的鏈表排序怎麼做
用冒泡排序!雖然效率低!比如:a[N]={3,2,5,4,2,6}for(int i = 0;i<(數組長度/2);i++) for(int j=i+1;j<數組長度;j++) if(a[i]>a[j]) { int temp = a[i]; a[i] = a[j]; a[j] = temp; }從第一個元素開始,依次往後面的元素比較,小的放前,大的放後,基本方法就這樣了~!
Ⅳ 以下c語言菜單(關於使用鏈表排序成績)如何改動
你的代碼不全,看不到具體鏈表構建。
從現有代碼看:
1、建議結構指針初始化版NULL(習慣)。struct person *head=NULL,*head1=NULL;
2、既然你定義權了頭指針head,正常習慣頭指針是不存放數據的,只是next指向鏈表第一個節點。那麼循環鏈表那裡應該寫while(p->next)而不是while(p!=NULL)。(除非你的head就是第一個節點)。
3、如像我上條所述,head是頭指針,循環改成while(p->next),那麼鏈表冒泡排序那裡,p1=p->next;可以寫p1=p->next->next;(當然你寫p1=p->next;也不錯,就是循環次數多點)
Ⅵ C語言如何對鏈表的數進行排序
同學,給你一段代碼,裡面涵蓋了鏈表的冒泡排序!
#include<stdio.h>
#include<malloc.h>
typedef struct node
{
int data;/*data代表成績分數*/
struct node *next;
}LNode,*LinkList;
LinkList Creat(void)/*創建鏈表,結束標志為當輸入的數據為0!*/
{
LinkList H,p1,p2;
int n;
n=0;
p1=p2=(LinkList)malloc(sizeof(LNode));
printf("輸入數據:");
scanf("%d",&p1->data);
H=NULL;
while(p1->data!=0)
{
n=n+1;
if(n==1)
H=p1;
else
p2->next=p1;
p2=p1;
p1=(LinkList)malloc(sizeof(LNode));
scanf("%d",&p1->data);
}
p2->next=NULL;
return(H);
}
LinkList Sort(LinkList SL)/*遞增排序函數:入口參數:鏈表的頭指針,此為鏈表中的排序函數*/
{
LinkList p,q;
int temp;
for(p=SL;p!=NULL;p=p->next)
{
for(q=p->next;q!=NULL;q=q->next)
{
if(p->data>q->data)
{
temp=q->data;
q->data=p->data;
p->data=temp;
}
}
}
return SL;
}
int main()
{
LinkList L,S,K;
L=Creat();
printf("初始化的單鏈表數據序列為:\n");
for(S=L;S!=NULL;S=S->next)
printf("%d ",S->data);
Sort(L);
printf("\n按遞增順序排序後的序列為:\n");
for(K=L;K!=NULL;K=K->next)
printf("%d==>",K->data);
return 0;
}
Ⅶ q建立2個班學生成績的無需鏈表,其中每個班包含10個節點數據,將每個班的成績鏈表按成績高低排序後輸出。
#include <iostream.h>
class node
{
public:
int num,a;
char b[30];
node *next;
}*p1,*p2;node *head1=NULL;node *head2=NULL;
node*creat1()
{
node *p1,*p2;
p1=new node;
head1=p1;
p2=p1;
cout<<"請輸入學號,以0結束:"<<endl;
cin>>p1->num;
if(p1->num!=0)
{
cout<<"請輸入學生名字:"<<endl;
cin>>p1->b;
cout<<"請輸入成績:"<<endl;
cin>>p1->a;
}
else
{
delete p1;
p2=NULL;
p2->next=NULL;
head1=NULL;
return head1 ;
}
while(p1->num!=0)
{
p2=p1;
p1=new node;
cout<<"請輸入學號,以0結束:"<<endl;
cin>>p1->num;
if(p1->num!=0)
{
cout<<"請輸入學生名字:"<<endl;
cin>>p1->b;
cout<<"請輸入成績:"<<endl;
cin>>p1->a;
}
p2->next=p1;
}
delete p1;
p2->next=NULL;
return head1;
}
node*creat2()
{
node *p3,*p4;
p3=new node;
head2=p3;
p4=p3;
cout<<"請輸入學號,以0結束:"<<endl;
cin>>p3->num;
if(p3->num!=0)
{
cout<<"請輸入學生名字:"<<endl;
cin>>p3->b;
cout<<"請輸入成績:"<<endl;
cin>>p3->a;
}
else
{
delete p3;
p4=NULL;
p4->next=NULL;
head2=NULL;
return head2 ;
}
while(p3->num!=0)
{
p4=p3;
p3=new node;
cout<<"請輸入學號,以0結束:"<<endl;
cin>>p3->num;
if(p3->num!=0)
{
cout<<"請輸入學生名字:"<<endl;
cin>>p3->b;
cout<<"請輸入成績:"<<endl;
cin>>p3->a;
}
p4->next=p3;
}
delete p3;
p4->next=NULL;
return head2;
}
void shownode(node*head)
{
cout<<"學生信息如下:"<<endl;
while (head!=NULL)
{
cout<<"學號"<<" "<<"名字"<<" "<<"成績"<<endl;
cout<<head->num<<" "<<head->b<<" "<<head->a<<endl;
head=head->next;
}
}
void findnode(node*head,int num)
{
cout<<"查找學號:";
cin>>num;
while(head!=NULL)
{
if(head->num==num)
{
cout<<"學號:"<<head->num<<" "<<"名字:"<<head->b<<" "<<"成績:"<<head->a<<endl; //這點讓我好找啊!!!!!
return;
}
if (head->next==NULL)
{
cout<<"無此學生"<<endl;
}
head=head->next;
}
}
void insert (node*head,int a)
{
node *p1,*p2;
int tnum;
int ta;
//char tb[30];
for (p1=head; p1; p1=p1->next)
{
for (p2=head; p2->next; p2=p2->next)
{
if (p2->a<p2->next->a)
{
ta=p2->a;
p2->a=p2->next->a;
p2->next->a=ta;
tnum=p2->num;
p2->num=p2->next->num;
p2->next->num=tnum;
/* tb=c->b;
c->b=c->next->b;
c->next->b=tb;*/
}
}
}
}
void hebing(node*head2)
{
node *p;
while(head1)
{
p=head1;
head1=head1->next;
}
p->next=head2; //這點讓我好找啊(為什麼p改為head1就不行呢?我覺得對他反而錯,操)
head2=head2->next;
}
int main ()
{
int num,a,c;
node*head1=NULL;
node*head2=NULL;
cout<<"----------------------以下是1班成績的統計------------------------"<<endl;
head1=creat1 ();
insert (head1, a);
shownode(head1);
cout<<"---------------------以下是2班成績的統計------------------------"<<endl;
head2=creat2 ();
insert (head2, a);
shownode(head2);
cout<<"---------------------以下是兩個班成績的統計------------------------"<<endl;
hebing(head2);
insert (head1, a);
shownode(head1);
findnode(head1, num);
return 0;
}
Ⅷ 數據結構 程序設計 用雙向循環鏈表建立一個學生管理系統,要求實現插入,刪除,排序,修改等功能。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct student
{
int num;
char name[10];
float score[3];
float total;
struct student *next,*prior;
}DLNode,*DLinkList;
//建立鏈表
DLinkList Creat_DLinkList()
{
int x;
char y[10];
float s[3];
DLinkList DL=NULL;
DLNode *p=NULL,*q=NULL;
DL=malloc(sizeof(DLNode));
if(DL==NULL)
exit(0);
q=DL;
DL->next=NULL;
DL->prior=NULL;
printf("\nPlease enter students' information:\n");
scanf("%d%s%f%f%f",&x,y,&s[0],&s[1],&s[2]);
while(x!=0)
{
int i;
p=malloc(sizeof(DLNode));
p->num=x;
strcpy(p->name,y);
for(i=0;i<3;i++)
p->score[i]=s[i];
p->total=p->score[0]+p->score[1]+p->score[2];
q->next=p;
p->prior=q;
p->next=DL;
q=p;
scanf("%d%s%f%f%f",&x,y,&s[0],&s[1],&s[2]);
}
DL->prior=q;
return q;
}
//查找
int search(DLinkList r,int n)
{
DLNode *p,*h;
h=r->next;
p=h->next;
while(p!=h)
{
if(p->num==n)
return 1;
p=p->next;
}
return 0;
}
//添加
DLinkList add(DLinkList r)
{
int x;
char y[10];
float s[3];
DLNode *h=r->next,*p=NULL;
p=malloc(sizeof(DLNode));
printf("\nPlease enter students' information:\n");
scanf("%d%s%f%f%f",&x,y,&s[0],&s[1],&s[2]);
while(x!=0)
{
int i;
p=malloc(sizeof(DLNode));
p->num=x;
strcpy(p->name,y);
for(i=0;i<3;i++)
p->score[i]=s[i];
p->total=p->score[0]+p->score[1]+p->score[2];
p->prior=r;
r->next=p;
p->next=h;
h->prior=p;
r=p;
scanf("%d%s%f%f%f",&x,y,&s[0],&s[1],&s[2]);
}
return r;
}
//刪除
void delete(DLinkList r)
{
char na[10];
DLNode *h=r->next;
DLNode *p=h->next;
DLNode *q=NULL;
while(p!=h)
{
q=p->next;
while(q!=h)
{
if(!strcmp(q->name,p->name))
{
q->prior->next=q->next;
q->next->prior=q->prior;
free(q);
}
q=q->next;
}
p=p->next;
}
}
//修改
int modify(DLinkList r,int n)
{
float s[3];
int i;
DLNode *h=r->next;
DLNode *p=h->next;
while(p!=h)
{
if(p->num==n)
{
printf("\nPlease enter new score:\n");
scanf("%f%f%f",&s[0],&s[1],&s[2]);
for(i=0;i<3;i++)
p->score[i]=s[i];
p->total=p->score[0]+p->score[1]+p->score[2];
break;
}
p=p->next;
}
if(p==h)
return 0;
else
return 1;
}
//排序
DLinkList sort(DLinkList r)
{
DLNode *t=NULL,*s=NULL;
DLNode *h=r->next;
DLNode *p=h->next;
DLNode *q=NULL;
while(p!=h)
{
q=h->next->next;
while(q!=h)
{
t=q->prior;
if(t->total<q->total)
{
s=t->prior;
t->next=q->next;
q->next->prior=t;
t->prior=q;
q->next=t;
q->prior=s;
s->next=q;
q=t;
}
q=q->next;
}
p=p->next;
}
return h->prior;
}
//輸出
void print_DLinkList(DLinkList r)
{
int i;
DLNode *p,*h;
h=r->next;
p=h->next;
while(p!=h)
{
printf("number:%3d\tname:%s\tscore:%5.2f\t%5.2f\t%5.2f\ttotal:%5.2f\n",p->num,p->name,p->score[0],p->score[1],p->score[2],p->total);
p=p->next;
}
}
//釋放內存
void destory(DLinkList r)
{
DLNode *h,*p,*t=NULL;
h=r->next;
p=h->next;
while(p!=h)
{
t=p->next;
free(p);
p=t;
}
free(h);
}
int main()
{
DLinkList r;
int x,n,k;
r=Creat_DLinkList();
print_DLinkList(r);
printf("\nChoose what you want:\n");
printf("1:Search information:\n");
printf("2:Add information:\n");
printf("3:Delete same name:\n");
printf("4:Modify score:\n");
printf("5:Sort degrdation:\n");
scanf("%d",&x);
switch(x)
{
case 1: printf("\nPlease enter a number:");
scanf("%d",&n);
k=search(r,n);
if(k)
print_DLinkList(r);
else
printf("\nerror!!!\n");
break;
case 2: r=add(r);
print_DLinkList(r);
break;
case 3: delete(r);
print_DLinkList(r);
break;
case 4: printf("\nPlease enter a number:");
scanf("%d",&n);
k=modify(r,n);
if(k)
print_DLinkList(r);
else
printf("\nNot found!!!\n");
break;
case 5: r=sort(r);
print_DLinkList(r);
break;
default:printf("\nEnter error!!!\n");
}
destory(r);
return 0;
}
最近在學這里,所以就幫樓主編了一下,順便鞏固鞏固所學。lz有什麼不懂的盡管問,我會盡量回答的。
Ⅸ C語言鏈表排序
#include"stdafx.h"
#include<stdlib.h>
//創建一個節點,data為value,指向NULL
Node*Create(intvalue){
Node*head=(Node*)malloc(sizeof(Node));
head->data=value;
head->next=NULL;
returnhead;
}
//銷毀鏈表
boolDestroy_List(Node*head){
Node*temp;
while(head){
temp=head->next;
free(head);
head=temp;
}
head=NULL;
returntrue;
}
//表後添加一個節點,Create(value)
boolAppend(Node*head,intvalue){
Node*n=Create(value);
Node*temp=head;
while(temp->next){
temp=temp->next;
}
temp->next=n;
return0;
}
//列印鏈表
voidPrint_List(Node*head){
Node*temp=head->next;
while(temp){
printf("%d->",temp->data);
temp=temp->next;
}
printf("\n");
}
//在鏈表的第locate個節點後(頭節點為0)插入創建的節點Create(value)
boolInsert_List(Node*head,intlocate,intvalue){
Node*temp=head;
Node*p;
Node*n=Create(value);
if(locate<0)
returnfalse;
while(locate--){
if(temp->next==NULL){
temp->next=Create(value);
returntrue;
}
temp=temp->next;
}
p=temp->next;
temp->next=n;
n->next=p;
returntrue;
}
//刪除第locate個節點後(頭節點為0)的節點
boolDelete_List(Node*head,intlocate){
Node*temp=head;
Node*p;
if(locate<0)
returnfalse;
while(locate--){
if(temp==NULL){
returnfalse;
}
temp=temp->next;
}
p=temp->next->next;
free(temp->next);
temp->next=NULL;
temp->next=p;
returntrue;
}
//獲取鏈表長度(不包括頭節點)
intSize_List(Node*head){
Node*temp=head;
intsize=0;
while(temp->next){
temp=temp->next;
size++;
}
returnsize;
}
//鏈表的三種排序(選擇,插入,冒泡)
boolSort_List(Node*head){
intt=0;
intsize=Size_List(head);
//選擇排序
/*for(Node*temp=head->next;temp!=NULL;temp=temp->next){
for(Node*p=temp;p!=NULL;p=p->next){
if(temp->data>p->data){
printf("換%d和%d\n",temp->data,p->data);
t=temp->data;
temp->data=p->data;
p->data=t;
}
}
}*/
//插入排序
/*for(Node*temp=head->next->next;temp!=NULL;temp=temp->next){
for(Node*p=head;p->next!=NULL;p=p->next){
if(p->next->data>temp->data)
{
printf("換%d和%d\n",temp->data,p->next->data);
t=temp->data;
temp->data=p->next->data;
p->next->data=t;
}
}
}*/
//冒泡排序
for(Node*temp=head->next;temp->next!=NULL;temp=temp->next){
for(Node*p=head->next;p->next!=NULL;p=p->next){
if(p->data>p->next->data){
t=p->data;
p->data=p->next->data;
p->next->data=t;
}
}
}
return0;
}
(9)用鏈表給學生成績排序擴展閱讀:
return表示把程序流程從被調函數轉向主調函數並把表達式的值帶回主調函數,實現函數值的返回,返回時可附帶一個返回值,由return後面的參數指定。
return通常是必要的,因為函數調用的時候計算結果通常是通過返回值帶出的。如果函數執行不需要返回計算結果,也經常需要返回一個狀態碼來表示函數執行的順利與否(-1和0就是最常用的狀態碼),主調函數可以通過返回值判斷被調函數的執行情況。
Ⅹ 鏈表成績排序
呵呵,這是我第一次用鏈表寫,以你要求的功能,程序如下,有點長~
-------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
struct Student
{
long number;
char name[10];
float score[3];
float sum;
struct Student *next;
};
char Menu(void);
struct Student *App(struct Student *head);
void Ave(struct Student *head);
void Ser(struct Student *head);
void Pri(struct Student *head);
void Fre(struct Student *head);
int main(void)
{
struct Student *head=NULL;
while(1)
{
switch(Menu())
{
case '1':
printf("請入學生信息,直接輸入'-1'結束\n");
head=App(head);
break;
case '2':
Ave(head);
break;
case '3':
Ser(head);
break;
case '4':
printf("學生信息如下:\n");
Pri(head);
break;
case '0':
printf("好累啊,88!\n");
exit(0);
default:
printf("請重新輸入!\n");
}
fflush(stdin);
printf("按任意鍵繼續~");
getchar();
system("cls");
}
Fre(head);
return 0;
}
char Menu(void)
{
char ch;
printf("------------請選擇-----------\n");
printf("1.添加學生信息\n2.求學生各科平均分\n3.查找學生信息\n4.輸出學生信息\n0.退出\n-----------------------------\n");
scanf(" %c",&ch);
return ch;
}
struct Student *App(struct Student *head)
{
struct Student *p=NULL,*q,*temp;
static i=1;
while(i)
{
p=(struct Student *)malloc(sizeof(struct Student));
q=head;
if(p==NULL)
{
printf("內存不夠!\n");
exit(0);
}
printf("請輸入第%d名學生的學號、姓名、語文/英語/數學分數:\n",i++);
p->next =NULL;
scanf("%ld",&p->number);
if(p->number ==-1)
{
i--;
break;
free(p);
}
scanf("%s%f%f%f",p->name ,&p->score[0],&p->score[1],&p->score[2]);
p->sum=p->score[0]+p->score[1]+p->score[2];
if(head==NULL)
head=p;
else
{
while(q->sum>p->sum&&q->next!=NULL)
{
temp=q;
q=q->next;
}
if(q->sum<=p->sum)
{
if(q==head)
{
p->next =q;
head=p;
}
else
{
temp->next = p;
p->next = q;
}
}
else
{
q->next =p;
}
}
}
return head;
}
void Ave(struct Student *head)
{
struct Student *p=head;
float sum[3]={0},ave[3];
int s[3]={0};
int i,count=0;
if(p!=NULL)
{
while(p!=NULL)
{
for(i=0;i<3;i++)
sum[i]+=p->score[i];
p=p->next;
count++;
}
for(i=0;i<3;i++)
ave[i]=sum[i]/count;
printf("語文/英語/數學平均分分別為:%.2f %.2f %.2f\n",ave[0],ave[1],ave[2]);
p=head;
while(p!=NULL)
{
for(i=0;i<3;i++)
if(p->score[i]<ave[i])
s[i]++;
p=p->next;
}
printf("語文/英語/數學低於平均分的人數分別為:%d %d %d\n",s[0],s[1],s[2]);
}
else
printf("你還沒有錄入學生成績!\n");
}
void Ser(struct Student *head)
{
struct Student *p=head;
long s,flag=0;
printf("請輸入要查找的學生學號:");
scanf("%ld",&s);
while(p!=NULL)
{
if(p->number ==s)
{
printf("學生信息如下:\n學號:%ld\n姓名:%s\n語文:%.1f 英語:%.1f 數學:%.1f\n",p->number ,p->name ,p->score[0],p->score[1],p->score[2]);
flag=1;
}
p=p->next ;
}
if(flag==0)
printf("沒有找到該學生信息!\n");
}
void Pri(struct Student *head)
{
struct Student *p=head;
int i=1;
while(p!=NULL)
{
printf("%d. %ld %s %.1f %.1f %.1f\n",i++,p->number ,p->name ,p->score[0],p->score[1],p->score[2]);
p=p->next ;
}
}
void Fre(struct Student *head)
{
struct Student *p;
while(head!=NULL)
{
p=head;
head=head->next ;
free(p);
}
}