Hiển thị các bài đăng có nhãn bai tap oop. Hiển thị tất cả bài đăng
Hiển thị các bài đăng có nhãn bai tap oop. Hiển thị tất cả bài đăng

Thứ Sáu, 10 tháng 4, 2015

Viết định nghĩa lớp TamGiac ,quay, tịnh tiến, phóng thu






Viết định nghĩa lớp TamGiac để biểu diễn khái niệm tam giác trong mặt phẳng với các phương thức thiết lập, huỷ bỏ (nếu có). Các hàm thành phần nhập, xuất, tịnh tiến, quay, phóng to, thu nhỏ và vẽ tam giác (tùy chọn).
Tịnh tiến theo vector v (a,b) : x’= x +a; y’=y+b;
Phép quay tâm O, góc quay α
x’ = xcosα – y sinα;

y’= xsinα  + ycosα;

diem.h

#pragma once
#include<iostream>
using namespace std;
class diem
{
private:
float x,y;
public:
diem();
diem(float,float);
~diem(void);
void nhap();
void xuat();
void setx(float);
void sety(float);
diem vector(diem);
float getx();
float gety();
void setxy(float,float);
void tinhtien(float=0,float=0);
void quay(float);
};


tamgiac.h

#pragma once
#include"diem.h"
class tamgiac
{
private:
diem A,B,C;
public:
tamgiac();
tamgiac(diem,diem,diem);
~tamgiac(void);
void nhap();
void xuat();
void tinhtien(float=0,float=0);
void quay(float);
void thuphong(float);
};


diem.cpp

#include "diem.h"
diem::diem()
{
}
void diem::setx(float a)
{
x=a;
}
void diem::sety(float a)
{
y=a;
}
float diem::getx()
{
return x;
}
float diem::gety()
{
return y;
}
void diem::setxy(float a,float b)
{
x=a;
y=b;
}
diem::diem(float a,float b)
{
x=a;y=b;
}
void diem::nhap()
{
cout<<"\nhoanh do x=";cin>>x;
cout<<"tung do y=";cin>>y;
}
void diem::xuat()
{
cout<<"( "<<x<<" , "<<y<<" )";
}
diem::~diem(void)
{
}
void diem::tinhtien(float a,float b)
{
x+=a;
y+=b;
}
void diem::quay(float rad)
{
x=x*cos(rad)+y*sin(rad);
y=x*sin(rad)+y*cos(rad);
}
diem diem::vector(diem a)
{
diem t;
t.setx(a.getx()-x);
t.sety(a.gety()-y);
return t;
}

tamgiac.cpp

#include "tamgiac.h"

tamgiac::tamgiac()
{
}
tamgiac::tamgiac(diem x,diem y,diem z)
{
A=x;B=y;C=z;
}
tamgiac::~tamgiac(void)
{
}
void tamgiac::nhap()
{
int kt;
do{
kt=0;
cout<<"\nnhap dinh A";
A.nhap();
cout<<"\nnhap dinh B";
B.nhap();
cout<<"\nnhap dinh C";
C.nhap();
diem d,e;
d=A.vector(B);
e=A.vector(C);
if(float(d.getx()*e.gety())==float(d.gety()*e.getx()))
{
kt=1;
cout<<"\ntam giac khong ton tai! nhap lai!";
}
cout<<"\n";
}while(kt);
}
void tamgiac::xuat()
{
cout<<"\ntam giac tao boi 3 diem: ";
A.xuat();cout<<" , ";B.xuat();cout<<" , ";C.xuat();
}
void tamgiac::tinhtien(float m,float n)
{
A.tinhtien(m,n);
B.tinhtien(m,n);
C.tinhtien(m,n);
}
void tamgiac::quay(float rad)
{
A.quay(rad);
B.quay(rad);
C.quay(rad);
}
void tamgiac::thuphong(float k)
{
A.setxy(A.getx()*k,A.gety()*k);
B.setxy(B.getx()*k,B.gety()*k);
C.setxy(C.getx()*k,C.gety()*k);
}

main.cpp

#include"tamgiac.h"
void main()
{
float x,y,rad,k;
tamgiac a;
a.nhap();
a.xuat();
cout<<"\n\ntinh tien theo vector v(x,y):\nx=";cin>>x;
cout<<"y=";cin>>y;
a.tinhtien(x,y);
a.xuat();
cout<<"\nquay goc: rad=";cin>>rad;
a.quay(rad);
a.xuat();
cout<<"\nnhap he so thu phong k=";cin>>k;
a.thuphong(k);
a.xuat();
system("pause");
}

Thứ Năm, 5 tháng 2, 2015

[OOP C++] quản lý danh sách học sinh làm luận văn tốt nghiệp, thi tốt nghiệp, thi lại

