bài tập C++
bài công nhân
#include<conio.h>
#include<stdio.h>
#include<string.h>
#include<iostream.h>
class congnhan
{
private:
char *hoten;
float hesoluong;
int namsinh;
public:
~congnhan(){if (this->hoten!=NULL)
delete (this->hoten);}
float gethesoluong(){
return this->hesoluong;
}
congnhan &operator =(congnhan & w);
friend istream & operator >>(istream & input,congnhan & w);
friend ostream & operator <<(ostream & output,congnhan w);
float tinhluong(int luongcoban=45);
};
istream & operator >>(istream & input,congnhan & w)
{
cout<<"nhap hoten:";
if(w.hoten==NULL)
w.hoten=new char[30];
fflush(stdin);
gets(w.hoten);
cout<<"nhap namsinh:";input>>w.namsinh;
cout<<"nhap hesoluong:";input>>w.hesoluong;
return input;
}
ostream & operator <<(ostream & output,congnhan w)
{
output<<"hoten:"<<w.hoten<<"\t namsinh:"<<w.namsinh<<"\thesoluong:"<<w.hesoluong;
return output;
}
float congnhan::tinhluong(int luongcoban)
{
float luong;
luong=hesoluong*luongcoban;
return luong;
}
congnhan& congnhan::operator =(congnhan &w)
{
if (this->hoten==NULL)
this->hoten= new char[strlen(w.hoten)];
strcpy(this->hoten, w.hoten);
this->namsinh=w.namsinh;
this->hesoluong=w.hesoluong;
return *this;
}
void main()
{
congnhan a[100];
int i,n;
float luong;
clrscr();
cout<<"
nhap bao nhieu cong nhan:";cin>>n;
for(i=0;i<n;i++)
{
cin>>a[i];
}
cout<<"
danh sach vua nhap la:"<<"
";
for(i=0;i<n;i++)
{
cout<<"
cong nhan thu "<<i<< "la:";
cout<<a[i];
}
cout<<"
luong cong nhan:";
for(i=0;i<n;i++)
{
luong=a[i].tinhluong();
cout<<luong;
}
getch();
}
bài vẽ hình chữ nhật
//Ve hinh chu nhat
#include<iostream.h>
#include<graphics.h>
#include<conio.h>
class cn
{
private:
int x1,y1, rong, dai,maunen;
public:
//void vecn(int x1, int y1,int rong, int dai);
void vecn();
void nhap();
};
void cn::vecn()
//void cn::vecn(int x1, int y1, int rong, int dai)
{
clrscr();
int gdriver=DETECT, gmode, errorcode;
int x2,y2;
initgraph (&gdriver, &gmode,"C:\\Laptrinh\\TC\\BGI");
errorcode=graphresult();
if(errorcode != grOk)
{
cout << "Loi do hoa" << grapherrormsg(errorcode);
cout << "An nut bat ki de thoat ra:";
getch();
// exit (1);
}
x2=this->x1+this->dai;
y2=this->y1+this->rong;
setbkcolor(this->maunen);
bar(this->x1,this->y1,x2,y2);
getch();
closegraph();
}
void cn::nhap()
{
cout << "
Nhap toa do diem x tren ben trai:";
cin >> this->x1;
cout << "
Nhap toa do diem y tren ben trai:";
cin >> this->y1;
cout << "
Nhap chieu rong:";
cin >> this->rong;
cout << "
Nhap chieu dai:";
cin >> this->dai;
cout << "
Nhap mau nen:";
cin >> this->maunen;
}
void main()
{
clrscr();
cn a;
a.nhap();
a.vecn();
getch();
}
bàihình chữ nhật
/#include<conio.h>
#include<stdio.h>
#include<iostream.h>
#include<graphics.h>
class hcn
{
int top,left,width,height,bgcolor;
public:
hcn(){
this->top=100;
this->left=100;
this->width=100;
this->height=100;
}
hcn::hcn(int top,int left,int width,int height,int backgroundcolor){
this->top=top;
this->left=left;
this->width=width;
this->height=height;
this->bgcolor=backgroundcolor;
}
void ve();
};
void hcn::ve(){
int gdriver = DETECT,gmode,errorcode;
initgraph(&gdriver,&gmode ,"C:\\LAPTRINH\\TC\\BGI");
errorcode=graphresult();
if (errorcode==grOk){
//rectangle(left, top, left+width, top+height);
setfillstyle(SOLID_FILL, this->bgcolor);
bar(left, top, left+width, top+height);
}else{
cout<<"
Loi khoi tao che do Do hoa";
}
getch();
closegraph();
}
void main(){
hcn a(100, 250, 300, 100, RED);
a.ve();
}
bài phân số
#include<conio.h>
#include<iostream.h>
#include<math.h>
#include<iomanip.h>
#include<stdlib.h>
#include<stdio.h>
class PS
{ public:
int ts,ms;
friend ostream& operator <<(ostream& os,PS &p)
{ if(p.ms == 1) os<<"
"<<p.ts;
else
{ if(p.ts==0) os<<"
0";
else os<<p.ts<<"/"<<p.ms;
}
return os;
};
friend istream& operator >>(istream& is,PS &p)
{ cout<<"
Tu:";is>>p.ts;
cout<<"Mau:";is>>p.ms;
return is;
};
int ucln();
PS rutgon();
void hien_hon_so();
PS operator +(PS p2);
PS operator -(PS p2);
PS operator *(PS p2);
PS operator /(PS p2);
int operator ==(PS ps2);
int operator !=(PS ps2);
int operator >(PS ps2);
int operator >=(PS ps2);
int operator <(PS ps2);
int operator <=(PS ps2);
void ghitep();
void doctep();
};
int uscln(int a, int b)
{
int du;
while (b!=0)
{
du= a%b;
a=b;
b=du;
}
return a;
}
PS PS::rutgon()
{
int x;
x=ucln();
ts=ts/x;
ms=ms/x;
if(ms<0){ ts=-ts;ms=-ms; }
return (*this);
};
void PS::hien_hon_so()
{ int nguyen;
PS a;
nguyen=ts/ms;
a.ts=ts-(nguyen*ms);
a.ms=ms;
if(nguyen!=0)
cout<<"("<<nguyen<<")";
cout<<a;
};
PS PS::operator +(PS p2)
{
PS q;
q.ts=ts*p2.ms+ms*p2.ts;
q.ms=ms*p2.ms;
return q.rutgon();
};
PS PS::operator -(PS p2)
{
PS q;
q.ts=ts*p2.ms-ms*p2.ts;
q.ms=ms*p2.ms;
return q.rutgon();
};
PS PS::operator *(PS p2)
{
PS q;
q.ts=ts*p2.ts;
q.ms=ms*p2.ms;
return q.rutgon();
};
PS PS::operator /(PS p2)
{
PS q;
q.ts=ts*p2.ms;
q.ms=ms*p2.ts;
return q.rutgon();
}
int PS::operator ==(PS ps2)
{
if( (ts*ps2.ms-ms*ps2.ts)==0 ) return 1; //TRUE dung
else return 0; //FALSE sai
};
int PS::operator !=(PS ps2)
{
if( (ts*ps2.ms-ms*ps2.ts)!=0 ) return 1; //TRUE dung
else return 0; //FALSE sai
};
int PS::operator >(PS ps2)
{
float kt=(float) (ts*ps2.ms-ms*ps2.ts)/(ms*ps2.ms);
if(kt>0) return 1; //TRUE dung
else return 0; //FALSE sai
};
int PS::operator >=(PS ps2)
{
float kt=(float) (ts*ps2.ms-ms*ps2.ts)/(ms*ps2.ms);
if(kt>=0) return 1; //TRUE dung
else return 0; //FALSE sai
};
int PS::operator <(PS ps2)
{
float kt=(float) (ts*ps2.ms-ms*ps2.ts)/(ms*ps2.ms);
if(kt<0) return 1; //TRUE dung
else return 0; //FALSE sai
};
int PS::operator <=(PS ps2)
{
float kt=(float) (ts*ps2.ms-ms*ps2.ts)/(ms*ps2.ms);
if(kt<=0) return 1; //TRUE dung
else return 0; //FALSE sai
};
void PS::ghitep()
{
FILE *f;
f=fopen("phanso.dat","a+b");
if(f==NULL)
{
cout<<"loi mo tep!";
getch();
}
else fwrite(this,sizeof(*this),1,f);
fclose(f);
};
void PS::doctep()
{
FILE *f=fopen("phanso.dat","r+b");
if(f==NULL)
{
cout<<"loi mo tep!";
getch();
}
else
while( fread(this,sizeof(*this),1,f)!=NULL )
cout<<*this;
fclose(f);
};
int menu()
{
int chon;
clrscr();
cout<<"
";
cout<<"
*-*-*-*-*-*- XAY DUNG LOP PHAN SO *-*-*-*-*-*-*-*-* ";
cout<<"
";
cout<<"
* CHUC NANG CHUONG TRINH *";
cout<<"
------------------------------";
cout<<"
| 1.SO SANH |";
cout<<"
| 2.DOI RA HON SO |";
cout<<"
| 3.RUT GON PHAN SO |";
cout<<"
| 4.CAC PHEP TINH TREN PHAN SO |";
cout<<"
| 0.THOAT |";
cout<<"
------------------------------";
cout<<"
MOI CHON CHUC NANG: ";
cin>>chon;
return chon;
};
void main()
{
int chon=1;
int u;
PS g,h;
while(chon)
{
chon=menu();
switch(chon)
{
case 1:
{
clrscr();
cout<<endl<<"Nhap phan so g:"<<endl;
cin>>g;
g.ghitep();g.rutgon();
cout<<"
nhap phan so h: ";
cin>>h;
h.ghitep();h.rutgon();
if(h<g)
cout<<h<<"<"<<g;
else if(h>g)
cout<<h<<">"<<g;
else
cout<<h<<"="<<g;
getch();break;
}
case 2:
{
clrscr();
cout<<"
phan so g"<<endl;
cin>>g;g.ghitep();
g.rutgon();g.hien_hon_so();
getch();
break;
}
case 3:
{ clrscr();
cout<<endl<<"nhap phan so: "<<endl;
cin>>g;
g.rutgon();
cout<<"
phan so sau khi rut gon:"<<g;getch(); break;
}
case 4:
{
clrscr();
cout<<endl<<"1.NHAN 2 PS";
cout<<endl<<"2.CHIA 2 PS";
cout<<endl<<"3.CONG 2 PS";
cout<<endl<<"4.TRU 2 PS";
cout<<endl<<"VUI LONG CHON 1 CHUC NANG: ";
cin>>u;
switch(u)
{
case 1:
{
PS kq;
clrscr();
cout<<endl<<"Nhap phan so g:"<<endl;
cin>>g;
g.ghitep();g.rutgon();
cout<<"
nhap phan so h: ";
cin>>h;
h.ghitep();h.rutgon();
kq=g*h;
cout<<"tich 2 PS vua nhap la: "<<kq;getch(); break;
}
case 2:
{
PS kq2;
clrscr();
cout<<endl<<"Nhap phan so g:"<<endl;
cin>>g;
g.ghitep();g.rutgon();
cout<<"
nhap phan so h: ";
cin>>h;
h.ghitep();h.rutgon();
kq2=g/h;
cout<<"chia 2 PS vua nhap la: "<<kq2;getch(); break;
}
case 3:
{
PS KQ3;
clrscr();
cout<<endl<<"Nhap phan so g:"<<endl;
cin>>g;
g.ghitep();g.rutgon();
cout<<"
nhap phan so h: ";
cin>>h;
h.ghitep();h.rutgon();
KQ3=g+h;
cout<<"cong 2 PS vua nhap la: "<<KQ3;getch(); break;
}
case 4:
{
PS KQ4;
clrscr();
cout<<endl<<"Nhap phan so g:"<<endl;
cin>>g;
g.ghitep();g.rutgon();
cout<<"
nhap phan so h: ";
cin>>h;
h.ghitep();h.rutgon();
KQ4=g-h;
cout<<"hieu 2 da thuc vua nhap la: "<<KQ4;getch(); break;
}
case 0:
{
break;
}
}
}
}
}
}
bài phân số
#include <conio.h>
#include <stdio.h>
#include <math.h>
#include <iostream.h>
class phanso
{
private:
int tu;
int mau;
public:
phanso()
{
this->tu=0;
this->mau=1;
}
phanso(int tu)
{
this->tu=tu;
this->mau=1;
}
phanso(int tu1, int mau1)
{
this->tu=tu1;
this->mau=mau1;
}
phanso operator - (phanso p)
{
phanso q;
q.tu=(this->tu*p.mau)-(this->mau*p.tu);
q.mau=this->mau*p.mau;
return q;
}
phanso operator + (phanso p)
{
phanso q;
q.tu=(this->tu*p.mau)+(this->mau*p.tu);
q.mau=this->mau*p.mau;
return q;
}
friend ostream &operator <<(ostream&output, phanso a)
{
cout<<a.tu<<"/"<<a.mau;
return output;
}
};
void main()
{
clrscr();
phanso a(2);
phanso b(1,3);
phanso c(2,5);
phanso d(1,2);
phanso tong;
tong=a+b+c-d;
cout<<"
";
cout<<a<<b<<c<<d;
cout<<" Ket qua cua bieu thuc:2+ 1/3 + 2/5 - 1/2 ="<<tong;
getch();
}
bài công nhân
/* Xay dung lop Cong nhan voi cac thanh phan
-Thuoc tinh: Hoten, Hesoluong
-Phuong thuc: Nhap(), Hien, Tinhluong(long Luongcoban=450000)
+Lap trinh nhap vao danh sach 10 CN
+Hien danh sach ra man hinh (Hoten, Luong)
+Tinh tong so luong phai tra */
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<iostream.h>
class congnhan
{
private:
char hoten[20];
float hesoluong;
public:
void nhap();
void hien();
float tinhluong(long luongcoban=450000); //CHU Y: De phong tran so!
};
void congnhan :: nhap()
{
cout << "
Ho ten: ";
fflush(stdin);
gets(this->hoten);
cout << "
He so luong: ";
cin>> this->hesoluong;
}
void congnhan :: hien()
{
cout << this->hoten << "\t" << this->hesoluong << "\t" << tinhluong() << endl;
}
float congnhan :: tinhluong(long luongcoban)
{
return this->hesoluong*luongcoban;
}
void main()
{
clrscr();
long tong=0;
congnhan a[100];
for(i=0;i<10;i++)
{
a[i].nhap();
a[i].hien();
}
for(i=0;i<10;i++)
tong += a[i].tinhluong(); //Co the truyen doi so khac vao hoac neu khong truyen thi se lay tham so mac dinh cua phuong thuc
cout << "
Tong luong phai tra la: " << tong;
getch();
}
bài đa thức
#include <iostream.h>
#include <conio.h>
#include <math.h>
class DT{
private:
double *a;
public:
DT(){
this->n = 0;
this->a = NULL;
}
DT( int n1 ){
this->n = n1;
this->a = new double[n1 + 1];
}
~DT(){
this->n = 0;
delete []this->a;
}
DT( const DT &d ){
this->n = d.n;
this->a = new double[d.n + 1];
for( int i = 0; i < n; i++ ){
a[i] = d.a[i];
}
}
friend ostream& operator<<( ostream& os, const DT &d );
friend istream& operator>>( istream& is, DT &d );
DT operator-();
DT operator+( const DT &d2 );
DT operator*( const DT &d2 );
DT operator-( const DT &d2 );
double operator^( const double &x );
double operator[]( int i ){
return a[i];
}
};
ostream& operator<<( ostream& os, const DT &d ){
os<<"-Cac he so ( tu ao ): ";
for( i = 0; i <= d.n; ++i ){
os<<d.a[i]<<" ";
}
return os;
}
istream& operator>>( istream& is, DT &d ){
if( d.a != NULL )
delete []d.a;
cout<<"-Bac cua da thuc: ";
cin>>d.n;
d.a = new double[d.n + 1];
cout<<"Nhap he so da thuc:
";
for( int i = 0; i <= d.n; ++i ){
cout<<"He so bac "<< i<<" = ";
is>>d.a[i];
}
return is;
}
DT DT::operator-(){
DT p( this->n );
for( int i = 0; i <= n; ++i ){
p.a[i] = -a[i];
}
return p;
}
DT DT::operator+( const DT &d2 ){
int i, k;
k = n > d2.n ? n : d2.n;
DT d( k );
for( i = 0; i <= k; ++i ){
if( i <= n && i <= d2.n )
d.a[i] = a[i] + d2.a[i];
else if( i <= n )
d.a[i] = a[i];
else
d.a[i] = d2.a[i];
}
return d;
}
DT DT::operator-( const DT &d2 ){
int i, k;
k = n > d2.n ? n : d2.n;
DT d( k );
for( i = 0; i <= k; ++i ){
if( i <= n && i <= d2.n )
d.a[i] = a[i] - d2.a[i];
else if( i <= n )
d.a[i] = a[i];
else
d.a[i] = d2.a[i];
}
return d;
}
DT DT::operator*( const DT &d2 ){
int k, i, j;
k = n + d2.n;
DT d( k );
for( i = 0; i <= k; ++i ){
d.a[i] = 0;
}
for( i = 0; i <= n; ++i )
for( j = 0; j <= d2.n; j++ )
d.a[i+j] += a[i]*d2.a[j];
return d;
}
double DT::operator^( const double &x ){
double s = 0.0, t = 1.0;
for( int i = 0; i <= n; ++i ){
s += a[i] * t;
t *= x;
}
return s;
}
int main(){
DT g, h, f, y, z, f1, f2;
cout<<"
Nhap da thuc G :";
cin>>g;
cout<<"
Da thuc G :"<<g;
/*cout<<"
Nhap da thuc H :";
cin>>h;
cout<<"
Da thuc H :"<<h;
cout<<"
Nhap da thuc Y :";
cin>>y;
cout<<"
Da thuc Y :"<<y;
cout<<"
Nhap da thuc Z :";
cin>>z;
cout<<"
Da thuc X :"<<z;*/
f1 = -g;
//f2 = y - z;
//f = f1 * f2;
cout<<"
Da thuc F1 :"<<f1;
//cout<<"
Da thuc F2 :"<<f2;
//cout<<"
Da thuc F :"<<f;
getch();
return 0;
}
Bạn đang đọc truyện trên: AzTruyen.Top