Về c++

Anh Phương Baby

Fire in the hole!
Biết là bài tập này vốn là bài tập cơ bản của C++ rồi, nhưng vì một số lý do cá nhân nên tớ chả biết gì mà làm :)

Các bác nào hảo tâm thì xin bỏ ra mấy phút làm giúp tớ nha :) Xin cảm ơn

Yêu cầu bài quản lý điểm học sinh viết bằng ngôn ngữ c++:
1.Xây dựng lớp DANHSACH gồm các thuộc tính sau:
DANHSACH: MAHS (mã học sinh), HOTEN (họ và tên học sinh), NGAYSINH (ngày sinh), DIACHI (địa chỉ), SDT (số điện thoại).
Có các phương thức: phương thức khởi tạo, phương thức hủy, phương thức nhập dữ liệu (có kiểm tra xem mã học sinh đã tồn tại chưa, nếu tồn tại thì nhập mã khác nếu chưa tồn tại thì nhập tiếp các thông tin khác của học sinh đó), phương thức hiển thị thông tin học sinh, phương thức sắp xếp các học sinh theo thứ tự tăng dần của họ tên.
2.Xây dựng lớp BANGDIEM gồm các thuộc tính sau:
BANGDIEM: MAHS (mã học sinh), HOTEN (họ và tên học sinh), DIEMTOAN (điểm môn toán), DIEMHOA (điểm môn hóa), DIEMLY (điểm môn lý), DIEMSINH (điểm môn sinh), DIEMANH (điểm môn anh), DIEMKT (điểm môn kỹ thuật), DTB (điểm trung bình), XEPLOAI (xếp loại)
Có các phương thức: phương thức khởi tạo, phương thức hủy, phương thức nhập dữ liệu (trong đó không nhập dữ liệu cho thuộc tính điểm trung bình và xếp loại), phương thức tính điểm trung bình và xếp loại (trong đó điểm trung bình được tính theo công thức: DTB =(DT+DH+DL+DS+DA+DKT)/6, xếp loại được thực hiện như sau: nếu DTB>9.0 thì XL là xuất sắc, 8.0<=DTB<9.0 thì XL là Giỏi, 7.0<=DTB<8.0 thì XL là khá, 6.0<=DTB<7.0 thì XL là trung bình, 5.0<=DTB<6.0 thì XL là yếu, còn lại là kém), phương thức sắp xếp học sinh theo thứ tự tăng dần của điểm trung bình, phương thức hiển thị theo từng nhóm xếp loại được nhập vào từ bàn phím.
(Xây dựng hai lớp độc lập với nhau)
 
... lớp DANHSACH bao gồm nhiều học sinh à? Thế cái lớp DANHSACH này là cho 1 lớp hay cho 1 trường? Có size cố định sẵn ko? Ví dụ 1 DANHSACH bao gồm tối đa 20 hs, 30 hs, 50 hs, hay là lúc khởi tạo mới biết kích cỡ?

thuộc tính HOTEN DIACHI bạn muốn là string hay character array? string thì code đơn giản còn character array thì rối rắm phức tạp, nhưng nếu là lớp cơ sở dữ liệu thì ít ai xài string...
tương tự NGAYSINH SDT cũng là string hay là số? Tốt hơn nên là string mặc dù NGAYSINH và SDT toàn số :-s

phương thức nhập dữ liệu lớp DANHSACH: cần thêm kiểm tra HOTEN DIACHI NGAYSINH SDT hợp lý ko? :-s

phương thức hiển thị thông tin học sinh: có cần đúng cột cho mỗi hs ko? Đúng cột hơi mất thời gian vì chả biết độ dài họ tên và địa chỉ của mỗi hs dài ngắn ra sao ~.~

Và cuối cùng là mình có được toàn quyền thêm thắt vài phương thức và thuộc tính để dễ điều khiển lớp DANHSACH ko?


Nếu toàn quyền thì sẽ như thế này :-"
Compile bằng VC++ Express 2008 (mấy compiler cũ hơn thì có vài lỗi nhỏ...)
Documentation (chú thích): chưa có :-"
Lớp BANGDIEM từ từ... để xem DANHSACH được chưa đã :|

DanhSach.h
[spoil]
Mã:
#include <iostream>
using namespace std;

#ifndef DANHSACH_H
#define DANHSACH_H