[OOP C++] quản lý danh sách học sinh làm luận văn tốt nghiệp,  thi tốt nghiệp, thi lại
lập trình hướng đối tượng quản lý họ tên, năm sinh, điểm chín môn học của tất cả các học viên của lớp học. Cho biết bao nhiêu học viên trong lớp được phép làm luận văn tốt nghiệp, bao nhiêu học viên thi tốt nghiệp, bao nhiêu người phải thi lại và tên môn thi lại. Tiêu chuẩn để xét: Làm luận văn phải có điểm trung bình lớn hơn 7 trong đó không có môn nào dưới 5. Thi tốt nghiệp khi điểm trung bình không lớn hơn 7 và điểm các môn không dưới 5. Thi lại có môn dưới 5

hs.h

#pragma once
#include<iostream>
#include<stdlib.h>
using namespace std;
class hs
{
private:
char ten[100];
int namsinh;
float toan,ly,hoa,van,su,dia,anh,tin;
public:
void nhap();
void xuatten();
void xuatnamsinh();
void monthilai();
float dtb();
int hanhdong();
hs(void);
~hs(void);
};


hs.cpp

#include "hs.h"
void hs::nhap()
{
cout<<"\nTen: ";
fflush(stdin);
gets(ten);
do{
cout<<"\nnam sinh: ";
cin>>namsinh;
if(namsinh<1900 || namsinh >2100) cout<<"\nkhong hop le! nhap lai!!";
}while(namsinh<1900 || namsinh >2100);
cout<<"\nnhap diem cac mon:";
do{
cout<<"\nToan: ";cin>>toan;
if(toan<0 || toan>10) cout<<"\nkhong hop le! nhap lai!!";
}while(toan<0 || toan>10);
do{
cout<<"\nLy: ";cin>>ly;
if(ly<0 || ly>10) cout<<"\nkhong hop le! nhap lai!!";
}while(ly<0 || ly>10);
do{
cout<<"\nhoa: ";cin>>hoa;
if(hoa<0 || hoa>10) cout<<"\nkhong hop le! nhap lai!!";
}while(hoa<0 || hoa>10);
do{
cout<<"\nvan: ";cin>>van;
if(van<0 || van>10) cout<<"\nkhong hop le! nhap lai!!";
}while(van<0 || van>10);
do{
cout<<"\nsu: ";cin>>su;
if(su<0 || su>10) cout<<"\nkhong hop le! nhap lai!!";
}while(su<0 || su>10);
do{
cout<<"\ndia: ";cin>>dia;
if(dia<0 || dia>10) cout<<"\nkhong hop le! nhap lai!!";
}while(dia<0 || dia>10);
do{
cout<<"\nanh: ";cin>>anh;
if(anh<0 || anh>10) cout<<"\nkhong hop le! nhap lai!!";
}while(anh<0 || anh>10);
do{
cout<<"\ntin: ";cin>>tin;
if(tin<0 || tin>10) cout<<"\nkhong hop le! nhap lai!!";
}while(tin<0 || tin>10);
}
int hs::hanhdong()
{
if(toan<5 || ly<5 || hoa<5 || van<5 || su<5||dia<5||anh<5||tin<5) return -1;
if(dtb()>7) return 1;
return 0;
}
void hs::xuatten()
{
cout<<ten;
}
void hs::monthilai()
{
cout<<"\n*nhung mon thi lai:";
if(toan<5) cout<<"\n\ttoan";
if(ly<5) cout<<"\n\tly";
if(hoa<5) cout<<"\n\thoa";
if(van<5) cout<<"\n\tvan";
if(su<5) cout<<"\n\tsu";
if(dia<5) cout<<"\n\tdia";
if(anh<5) cout<<"\n\tanh";
if(tin<5) cout<<"\n\ttin";
}
void hs::xuatnamsinh()
{
cout<<namsinh;
}
float hs::dtb()
{
return (toan+ly+hoa+van+su+dia+anh+tin)/8;
}
hs::hs(void)
{
}


hs::~hs(void)
{
}


danhsachhs.h

#pragma once
#include"hs.h"
class danhsachhs
{
private:
hs x[1000];
int n;
public:
void nhap();
void xuatthilai();
void xuatthitotnghiep();
void xuatluanvan();
danhsachhs(void);
~danhsachhs(void);
};


danhsachhs.cpp

#include "danhsachhs.h"

