lâp trình oop c++ định nghĩa và nhập xuất điểm trong tọa độ oxy , định nghĩa lớp điểm theo hướng đối tượng c++.nhập xuất nạp chồng toán tử
Showing posts with label bai tap oop. Show all posts
Showing posts with label bai tap oop. Show all posts
Friday, May 29, 2015
Tuesday, May 26, 2015
class MYINT định nghĩa kiểu dữ liệu giống int c/c++
lập trình oop c++ định nghĩa lớp MYINT có hoạt động như kiểu dữ liệu 'int' nhưng phép cộng hai MYINT hoạt động như phép trừ hai int và ngược lại.
MYINT.h
MYINT.h
#pragma once
#include<iostream>
using namespace std;
class MYINT
{
private:
float x;
public:
MYINT(void);
~MYINT(void);
friend ostream &operator<<(ostream &out,const MYINT&);
friend istream &operator>>(istream &in,MYINT&);
MYINT operator+(const MYINT&);
MYINT operator-(const MYINT&);
MYINT operator*(const MYINT&);
MYINT operator/(const MYINT&);
void operator++();
void operator--();
};
MYINT.cpp
#include "MYINT.h"
MYINT::MYINT(void)
{
}
MYINT::~MYINT(void)
{
}
ostream &operator<<(ostream &out,const MYINT &a)
{
out<<a.x;
return out;
}
istream &operator>>(istream &in,MYINT &a)
{
in>>a.x;
return in;
}
MYINT MYINT::operator+(const MYINT &a)
{
MYINT z;
z.x=this->x-a.x;
return z;
}
MYINT MYINT::operator-(const MYINT &a)
{
MYINT z;
z.x=this->x+a.x;
return z;
}
MYINT MYINT::operator*(const MYINT &a)
{
MYINT z;
z.x=this->x*a.x;
return z;
}
MYINT MYINT::operator/(const MYINT &a)
{
MYINT z;
z.x=this->x/a.x;
return z;
}
void MYINT::operator++()
{
x++;
}
void MYINT::operator--()
{
x--;
}
main.cpp
#include"MYINT.h"
void main()
{
MYINT x,y;
cin>>x>>y;
cout<<x+y<<endl;
cout<<x-y<<endl;
system("pause");
}
class INTEGER định nghĩa kiểu dữ liệu giống int của C/C++
lập trình oop c++ định nghĩa lớp INTEGER có thể hoạt động như để mỗi INTEGER giống hệt như một 'int' của ngôn ngữ C/C++.(tự định nghĩa lại kiểu dữ liệu int)
INTEGER.h
INTEGER.h
#pragma once
#include<iostream>
using namespace std;
class INTEGER
{
private:
float x;
public:
INTEGER(void);
~INTEGER(void);
friend ostream &operator<<(ostream &out,const INTEGER&);
friend istream &operator>>(istream &in,INTEGER&);
INTEGER operator+(const INTEGER&);
INTEGER operator-(const INTEGER&);
INTEGER operator*(const INTEGER&);
INTEGER operator/(const INTEGER&);
void operator++();
void operator--();
};
INTEGER.cpp
#include "INTEGER.h"
INTEGER::INTEGER(void)
{
}
INTEGER::~INTEGER(void)
{
}
ostream &operator<<(ostream &out,const INTEGER &a)
{
out<<a.x;
return out;
}
istream &operator>>(istream &in,INTEGER &a)
{
in>>a.x;
return in;
}
INTEGER INTEGER::operator+(const INTEGER &a)
{
INTEGER z;
z.x=this->x+a.x;
return z;
}
INTEGER INTEGER::operator-(const INTEGER &a)
{
INTEGER z;
z.x=this->x-a.x;
return z;
}
INTEGER INTEGER::operator*(const INTEGER &a)
{
INTEGER z;
z.x=this->x*a.x;
return z;
}
INTEGER INTEGER::operator/(const INTEGER &a)
{
INTEGER z;
z.x=this->x/a.x;
return z;
}
void INTEGER::operator++()
{
x++;
}
void INTEGER::operator--()
{
x--;
}
main.cpp
#include"INTEGER.h"
void main()
{
INTEGER x,y;
cin>>x>>y;
cout<<x+y<<endl;
cout<<x-y<<endl;
cout<<x*y<<endl;
cout<<x/y<<endl;
x++;y--;
cout<<x<<" "<<y;
system("pause");
}
class CString biểu diễn khái niệm chuỗi ký tự
Hãy định nghĩa lớp CString biểu diễn khái niệm chuỗi ký tự với các phương thức thiết lập, huỷ bỏ, các hàm thành phần và các phép toán cần thiết (+, gán, so sánh hai chuỗi).
cstring.h
cstring.h
#pragma once
#include<iostream>
#include<string>
using namespace std;
class cstring
{
private:
string s;
public:
cstring();
~cstring();
friend ostream &operator<<(ostream &out,const cstring &a);
friend istream &operator>>(istream &in,cstring &a);
cstring operator+(const cstring &a);
void operator=(const cstring &a);
bool operator<(const cstring &a);
bool operator<=(const cstring &a);
bool operator>(const cstring &a);
bool operator>=(const cstring &a);
bool operator==(const cstring &a);
bool operator!=(const cstring &a);
};
cstring.cpp
#include"cstring.h"
cstring::cstring()
{
}
cstring::~cstring()
{
}
ostream &operator<<(ostream &out,const cstring &a)
{
out<<a.s;
return out;
}
istream &operator>>(istream &in,cstring &a)
{
getline(in,a.s);
return in;
}
cstring cstring::operator+(const cstring &a)
{
cstring z;
z.s=s;
z.s+=a.s;
return z;
}
void cstring::operator=(const cstring &a)
{
s=a.s;
}
bool cstring::operator<(const cstring &a)
{
if(s<a.s) return true;
return false;
}
bool cstring::operator<=(const cstring &a)
{
if(s<=a.s) return true;
return false;
}
bool cstring::operator>(const cstring &a)
{
if(s>a.s) return true;
return false;
}
bool cstring::operator>=(const cstring &a)
{
if(s>=a.s) return true;
return false;
}
bool cstring::operator==(const cstring &a)
{
if(s==a.s) return true;
return false;
}
bool cstring::operator!=(const cstring &a)
{
if(s!=a.s) return true;
return false;
}
main.cpp
#include"cstring.h"
void main()
{
cstring s,x;
cin>>s>>x;
if(s>x) cout<<"OK";
system("pause");
}
Friday, May 22, 2015
class CDate biểu diễn ngày tháng năm và các phép toán+-
5. Định nghĩa lớp CDate biểu diễn khái niệm ngày, tháng, năm với các phép toán +, - (cộng, trừ thêm một số ngày), ++, -- (thêm bớt một ngày), - (khoảng cách giữa hai CDate tính bằng ngày). Phép toán <<, >> để xuất, nhập dữ liệu loại CDate.
cdate.h
cdate.h
#pragma once
#include<iostream>
using namespace std;
class cdate
{
private:
int ngay,thang,nam;
public:
cdate(void);
~cdate(void);
bool check(const int&,const int&,const int&);
cdate operator+(const int&);
cdate operator-(const int&);
long long operator-(const cdate&);
void operator++();
void operator--();
friend ostream &operator<<(ostream &out,const cdate&);
friend istream &operator>>(istream &in,cdate &);
};
cdate.cpp
#include "cdate.h"
cdate::cdate(void)
{
}
cdate::~cdate(void)
{
}
bool cdate::check(const int &a,const int &b,const int &c)
{
switch(b)
{
case 1:case 3:case 5: case 7: case 8: case 10: case 12:
if(a<=0 || a>31) return false;
break;
case 4: case 6: case 9: case 11:
if(a<=0 || a>30) return false;
break;
case 2:
if(c%400==0 ||(c%4==0 && c%100!=0))
{
if(a<=0 || a>29) return false;
}
else if(a<=0 || a>28) return false;
break;
default: return false;
}
return true;
}
istream &operator>>(istream &in,cdate &d)
{
bool k;
do{
k=false;
cout<<"ngay =";in>>d.ngay;
cout<<"thang=";in>>d.thang;
cout<<"nam =";in>>d.nam;
k=d.check(d.ngay,d.thang,d.nam);
if(k==false) cout<<"khong hop le!\n";
}while(!k);
return in;
}
cdate cdate::operator+(const int &x)
{
cdate d;
d.ngay=ngay;
d.thang=thang;
d.nam=nam;
int k=d.ngay+x;
do{
switch(d.thang)
{
case 1:case 3:case 5: case 7:case 8: case 10: case 12:
if(k>31)
{
d.thang++;
if(d.thang==13)
{
d.thang=1;
d.nam++;
}
k-=31;
}
else
{
d.ngay=k;
k=0;
}
break;
case 4:case 6: case 9: case 11:
if(k>30)
{
d.thang++;
k-=30;
}
else
{
d.ngay=k;
k=0;
}
break;
case 2:
if(d.nam%400==0 || (d.nam%4==0 && d.nam%100!=0))
{
if(k>29)
{
d.thang++;
k-=29;
}
else
{
d.ngay=k;
k=0;
}
}
else
{
if(k>28)
{
d.thang++;
k-=28;
}
else
{
d.ngay=k;
k=0;
}
}
break;
}
}while(k>0);
return d;
}
ostream &operator<<(ostream &out,const cdate& d)
{
out<<d.ngay<<"/"<<d.thang<<"/"<<d.nam;
return out;
}
cdate cdate::operator-(const int &a)
{
cdate d;
d.ngay=ngay;
d.thang=thang;
d.nam=nam;
int x=a;
do{
if(d.ngay>x)
{
d.ngay-=x;
x=0;
}
else
{
x-=d.ngay;
d.thang--;
switch(d.thang)
{
case 1:case 3: case 5: case 7: case 8: case 10:
d.ngay=31;
break;
case 4: case 6: case 9: case 11:
d.ngay=30;
break;
case 2:
if(d.nam%400==0 || (d.nam%4==0 && d.nam%100!=0)) d.ngay=29;
else d.ngay=28;
break;
case 0:
d.thang=12;
d.nam--;
d.ngay=31;
break;
}
}
}while(x>0);
return d;
}
long long cdate::operator-(const cdate &d)
{
cdate x;
x.ngay=ngay;
x.thang=thang;
x.nam=nam;
long long a=x.ngay;
do{
switch(x.thang)
{
case 1: case 3: case 5: case 7: case 8: case 10: case 12:
a+=31;
x.thang--;
if(x.thang==0)
{
x.nam--;
x.thang=12;
}
break;
case 4: case 6: case 9: case 11:
a+=30;
x.thang--;
break;
case 2:
if(x.nam%400==0 || (x.nam%4==0 && x.nam%100!=0))
{
a+=29;
x.thang--;
}
else
{
a+=28;
x.thang--;
}
}
}while(x.nam>0);
x.ngay=d.ngay;
x.thang=d.thang;
x.nam=d.nam;
long long b=x.ngay;
do{
switch(x.thang)
{
case 1: case 3: case 5: case 7: case 8: case 10: case 12:
b+=31;
x.thang--;
if(x.thang==0)
{
x.nam--;
x.thang=12;
}
break;
case 4: case 6: case 9: case 11:
b+=30;
x.thang--;
break;
case 2:
if(x.nam%400==0 || (x.nam%4==0 && x.nam%100!=0))
{
b+=29;
x.thang--;
}
else
{
b+=28;
x.thang--;
}
}
}while(x.nam>0);
return a-b;
}
void cdate::operator++()
{
ngay++;
switch(thang)
{
case 1: case 3: case 5: case 7: case 8: case 10: case 12:
if(ngay>31)
{
ngay=1;
thang++;
if(thang>12)
{
nam++;
thang=1;
}
}
break;
case 4: case 6: case 9: case 11:
if(ngay>30)
{
ngay=1;
thang++;
}
break;
case 2:
if(nam%400==0 || (nam%4==0 && nam%100!=0))
{
if(ngay>29)
{
ngay=1;
thang++;
}
}
else
{
if(ngay>28)
{
ngay=1;
thang++;
}
}
}
}
void cdate::operator--()
{
ngay--;
if(ngay==0)
{
thang--;
switch(thang)
{
case 1: case 3: case 5: case 7: case 8: case 10:
ngay=31;
break;
case 0:
ngay=31;
thang=12;
nam--;
break;
case 4: case 6: case 9: case 11:
ngay=30;
break;
case 2:
if(nam%400==0 || (nam%4==0 && nam%100!=0)) ngay=29;
else ngay=28;
break;
}
}
}
main.cpp
#include"cdate.h"
void main()
{
cdate x,y;
cin>>x;
x++;
cout<<x<<endl;
x--;
cout<<x;
system("pause");
}
class CTime biểu diễn giờ phút giây và các phép toán
4. Định nghĩa lớp CTime biểu diễn khái niệm thời điểm có các thành phần giờ phút giây. Định nghĩa các phép toán +, - (cộng, trừ thêm một số nguyên giây), - (phép trừ hai CTime để được một CTimSpan), ++, -- (thêm bớt một giây). Phép toán <<, >> để xuất, nhập dữ liệu loại CTime.
ctime.h
ctime.h
#pragma once
#include<iostream>
using namespace std;
class ctime
{
private:
int hour,minutes,second;
public:
ctime(int=0,int=0,int=0);
~ctime(void);
ctime operator+(const int&);
ctime operator-(const int&);
void operator++();
void operator--();
friend ostream &operator<<(ostream &out,const ctime &a);
friend istream &operator>>(istream &in,ctime &a);
};
ctime.cpp
#include "ctime.h"
ctime::ctime(int a,int b,int c)
{
hour=(a<0 || a>23)?0:a;
minutes=(b<0 || b>59)?0:b;
second=(c<0 || c>59) ? 0:c;
}
ctime::~ctime(void)
{
}
ctime ctime::operator+(const int& x)
{
ctime c;
int a,b;
a=(this->second+x)/60;
c.second=(this->second+x)%60;
b=(this->minutes+a)/60;
c.minutes=(this->minutes+a)%60;
c.hour=hour+b;
return c;
}
ostream &operator<<(ostream &out,const ctime &a)
{
if(a.hour<0 || a.minutes<0||a.second<0) out<<"-";
out<<(a.hour<10 && a.hour>=0?"0":"")<<abs(a.hour)<<":"
<<(a.minutes<10 && a.minutes>=0?"0":"")<<abs(a.minutes)<<":"
<<(a.second<10 && a.second>=0?"0":"")<<abs(a.second);
return out;
}
istream &operator>>(istream &in,ctime &a)
{
in>>a.hour>>a.minutes>>a.second;
return in;
}
ctime ctime::operator-(const int&x)
{
ctime c;
int a,b;
a=(this->second-x<0)?-1:0;
c.second=(a==-1)?60+second-x:second-x;
b=(minutes+a)<0?-1:0;
c.minutes=(b==-1)?60+minutes+a:minutes+a;
c.hour=b+hour;
if(c.hour<0)
{
a=(-this->second+x<0)?-1:0;
c.second=(a==-1)?60-second+x:-second+x;
b=(-minutes+a)<0?-1:0;
c.minutes=(b==-1)?60-minutes+a:-minutes+a;
c.hour=-(b-hour);
if(c.hour==0)
{
if(c.minutes!=0) c.minutes=0-c.minutes;
else if(c.second!=0) c.second=0-c.second;
}
return c;
}
return c;
}
void ctime::operator++()
{
ctime c;
int a,b;
a=(this->second+1)/60;
c.second=(this->second+1)%60;
b=(this->minutes+a)/60;
c.minutes=(this->minutes+a)%60;
c.hour=hour+b;
hour=c.hour;
minutes=c.minutes;
second=c.second;
}
void ctime::operator--()
{
ctime c;
int a,b;
a=(this->second-1<0)?-1:0;
c.second=(a==-1)?60+second-1:second-1;
b=(minutes+a)<0?-1:0;
c.minutes=(b==-1)?60+minutes+a:minutes+a;
c.hour=b+hour;
if(c.hour<0)
{
a=(-this->second+1<0)?-1:0;
c.second=(a==-1)?60-second+1:-second+1;
b=(-minutes+a)<0?-1:0;
c.minutes=(b==-1)?60-minutes+a:-minutes+a;
c.hour=-(b-hour);
if(c.hour==0)
{
if(c.minutes!=0) c.minutes=0-c.minutes;
else if(c.second!=0) c.second=0-c.second;
}
hour=c.hour;
minutes=c.minutes;
second=c.second;
}
hour=c.hour;
minutes=c.minutes;
second=c.second;
}
main.cpp
#include"ctime.h"
void main()
{
ctime x;
cin>>x;
x++;
cout<<x<<endl;
x--;
cout<<x<<endl;;
system("pause");
}
Wednesday, May 20, 2015
class ctimespan biểu diễn thời gian trong OOP C++
Định nghĩa lớp dữ liệu CTimeSpan để biểu diễn khái niệm khoảng thời gian, các hàm thành phần và các phép toán cần thiết.
ctimespan.h
ctimespan.h
#pragma once
#include<iostream>
using namespace std;
class ctimespan
{
private:
int hour,minutes,second;
public:
ctimespan(int=0,int=0,int=0);
~ctimespan(void);
ctimespan operator+(const ctimespan&);
ctimespan operator-(const ctimespan&);
bool operator<(ctimespan&);
bool operator<=(ctimespan&);
bool operator>(ctimespan&);
bool operator>=(ctimespan&);
bool operator==(ctimespan&);
bool operator!=(ctimespan&);
friend ostream &operator<<(ostream &out,const ctimespan &a);
friend istream &operator>>(istream &in,ctimespan &a);
};
ctimespan.cpp
#include "ctimespan.h"
ctimespan::ctimespan(int a,int b,int c)
{
hour=(a<0 || a>23)?0:a;
minutes=(b<0 || b>59)?0:b;
second=(c<0 || c>59) ? 0:c;
}
ctimespan::~ctimespan(void)
{
}
ctimespan ctimespan::operator+(const ctimespan &x)
{
ctimespan c;
int a,b;
a=(this->second+x.second)/60;
c.second=(this->second+x.second)%60;
b=(this->minutes+x.minutes+a)/60;
c.minutes=(this->minutes+x.minutes+a)%60;
c.hour=hour+b+x.hour;
return c;
}
ostream &operator<<(ostream &out,const ctimespan &a)
{
if(a.hour<0 || a.minutes<0||a.second<0) out<<"-";
out<<(a.hour<10 && a.hour>=0?"0":"")<<abs(a.hour)<<":"
<<(a.minutes<10 && a.minutes>=0?"0":"")<<abs(a.minutes)<<":"
<<(a.second<10 && a.second>=0?"0":"")<<abs(a.second);
return out;
}
istream &operator>>(istream &in,ctimespan &a)
{
in>>a.hour>>a.minutes>>a.second;
return in;
}
ctimespan ctimespan::operator-(const ctimespan&x)
{
ctimespan c;
int a,b;
a=(this->second-x.second<0)?-1:0;
c.second=(a==-1)?60+second-x.second:second-x.second;
b=(minutes-x.minutes+a)<0?-1:0;
c.minutes=(b==-1)?60+minutes-x.minutes+a:minutes-x.minutes+a;
c.hour=b+hour-x.hour;
if(c.hour<0)
{
a=(-this->second+x.second<0)?-1:0;
c.second=(a==-1)?60-second+x.second:-second+x.second;
b=(-minutes+x.minutes+a)<0?-1:0;
c.minutes=(b==-1)?60-minutes+x.minutes+a:-minutes+x.minutes+a;
c.hour=-(b-hour+x.hour);
return c;
}
return c;
}
bool ctimespan::operator<(ctimespan&x)
{
ctimespan c;
int a,b;
a=(this->second-x.second<0)?-1:0;
c.second=(a==-1)?60+second-x.second:second-x.second;
b=(minutes-x.minutes+a)<0?-1:0;
c.minutes=(b==-1)?60+minutes-x.minutes+a:minutes-x.minutes+a;
c.hour=b+hour-x.hour;
if(c.hour<0) return true;return false;
}
bool ctimespan::operator<=(ctimespan&x)
{
ctimespan c;
int a,b;
a=(this->second-x.second<0)?-1:0;
c.second=(a==-1)?60+second-x.second:second-x.second;
b=(minutes-x.minutes+a)<0?-1:0;
c.minutes=(b==-1)?60+minutes-x.minutes+a:minutes-x.minutes+a;
c.hour=b+hour-x.hour;
if(c.hour<=0) return true;return false;
}
bool ctimespan::operator>(ctimespan&x)
{
ctimespan c;
int a,b;
a=(this->second-x.second<0)?-1:0;
c.second=(a==-1)?60+second-x.second:second-x.second;
b=(minutes-x.minutes+a)<0?-1:0;
c.minutes=(b==-1)?60+minutes-x.minutes+a:minutes-x.minutes+a;
c.hour=b+hour-x.hour;
if(c.hour>=0 && (c.minutes!=0 || c.second!=0)) return true;return false;
}
bool ctimespan::operator>=(ctimespan&x)
{
ctimespan c;
int a,b;
a=(this->second-x.second<0)?-1:0;
c.second=(a==-1)?60+second-x.second:second-x.second;
b=(minutes-x.minutes+a)<0?-1:0;
c.minutes=(b==-1)?60+minutes-x.minutes+a:minutes-x.minutes+a;
c.hour=b+hour-x.hour;
if(c.hour>=0 && c.minutes>=0 && c.second>=0) return true;return false;
}
bool ctimespan::operator==(ctimespan&x)
{
if(second==x.second && minutes==x.minutes && hour==x.hour) return true;return false;
}
bool ctimespan::operator!=(ctimespan&x)
{
if(second!=x.second || minutes!=x.minutes || hour!=x.hour) return true;return false;
}
main.cpp
#include"ctimespan.h"
void main()
{
ctimespan x(1,1,1),y(28,28,28);
cout<<x+y<<endl;
cout<<x-y<<endl;
if(x<y) cout<<1; else cout<<0;
if(x<=y) cout<<1; else cout<<0;
if(x>y) cout<<1; else cout<<0;
if(x>=y) cout<<1; else cout<<0;
if(x==y) cout<<1; else cout<<0;
if(x!=y) cout<<1; else cout<<0;
system("pause");
}
Saturday, May 16, 2015
[OOP C++] quản lý nhân viên và tính lương cho từng nhân viên
bài tập OOP C++. Giả sử Công ty có hai loại nhân viên: Nhân viên văn phòng và Nhân viên sản xuất. Viết chương trình quản lý và tính lương cho từng nhân viên của công ty: Mỗi nhân viên cần quản lý các thông tin sau: Họ tên, ngày sinh, lương Công ty cần tính lương cho nhân viên như sau: - Đối với nhân viên sản xuất: Lương=lương căn bản + số sản phẩm * 5.000 - Đối nhân viên văn phòng: lương = số ngày làm việc * 100.000
nhanvien.h
nhanvien.h
#pragma once
#include<iostream>
using namespace std;
class nhanvien
{
protected:
char hoten[100];
int ngay,thang,nam;
public:
nhanvien(void);
~nhanvien(void);
virtual void nhap();
virtual void xuat();
virtual long luong();
};
nhanvien.cpp
#include "nhanvien.h"
nhanvien::nhanvien(void)
{
}
nhanvien::~nhanvien(void)
{
}
void nhanvien::nhap()
{
cout<<"ho ten:";fflush(stdin);gets(hoten);
cout<<"ngay sinh d/m/y :";
cin>>ngay;cin.ignore();cin>>thang;cin.ignore();cin>>nam;
}
void nhanvien::xuat()
{
cout<<hoten<<endl<<"ngay sinh: "<<ngay<<"/"<<thang<<"/"<<nam<<endl;
}
long nhanvien::luong()
{
return 0;
}
sanxuat.h
#pragma once
#include "nhanvien.h"
class sanxuat :
virtual public nhanvien
{
private:
long luongcoban,sosp;
public:
sanxuat(void);
~sanxuat(void);
virtual void nhap();
virtual void xuat();
virtual long luong();
};
sanxuat.cpp
#include "sanxuat.h"
sanxuat::sanxuat(void)
{
}
sanxuat::~sanxuat(void)
{
}
void sanxuat::nhap()
{
nhanvien::nhap();
cout<<"luong co ban:";cin>>luongcoban;
cout<<"so san pham:";cin>>sosp;
}
void sanxuat::xuat()
{
nhanvien::xuat();
cout<<"luong co ban:"<<luongcoban<<endl<<"so san pham :"<<sosp<<endl;
}
long sanxuat::luong()
{
return luongcoban+sosp*5000;
}
vanphong.h
#pragma once
#include "nhanvien.h"
class vanphong :
virtual public nhanvien
{
private:
int songay;
public:
vanphong(void);
~vanphong(void);
virtual void nhap();
virtual void xuat();
virtual long luong();
};
canphong.cpp
#include "vanphong.h"
vanphong::vanphong(void)
{
}
vanphong::~vanphong(void)
{
}
void vanphong::nhap()
{
nhanvien::nhap();
cout<<"so ngay lam viec: ";cin>>songay;
}
void vanphong::xuat()
{
nhanvien::xuat();
cout<<"so ngay lam viec:"<<songay<<endl;
}
long vanphong::luong()
{
return songay*100000;
}
quanlynhanvien.h
#pragma once
#include"nhanvien.h"
#include<vector>
using namespace std;
class quanlynhanvien
{
private:
nhanvien **x;
int n;
public:
quanlynhanvien(void);
~quanlynhanvien(void);
void nhap();
void xuat();
};
quanlynhanvien.cpp
#include "quanlynhanvien.h"
#include"vanphong.h"
#include"sanxuat.h"
quanlynhanvien::quanlynhanvien(void)
{
n=0;
x=new nhanvien*[1000];
}
quanlynhanvien::~quanlynhanvien(void)
{
delete []x;
}
void quanlynhanvien::nhap()
{
int k;
for(int i=0;;i++)
{
do{
cout<<"\nloai nhan vien: \n1-van phong\n2-san xuat\n0-ket thuc\n";
cin>>k;
if(k!=1 && k!=2 && k!=0) cout<<"\nkhong hop le!\n";
}while(k!=1 && k!=2 && k!=0);
if(k==0) return;
else if(k==1)
{
n++;
x[i]=new vanphong;
}
else
{
n++;
x[i]=new sanxuat;
}
x[i]->nhap();
}
}
void quanlynhanvien::xuat()
{
cout<<"so nhan vien = "<<n<<"\n";
for(int i=0;i<n;i++)
{
cout<<endl;
x[i]->xuat();
cout<<"luong = "<<x[i]->luong();
cout<<endl;
}
}
main.cpp
#include"quanlynhanvien.h"
void main()
{
quanlynhanvien x;
x.nhap();
x.xuat();
system("pause");
}
Friday, May 15, 2015
OOP C++ cộng trừ nhân chia số phức
Làm lại bài số phức với một phương thức thiết lập duy nhất cho phép quan điểm một số thực như một số phức đặc biệt (phần ảo bằng 0). Định nghĩa các phép toán cộng ,trừ nhân chia, so sánh,….. +, -, *, /, = =, !=, ! trên số phức. Định nghĩa phép toán << và >> để xuất và nhập dữ liệu vào số phức.sophuc.h
#pragma once
#include<iostream>
using namespace std;
class sophuc
{
private:
float t,a;
public:
sophuc(float =0,float =0);
~sophuc(void);
float dodai();
sophuc operator+(const sophuc &);
sophuc operator-(const sophuc &);
sophuc operator*(const sophuc &);
sophuc operator/(const sophuc &);
bool operator<(sophuc &);
bool operator<=(sophuc &);
bool operator>(sophuc &);
bool operator>=(sophuc&);
bool operator==(sophuc&);
bool operator!=(sophuc&);
friend ostream &operator<<(ostream &out,const sophuc &a);
friend istream &operator>>(istream &in,sophuc &a);
};
sophuc.cpp
#include "sophuc.h"
sophuc::sophuc(float b,float c)
{
t=b;a=c;
}
float sophuc::dodai()
{
return sqrt(t*t+a*a);
}
sophuc::~sophuc(void)
{
}
sophuc sophuc::operator+(const sophuc &x)
{
sophuc c;
c.t=x.t+this->t;
c.a=x.a+this->a;
return c;
}
ostream &operator<<(ostream &out,const sophuc &a)
{
if(a.t==0 && a.a!=0) out<<a.a<<"i";
else if(a.a==0 && a.t!=0) out<<a.t;
else if(a.t==0 && a.a==0) out<<0;
else out<<a.t<<((a.a>0)?" + ":" - ")<<abs(a.a)<<"i";
return out;
}
istream &operator>>(istream &in,sophuc &a)
{
cout<<"phan thuc=";in>>a.t;
cout<<"phan ao=";in>>a.a;
return in;
}
sophuc sophuc::operator-(const sophuc&x)
{
sophuc c;
c.t=t-x.t;
c.a=a-x.a;
return c;
}
sophuc sophuc::operator*(const sophuc&x)
{
sophuc c;
c.t=t*x.t-a*x.a;
c.a=t*x.a+a*x.t;
return c;
}
sophuc sophuc::operator/(const sophuc&x)
{
sophuc c;
c.t=(t*x.t+a*x.a)/(x.t*x.t+x.a*x.a);
c.a=(x.t*a-t*x.a)/(x.t*x.t+x.a*x.a);
return c;
}
bool sophuc::operator<(sophuc&x)
{
if(dodai()<x.dodai()) return true;return false;
}
bool sophuc::operator<=(sophuc&x)
{
if(dodai()<=x.dodai()) return true;return false;
}
bool sophuc::operator>(sophuc&x)
{
if(dodai()>x.dodai()) return true;return false;
}
bool sophuc::operator>=(sophuc&x)
{
if(dodai()>=x.dodai()) return true;return false;
}
bool sophuc::operator==(sophuc&x)
{
if(dodai()==x.dodai()) return true;return false;
}
bool sophuc::operator!=(sophuc&x)
{
if(dodai()!=x.dodai()) return true;return false;
}
main.cpp
#include"sophuc.h"
void main()
{
sophuc a,b,c;
cin>>a>>b;
cout<<a+b<<endl;
cout<<a-b<<endl;
cout<<a*b<<endl;
cout<<a/b<<endl;
if(a>b) cout<<1<<endl;else cout<<0<<endl;
if(a>=b) cout<<1<<endl;else cout<<0<<endl;
if(a<b) cout<<1<<endl;else cout<<0<<endl;
if(a<=b) cout<<1<<endl;else cout<<0<<endl;
if(a==b) cout<<1<<endl;else cout<<0<<endl;
if(a!=b) cout<<1<<endl;else cout<<0<<endl;
system("pause");
}
Friday, April 10, 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");
}
Thursday, February 5, 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
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
Wednesday, February 4, 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();
};
Tuesday, February 3, 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");
}






.png)

.png)
.png)
.png)