#ifndef _STRINGS_SIZES
#define _STRINGS_SIZES
#define HOTEN_SIZE 41
#define NGAYSINH_SIZE 11
#define DIACHI_SIZE 81
#define SODT_SIZE 21
#define BUFFER_SIZE 101
#endif

class DanhSach
{
private:
   int soHSToiDa;
   int tongSoHS;
   int *maHS;
   char **hoTen;
   char **ngaySinh;
   char **diaChi;
   char **soDT;
public:
   friend ostream& operator<<(ostream&, const DanhSach&);  //Dung nhu phuong thuc hien thi HS
   DanhSach(int soHSToiDa = 20);
   ~DanhSach();
   void nhapThongTinHS();
   void sapXepTangDanHoTen();
private:
   bool isNumericString(const char *str);
   void getStringInput(char *buffer, const int _BUFFER_SIZE, const char* label, const int _STRING_LENGTH);
};
#endif
[/spoil]

DanhSach.cpp
[spoil]
Mã:
#include "DanhSach.h"

#pragma warning (disable:4996)
#pragma warning (disable:4018)

//---------------------------------------------------------
DanhSach::DanhSach(int soHSToiDa)
{
   this->soHSToiDa = soHSToiDa;
   tongSoHS = 0;
   maHS = new int[soHSToiDa];
   hoTen = new char*[soHSToiDa];
   ngaySinh = new char*[soHSToiDa];
   diaChi = new char*[soHSToiDa];
   soDT = new char*[soHSToiDa];
   for (int i = 0; i < soHSToiDa; i++)
   {
      maHS[i] = 0;
      hoTen[i] = new char[HOTEN_SIZE];
      hoTen[i][0] = '\0';
      ngaySinh[i] = new char[NGAYSINH_SIZE];
      ngaySinh[i][0] = '\0';
      diaChi[i] = new char[DIACHI_SIZE];
      diaChi[i][0] = '\0';
      soDT[i] = new char[SODT_SIZE];
      soDT[i][0] = '\0';
   }
}

//---------------------------------------------------------
DanhSach::~DanhSach()
{
   delete [] maHS;
   for (int i = 0; i < soHSToiDa; i++)
   {
      delete [] hoTen[i];
      delete [] ngaySinh[i];
      delete [] diaChi[i];
      delete [] soDT[i];
   }
   delete [] hoTen;
   delete [] ngaySinh;
   delete [] diaChi;
   delete [] soDT;
}

//---------------------------------------------------------
ostream& operator<<(ostream& strm, const DanhSach& rhs)
{
   for (int i = 0; i < rhs.tongSoHS; i++)
   {
      strm << rhs.maHS[i] << "  -  "
           << rhs.hoTen[i] << "  -  "
           << rhs.ngaySinh[i] << "  -  "
           << rhs.diaChi[i] << "  -  "
           << "DT: " << rhs.soDT[i] << endl;
   }
   return strm;
}

//---------------------------------------------------------
void DanhSach::nhapThongTinHS()
{
   char buffer[BUFFER_SIZE];
   char tiepTuc = 'y';
   int maHSTamThoi;
   bool maHSLoi;

   while (tongSoHS < soHSToiDa && toupper(tiepTuc) == 'Y')
   {
      cout << "Ma hoc sinh: ";
      cin.getline(buffer, BUFFER_SIZE);
      while (!isNumericString(buffer))
      {
         cout << "Ma hoc sinh khong hop le. Vui long nhap lai.\n";
         cout << "Ma hoc sinh: ";
         cin.getline(buffer, BUFFER_SIZE);
      }
      maHSLoi = false;
      maHSTamThoi = atoi(buffer);
      if (maHSTamThoi == 0)
      {
         maHSLoi = true;
      }
      else
      {
         for (int i = 0; i < tongSoHS; i++)
         {
            if (maHSTamThoi == maHS[i])
            {
               maHSLoi = true;
               break;
            }
         }
      }
      if (maHSLoi)
      {
         cout << "Hoc sinh da co trong danh sach hoac ma hoc sinh khong hop le.\n";
      }
      else
      {
         maHS[tongSoHS] = maHSTamThoi;
         getStringInput(buffer, BUFFER_SIZE, "Ho ten", HOTEN_SIZE);
         strncpy(hoTen[tongSoHS], buffer, HOTEN_SIZE);
         getStringInput(buffer, BUFFER_SIZE, "Ngay sinh", NGAYSINH_SIZE);
         strncpy(ngaySinh[tongSoHS], buffer, NGAYSINH_SIZE);
         getStringInput(buffer, BUFFER_SIZE, "Dia chi", DIACHI_SIZE);
         strncpy(diaChi[tongSoHS], buffer, DIACHI_SIZE);
         getStringInput(buffer, BUFFER_SIZE, "So dien thoai", SODT_SIZE);
         strncpy(soDT[tongSoHS], buffer, SODT_SIZE);
         tongSoHS++;

         cout << "\n... Da luu hoc sinh " << this->hoTen[tongSoHS - 1];
         cout << "\nTiep tuc?(y/n) ";
         cin >> tiepTuc;
         cin.ignore();
         cout << endl;
      }
   }
}

