Đề bài : ThucHanh11_Class_KhoiTaoHuyCongCuKhac.pdf
Thiết kế và cài đặt lớp TamGiac với 3 thuộc tính là 3 điểm trong mặt phẳng tọa độ tương đương với 3 đỉnh của tam giác (sử dụng lớp Diem2D - đối tượng Diem2D là thuộc tính của lớp TamGiac). Yêu cầu phải có các hàm khởi tạo cần thiết.
Lớp cần có phương thức kiểm tra xem 3 đỉnh có thỏa mãn điều kiện tạo thành 1 tam giác không. Thêm các hàm trả về chu vi, diện tích nếu thỏa mãn. Thông báo xem đó là tam giác loại gì.
___________________________________________________________________
Code : TamGiac.rar
___________________________________________________________________
Diem2D.h
#ifndef Diem2D_H
#define Diem2D_H
#include <iostream>
using std::ostream;
class Diem2D{
private:
double x, y;
public:
Diem2D();
Diem2D(double x, double y);
Diem2D(const Diem2D& p);
double getX() const;
double getY() const;
double dist(const Diem2D& p);
friend ostream& operator<< (ostream&, const Diem2D&);
};
void in2DauPhay(double);
#endif
___________________________________________________________________
#include "Diem2D.h"#include <cmath>#include <iostream>using std::cout;using std::ostream;Diem2D::Diem2D(){ x = 0.0; y = 0.0;}Diem2D::Diem2D(double x, double y) : x(x), y(y){}Diem2D::Diem2D(const Diem2D& p){ Diem2D(p.x, p.y);}double Diem2D::getX() const{ return x;}double Diem2D::getY() const{ return y;}double Diem2D::dist(const Diem2D& p){ return sqrt((x - p.x) * (x - p.x) + (y - p.y) * (y - p.y));}void in2DauPhay(double soThuc){ int phanNguyen = int(soThuc); int phanThapPhan = int((soThuc - double(phanNguyen)) * 100); if (phanThapPhan < 10) cout << phanNguyen << ".0" << phanThapPhan; else cout << phanNguyen << "." << phanThapPhan;}ostream& operator<< (ostream& outStream, const Diem2D& diem){ outStream << "(" << diem.x << ", " << diem.y << ")"; return outStream;}___________________________________________________________________
#ifndef TAMGIAC_H#define TAMGIAC_H#include "Diem2D.h"#include <string>class TamGiac { private: Diem2D dinh1; Diem2D dinh2; Diem2D dinh3; public: TamGiac(); TamGiac(const Diem2D&, const Diem2D&, const Diem2D&); TamGiac(const TamGiac&); const Diem2D getDinh1(); const Diem2D getDinh2(); const Diem2D getDinh3(); const double getCanh12(); const double getCanh23(); const double getCanh31(); bool kiemTraTamGiac(); const double dienTich(); const double chuVi(); const std::string loaiTamGiac();}; #endif___________________________________________________________________
#include "Diem2D.h"#include "TamGiac.h"#include <iostream>#include <cmath>#include <string>TamGiac::TamGiac(){ dinh1 = Diem2D(); dinh2 = Diem2D(); dinh3 = Diem2D();}TamGiac::TamGiac(const Diem2D& dinh1, const Diem2D& dinh2, const Diem2D& dinh3){ this->dinh1 = dinh1; this->dinh2 = dinh2; this->dinh3 = dinh3;}TamGiac::TamGiac(const TamGiac& tg){ dinh1 = tg.dinh1; dinh2 = tg.dinh2; dinh3 = tg.dinh3;}const Diem2D TamGiac::getDinh1() {return Diem2D(dinh1.getX(), dinh1.getY());}const Diem2D TamGiac::getDinh2() {return Diem2D(dinh2.getX(), dinh2.getY());}const Diem2D TamGiac::getDinh3() {return Diem2D(dinh3.getX(), dinh3.getY());}const double TamGiac::getCanh12() {return dinh1.dist(dinh2);}const double TamGiac::getCanh23() {return dinh2.dist(dinh3);}const double TamGiac::getCanh31() {return dinh3.dist(dinh1);}bool TamGiac::kiemTraTamGiac(){ double canh12 = getCanh12(), canh23 = getCanh23(), canh31 = getCanh31(); if (((canh12 + canh23) > canh31) && ((canh23 + canh31) > canh12) && ((canh31 + canh12) > canh23)) return true; else return false;}const double TamGiac::dienTich(){ double canh12 = getCanh12(), canh23 = getCanh23(), canh31 = getCanh31(); double nuaChuVi = chuVi() / 2.0; return sqrt(nuaChuVi * (nuaChuVi - canh12) * (nuaChuVi - canh23) * (nuaChuVi - canh31));}const double TamGiac::chuVi(){ return (getCanh12() + getCanh23() + getCanh31());}const std::string TamGiac::loaiTamGiac(){ double canh12 = getCanh12(), canh23 = getCanh23(), canh31 = getCanh31(); double Vuong1 = 2 * dienTich() - canh12 * canh31; double Vuong2 = 2 * dienTich() - canh12 * canh23; double Vuong3 = 2 * dienTich() - canh23 * canh31; if (canh12 == canh23 && canh12 == canh31) return "Tam Giac Deu"; if ((Vuong1 == 0) || (Vuong2 == 0) || (Vuong3 == 0)) return "Tam Giac Vuong"; if ((canh12 == canh23) || (canh23 == canh31) || (canh31 == canh12)) return "Tam Giac Can"; else return "Tam Giac Thuong";} ___________________________________________________________________
#include <iostream>#include <cstdlib>#include "TamGiac.h"#include "Diem2D.h"using namespace std;int main(){ double x1, y1, x2, y2, x3, y3; cout << "Moi nhap toa do dinh thu nhat: "; cin >> x1 >> y1; cout << "Moi nhap toa do dinh thu hai: "; cin >> x2 >> y2; cout << "Moi nhap toa do dinh thu ba: "; cin >> x3 >> y3; Diem2D dinh1(x1, y1), dinh2(x2, y2), dinh3(x3, y3); TamGiac tg(dinh1, dinh2, dinh3); cout << endl << "Toa do 3 diem ban vua nhap la: " << endl; cout << "Diem1:" << tg.getDinh1() << endl << "Diem2:" << tg.getDinh2() << endl << "Diem3:" << tg.getDinh3() << endl; if (tg.kiemTraTamGiac()) { cout << endl << "3 diem tren la 3 dinh cua 1 tam giac." << endl; cout << "Dien Tich: " << tg.dienTich() << endl; cout << "Chu Vi: " << tg.chuVi() << endl; cout << "Loai: " << tg.loaiTamGiac() << endl; } else cout << endl << "3 diem tren khong phai 3 dinh cua 1 tam giac." << endl; system("PAUSE"); return 0;}_____________________________________________________________________