Code C\C++ : Làm việc với mảng cấu trúc (Bài thực hành KTLT ngày 30-11-2012)
/*
Viet cac ham:
- Nhap danh sach sinh vien
- In danh sach sinh vien duoi dang bang
- Tính diem tong ket
- In thong tin sinh vien co diem tong ket cao nhat
- Dem so sinh vien diem tk >=8, sinh nam 1993
- Sap xep sinh vien theo chieu giam cua diem
*/
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
// khai bao bien
struct SV{
char maSV[6], hoTen[30], dc[50], lop[10];
int namSinh;
float d1,d2,d3,dtk;
};
SV a[30]; // mang cau truc SV
int n; // chua so sinh vien
// ham nhap thong tin
void NhapTT(){
// nhap so sinh vien
do{ cout<<"\n n="; cin>>n;
if (n<3||n>30) cout<<"\n Nhap lai n!";
}while(n<3||n>30);
// nhap day sinh vien
cout<<"\n Nhap vao thong tin sinh vien:";
for(int i=0;i<n;i++)
{
cout<<"\n SV thu "<<i+1<<" : ";
cout<<"\n - Ho ten : "; gets(a[i].hoTen);
cout<<"\n - Ma SV : "; gets(a[i].maSV);
cout<<"\n - Lop : "; gets(a[i].lop);
cout<<"\n - Dia chi : "; gets(a[i].dc);
cout<<"\n - Nam sinh : ";cin>>a[i].namSinh;
cout<<"\n - Diem ky I : "; cin>>a[i].d1;
cout<<"\n - Diem ky II : "; cin>>a[i].d2;
cout<<"\n - Diem ky III : "; cin>>a[i].d3;
}
}
// Ham In thong tin sinh vien
void InTT(){
cout<<"\n \n DANH SACH SINH VIEN:\n";
cout<<"\n|Ma SV\t|Ho ten \t|Dia chi\t|Nam sinh \t|Lop \t|Diem I \t|Diem II \t|DiemIII \t|";
cout<<"\n|---- \t|----- \t|-------\t|------- \t|---\t|\t-----\t|------\t|------\t|";
for (int i=0;i<n;i++){
cout<<"\n|"<<a[i].maSV<<"\t|"<<a[i].hoTen<<"\t|"<<a[i].dc<<"\t|"<<a[i].namSinh<<"\t|"<<a[i].lop<<"\t|"<<a[i].d1<<"\t|"<<a[i].d2<<"\t|"<<a[i].d3<<"\t|";
}
}
// ham tinh diem
void TinhDiem(){
for(int i=0;i<n;i++)
a[i].dtk=(a[i].d1+a[i].d2+a[i].d3)/3;
}
// In sinh vien co diem tk cao nhat
void InSV_Diem_Max(){
float max=a[0].dtk;
int m=0;
for(int i=1;i<n;i++)
if (a[i].dtk>max){ max=a[i].dtk ; m=i;}
// in sinh vien co diem cao nhat
cout<<"\n * SINH VIEN CO DIEM CAO NHAT:\n";
cout<<a[m].maSV<<"\t|\t"<<a[m].hoTen<<"\t|\t"<<a[m].dc<<"\t|\t"<<a[m].namSinh<<"\t|\t"<<a[m].lop<<"\t|\t"<<a[m].dtk;
}
// ham diem sinh vien diem tong ket >=8, sinh nam 1993
void DemSV(){
int dem=0;
for(int i=0;i<n;i++)
if (a[i].dtk>=8 && a[i].namSinh==1993) dem++;
cout<<"\n * So sinh vie co diem tk >=8 va sinh nam 1993 la: "<<dem;
}
// doi cho
void DoiCho(SV &a, SV &b){
char str[50];
int ns;
float diem;
diem=a.d1;
a.d1=b.d1;
b.d1=diem;
diem=a.d2;
a.d2=b.d2;
b.d2=diem;
diem=a.d3;
a.d3=b.d3;
b.d3=diem;
ns=a.namSinh;
a.namSinh=b.namSinh;
b.namSinh=diem;
strcpy(str,a.hoTen);
strcpy(a.hoTen,b.hoTen);
strcpy(b.hoTen,str);
strcpy(str,a.dc);
strcpy(a.dc,b.dc);
strcpy(b.dc,str);
strcpy(str,a.maSV);
strcpy(a.maSV,b.maSV);
strcpy(b.maSV,str);
strcpy(str,a.lop);
strcpy(a.lop,b.lop);
strcpy(b.lop,str);
}
// Ham sap xep diem giam dan
void SapXep(){
// sap xep
for (int i=0;i<n-1;i++)
for(int j= i+1;j<n;j++)
if (a[j].dtk>a[i].dtk)
DoiCho(a[j],a[i]);
}
// Ham chinh
void main(){
clrscr();
NhapTT();
InTT();
TinhDiem();
InSV_Diem_Max();
DemSV();
SapXep();
InTT();
getch();
}
0 nhận xét:
Đăng nhận xét