Code C\C++: Ví dụ về màng cấu trúc - struct array


/* BAI TAP MANG CAU TRUC
  Viet cac ham thuc hien:
  1. Nhap vao thong tin cua n sinh vien (5<n<100), thong tin gom:
- Ho ten
- Tuoi
- Dia chi
- Lop
- Diem ky1 (he so 2)
- Diem ky2 (he so 3)
- Diem ky3 (he so 4)
- Diem ky4 (he so 5)
  2. Tinh diem Trung binh toan khoa hoc cho  n sinh vien vua nhap
  3. Tim va in ra sinh vien co diem TB lon nhat
  4. Dem so sinh vien co diem TB >=5.0
  5. Xet danh hieu cho sinh vien

*/
#include<conio.h>#include<stdio.h>
#define MAX 100#define MIN 5
// Dinh nghia 1 cau truc SVstruct SV{  char hoTen[30], diaChi[50],lop[5];  int tuoi;  float dk1,dk2,dk3,dk4,dtb;// dtb: diem trung binh};// khai bao mang cau trucSV m[MAX];int n; // so sinh vien
//1. Ham nhap danh sach sinh vienvoid Nhap(){ // Nhap so sinh vien printf("\n + Nhap so sinh vien: "); do{ printf("\n n= "); scanf("%d",&n); if (n<MIN||n>MAX) printf("\n => Moi ban nhap lai so sinh vien!"); }while(n<MIN||n>MAX);
// Nhap danh sach sinh vien printf("\n + Nhap danh sach sinh vien:"); for(int i=0;i<n;i++) { printf("\n - Sinh vien %d",i+1);
fflush(stdin); printf("\n  Ho ten: "); gets(m[i].hoTen);
fflush(stdin); printf("\n  Dia chi: "); gets(m[i].diaChi);
fflush(stdin); printf("\n  Lop: "); gets(m[i].lop);
printf("\n  Tuoi: "); scanf("%d",&m[i].tuoi);
printf("\n  Diem hoc ky 1: "); scanf("%f",&m[i].dk1);
printf("\n  Diem hoc ky 2: "); scanf("%f",&m[i].dk2);
printf("\n  Diem hoc ky 3: "); scanf("%f",&m[i].dk3);
printf("\n  Diem hoc ky 4: "); scanf("%f",&m[i].dk4); }}//2. Tinh diem trung binh void TinhDiem(){ // tinh diem for(int i=0;i<n;i++) m[i].dtb=(m[i].dk1*2+m[i].dk2*3+m[i].dk3*4+m[i].dk4*5)/14; // In diem printf(" + In diem trung binh cua sinh vien: "); for (i=0;i<n;i++) printf("\n - Sinh vien %s co diem TB: %f",m[i].hoTen,m[i].dtb);}//3. Tim va in ra nhung sinh vien co diem lon nhatvoid TimSV_DiemMax(){ // tim diem trung binh lon nhat float max=m[0].dtb; for(int i=1;i<n;i++) if (m[i].dtb>max) max=m[i].dtb;
// In ra ho ten cua nhung sinh vien co diem trung binh lon nhat printf("\n + Nhung sinh vien co diem trung binh cao nhat (%f):",max); for (i=0;i<n;i++) if (m[i].dtb==max) printf("\n - %s",m[i].hoTen);
}//4. Dem so sinh vien co diem >=5int DemSV(){ int dem=0; for(int i=0;i<n;i++) if (m[i].dtb>=5) dem++; return dem;}//5. Xet danh hieu sinh vienvoid XetDanhHieu(){ printf("\n + Xet danh hieu:"); for(int i=0;i<n;i++) {  float d=m[i].dtb;  printf("\n Sinh vien %s dat danh hieu: ",m[i].hoTen);  if (d.dtb<3.5) printf("Kem");  else if (d<5) printf("Yeu"); else if (d<7) printf("TB"); else if(d<8) printf("Kha"); else printf("Gioi"); }}// ham chinhint main(){ Nhap(); TinhDiem(); TimSV_DiemMax(); printf("\n + So sinh vien co diem trung binh >=5 la: %d",DemSV()); XetDanhHieu();
 getch(); return 0;}

[Tải Code Chương trình tại đây (Lưu ý: Sau 5s, click Bỏ qua quảng cáo - Skin Ad)]