code C\C++: Ví dụ 1, xử lý struct (cấu trúc) - 14/01/13


/*Vi du struct 1
Viet cac ham:
1. Nhap vao thong tin 1 sv gom: ho ten, tuoi, lop, diem LT C, diem TA, Diem LT Thuat toan. (0<= diem <= 10, 0 < tuoi <200)
2. In thong thong tin sv vua nhap
3. Tinh diem trung binh cho sv
4. In danh hieu sinh vien
*/
#include<conio.h>
#include<stdio.h>
#include<string.h>

// dinh nghia cau truc (struct)
struct SV{
  char hoTen[30], lop[5];
  int tuoi;
  float dLTC,dTA,dLTTT;
};
// khai bao bien struct SV
SV t;

// 1. Nhap thong tin sinh vien
void NhapTT(){
  printf("\n NHAP THONG TIN CHO SV: \n");
  printf("\n Ho ten: ");
  fflush(stdin); gets(t.hoTen);

  printf("\n Lop: ");
  fflush(stdin); gets(t.lop);

  do{
 printf("\n Tuoi: ");
 scanf("%d",&t.tuoi);
 if (t.tuoi<0||t.tuoi>200)
printf("\n Tuoi khong hop le, nhap lai.");
  } while(t.tuoi<0||t.tuoi>200);

  do{
 printf("\n Diem lap trinh C: ");
 scanf("%f",&t.dLTC);
 if (t.dLTC<0.0||t.dLTC>10.0)
printf("\n Diem khong hop le, nhap lai diem.");
  } while (t.dLTC<0.0||t.dLTC>10.0);

  do{
printf("\n Diem Tieng Anh: ");
scanf("%f",&t.dTA);
if (t.dTA<0.0||t.dTA>10.0)
printf("\n Diem khong hop le, nhap lai diem.");
  } while (t.dTA<0.0||t.dTA>10.0);

  do{
 printf("\n Diem Ly thuyet thuat toan: ");
 scanf("%f",&t.dLTTT);
 if (t.dLTTT<0.0||t.dLTTT>10.0)
printf("\n Diem khong hop le, nhap lai diem.");
  } while (t.dLTTT<0.0||t.dLTTT>10.0);
}
// Ham in thong tin
void InTT(){
printf("\n IN THONG TIN CUA SINH VIEN: \n");
printf("\n - Ten sinh vien: %s",t.hoTen);
printf("\n - Tuoi sinh vien: %d",t.tuoi);
printf("\n - Sinh vien lop: %s",t.lop);
printf("\n - Diem Lap trinh C: %f",t.dLTC);
printf("\n - Diem Tieng Anh: %f",t.dTA);
printf("\n - Diem Ly thuyet thuat toan: %f",t.dLTTT);
}
// Ham tinh diem trung binh
float DiemTB(){
 return (t.dLTC+t.dTA+t.dLTTT)/3;
}
// Ham xet danh hieu cua sinh vien
void XetDanhHieu(){
 printf("\n DANH HIEU CUA SINH VIEN: ");
 float dtb=DiemTB();
 if(dtb<3.5) printf("Kem");
 else
if(dtb<5.0) printf("Yeu");
else
if(dtb<7.0) printf("Trung binh");
else
if(dtb<8.0) printf("Kha");
else printf("Gioi");
}

// ham main
int main(){
 NhapTT();
 InTT();
 printf("\n DIEM TRUNG CUA SINH VIEN: %f",DiemTB());

 XetDanhHieu();

 getch();
 return 0;
}

Danh mục bài viết