//---------------------------------------------------------
void DanhSach::sapXepTangDanHoTen()
{
   int startScan, minIndex;
   int maHSTamThoi;
   char hoTenTamThoi[HOTEN_SIZE];
   char ngaySinhTamThoi[NGAYSINH_SIZE];
   char diaChiTamThoi[DIACHI_SIZE];
   char soDTTamThoi[SODT_SIZE];

   for (startScan = 0; startScan < (tongSoHS - 1); startScan++)
   {
      minIndex = startScan;
      strncpy(hoTenTamThoi, this->hoTen[startScan], HOTEN_SIZE);
      for (int index = startScan + 1; index < tongSoHS; index++)
      {
         if (strcmp(this->hoTen[index], hoTenTamThoi) < 0)
         {
            strncpy(hoTenTamThoi, this->hoTen[index], HOTEN_SIZE);
            minIndex = index;
         }
      }
      maHSTamThoi = this->maHS[minIndex];
      strncpy(ngaySinhTamThoi, this->ngaySinh[minIndex], NGAYSINH_SIZE);
      strncpy(diaChiTamThoi, this->diaChi[minIndex], DIACHI_SIZE);
      strncpy(soDTTamThoi, this->soDT[minIndex], SODT_SIZE);

      this->maHS[minIndex] = this->maHS[startScan];
      strncpy(this->hoTen[minIndex], this->hoTen[startScan], HOTEN_SIZE);
      strncpy(this->ngaySinh[minIndex], this->ngaySinh[startScan], NGAYSINH_SIZE);
      strncpy(this->diaChi[minIndex], this->diaChi[startScan], DIACHI_SIZE);
      strncpy(this->soDT[minIndex], this->soDT[startScan], SODT_SIZE);

      this->maHS[startScan] = maHSTamThoi;
      strncpy(this->hoTen[startScan], hoTenTamThoi, HOTEN_SIZE);
      strncpy(this->ngaySinh[startScan], ngaySinhTamThoi, NGAYSINH_SIZE);
      strncpy(this->diaChi[startScan], diaChiTamThoi, DIACHI_SIZE);
      strncpy(this->soDT[startScan], soDTTamThoi, SODT_SIZE);
   }
}

//---------------------------------------------------------
// PHUONG THUC PHU TRO
//---------------------------------------------------------
bool DanhSach::isNumericString(const char *str)
{
   for (int i = 0; i < strlen(str); ++i)
   {
      if (str[i] < '0' || str[i] > '9')
      {
         return false;
      }
   }
   return true;
}
//---------------------------------------------------------
void DanhSach::getStringInput(char *buffer, const int _BUFFER_SIZE, const char* label, const int _STRING_LENGTH)
{
   cout << label << ": ";
   cin.getline(buffer, _BUFFER_SIZE);
   while (strlen(buffer) >= _STRING_LENGTH)
   {
      cout << label << " qua dai. Xin nhap " << (char)tolower(label[0]) << (label + 1) << " ngan hon.\n";
      cout << label << ": ";
      cin.getline(buffer, _BUFFER_SIZE);
   }
}
[/spoil]

Mới là test driver để test thử xem mấy functions của class có work ko
TestDanhSach.cpp
[spoil]
Mã:
#include <iostream>
#include "DanhSach.h"
using namespace std;

#define SOHSTOIDA 50