void danhsachhs::nhap()
{
int k,i=1;
cout<<"\n*nhap hoc sinh 1";
x[0].nhap();
do{
cout<<"\n------menu-----";
cout<<"\n| 1.nhap tiep.|";
cout<<"\n| 0.ket thuc. |";
cout<<"\n---------------";
do{
cout<<"\nlua chon cua ban: ";
cin>>k;
}while(k!=1 && k!=0);
if(k==1)
{
cout<<"\n\t* nhap hoc sinh "<<i+1;
x[i++].nhap();
}
}while(k==1);
n=i;
}
void danhsachhs::xuatthilai()
{
cout<<"\n\t* DANH SACH THI LAI *\n";
int i,j=0;
for(i=0;i<n;i++)
if(x[i].hanhdong()==-1)
{
j++;
cout<<"\n"<<j<<". ";x[i].xuatten();cout<<" - ";x[i].xuatnamsinh();cout<<"\n";
x[i].monthilai();
}
if(j==0) cout<<"\n\tkhong co!";
}
void danhsachhs::xuatthitotnghiep()
{
cout<<"\n\t* DANH SACH THI TOT NGHIEP *\n";
int i,j=0;
for(i=0;i<n;i++)
if(x[i].hanhdong()==0)
{
j++;
cout<<"\n"<<j<<". ";x[i].xuatten();cout<<" - ";x[i].xuatnamsinh();cout<<"\n";
}
if(j==0) cout<<"\n\tkhong co!\n";
}
void danhsachhs::xuatluanvan()
{
cout<<"\n\t* DANH SACH LAM LUAN VAN TOT NGHIEP *\n";
int i,j=0;
for(i=0;i<n;i++)
if(x[i].hanhdong()==1)
{
j++;
cout<<"\n"<<j<<". ";x[i].xuatten();cout<<" - ";x[i].xuatnamsinh();cout<<"\n";
}
if(j==0) cout<<"\n\tkhong co!\n";
}
danhsachhs::danhsachhs(void)
{
}


danhsachhs::~danhsachhs(void)
{
}


source.cpp

#include"danhsachhs.h"
void main()
{
danhsachhs x;
x.nhap();
cout<<"\n-------------------------------------------------------\n";
x.xuatluanvan();
cout<<"\n-------------------------------------------------------\n";
x.xuatthitotnghiep();
cout<<"\n-------------------------------------------------------\n";
x.xuatthilai();
system("pause");
}

Thứ Ba, 3 tháng 2, 2015

[OOP C++] tìm ma trận nghịch đảo của mt vuông cấp n

lập trình hướng đối tượng tìm ma trận nghịch đảo của 1 ma trận vuông cấp n tùy ý

matran.h

#pragma once
#include<iostream>
#include<iomanip>
using namespace std;
class matran
{
private:
float a[100][100];
int n;
public:
matran(void);
~matran(void);
void nhap();
void xuat();
float get(int,int);
void set(float,int,int);
void setn(int);
float det();
float con(int,int);
void nghichdao();
};


matran.cpp

#include "matran.h"
matran::matran(void)
{
}
matran::~matran(void)
{
}
void matran::nhap()
{
do{
cout<<"\n*nhap cap ma tran N=";
cin>>n;
if(n<=0) cout<<"khong hop le! nhap lai!";
}while(n<=0);
for(int i=0;i<n;i++)
{
cout<<"\nnhap hang "<<i+1<<":\n";
for(int j=0;j<n;j++)
cin>>a[i][j];
}
cout<<"\n*ma tran da nhap la:\n";
xuat();
cout<<"\n---------------------------------------\n";
}
void matran::xuat()
{
for(int i=0;i<n;i++)
{
cout<<"\n\n";
for(int j=0;j<n;j++)
cout<<setw(10)<<a[i][j];
}
}
float matran::get(int i,int j)
{
return a[i][j];
}
void matran::set(float x,int i,int j)
{
a[i][j]=x;
}
float matran::det()
{
int i,j,k,dem=0,kt;
float b[100],kq=1,h;
for(i=0;i<n-1;i++)
{
if(a[i][i]==0)
{
kt=0;
for(j=i+1;j<n;j++)
{
if(a[i][j]!=0)
{
for(k=0;k<n;k++)
{
h=a[k][i];
a[k][i]=a[k][j];
a[k][j]=h;
}
dem++;
kt=1;
break;
}
if(kt==0) return 0;
}
}
b[i]=a[i][i];
for(j=0;j<n;j++) a[i][j]/=b[i];
for(j=i+1;j<n;j++)
{
h=a[j][i];
for(k=0;k<n;k++) a[j][k]=a[j][k]-h*a[i][k];
}
}
b[n-1]=a[n-1][n-1];
for(i=0;i<n;i++) kq*=b[i];
if(dem%2==0) return kq;
return -kq;
}
void matran::setn(int x)
{
n=x;
}
float matran::con(int h,int c)
{
matran b;
b.setn(n-1);
int i,j,x=-1,y;
for(i=0;i<n;i++)
{
if(i==h) continue;
x++;y=-1;
for(j=0;j<n;j++)
{
if(j==c)continue;
y++;
b.set(a[i][j],x,y);
}
}
if((h+c)%2==0) return b.det();
return -b.det();
}
void matran::nghichdao()
{
matran x;
int i,j;
float t;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
x.set(con(i,j),i,j);
x.setn(n);
for(i=0;i<n;i++)
for(j=i+1;j<n;j++)
{
t=x.get(i,j);
x.set(x.get(j,i),i,j);
x.set(t,j,i);
}
float k=det();
if(k==0) cout<<"\n*khong co ma tran nghich dao!";
else
{
for(i=0;i<n;i++)
for(j=0;j<n;j++)
x.set(x.get(i,j)/k,i,j);
cout<<"\n*ma tran nghich dao la:\n";
x.xuat();
}
}

