News Ticker

Menu

Bài Tập 2 - Lập Trình Hướng Đối Tượng


Code Kham Khảo Cách 1
#include<iostream>
#include<string.h>

using namespace std;

class ThiSinh{
 private:
  char Maso[32], Hoten[32], Ketqua[9];
  float DiemTB, DiemLT, DiemTH;
 public:
  ThiSinh();
  
  char* getMaso(){
   return Maso;
  }
  
  char* getHoten(){
   return Hoten;
  }
  
  char* getKetqua(){
   return Ketqua;
  }
  
  float getDiemTB(){
   return DiemTB;
  }
  
  float getDiemLT(){
   return DiemLT;
  }
  
  float getDiemTH(){
   return DiemTH;
  }
  
  void Nhap();
  void Xuat();
};

class MangThiSinh{
 private:
  ThiSinh mang[100];
  int n;
 public:
  void NhapMang();
  void XuatMang();
  void LietKe_ThiSinh_DiemTH_Va_LT_Tren7();
  float TrungBinhCong_DiemThiSinh_List();
  void Search_with_code();
  void XapXep_TheoTen();
  
};

int main(){
 // Mang Thi Sinh
 MangThiSinh mang_ts;
 
 //Bat dau nhap mang thi sinh
 cout<<"\n============== NHAP MANG ===============\n"<<endl;
 mang_ts.NhapMang();
 
 //Xuat Mang Da Nhap
 cout<<"\n============== XUAT MANG ===============\n"<<endl;
 mang_ts.XuatMang();
 
 //Liet Ke Thi sinh co diem LT & TH tren 7
 cout<<"\n============== Tren 7 ===============\n"<<endl;
 mang_ts.LietKe_ThiSinh_DiemTH_Va_LT_Tren7();
 
 //Tinh Tong Diem Trong List
 cout<<"\n============== TONG MANG ===============\n"<<endl;
 cout<<"Tong Mang = "<<mang_ts.TrungBinhCong_DiemThiSinh_List()<<endl;
 
 // Tim Kiem bang ho ten hoac bang ma so
 cout<<"\n============== Search MANG ===============\n"<<endl;
 mang_ts.Search_with_code();
 
 //xap xiep theo ten
 cout<<"\n============== XAP XEP MANG ==============="<<endl;
 mang_ts.XapXep_TheoTen();
 mang_ts.XuatMang();
 
 
 return 0;
}

ThiSinh::ThiSinh(){
 DiemLT = DiemTB = DiemTH = 0.0;
 strcpy(Hoten," ");
 strcpy(Maso," ");
 strcpy(Ketqua," ");
}

void ThiSinh::Nhap(){
 cin.ignore();
 cout<<"Nhap Ten Thi Sinh: "; gets(Hoten);
 cout<<"Nhap Ma So Thi Sinh: "; gets(Maso);
 cout<<"Nhap Diem Ly Thuyet: "; cin>>DiemLT;
 while(DiemLT > 10 || DiemLT < 0){
  cout<<"Diem Phai > 0 Va < 10"<<endl;
  cout<<"Nhap Diem Ly Thuyet: "; cin>>DiemLT;
 }
 cout<<"Nhap Diem Thuc Hanh: "; cin>>DiemTH;
 while(DiemTH > 10 || DiemTH < 0){
  cout<<"Diem Phai > 0 Va < 10"<<endl;
  cout<<"Nhap Diem Thuc Hanh: "; cin>>DiemTH; 
 }
 if(((getDiemLT() + getDiemTH()*2)/3) >= 5) strcpy(Ketqua,"DAT");
 else strcpy(Ketqua,"KHONG DAT");
 DiemTB = (DiemLT + (DiemTH*2))/3;
}

void ThiSinh::Xuat(){
 cout<<"Ten Thi Sinh: \t\t"<<getHoten()<<endl;
 cout<<"Ma So Thi Sinh: \t"<<getMaso()<<endl;
 cout<<"Diem Ly Thuyet: \t"<<getDiemLT()<<endl;
 cout<<"Diem Thuc Hanh: \t"<<getDiemTH()<<endl;
 cout<<"Diem TB: \t\t"<<getDiemTB()<<endl;
 cout<<"Ket Qua: \t\t"<<getKetqua()<<endl;
}

