Bài giải thực hành C++Thứ tư - 31/08/2011 21:24
Lời giải các bài thực hành tiếp theo sẽ được post ngay trong mục này
Các bạn có thể tham khảo code mình viết (^^ có nhiều chỗ chắc là sai ^^)
Mình sẽ post lời giải bài thực hành trước các buổi thực hành 1 ngày cho các bạn tham khảo !
Bài thực hành số 1:
Câu 1: #include
#include
class tamgiac{
private:
int a,b,c; // a,b,c la 3 canh cua tam giac
// kiem tra tinh hop le cua tam giac
int checkTamGiac(){
if(a>0 && b>0 && c>0){
if(a+b cout<<"3 canh nhap vao khong the tao thanh 1 tam giac!";
return 0;
}
}
return 1;
}
// kiem tra loai tam giac.
loaiTamGiac() {
if(a==b==c){
cout<<"Tam giac deu";
}
if((a==b || a==c || b==c)&&(a!=b || a!=c || b!=c)){
cout<<"Tam giac can";
}
if(a==sqrt(b*b+c*c) || b==sqrt(a*a+c*c) || c==sqrt(a*a+b*b)){
if(a==b || a==c || b==c){
cout<<"Tam giac vuong can";
}else{
cout<<"Tam giac vuong";
}
}
return 0;
}
public:
// nhap 3 canh cua 1 tam giac
nhap(){
cout<<"nhap canh thu nhat :";
cin>>a;
cout<<"nhap canh thu hai :";
cin>>b;
cout<<"nhap canh thu ba :";
cin>>c;
return 0;
}
//in ra do dai 3 canh cua tam giac, loai tam giac, dien tich tam giac
inTamGiac(){
float p;
p=(a+b+c)/2;
cout<<"canh thu nhat:"<
<<"\n"; cout<<"canh thu hai:"<<<"\n"; cout<<"canh thu ba:"<<<"\n"; if(checkTamGiac()==1){ loaiTamGiac(); cout<<"\n dien tich tam giac la: "< } } } ; int main(){ tamgiac T; // doi tuong T thuoc class tamgiac T.nhap(); T.inTamGiac(); return 0; } <<"\n"; <<"\n"; <<"\n";
Câu 2: #include
#include
class point{
private:
int x;
int y;
int z;
int x1,y1,z1; // toa do diem doi xung
public:
// phuong thuc khoi tao
point(){
x=y=z=0;
}
// phuong thuc xac dinh diem doi xung qua goc toa do
doiXung(){
if(x>0){
x1=-x;
}else x1=abs(x);
if(y>0){
y1=-y;
}else y1=abs(y);
if(z>0){
z1=-z;
}else z1=abs(z);
// in toa do diem doi xung
cout<<"\n toa do diem doi xung qua goc toa do la
"<<<","<<<","<<
}
khoangCach(){
cout<<"\n khoang cach 2 diem bang:"<
}
hienThi(){
cout<<"\n (x,y,z)=("<<<","<<<","<<<")";
}
nhap(){
cout<<"\n nhap toa do x:";
cin>>x;
cout<<"\n nhap toa do y:";
cin>>y;
cout<<"\n nhap toa do z:";
cin>>z;
}
};
int main(){
point P;
P.nhap();
P.doiXung();
P.khoangCach();
P.hienThi();
return 0;
}<<","<<<","<<<")";
<<","<<<","<<
Câu 3: #include
class time{
private:
int h; // 0-23
int m; // 0-59
int s; // 0-59
public:
// phuong thuc khoi tao
time(){
h=m=s=0;
}
// phuong thuc setTime
void setTime(){
cout<<"ban nhap so giay:";
cin>>s;
}
// phuong thuc chuyen doi tu giay sang gio
void convertTime(){
h= s / 3600;
m= (s % 3600)/60;
s= (s % 3600)%60;
}
// phuong thuc in gio quan doi
void gioQuanDoi(){
cout << (h<10 ? "0" : "") <<<":"
<< (m<10 ? "0" : "") <<<":"
<< (s<10 ? "0" : "") <<<":";
}
// phuong thuc in gio chuan
void gioChuan(){
cout << ((h==0 || h==12)? 12 : h%12)
<<":"<<(m<10 ? "0" : "" ) <
<<":"<<(s<10 ? "0" : "" ) <
<< (h <12 ? "AM" : "BM");
}
};
int main(){
time T;
T.setTime();
T.convertTime();
cout<<"\n Gio theo quan doi:";
T.gioQuanDoi();
cout<<"\n Gio theo chuan:";
T.gioChuan();
return 0;
}
<<":";
<<":"
<<":"
Bài thực hành số 2:
Câu 1: #include
class date{
private:
int day;
int month;
int year;
public:
date(){
day=1;
month=1;
year=1900;
}
void nhap(){
cout<<"\n Nhap ngay:";
cin>>day;
cout<<"\n Nhap thang:";
cin>>month;
cout<<"\n Nhap nam:";
cin>>year;
}
int checkYear(){
if(year>0){
return 1;
}else{
return 0;
}
}
int checkDay(){
if(day>0 && day<=31){
switch(month){
case 4,6,9,11:
if(day<0 || day >31){
return 0;
}else{
return 1;
}
break;
case 1,3,5,7,8,12:
if(day <0 || day>30){
return 0;
}else{
return 1;
}
break;
case 2:
if(year % 4 ==0){
if(day<0 || day>29){
return 0;
}else{
return 1;
}
}else{
if(day<0||day>28){
return 0;
}else{
return 1;
}
}
break;
}
return 1;
}else{
return 0;
}
}
int checkMonth(){
if(month>0 && month<=12){
return 1;
}else{
return 0;
}
}
void inDate(){
if(checkYear()==1 && checkDay()==1 && checkMonth()==1){
cout<<"\n Ngay thang nam ban vua nhap :";
cout <<((day>0 && day<9) ? "0" : "")<
<<"/"<<((month>0 && month<9) ? "0" : "" ) <
<<"/"< }else{
cout<<"\n Ban nhap ngay thang nam sai!";
}
}
};
int main(){
date D;
D.nhap();
D.inDate();
return 0;
}
Câu 2: #include
class vector{
private:
int soChieu;
float values[100];
public:
void addVector(){
cout<<"\n Ban nhap so chieu Vector:";
cin>>soChieu;
cout<<"\n Nhap gia tri cho vector:";
for(int i=0;i
cout<<"\n X["<
<<"]"; cin>>values[i]; } } int countEmpty(){ int count=0; for(int i=0; i if(values[i]!=0){ count++; } } return count; } float avg(){ float sum=0; for(int i=0; i sum=sum+values[i]; } return sum/soChieu; } void inVector(){ cout<<"\n Vector ban vua nhap la:X=("; for(int i=0;i cout << values[i] <<","; } cout<<")"; } }; int main(){ vector V; V.addVector(); cout<<"\n So luong toa do khac 0 la:"< cout<<"\n Trung binh cong toa do cua vector:"< V.inVector(); return 0; } <<"]";
Câu 3: #include
const int max=1000;
class stack{
private:
int top;
int node[max];
public:
// phuong thuc khoi tao dat gia tri ban dau top=-1
// nghia la danh sach chua co phan tu nao
stack(){
top=-1;
}
// phuong thuc kiem tra danh sach co rong hay khong?
int empty(){
if(top == -1){
return 1;
}else{
return 0;
}
}
// chen phan tu trong stack va kiem tra stack co day hay khong?
void push(int data){
if(top==max-1){
cout<<" Danh sach bi day !\n";
}else{
node[++top]=data;
}
}
// lay 1 phan tu trong stack va kiem tra stack co rong hay khong?
int pop(){
if(empty()==1){
cout<<" Danh sach khong co phan tu nao !";
}else{
return node[top--];
}
}
// in stack
void inStack(){
int j;
if(empty()==1){
cout<<"Danh sach khong co phan tu nao !";
}else{
for(j=top;j>=0;j--){
cout<<"\t"<
}
}
}
};
int main(){
stack S;
int i,n,data;
cout<<" \n ban muon nhap bao nhieu phan tu:";
cin>>n;
for(i=0;i
cout<<"\n Nhap phan tu thu \t "<<<"\t:";
cin>>data;
S.push(data);
}
cout<<"\n Danh sach vua nhap la:";
S.inStack();
cout<<"\n Lay phan tu trong stack ra ngoai stack" ;
S.pop();
cout<<"\n Danh sach sau khi bi lay:";
S.inStack();
cout<<"\n Lay phan tu trong stack ra ngoai stack" ;
S.pop();
cout<<"\n Danh sach sau khi bi lay:";
S.inStack();
cout<<"\n Lay phan tu trong stack ra ngoai stack" ;
S.pop();
cout<<"\n Danh sach sau khi bi lay:";
S.inStack();
return 0;
}<<"\t:";
Bài thực hành số 3
Câu 1: #include
#include
#include
class sinhVien{
private:
char sbd[5],hoTen[100],ngaySinh[20];
float diemToan,diemLy,diemHoa,diemUT;
public:
sinhVien(){
}
// phuong thuc khoi tao co tham so truyen vao tu ban phim
sinhVien(char sbd[],char hoTen[],char ngaySinh[],float diemToan,float diemLy,float diemHoa,float diemUT){
strcpy(this->sbd,sbd);
strcpy(this->hoTen,hoTen);
strcpy(this->ngaySinh,ngaySinh);
this->diemToan=diemToan;
this->diemLy=diemLy;
this->diemHoa=diemHoa;
this->diemUT=diemUT;
}
// phuong thuc nhap thi sinh
void nhap(){
cout<<"\n Nhap so bao danh;";
cin>>this->sbd;
fflush(stdin);
cout<<"\n Nhap ho ten:";
cin>>this->hoTen;
fflush(stdin);
cout<<"\n Nhap ngay sinh:";
cin>>this->ngaySinh;
fflush(stdin);
cout<<"\n Nhap diem toan:";
cin>>this->diemToan;
fflush(stdin);
cout<<"\n Nhap diem ly:";
cin>>this->diemLy;
fflush(stdin);
cout<<"\n Nhap diem hoa:";
cin>>this->diemHoa;
fflush(stdin);
cout<<"\n Nhap diem uu tien:";
cin>>this->diemUT;
fflush(stdin);
}
// phuong thuc hien thi thong tin sinh vien
in(){
printf("\n%-6s%-15s%-16s%-10.2f%-10.2f%-10.2f%-10.2f",this->sbd,this->hoTen,this->ngaySinh,this->diemToan,this->diemLy,this->diemHoa,this->diemUT);
return 0;
}
float tongDiem(){
return this->diemToan+this->diemLy+this->diemHoa+this->diemUT;
}
int check(float diem){
if(tongDiem()>=diem){
return 1;
}else return 0;
}
};
int main(){
int n=5,i;
sinhVien sv[20];
for(i=0;i
cout<<"Nhap sinh vien thu : "<
sv[i].nhap();
}
printf("\n--------------------------------------------------------------------------");
printf("\n|sbd | Ho Ten | Ngay Sinh | Toan | Ly | Hoa | Diem UT|");
printf("\n--------------------------------------------------------------------------");
//printf("\n 001 |Le Van Luyen| 18/10/1993 | 10 | 10 | 10 | 1 |");
for(i=0;i
sv[i].in();
}
printf("\n--------------------------------------------------------------------------");
cout<<"\n Danh sach tong diem :\n";
printf("\n--------------------------------------------------------------------------------");
printf("\n|sbd | Ho Ten | Ngay Sinh | Toan | Ly | Hoa | Diem UT| TD |");
printf("\n--------------------------------------------------------------------------------");
//printf("\n 001 |Le Van Luyen| 18/10/1993 | 10 | 10 | 10 | 1 |");
for(i=0;i
sv[i].in();
cout<
}
printf("\n--------------------------------------------------------------------------------");
cout<<"\n danh sach sinh vien trung tuyen: \n";
printf("\n--------------------------------------------------------------------------");
printf("\n|sbd | Ho Ten | Ngay Sinh | Toan | Ly | Hoa | Diem UT|");
printf("\n--------------------------------------------------------------------------");
for(i=0;i
if(sv[i].check(15)){
sv[i].in();
}
}
printf("\n--------------------------------------------------------------------------");
return 0;
}
Câu 2: #include
#include
#include
class tienDien{
private:
char hoTen[10];
int soDien;
public:
tienDien(){
}
tienDien( char hoTen[], int soDien){
strcpy(this->hoTen,hoTen);
this->soDien=soDien;
}
void nhap(){
cout<<"\n Nhap ho ten:";
gets(this->hoTen);
fflush(stdin);
cout<<"\n Nhap so dien:";
cin>>this->soDien;
}
float tinhTienDien(){
float tongTien=0;
float soDien=this->soDien;
if(soDien-301>0){
tongTien+=(soDien-301)*2500;
soDien=301;
}
if(soDien-201>0){
tongTien+=(soDien-201)*1700;
soDien=201;
}
if(soDien-100>0){
tongTien+=(soDien-100)*1000;
soDien=100;
}
if(soDien<=100){
tongTien+=soDien*500;
}
return tongTien;
}
void in(){
printf("\n%-20s%-14d",this->hoTen,this->soDien);
}
};
int main(){
tienDien hoDan[50];
int n=1,i;
float tong=0;
for(i=0;i
cout<<"\n Nhap ho dan thu "<
hoDan[i].nhap();
}
cout<<"\nDanh sach va tong tien cac ho dan:";
cout<<"\n-------------------------------------------------";
cout<<"\n| Ho Ten | So Dien | Tien Dien |";
cout<<"\n-------------------------------------------------";
for(i=0;i
hoDan[i].in();
cout<
tong+=hoDan[i].tinhTienDien();
cout<<"\n-------------------------------------------------";
}
printf("\n\t\t\tTong tien dien:%9.0f",tong);
return 0;
}
Câu 3:
#include
#include
#include
class hoaDon{
private:
char maHang[5];
char tenHang[100];
float donGia;
int soLuong;
public:
hoaDon(){
}
hoaDon(char maHang[5], char tenHang[100],float donGia, int soLuong){
strcpy(this->maHang,maHang);
strcpy(this->tenHang,tenHang);
this->donGia=donGia;
this->soLuong=soLuong;
}
float tinhTien(){
if(this->soLuong<5){
return this->soLuong*this->donGia;
}
if(this->soLuong>=5 && this->soLuong<10){
return ((this->soLuong*this->donGia)-(this->soLuong*this->donGia*0.05));
}
if(this->soLuong>=10 && this->soLuong<20){
return ((this->soLuong*this->donGia)-(this->soLuong*this->donGia*0.1));
}
if(this->soLuong>20){
return ((this->soLuong*this->donGia)-(this->soLuong*this->donGia*0.15));
}
}
void nhap(){
cout<<"\n Nhap ma so hang:";
cin>>this->maHang;
fflush(stdin);
cout<<"\n Nhap ma ten hang:";
cin>>this->tenHang;
fflush(stdin);
cout<<"\n Nhap don gia:";
cin>>this->donGia;
fflush(stdin);
cout<<"\n Nhap so luong:";
cin>>this->soLuong;
fflush(stdin);
}
void inDanhSach(){
printf("\n%-15s%-21s",this->maHang,this->tenHang);
}
};
int main(){
hoaDon matHang[20];
int traLoi=1,chon,n=0,i;
float tongTien=0;
while(traLoi==1){
cout<<"\n ===============Menu============";
cout<<"\n 1. Nhap danh hang.";
cout<<"\n 2. In hoa don.";
cout<<"\n 0. Thoat.";
cout<<"\n Lua chon:";
cin>>chon;
switch(chon){
case 1:
cout<<"\n ban nhap bao nhieu hang:";
cin>>n;
for(i=0;i
cout<<"\n Nhap hang thu "<
matHang[i].nhap();
}
break;
case 2:
cout<<"\n-------------------------------";
cout<<"\n| Ma hang| Ten Hang |";
cout<<"\n-------------------------------";
if(n>0){
for(i=0;i
matHang[i].inDanhSach();
tongTien=tongTien+matHang[i].tinhTien();
}
}else{
cout<<"\n Chua co san pham";
}
cout<<"\n-------------------------------";
cout<<"\n\t Tong Tien:"<
break;
case 0:
break;
}
cout<<"\n Ban co muon tiep tuc hay khong?";
cout<<"\n Co bam 1.";
cout<<"\n Khong bam 0.";
cout<<"\n Lua chon:";
cin>>traLoi;
}
return 0;
}
Bài thực hành số 4 Câu 1: #include
#include
class complex
{
private:
float real,image;
public:
complex(float r=0,float i=0);
complex operator+(complex c);
complex operator-(complex c);
friend complex operator+(float x,complex c);
friend ostream& operator<<(ostream &out,complex c);
friend istream& operator>>(istream &inp,complex& c);
};
complex::complex(float r,float i)
{
real=r;
image=i;
}
complex complex::operator+(complex c)
{
complex temp;
temp.real=real+c.real;
temp.image=image+c.image;
return temp;
}
complex complex::operator-(complex c)
{
complex temp;
temp.real=real-c.real;
temp.image=image-c.image;
return temp;
}
complex operator+(float x, complex c)
{
complex temp;
temp.real=c.real+x;
temp.image=c.image;
return temp;
}
ostream& operator<<(ostream &out,complex c)
{
cout<<<"+"<<<"i"<
return out;
}
istream& operator>>(istream &inp,complex &c)
{
cout<<" Nhap vao phan thuc:";
cin>>c.real;
cout<<" Nhap vao phan ao:";
cin>>c.image;
return inp;
}
void main()
{
complex c,c1,c2,c3;
float x;
cin>>c;
cin>>c1;
c2=c+c1;
cout<<" Cong hai so phuc :"<
c3=c1-c;
cout<<" Tru hai so phuc :"<
cout<<" Nhap vao x :";
cin>>x;
c3=c3+x;
cout<< c3;
getch();
}
<<"+"<<<"i"<
Câu 2: #include
class phanSo{
private:
int tuSo;
int mauSo;
public:
phanSo(){
}
int USC(int a,int b){
if (a%b==0) return b;
else return USC(b,a%b);
}
phanSo tg(phanSo x)
{
int s = USC(x.tuSo,x.mauSo);
x.tuSo=x.tuSo/s;
x.mauSo=x.mauSo/s;
return x;
}
friend ostream& operator<< (ostream& os,phanSo x)
{
os<< x.tuSo << "/" << x.mauSo;
return os;
}
friend istream& operator>> (istream& is,phanSo &x)
{
cout<< "Nhap tu : ";
is>> x.tuSo;
cout<<"nhap mau : ";
is>>x.mauSo;
return is;
}
phanSo operator + (phanSo b){
phanSo c;
c.tuSo=tuSo*b.mauSo+mauSo*b.tuSo;
c.mauSo=mauSo*b.mauSo;
return tg(c);
}
phanSo operator-(phanSo b)
{
phanSo c;
c.tuSo = tuSo*b.mauSo-mauSo*b.tuSo;
c.mauSo = mauSo*b.mauSo;
return tg(c);
}
phanSo operator*(phanSo b)
{
phanSo c;
c.tuSo = tuSo*b.tuSo;
c.mauSo=mauSo*b.mauSo;
return tg(c);
}
phanSo operator/(phanSo b)
{
phanSo c;
c.tuSo=tuSo*b.mauSo;
c.mauSo = mauSo*b.tuSo;
return tg(c);
}
};
int main(){
phanSo P,c,a,b;
cin>>a;
cin>>b;
c=a+b;
cout<<"Tong : "<<<"\n";
c=a-b;
cout<<"Tru : "<<<"\n";
c=a*b;
cout<<"Nhan : "<<<"\n";
c=a/b;
cout<<"Chia : "<<<"\n";
return 0;
}
<<"\n";
<<"\n";
<<"\n";
<<"\n";
Câu 3: #include
#include
class date1
{
private:
int day;
int month;
int year;
static const int days[];
void helpIncrement();
public:
date1(int d=1,int m=1,int y=1900);
void setdate(int d,int m,int y);
date1 &operator++();
date1 &operator+=(int addday);
bool leapyear(int y);//kiem tra nam nhuan
bool endofmonth(int d);//kiem tra ngay cuoi thang
friend ostream & operator<<(ostream & out,date1 &date);
};
const int date1::days[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
date1::date1(int d,int m,int y)
{
setdate(d,m,y);
}
void date1::setdate(int d,int m,int y)
{
month=(m>=1 && m<=12)? m:1;
year=(y>=1900 && y<=2100)?y:1900;
if( month==2 && leapyear(y))
day=(d>=1 && d<=29)? d:1;
else
day=(d>=1 && d<=days[month])? d:1;
}
bool date1::leapyear(int y)
{
if ((y%400 ==0)||(y%100 !=0 && y%4==0))
return true;
else
return false;
}
bool date1::endofmonth(int d)
{
if(month==2 && leapyear(year))
return d==29;
else
return d==days[month];
}
void date1::helpIncrement()
{
if(month==2 && leapyear(year))//ngay cuoi nam
{
day=1;
month=1;
++year;
}
else
if(endofmonth(day))//ngay cuoi thang
{
day=1;
++month;
}
else
++day;
}
date1 &date1::operator++()
{
helpIncrement();
return * this;
}
date1 & date1::operator+=(int addday)
{
for(int i=0;i
{
helpIncrement();
}
return *this;
}
ostream & operator<<(ostream & out,date1 & date)
{
char * monthname[13]={" " , "January","February","Match","April","May",
"June","July","August","September","October","November","December"};
out<< date.day <<"/"<<<><<>
Bài thực hành số 5 Câu 1: #include
#include
class string{
private:
char chuoi[50];
public:
string(){
}
string(char chuoi[50]){
strcpy(this->chuoi,chuoi);
}
string operator + (string b){
return strcat(chuoi,b.chuoi);
}
string operator = (string b){
return strcpy(chuoi,b.chuoi);
}
char &string::operator [] (int k){
return chuoi[k];
}
operator ==(string b){
if(strcmp(chuoi,b.chuoi)==0){
cout<<"\n 2 chuoi bang nhau";
} else cout<<"\n 2 chuoi khong bang nhau";
}
int operator != (string b){
if(strcmp(chuoi,b.chuoi)!=0){
cout<<" \n 2 chuoi khac nhau" ;
}else cout<<"\n 2 chuoi khong khac nhau !";
}
int operator > (string b){
if(strcmp(chuoi,b.chuoi)>0){
cout<<"\n chuoi "<<<"> " <
}else cout<<"\n chuoi "<<<" 0="" br="" chuoi="" else="" hon="" int="" khong="" lon="" n="" nho="" operator="" string="">= " <
}else cout<<"\n chuoi "<<<"
0="" bang="" br="" chuoi="" cout="" else="" friend="" hien="" hoac=""
hon="" int="" khong="" lon="" n="" nho="" operator="" return=""
s="s1;<<">
<<">
<<">
Câu 2: #include
class matrix{
private:
int **mang;
int soHang;
int soCot;
public:
matrix(){
}
matrix(int soHang,int soCot){
this->soHang=soHang;
this->soCot=soCot;
mang=new int*[soHang];
for(int k=0;k
mang[k]= new int[soCot];
}
for(int t=0;t
for(int h=0;h
mang[h][t]=0;
}
}
}
~matrix(){
if(mang!=NULL)
for(int i=1;i<=soHang;i++)
for(int j=1;j<=soCot;j++){
mang[i][j]=0;
}
delete mang;
mang=NULL;
int n=0;
}
matrix matrix::operator+(const matrix &right){
// if ( ( soHang != right.soHang ) || ( soCot != right.soCot )) return 0 ;
matrix temp(soHang,soCot);
for ( int i = 0 ; i < soHang ; i++)
for ( int j = 0 ; j < soCot ; j++)
temp.mang[i][j] = mang[i][j] + right.mang[i][j] ;
return temp ;
}
matrix matrix::operator-(const matrix &right){
// if ( ( soHang != right.soHang ) || ( soCot != right.soCot ) ) break ;
matrix temp (soHang,soCot);
for ( int i = 0 ; i < soHang ; i++)
for ( int j = 0 ; j < soCot ; j++)
temp.mang[i][j] = mang[i][j] - right.mang[i][j] ;
return temp ;
}
friend ostream &operator<<(ostream &output , const matrix &right){
output << "Chi so hang : " << right.soHang ;
output << "Chi so cot : " << right.soCot ;
output <<"\n";
for ( int i = 0 ; i < right.soHang ; i++){
for ( int j = 0 ; j < right.soCot ; j++)
output << "\t" < output <<"\n";
}
return output ;
}
friend istream &operator>>(istream &input , matrix &right){
for ( int i = 0 ; i < right.soHang ; i++)
for ( int j = 0 ; j < right.soCot ; j++)
input >>right.mang[i][j];
return input;
}
};
int main(){
matrix mang (3,3) , mang1 ( 3,3 ) ;
cout<<"Nhap mang 1:";
cin >> mang;
cout<<"Nhap mang 2 :";
cin>>mang1;
cout << " + " << mang + mang1 ;
cout << " - " << mang - mang1;
return 0;
}
Câu 3: Bài thực hành số 6
Câu 1:
#include
class ps{
protected:
int tuSo;
int mauSo;
public:
ps(){
this->tuSo=0;
this->mauSo=1;
}
ps(int tuSo,int mauSo){
this->tuSo=tuSo;
this->mauSo;
}
int USC(int a,int b){
if (a%b==0) return b;
else return USC(b,a%b);
}
ps toiGian(ps x){
int s=USC(x.tuSo,x.mauSo);
x.tuSo=x.tuSo/s;
x.mauSo=x.mauSo/s;
return x;
}
void quyDongMauSo(ps x,ps y){
x.tuSo=x.tuSo*y.mauSo;
x.mauSo=y.tuSo*x.mauSo;
y.tuSo=y.tuSo*x.mauSo;
y.mauSo=x.tuSo*y.mauSo;
}
void nhap(){
cout<<"\n Nhap tu so :";
cin>>this->tuSo;
cout<<"\n Nhap mau so:";
cin>>this->mauSo;
}
void display(){
cout