int main(int argc, char *argv[])
{
   //khoi tao danh sach
   DanhSach lopA(SOHSTOIDA);

   //hien thi tat ca thong tin hoc sinh trong danh sach
   cout << "lopA:\n" << lopA << endl;

   //nhap du lieu hoc sinh
   cout << endl;
   lopA.nhapThongTinHS();

   //xem lai tat ca hoc sinh trong danh sach
   cout << endl;
   cout << "lopA:\n" << lopA << endl;

   //sap xep theo thu tu tang dan ho ten
   cout << endl;
   lopA.sapXepTangDanHoTen();

   //xem lai tat ca hoc sinh trong danh sach
   cout << endl;
   cout << "lopA:\n" << lopA << endl;

   return 0;
}
[/spoil]
 
Chỉnh sửa cuối:
BangDiem.h
[spoil]
Mã:
#include <iostream>
using namespace std;

#ifndef BANGDIEM_H
#define BANGDIEM_H

#ifndef _STRINGS_SIZES
#define _STRINGS_SIZES
#define HOTEN_SIZE 41
#define BUFFER_SIZE 101
#endif

class BangDiem
{
private:
   int soHSToiDa;
   int tongSoHS;
   int *maHS;
   char **hoTen;
   double *diemToan;
   double *diemHoa;
   double *diemLy;
   double *diemSinh;
   double *diemAnh;
   double *diemKT;
   double *diemTB;
   int *xepLoai;
public:
   friend ostream& operator<<(ostream&, const BangDiem&);
   enum NhomHS { XuatSac, Gioi, Kha, TB, Yeu, Kem };
   BangDiem(int soHSToiDa = 20);
   ~BangDiem();
   void nhapDiemHS();
   void sapXepTangDanDiemTB();
   void hienThiNhom(NhomHS nhom);
private:
   bool isNumericString(const char *str);
   void getStringInput(char *buffer, const int _BUFFER_SIZE, const char* label, const int _STRING_LENGTH);
   bool hasUDoubleFormat(const char *str);
   bool hasLessThanNDecimalPlaces(const char *str, int decimaPlaces);
   void getUDoubleInput(double &output, const char* label, double min, double max);
};
#endif
[/spoil]

BangDiem.cpp
[spoil]
Mã:
#include "BangDiem.h"
#include <iomanip>

#pragma warning (disable:4996)
#pragma warning (disable:4018)

//---------------------------------------------------------
BangDiem::BangDiem(int soHSToiDa)
{
   this->soHSToiDa = soHSToiDa;
   tongSoHS = 0;
   maHS = new int[soHSToiDa];
   hoTen = new char*[soHSToiDa];
   diemToan = new double[soHSToiDa];
   diemHoa = new double[soHSToiDa];
   diemLy = new double[soHSToiDa];
   diemSinh = new double[soHSToiDa];
   diemAnh = new double[soHSToiDa];
   diemKT = new double[soHSToiDa];
   diemTB = new double[soHSToiDa];
   xepLoai = new int[soHSToiDa];
   for (int i = 0; i < soHSToiDa; i++)
   {
      maHS[i] = 0;
      hoTen[i] = new char[HOTEN_SIZE];
      hoTen[i][0] = '\0';
      diemToan[i] = 0.0;
      diemHoa[i] = 0.0;
      diemLy[i] = 0.0;
      diemSinh[i] = 0.0;
      diemAnh[i] = 0.0;
      diemKT[i] = 0.0;
   }
}

//---------------------------------------------------------
BangDiem::~BangDiem()
{
   delete [] maHS;
   for (int i = 0; i < soHSToiDa; i++)
   {
      delete [] hoTen[i];
   }
   delete [] hoTen;
   delete [] diemToan;
   delete [] diemHoa;
   delete [] diemLy;
   delete [] diemSinh;
   delete [] diemAnh;
   delete [] diemKT;
   delete [] diemTB;
   delete [] xepLoai;
}