source.cpp

#include"matran.h"
void main()
{
matran a;
a.nhap();
a.nghichdao();
system("pause");
}

Thứ Hai, 2 tháng 2, 2015

[OOP C++] tính tổng các phân số - lập trình hướng đối tượng

[OOP C++] tính tổng các phân số - lập trình hướng đối tượng

phanso.h

#pragma once
#include<iostream>
#include<stdlib.h>
#include<string.h>
using namespace std;
class phanso
{
private:
int tu,mau;
public:
phanso(void);
~phanso(void);
void settu(int);
void setmau(int);
int gettu();
int getmau();
int rutgon();
void nhapps();
void xuatps();
};


phanso.cpp

#include "phanso.h"


phanso::phanso(void)
{
}


phanso::~phanso(void)
{
}
void phanso::nhapps()
{
char s[100];
int i,j,k,kt,dem;
do{
kt=0;dem=0;k=0;
cout<<"\nnhap phan so dang a/b: ";
fflush(stdin);
gets(s);
for(i=0;i<strlen(s);i++)
if((s[i]>='0' && s[i]<='9') || s[i]=='/')
{
dem++;
if(s[i]=='/') k++;
}
if(dem!=strlen(s) || k!=1)
{
cout<<"\nloi dinh dang! nhap lai!!";
kt=1;
}
}while(kt);
char a[100];
i=0;k=0;
while(s[i]!='/')
a[k++]=s[i++];
a[k]='\0';
tu=atoi(a);
i++;k=0;
while(i<strlen(s)) a[k++]=s[i++];
a[k]='\0';
mau=atoi(a);
}
void phanso::xuatps()
{
cout<<tu<<"/"<<mau;
}
void phanso::settu(int x)
{
tu=x;
}
void phanso::setmau(int x)
{
mau=x;
}
int phanso::gettu()
{
return tu;
}
int phanso::getmau()
{
return mau;
}
int phanso::rutgon()
{
int a=abs(tu),b=abs(mau);
while(a!=b)
if(a>b) a-=b;
else b-=a;
if(a==1) return 0;
tu/=a;
mau/=a;
return 1;
}

mangps.h

#pragma once
#include"phanso.h"
class mangps
{
private:
phanso a[100];
int n;
public:
mangps(void);
~mangps(void);
void nhapmangps();
void xuatmangps();
void tong();
};



mangps.cpp


#include "mangps.h"


mangps::mangps(void)
{
}


mangps::~mangps(void)
{
}
void mangps::nhapmangps()
{
int kt,i=0,dem=0,k;
cout<<"\n *nhap phan so thu "<<i+1<<":";
a[i].nhapps();
i++;
nhap:;
cout<<"\n *nhap phan so thu "<<i+1<<":";
a[i].nhapps();
loop:;
cout<<"\n-----menu-----------";
cout<<"\n| 1. nhap tiep.    |";
cout<<"\n| 0. xong.         |";
cout<<"\n--------------------";
cout<<"\nlua chon cua ban: ";
cin>>k;
if(k!=0 && k!=1)
{
cout<<"\nkhong hop le!!!";
goto loop;
}
if(k==1)
{
i++;
goto nhap;
}
n=i+1;
}
void mangps::xuatmangps()
{
for(int i=0;i<n;i++)
{
if(i>=1) cout<<" + ";
a[i].xuatps();
}
}
void mangps::tong()
{
int i;
phanso x;
x.settu(a[0].gettu());
x.setmau(a[0].getmau());
for(i=1;i<n;i++)
{
x.settu(x.gettu()*a[i].getmau()+ x.getmau()*a[i].gettu());
x.setmau(x.getmau()*a[i].getmau());
}
xuatmangps();cout<<" = ";x.xuatps();
if(i=x.rutgon()) 
{
cout<<" = ";x.xuatps();
}
}

source.cpp

#include"mangps.h"
void main()
{
mangps x;
x.nhapmangps();
cout<<"\ntong cac phan so: \n";
x.tong();
system("pause");
}