6/10/2011, 2:07 pm
- Code:
#include<iostream.h>
#include <conio.h>
struct data
{
char hoten[30];
char lop[10];
float diem;
};
struct node
{
data info;
node *tiep;
};
struct list
{
node *dau;
node *cuoi;
};
void nhapnode(node *p)
{
cin.ignore();
cout<<"\nNhap ten sv : ";
cin.getline(p->info.hoten,30);
cout<<"\nNhap lop : ";
cin.getline(p->info.lop,10);
cout<<"\nNhap diem : ";
cin>>p->info.diem;
p->tiep=NULL;
}
void chensau(list &l,node *p)
{
if (l.dau==NULL)
{
l.dau=p;
l.cuoi=l.dau;
}
else
{
l.cuoi->tiep=p;
l.cuoi=p;
}
}
void themsv(list &l)
{
node *p;
p=new node[1];
if(p==NULL)
return;
else
nhapnode(p);
chensau(l,p);
}
void hienthi(list l)
{
node *p;
p=l.dau;
while(p!=NULL)
{
cout<<"\nHo ten : "<<p->info.hoten;
cout<<"\nLop : "<<p->info.lop;
cout<<"\n Tong diem : "<<p->info.diem;
p=p->tiep;
}
}
void nhapsv(list &l)
{
int x;
do
{
themsv(l);
cout<<"Ban co muon nhap tiep khong ? 1/0 \n";
cin>>x;
if (x==1)
themsv(l);
}
while(x!=1);
}
void sapxep(list &l)
{
node *p,*q;
node *temp;
p=l.dau;
while(p!=NULL && p->tiep!=NULL)
{
q=p->tiep;
while (q!=NULL)
{
if(p->info.diem<q->info.diem)
temp->info.diem=q->info.diem;
q->info.diem=p->info.diem;
p->info.diem=temp->info.diem;
}
}
}
void main()
{
clrscr();
list l;
int x;
lap:
cout<<"-----------MENU----------\n";
cout<<"1.Nhap thong tin \n";
cout<<"2.Hien thi danh sach \n";
cout<<"3.Them 1 sinh vien moi \n";
cout<<"4.Hien thi ds sap xep \n";
cout<<"\n NHap vao su lua chon cua ban : ";
cin>>x;
switch(x)
{
case 1:nhapsv(l);break;
case 2:hienthi(l);break;
case 3:themsv(l);break;
case 4:sapxep(l);hienthi(l);break;
default: goto lap;
}
getch();
}