//---------------------------------------------------------
ostream& operator<<(ostream& strm, const BangDiem&rhs)
{
   for (int i = 0; i < rhs.tongSoHS; i++)
   {
      strm << setw(3) << rhs.maHS[i] << " - "
           << left << setw(40) << rhs.hoTen[i] << " - "
           << fixed << setprecision(1)
           << right << setw(4) << rhs.diemToan[i] << "  "
           << setw(4) << rhs.diemHoa[i] << "  "
           << setw(4) << rhs.diemLy[i] << "  "
           << setw(4) << rhs.diemSinh[i] << "  "
           << setw(4) << rhs.diemAnh[i] << "  "
           << setw(4) << rhs.diemKT[i] << "    "
           << "DTB: " << setw(4) << rhs.diemTB[i] << "  "
           << "XL: ";
      if (rhs.xepLoai[i] == 0)
         strm << "Xuat sac";
      else if (rhs.xepLoai[i] == 1)
         strm << "Gioi";
      else if (rhs.xepLoai[i] == 2)
         strm << "Kha";
      else if (rhs.xepLoai[i] == 3)
         strm << "TB";
      else if (rhs.xepLoai[i] == 4)
         strm << "Yeu";
      else if (rhs.xepLoai[i] == 5)
         strm << "Kem";
      strm << endl;
   }
   return strm;
}

//---------------------------------------------------------
void BangDiem::nhapDiemHS()
{
   char buffer[BUFFER_SIZE];
   char tiepTuc = 'y';
   int maHSTamThoi;
   bool maHSLoi;

   while (tongSoHS < soHSToiDa && toupper(tiepTuc) == 'Y')
   {
      cout << "Ma hoc sinh: ";
      cin.getline(buffer, BUFFER_SIZE);
      while (!isNumericString(buffer))
      {
         cout << "Ma hoc sinh khong hop le. Vui long nhap lai.\n";
         cout << "Ma hoc sinh: ";
         cin.getline(buffer, BUFFER_SIZE);
      }
      maHSLoi = false;
      maHSTamThoi = atoi(buffer);
      if (maHSTamThoi == 0)
      {
         maHSLoi = true;
      }
      else
      {
         for (int i = 0; i < tongSoHS; i++)
         {
            if (maHSTamThoi == maHS[i])
            {
               maHSLoi = true;
               break;
            }
         }
      }
      if (maHSLoi)
      {
         cout << "Hoc sinh da co trong danh sach hoac ma hoc sinh khong hop le.\n";
      }
      else
      {
         maHS[tongSoHS] = maHSTamThoi;
         getStringInput(buffer, BUFFER_SIZE, "Ho ten", HOTEN_SIZE);
         strncpy(hoTen[tongSoHS], buffer, HOTEN_SIZE);
         getUDoubleInput(diemToan[tongSoHS], "Diem toan", 0.0, 10.0);
         getUDoubleInput(diemHoa[tongSoHS], "Diem hoa", 0.0, 10.0);
         getUDoubleInput(diemLy[tongSoHS], "Diem ly", 0.0, 10.0);
         getUDoubleInput(diemSinh[tongSoHS], "Diem sinh", 0.0, 10.0);
         getUDoubleInput(diemAnh[tongSoHS], "Diem Anh", 0.0, 10.0);
         getUDoubleInput(diemKT[tongSoHS], "Diem ky thuat", 0.0, 10.0);
         diemTB[tongSoHS] = (diemToan[tongSoHS] + diemHoa[tongSoHS] + diemLy[tongSoHS] + diemSinh[tongSoHS] + diemAnh[tongSoHS] + diemKT[tongSoHS]) / 6.0;
         if (diemTB[tongSoHS] >= 9.0)
            xepLoai[tongSoHS] = 0;
         else if (diemTB[tongSoHS] >= 8.0)
            xepLoai[tongSoHS] = 1;
         else if (diemTB[tongSoHS] >= 7.0)
            xepLoai[tongSoHS] = 2;
         else if (diemTB[tongSoHS] >= 6.0)
            xepLoai[tongSoHS] = 3;
         else if (diemTB[tongSoHS] >= 5.0)
            xepLoai[tongSoHS] = 4;
         else
            xepLoai[tongSoHS] = 5;
         tongSoHS++;

         cout << "\n... Da luu hoc sinh " << this->hoTen[tongSoHS - 1];
         cout << "\nTiep tuc?(y/n) ";
         cin >> tiepTuc;
         cin.ignore();
         cout << endl;
      }
   }
}