//----------------------------------------------------
void MangThiSinh::NhapMang(){
 cout<<"Nhap So Luong Thi Sinh: "; cin>>n;
 for(int i=0; i<n; i++){
  cout<<"Nhap Thi Sinh Thu [" << i+1<<"] / ["<<n<<"]"<<endl;
  mang[i].Nhap();
  cout<<endl;
 }
}

void MangThiSinh::XuatMang(){
 for(int i=0; i<n; i++){
  cout<<"Thi Sinh Thu [" << i+1<<"] / ["<<n<<"]"<<endl;
  mang[i].Xuat();
  cout<<endl;
 }
}

void MangThiSinh::LietKe_ThiSinh_DiemTH_Va_LT_Tren7(){
 int a = 0;
 for(int i=0; i<n; i++)
  if(mang[i].getDiemLT()>7 && mang[i].getDiemTH()>7){
   mang[i].Xuat();
   cout<<endl;
   a+= 1;
  }
 if(a == 0) cout<<"Khong co thi sinh LT & TH > 7"<<endl;
}

float MangThiSinh::TrungBinhCong_DiemThiSinh_List(){
 float result = 0;
 for(int i=0; i<n; i++)
  result+= mang[i].getDiemTB();
 return result;
}

void MangThiSinh::Search_with_code(){
// int b;
// do{
//  
// cout<<"==== MENU ===="<<endl;
// cout<<"0. Exit\t1. Tim Tiep"<<endl;
// cout<<"Ban Muon Chon: "; cin>>b;
//  
// char x[32]; bool a = true;
// cout<<"Nhap Ma So Hoac Ten Thi Sinh Can Tim: ";  fflush(stdin); gets(x);
// 
// for(int i=0; i<n; i++){
//  if((strcmp(mang[i].getMaso(),x) == 0) || (strcmp(mang[i].getHoten(),x) == 0)){
//   cout<<"Da Tim Thay Thi Sinh ["<<x<<"]"<<endl;
//   mang[i].Xuat();
//  }
//  else a = false;
// }
// if(a == false) cout<<"Khong tim thay ["<<x<<"]"<<endl; 
// }while(b!=0);

 
 
 char x[32]; bool a = true;
 cout<<"Nhap Ma So Hoac Ten Thi Sinh Can Tim: ";  fflush(stdin); gets(x);
 
 for(int i=0; i<n; i++){
  if((strcmp(mang[i].getMaso(),x) == 0) || (strcmp(mang[i].getHoten(),x) == 0)){
   cout<<"Da Tim Thay Thi Sinh ["<<x<<"]"<<endl;
   mang[i].Xuat();
  }
  else a = false;
 }
 if(a == false) cout<<"Khong tim thay ["<<x<<"]"<<endl; 
}

void MangThiSinh::XapXep_TheoTen(){
 ThiSinh temp;
 for(int i=0; i<n-1; i++)
  for(int j=i+1; j<n; j++){
   if(strcmp(mang[i].getHoten(),mang[j].getHoten()) > 0){
    temp = mang[i];
    mang[i] = mang[j];
    mang[j] = temp;
   }
  }
}

Kham Khảo Cách 2


Share This:

Welcome To Task Marks

I'm Task Marks. Tôi là chủ trang web này, trang này tôi dùng để chia sẽ tài liệu và những thứ linh tinh khác. Cảm ơn mọi người đã ghé thăm trang web của chúng tôi.Nếu có thắt mắt xin vui lòng liên hệ
Mail: devnguhoc@gmail.com

No Comment to " Bài Tập 2 - Lập Trình Hướng Đối Tượng "

  • To add an Emoticons Show Icons
  • To add code Use [pre]code here[/pre]
  • To add an Image Use [img]IMAGE-URL-HERE[/img]
  • To add Youtube video just paste a video link like http://www.youtube.com/watch?v=0x_gnfpL3RM