baitapc
/*xay dung lop hoc sinh gom ho ten va diem thi(che dau ho ten va diem)
sap xep danh sach theo diem giam dan*/
#include<stdio.h>
#include<conio.h>
#include<iostream.h>
#include <iomanip.h>
class hs
{
char hoten[30];
float diem;
public:
void nhap()
{
cout<<"
Nhap ten:";gets(hoten);
cout<<"
Nhap diem:";cin>>diem;
}
void inra()
{
cout.precision(2);
cout<<setw(30)<<hoten<<setw(5)<<diem<<"
";
}
float laydiem()
{
return diem;
}
};
void doicho(hs &x, hs &y)
{ hs tg;
tg = x;
x = y;
y = tg;
}
void main()
{ hs x[100]; int i, k, n;
cout<<"
Cho biet so hoc sinh"; cin>>n;
cout<<"
Nhap danh sach hoc sinh";
for(i=0; i<n;i++)
x[i].nhap();
for (i=0; i<n-1; i++)
{
int vt;
vt=i;
for (k = i+1; k<n; k++)
if (x[vt].laydiem()<x[k].laydiem())
vt=k;
if (vt!=i)
doicho(x[i], x[k]);
}
cout<<"
Danh sach sau khi sap:
";
for(i=0; i<n; i++)
x[i].inra();
getch();
}
bai phan so
#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 PS::ucln()
{ int x=abs(ts),y=abs(ms);
if(x==0) return 1;
else
{ while(x!=y)
if(x>y) x-=y;
else y-=x;
return(x);
}
};
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<<"5.TINH BIEU THUC";
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 5:
{
clrscr();
PS a,b,c,d,e,f,tong;
cin>>a;
cin>>b;
cin>>c;
cin>>d;
cin>>e;
cin>>f;
cout<<a<<endl;
cout<<b<<endl;
cout<<c<<endl;
cout<<d<<endl;
cout<<e<<endl;
cout<<f<<endl;
tong=a+b+((c-d)*e)/f;
cout<<" Ket qua la:"<<tong;
getch();break;
}
case 0:
{
break;
}
}
}
}
}
}
bai cong nhan
/* 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();
int i;
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 mang so nguyen
/*Nhap so nguyen duong cho den khi gap 0 thi dung
-Tim so lon nhat va nho nhat
-Xay dung ham tim Min, Max */
#include<iostream.h>
#include<conio.h>
typedef int mang[100];
int max(mang a, int i)
{
int j=0, max=-32767;
for(j=0; j<i; j++)
{
if(a[j]>max)
max=a[j];
}
return max;
}
int min(mang a, int i)
{
int j=0, min=32767;
for(j=0; j<i; j++)
{
if(a[j]<min)
min=a[j];
}
return min;
}
void main()
{
mang a;
clrscr();
int b=0,c,dem=0,i,j;
do
{
for(i=0; i<100; i++)
{ cout << "Nhap so bat ki:"; //Den so 0 se dung lai
cin >> a[i];
if(a[i]==0)
goto T;
if(dem==99)
goto T;
dem++;
}
}
while(1);
T: cout << "So lon lon nhat trong cac so vua nhap la:" << max(a,dem) << "
";
cout << "So nho nhat trong cac so vua nhap (Khong tinh 0) la:" << min(a,dem) << "
"; //Khong tinh phan tu 0
getch();
}
HINH CHU NHAT
#include<conio.h>
#include<stdio.h>
#include<iostream.h>
#include<graphics.h>
#include<string.h>
#include<math.h>
class hcn
{
int top,left,width,height,bgcolor;
public:
int gettop()
{
return this->top;
}
int getleft()
{
return this->left;
}
int getwidth()
{
return this->width;
}
int getheight()
{
return this->height;
}
int getbgcolor()
{
return this->bgcolor;
}
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); //Cach to mau nhu to dac, to cham, ...
bar(left, top, left+width, top+height);
}
else
{
cout<<"
Loi khoi tao che do Do hoa";
}
getch();
closegraph();
}
class MenuItem : public hcn
{
private:
char caption[100];
int color;
public:
MenuItem(int top,int left,int width,int height,int backgroundcolor, char caption[30], int color)
: hcn(top,left,width,height,backgroundcolor)
{
strcpy(this->caption,caption);
this->color = color;
}
void draw()
{
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, hcn::getbgcolor()); //Cach to mau nhu to dac, to cham, ...
bar( hcn::getleft(), hcn::gettop(), (hcn::getleft()) + (hcn::getwidth()), hcn::gettop() + hcn::getheight());
setcolor(this->color);
//outtextxy( ( hcn::gettop()+hcn::getwidth() )/2,
// (hcn::getleft() + hcn::getheight())/2,
// this->caption);
int a,b;
a=(hcn::gettop()+hcn::getwidth()/2);
b=(hcn::getleft()+hcn::getheight()/2);
outtextxy(a,b,this->caption);
}
else
{
cout<<"
Loi khoi tao che do Do hoa";
}
getch();
closegraph();
}
};
void main(){
MenuItem a(100, 100, 100, 100, YELLOW, "a y e", GREEN);
//int gdriver = DETECT,gmode,errorcode;
//initgraph(&gdriver,&gmode ,"C:\\LAPTRINH\\TC\\BGI");
a.draw();
closegraph();
}
/* Da nang hoa toan tu +,- voi mang 1 chieu
Pham Phuong Tu */
#include<iostream.h>
#include<conio.h>
class MANG
{
int x[100],n;
public:
void nhap();
void hien();
MANG operator+(MANG c);
MANG operator-(MANG c);
};
void MANG::nhap()
{
cout<<"Nhap so luong phan tu cho mang:";
cin>>this->n;
cout << "Nhap cac phan tu:
";
for(int i=0;i<n;i++)
{
cout << "a[" << i <<"]=";
cin >> this->x[i];
}
}
void MANG::hien()
{
for(int i=0;i<n;i++)
cout << this->x[i] <<" ";
}
MANG MANG::operator+(MANG c)
{
int i;
MANG e;
e.n=this->n;
for(i=0;i<n;i++)
e.x[i]=this->x[i]+c.x[i];
return e;
}
MANG MANG::operator-(MANG c)
{
int i;
MANG e;
e.n=this->n;
for(i=0;i<n;i++)
e.x[i]=this->x[i]-c.x[i];
return e;
}
void main()
{
Z:clrscr();
char z;
MANG a,b;
a.nhap();
b.nhap();
MANG kq;
cout << "
Ket qua cong mang 1 cho mang 2 la:
";
kq=a+b;
kq.hien();
cout << "
Ket qua tru mang 1 cho mang 2 la:
";
kq=a-b;
kq.hien();
cout << "
Ban co muon thu lai voi 2 mang khac?
";
cout << " -Nhan 1 de thu lai
";
cout << " -Nhan phim bat ki de thoat
";
z=getch();
if(z=='1')
goto Z;
getch();
}
http://www.wattpad.com/415177-baitap
Bạn đang đọc truyện trên: AzTruyen.Top