//---------------------------------------------------------
void BangDiem::sapXepTangDanDiemTB()
{
   int startScan, minIndex;
   int maHSTamThoi;
   char hoTenTamThoi[HOTEN_SIZE];
   double diemToanTamThoi;
   double diemHoaTamThoi;
   double diemLyTamThoi;
   double diemSinhTamThoi;
   double diemAnhTamThoi;
   double diemKTTamThoi;
   double diemTBTamThoi; //
   int xepLoaiTamThoi;

   for (startScan = 0; startScan < (tongSoHS - 1); startScan++)
   {
      minIndex = startScan;
      diemTBTamThoi = this->diemTB[startScan];
      for (int index = startScan + 1; index < tongSoHS; index++)
      {
         if (this->diemTB[index] < diemTBTamThoi)
         {
            diemTBTamThoi = this->diemTB[index];
            minIndex = index;
         }
      }
      maHSTamThoi = this->maHS[minIndex];
      strncpy(hoTenTamThoi, this->hoTen[minIndex], HOTEN_SIZE);
      diemToanTamThoi = this->diemToan[minIndex];
      diemHoaTamThoi = this->diemHoa[minIndex];
      diemLyTamThoi = this->diemLy[minIndex];
      diemSinhTamThoi = this->diemSinh[minIndex];
      diemAnhTamThoi = this->diemAnh[minIndex];
      diemKTTamThoi = this->diemKT[minIndex];
      xepLoaiTamThoi = this->xepLoai[minIndex];

      this->maHS[minIndex] = this->maHS[startScan];
      strncpy(this->hoTen[minIndex], this->hoTen[startScan], HOTEN_SIZE);
      this->diemToan[minIndex] = this->diemToan[startScan];
      this->diemHoa[minIndex] = this->diemHoa[startScan];
      this->diemLy[minIndex] = this->diemLy[startScan];
      this->diemSinh[minIndex] = this->diemSinh[startScan];
      this->diemAnh[minIndex] = this->diemAnh[startScan];
      this->diemKT[minIndex] = this->diemKT[startScan];
      this->diemTB[minIndex] = this->diemTB[startScan];
      this->xepLoai[minIndex] = this->xepLoai[startScan];

      this->maHS[startScan] = maHSTamThoi;
      strncpy(this->hoTen[startScan], hoTenTamThoi, HOTEN_SIZE);
      this->diemToan[startScan] = diemToanTamThoi;
      this->diemHoa[startScan] = diemHoaTamThoi;
      this->diemLy[startScan] = diemLyTamThoi;
      this->diemSinh[startScan] = diemSinhTamThoi;
      this->diemAnh[startScan] = diemAnhTamThoi;
      this->diemKT[startScan] = diemKTTamThoi;
      this->diemTB[startScan] = diemTBTamThoi;
      this->xepLoai[startScan] = xepLoaiTamThoi;
   }
}

//---------------------------------------------------------
void BangDiem::hienThiNhom(NhomHS nhom)
{
   for (int i = 0; i < tongSoHS; i++)
   {
      if (xepLoai[i] == nhom)
      {
         cout << setw(3) << maHS[i] << " - "
              << left << setw(40) << hoTen[i] << " - "
              << fixed << setprecision(1)
              << right << setw(4) << diemToan[i] << "  "
              << setw(4) << diemHoa[i] << "  "
              << setw(4) << diemLy[i] << "  "
              << setw(4) << diemSinh[i] << "  "
              << setw(4) << diemAnh[i] << "  "
              << setw(4) << diemKT[i] << "    "
              << "DTB: " << setw(4) << diemTB[i] << "  "
              << "XL: ";
         if (xepLoai[i] == 0)
            cout << "Xuat sac";
         else if (xepLoai[i] == 1)
            cout << "Gioi";
         else if (xepLoai[i] == 2)
            cout << "Kha";
         else if (xepLoai[i] == 3)
            cout << "TB";
         else if (xepLoai[i] == 4)
            cout << "Yeu";
         else if (xepLoai[i] == 5)
            cout << "Kem";
         cout << endl;
      }
   }
}


//---------------------------------------------------------
// PHUONG THUC PHU TRO
//---------------------------------------------------------
bool BangDiem::isNumericString(const char *str)
{
   for (int i = 0; i < strlen(str); ++i)
   {
      if (str[i] < '0' || str[i] > '9')
      {
         return false;
      }
   }
   return true;
}
//---------------------------------------------------------
void BangDiem::getStringInput(char *buffer, const int _BUFFER_SIZE, const char* label, const int _STRING_LENGTH)
{
   cout << label << ": ";
   cin.getline(buffer, _BUFFER_SIZE);
   while (strlen(buffer) >= _STRING_LENGTH)
   {
      cout << label << " qua dai. Xin nhap " << (char)tolower(label[0]) << (label + 1) << " ngan hon.\n";
      cout << label << ": ";
      cin.getline(buffer, _BUFFER_SIZE);
   }
}
//---------------------------------------------------------
bool BangDiem::hasUDoubleFormat(const char *str)
{
   bool hasDecimalPoint = false;

   for (const char *ptr = str; *ptr != 0; ++ptr)
   {
      if (*ptr < '0' || *ptr > '9')
      {
         if (*ptr == '.' && !hasDecimalPoint)
         {
            hasDecimalPoint = true;
         }
         else
         {
            return false;
         }
      }
   }
   return true;
}
//---------------------------------------------------------
bool BangDiem::hasLessThanNDecimalPlaces(const char *str, int decimaPlaces)
{
   if (!hasUDoubleFormat(str))
   {
      return false;
   }
   if (isNumericString(str))  //integer has .00 decimal places
   {
      return true;
   }
   int dotPos;  //decimal point position
   for (dotPos = 0; *(str + dotPos) != '\0'; ++dotPos)
   {
      if (*(str + dotPos) == '.')
      {
         break;
      }
   }
   if (strlen(str) - 1 - dotPos > decimaPlaces)
   {
      return false;
   }
   return true;
}
//---------------------------------------------------------
void BangDiem::getUDoubleInput(double &output, const char* label, double min, double max)
{
   char tempDouble[40];
   double result = 0;

   cout << label << ": ";
   cin.getline(tempDouble, 40);
   while (!hasLessThanNDecimalPlaces(tempDouble, 1))
   {
      cout << "ERROR: " << label << " co chua ky tu khong phai la so thap phan hoac co hon 1 so le\n";
      cout << label << ": ";
      cin.getline(tempDouble, 40);
   }
   result = atof(tempDouble);
   while (result < min && result > max)
   {
      cout << "ERROR: " << label << " qua lon hoac qua be.\n";
      cout << label << ": ";
      cin.getline(tempDouble, 40);
      while (!hasLessThanNDecimalPlaces(tempDouble, 1))
      {
         cout << "ERROR: " << label << " co chua ky tu khong phai la so thap phan.\n";
         cout << label << ": ";
         cin.getline(tempDouble, 40);
      }
      result = atof(tempDouble);
   }
   output = result;
}
//---------------------------------------------------------
[/spoil]

TestBangDiem.cpp
[spoil]
Mã:
#include <iostream>
#include "BangDiem.h"
using namespace std;

#define SOHSTOIDA 50

int main(int argc, char *argv[])
{
   //khoi tao danh sach
   BangDiem lopB(SOHSTOIDA);

   //hien thi tat ca thong tin hoc sinh trong bang diem
   cout << "lopB:\n" << lopB << endl;

   //nhap du lieu hoc sinh
   cout << endl;
   lopB.nhapDiemHS();

   //xem lai tat ca hoc sinh trong bang diem
   cout << endl;
   cout << "lopB:\n" << lopB << endl;

   //sap xep theo thu tu tang dan ho ten
   cout << endl;
   lopB.sapXepTangDanDiemTB();

   //xem lai tat ca hoc sinh trong bang diem
   cout << endl;
   cout << "lopB:\n" << lopB << endl;

   //hien thi tat ca hoc sinh xuat sac
   cout << endl;
   lopB.hienThiNhom(BangDiem::XuatSac);

   //hien thi tat ca hoc sinh gioi
   cout << endl;
   lopB.hienThiNhom(BangDiem::Gioi);

   //hien thi tat ca hoc sinh kha
   cout << endl;
   lopB.hienThiNhom(BangDiem::Kha);

   //hien thi tat ca hoc sinh TB
   cout << endl;
   lopB.hienThiNhom(BangDiem::TB);

   //hien thi tat ca hoc sinh kem
   cout << endl;
   lopB.hienThiNhom(BangDiem::Kem);

   //hien thi tat ca hoc sinh yeu
   cout << endl;
   lopB.hienThiNhom(BangDiem::Yeu);

   return 0;
}
[/spoil]
 
Back
Top