Baitapc
//Bai 1:Tinh dien tich va chu vi duong tron.
#include<iostream.h>
#include<math.h>
#include<conio.h>
void main()
{
nl: clrscr();
float r, PI, c, s;
PI=3.14;
cout<<"
Nhap ban kinh duong tron: ";
cin>>r;
c=2*r*PI;
s=r*r*PI;
cout<<"
Chu vi duong tron ban kinh "
<<r<<" la: "<<c;
cout<<"
Dien tich duong tron ban kinh "<<r<<" la: "<<s;
getch();
}
//Bai 2:Tinh chu vi va dien tich tam giac.
#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()
{
nl: clrscr();
float x, y, z,p, s;
cout<<"
Nhap 3 canh cua tam giac: "; cin>>x>>y>>z;
if (x+y>z && y+z>x)
{
p = (x+y+z)/2;
s=sqrt(p*(p-x)*(p-y)*(p-z));
cout<<"
Chu vi tam giac la: "<<2*p;
cout<<"
Dien tich tam giac la: "<<s;
getch();
}
else
{
cout<<"
Khong phai la tam giac! ";
cout<<"
Vui long nhap lai!";
getch();
goto nl;
}
}
//Bai 3:Giai phuong trinh bac 2.
#include<iostream.h>
#include<math.h>
#include<conio.h>
void main()
{ nl: clrscr();
float a, b, c, delta, x1, x2;
cout<<"
Nhap 3 he so cua phuong trinh: "; cin>>a>>b>>c;
if (a==0)
{
cout<<"
Khong phai phuong trinh bac 2!"<<endl;
cout<<"
Vui long nhap lai!";
getch();
goto nl;
}
else
{
delta=b*b-4*a*c;
if (delta == 0)
cout<<"
Phuong trinh co nghiem kep: "<<-b/(2*a);
else if (delta > 0)
{
x1=(-b+sqrt(delta))/(2*a);
x2=(-b-sqrt(delta))/(2*a);
cout<<"
Nghiem cua phuong trinh la: ";
cout<<"
x1= "<<x1;
cout<<"
x2= "<<x2;
}
else
cout<<"
Phuong trinh vo nghiem!";
}
getch();
}
//Bai 4:Tinh bieu thuc y=...
#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()
{ clrscr();
float x, y;
cout<<"Nhap vao gia tri cua x: "; cin>>x;
if (x<5)
{
y=abs(x)+log(x*x+4)/log(5);
cout<<"Gia tri cua y= "<<y;
}
else
{
y=pow(5,x)+pow((x*x+4),1/3);
cout<<"Gia tri cua y= "<<y;
}
getch();
}
//Bai 5:Tinh PI
#include<iostream.h>
#include<conio.h>
void main()
{ clrscr();
float s;
int n,i;
s = 0;
cout<<"
Nhap n: "; cin>>n;
for (i = 0; i < n; i ++)
if (i%2 == 0)
s +=1/(2 * i + 1);
else
s -= 1 /(2*i+1);
s = 4*s;
cout<<"
So PI= "<<s;
getch();
}
//Bai 6:Tinh e^x.
#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()
{ clrscr();
float e,i,s,x;
cout<<"
Nhap gia tri x: "; cin>>x;
i = e = s =1;
do
{
e *= x/i;
s += e;
i ++;
}
while(e > pow(10,-4));
cout<<"
e^"<<x<<" la: "<<s;
getch();
}
//Bai 7:Sap xep day so tang dan
#include<iostream.h>
#include<conio.h>
void main()
{ clrscr();
int a[100];
int n, i, j, k, tg;
cout<<"Nhap so phan tu cua mang: "; cin>>n;
for (i = 0; i< n; i ++)
{
cout<<"a"<<i+1<<": ";
cin>>a[i];
}
for (i = 0; i < n; i ++)
{
k = i;
for (j = i+1; j < n; j ++)
if (a[j]<a[k])
k = j;
if (k!=i)
{
tg=a[k];
a[k]=a[i];
a[i]=tg;
}
}
cout<<"Day so da sap xep la: ";
for(i = 0; i < n; i ++)
cout<<a[i]<<" ";
getch();
}
//Bai 8: Tinh tong va trung binh cong cac phan tu tren duong cheo chinh cua ma tran.
#include<iostream.h>
#include<conio.h>
void main()
{ clrscr();
int a[10][10], n, i, j,s = 0;
float tb;
cout<<"
Nhap gia tri n de xac dinh ma tran cap nxn: "; cin>>n;
for (i = 0; i < n; i ++)
for (j = 0; j < n; j ++)
{
cout<<"
a"<<i+1<<j+1<<": ";
cin>>a[i][j];
}
for (i=0; i<n; i++)
for (j=0; j<n; j++)
if(i == j)
s += a[i][j];
tb =float(s)/float(n);
cout<<endl<<"Tong cac phan tu tren duong cheo chinh la: "<<s<<endl;
cout<<"
Trung binh cong cac phan tu tren duong cheo chinh la: "<<tb;
getch();
}
//Bai 9:Nhap so nguyen dua ra so nhi phan.
#include<iostream.h>
#include<string.h>
#include<conio.h>
void main()
{ clrscr();
int n, i,sodu;
char s[10];
cout<<endl<<"Nhap so nguyen: "; cin>>n;
i = 0;
do
{
sodu=n%2;
n /= 2;
if (sodu == 1)
s[i] = '1';
else
s[i] = '0';
i ++;
}
while(n != 0);
cout<<endl<<"So nhi phan la : "<<strrev(s);
getch();
}
//Bai 10:Nhap so nhi phan dua ra so nguyen tuong ung.
#include<iostream.h>
#include<math.h>
#include<conio.h>
void main()
{ clrscr();
float s;
char ch;
cout<<endl<<"Nhap day so nhi phan: ";
ch = getch();
cout<<ch;
if (ch == 1)
s = - pow(2,15);
else
s = 0;
for (i = 0; i < 15; i ++)
{
ch = getch();
cout<<ch;
if (ch == 1)
s += pow(2,14- float(i));
}
cout<<endl<<"So nhi phan la : "<<s;
getch();
}
//Bai 12:Tinh to hop chap k cua n phan tu.
#include<iostream.h>
#include<conio.h>
void main()
{ clrscr();
int long k, i, t,n;
t = 1;
cout<<"
Nhap k va n de tinh to hop chap k cua n phan tu la: "; cin>>k>>n;
for(i =1 ; i<=(n - k); i++)
t *= (k+i)/i;
cout<<"
To hop chap"<<k<<" cua "<<n<<"la : "<<t;
getch();
}
//Bai 13:Tao Menu.
#include<iostream.h>
#include<iomanip.h>
#include<math.h>
#include<conio.h>
void main()
{ tt: clrscr();
float x, y, z, p, r, c, s;
float PI;
PI=3.14;
cout<<endl<<"1: Hinh tam giac."<<endl;
cout<<endl<<"2: Hinh tron."<<endl;
cout<<endl<<"3: Hinh chu nhat."<<endl;
cout<<endl<<"4: Ketthuc"<<endl;
cout<<endl<<"Nhap hinh can tinh: "; cin>>t;
switch(t)
{
case 1:
{
cout<<endl<<"Nhap 3 canh cua tam giac: "; cin>>x>>y>>z;
if (x+y>z && y+z>x && x>0 && y>0 && z>0)
{
c=x+y+z;
p=c/2;
s=sqrt(p*(p-x)*(p-y)*(p-z));
cout<<endl<<"Chu vi tam giac la: "<<c;
cout<<endl<<endl<<"Dien tich tam giac la: "<<s;
}
break;
}
case 2:
{
cout<<endl<<"Nhap ban kinh duong tron: "; cin>>r;
c=2*r*PI;
s=r*r*PI;
cout<<endl<<"Chu vi duong tron ban kinh "<<r<<" la: "<<c;
cout<<endl<<endl<<"Dien tich duong tron ban kinh "<<r<<" la: "<<s;
break;
}
case 3:
{
cout<<endl<<"Nhap cac canh cua hinh chu nhat: "; cin>>x>>y;
if (x>0 && y>0)
{
c=(x+y)*2;
s=x*y;
cout<<endl<<"Chu vi hinh chu nhat la: "<<c;
cout<<endl<<endl<<"Dien tich hinh chu nhat la: "<<s;
}
else
cout<<endl<<"Day khong phai hinh chu nhat!";
break;
}
case 4:
{
goto dl;
break:
}
}
getch();
dl: getch();
}
//Bai 14:Tinh sin(x)
#include<iostream.h>
#include<math.h>
#include<conio.h>
void main()
{ clrscr();
int i,n,m,dem;
float x,s,t;
cout<<"
Nhap gia tri x trong khoang (-PI -> PI): "; cin>>x;
i =1; t = 1; s = x;
do
{
n = 2*i +1;
m = n -2;
for ( dem = m ; dem <= n; dem ++)
t *= x/float(dem);
if (i % 2 == 0)
s += t;
else
s -= t;
i ++;
}
while(t>=pow(10,-4));
cout<<"
Sin ("<<x<<") : "<<s;
getch();
}
//Bai 15:Xac dinh so nguyen to
#include<iostream.h>
#include<math.h>
#include<conio.h>
void main()
{ clrscr();
int long x,s,t;
t =1;
cout<<"
Nhap gia tri x de xac dinh so nguyen to trong khoang tu ( 0 -> x): "; cin>>x;
cout<<"
So nguyen to la : ";
while(t <= x)
{
int dung = 1;
for(i = 2; i<=sqrt(t)+1;i++)
if (t%i == 0) dung = 0;
if (dung) cout<<t<<" ";
t ++;
};
getch();
}
//Bai 18: Viết chương trình nhập vào một xâu ký tự. Hãy kiểm tra xem xâu đó có đối xứng không? Đưa ra màn hình ký tự đầu và ký tự cuối của xâu
#include<iostream.h>
#include<string.h>
#include<conio.h>
void main()
{
char s[100];
int n,ketqua =1 ;
cout<<"
Nhap mot xau bat ky: "; //qwewq 01234; 0 - 4,1-3,2-2,3-1
cin.get(s,100); cin.ignore();
n = strlen(s);
for( int i =0 ; i <=n/2;i++)
if(s[i] != s[n-1-i])
{
ketqua = 0;
break;
}
if (ketqua)
cout<<"
Xau vua nhap doi xung";
else
cout<<"
Xau vua nhap khong doi xung";
cout<<"
Ky tu dau la: "<<s[0]<<"
Ky tu cuoi cung la: "<<s[n-1];
getch();
}
//Bai 19:Nhap so nguyen dua ra so hex
#include<iostream.h>
#include<string.h>
#include<conio.h>
void main()
{ clrscr();
long int n, i,sodu;
char s[10] = "";
cout<<endl<<"Nhap so nguyen: "; cin>>n;
i = 0;
do
{
sodu=n%16;
n /= 16;
if (sodu >=0 && sodu <=9)
s[i] = sodu +48;
else
s[i] = sodu +55;
i ++;
}
while(n != 0);
cout<<endl<<"So nhi phan la : "<<strrev(s);
getch();
}
//Bai 20: Sap xep giam dan, tinh tong va trung binh cong.
#include<iostream.h>
#include<iomanip.h>
void tangdan(int *s, int n);
float trungbinh(int *s, int n);
int tong(int *s,int n);
void main()
{
int *p,n,i;
cout<<"
Nhap gioi so gioi han cua day: "; cin>>n;
p = new int(n);
for(i=0;i<n; i ++)
{
cout<<"
Phan tu thu: "<<i+1<<" : ";
cin>>p[i];
}
cout<<"
Day duoc sap xep khong tang la :
";
tangdan(p,n);
cout<<"
Trung binh cong cua day so tren la : "<<trungbinh(p,n);
cout<<"
Tong cua mang la:" << tong(p,n);
delete [] p;
}
void tangdan( int *s, int n)
{
int i, j, k, tg;
for (i = 0; i < n; i ++)
{
k = i;
for (j = i+1; j < n; j ++)
if (s[j]>s[k])
k = j;
if (k!=i)
{
tg=s[k];
s[k]=s[i];
s[i]=tg;
}
}
cout<<"Day so da sap xep la: ";
for(i = 0; i < n; i ++)
cout<<s[i]<<" ";
}
float trungbinh(int *s, int n)
{
float tb,i;
int to = 0;
for( i=0; i<n;i ++) to += s[i];
tb = (float)(to)/ (float)(n);
return tb;
}
int tong(int *s, int n)
{
float i,kq = 0;
for (i = 0; i<n; i++) kq += s[i];
return kq;
}
/Bai 19:Nhap so nguyen dua ra so hex
#include<iostream.h>
#include<string.h>
#include<conio.h>
void main()
{ clrscr();
long int n, i,sodu;
char s[10] = "";
cout<<endl<<"Nhap so nguyen: "; cin>>n;
i = 0;
do
{
sodu=n%16;
n /= 16;
if (sodu >=0 && sodu <=9)
s[i] = sodu +48;
else
s[i] = sodu +55;
i ++;
}
while(n != 0);
cout<<endl<<"So nhi phan la : "<<strrev(s);
getch();
}
Bai 21
#include <iostream.h>
class Stack
{
private:
int *p;
static tong;
public:
Stack()
{
p= new int[50];
}
Stack (int n)
{
p= new int[n];
}
int Push (int);
int Pop();
int NotEmpty();
int Top();
};
int Stack::tong=0;
int Ktsonguyento(int);
void main()
{
int i,n,j;
Stack A;
cout<<"Nhap vao gia tri n: ";
cin>>n;
for (i=0;i<n;i++)
{
cout<<"A["<<i<<"] = ";
cin>>j;
A.Push(j);
}
n=A.Top();
while (A.NotEmpty())
{
if (Ktsonguyento(A.Pop())) cout<<A.Top()<<" ";
}
}
Stack::Push (int k)
{
p[tong]=k;
tong=tong+1;
return 0;
}
Stack::Pop()
{
if (tong>0) tong=tong-1;
return p[tong];
}
Stack::Top()
{
return p[tong];
}
Stack::NotEmpty()
{
return tong;
}
int Ktsonguyento(int m)
{
for (i=2;i<m;i++)
if ((m%i)==0)
return 0;
return 1;
}
bai 22
#include <iostream.h>
#include <string.h>
#include <conio.h>
class hocsinh
{
private:
char hodem[20];
char ten[7];
char ngaysinh[10];
char lop[4];
float diemcc, diemkt, diemthi, diemmh;
public:
int Put();
char* Getten()
{
return ten;
};
float getdiemmh()
{
return diemmh;
};
hocsinh()
{};
int Show();
};
void main()
{
hocsinh A[10];
hocsinh tg;
int n,i,k,j;
cout<<"Nhap vao so phan tu: ";
cin>>n;
for (i=0;i<n;i++)
{
clrscr();
cout<<"Nhap thong tin cho hoc sinh thu "<<i+1<<endl;
A[i].Put();
}
for (i=0;i<n-1;i++)
{
k=i;
for (j=i+1;j<n;j++)
if (strcmp(A[i].Getten(),A[j].Getten())>0) k=j;
if (k!=i)
{
tg=A[i];
A[i]=A[k];
A[k]=tg;
};
};
cout<<"Sap xep xong"<<endl;
for (i=0;i<n;i++) A[i].Show();
for (i=0;i<n;i++)
{
if (A[i].getdiemmh()>5) A[i].Show();
}
}
hocsinh::Put()
{
cout<<"Nhap vao ho dem ";
cin>>hodem;
cout<<"Nhap vao ten ";
cin>>ten;
cout<<"Nhap vao ngay sinh ";
cin>>ngaysinh;
cout<<"Nhap vao lop ";
cin>>lop;
cout<<"Nhap vao diem chuyen can: ";
cin>>diemcc;
cout<<"Nhap vao diem kiem tra: ";
cin>>diemkt;
cout<<"Nhap vao diem thi: ";
cin>>diemthi;
diemmh=0.1*diemcc+0.3*diemkt+0.6*diemthi;
return 0;
}
hocsinh::Show()
{
cout<<hodem<<" "<<ten<<" "<<ngaysinh<<" "<<lop<<" "<<diemcc<<" "<<diemkt<<" "<<diemthi<<" "<<diemmh<<endl;
return 0;
}
bai 23
#include <iostream.h>
#include <string.h>
class xau
{
private:
enum {size=256};
char* chuoi;
public:
xau()
{}
xau(char* xaukitu):chuoi(xaukitu)
{}
void show();
xau operator+(char*);
int operator>(xau right);
int operator<(xau right);
int operator>=(xau right);
int operator<=(xau right);
int operator==(xau right);
friend xau operator+(xau, xau);
//xau::operator char*()
//{
//return chuoi;
//}
};
int main()
{
xau a,b,c;
char *d,e;
a="AB";
b="AM";
a.show();
cout<<"D";
b.show();
cout<<endl;
c=a+b;
a.show();
cout<<endl;
b.show();
cout<<endl;
c.show();
cout<<endl;
return 0;
}
void xau::show()
{
cout<<chuoi;
}
int xau::operator>(xau right)
{
if (strcmp(chuoi,right.chuoi)>0)
return 1;
return 0;
}
int xau::operator>=(xau right)
{
if (strcmp(chuoi,right.chuoi)>=0)
return 1;
return 0;
}
int xau::operator==(xau right)
{
if (strcmp(chuoi,right.chuoi)==0)
return 1;
return 0;
}
int xau::operator<(xau right)
{
if (strcmp(chuoi,right.chuoi)<0)
return 1;
return 0;
}
int xau::operator<=(xau right)
{
if (strcmp(chuoi,right.chuoi)<=0)
return 1;
return 0;
}
xau operator+(xau left,xau right)
{
char* chuoit;
char* chuoip;
chuoit=left.chuoi;
chuoip=right.chuoi;
strcat(chuoit,chuoip);
return chuoit;
}
/*
xau xau::operator+(char* x)
{
strcat(chuoi,x);
return *this;
}
xau xau::operator+(xau& right)
{
xau tmp(chuoi);
strcat(tmp.chuoi,right.chuoi);
return tmp;
}
*/
Bai 24
#include <iostream.h>
class phanso
{
private:
int tuso,mauso;
public:
void show();
void get();
phanso()
{}
phanso(int t,int m): tuso(t),mauso(m)
{}
phanso operator+(phanso right);
phanso operator-(phanso right);
phanso operator*(phanso right);
phanso operator/(phanso right);
phanso rutgon();
};
int UCNN(int a,int b);
int main()
{
phanso a(1,3),b(3,15),c,d;
c=a-b;
c.show();
d=c.rutgon();
d.show();
return 0;
}
void phanso::show()
{
cout<<tuso<<'/'<<mauso;
}
phanso phanso::operator*(phanso right)
{
tuso=tuso*right.tuso;
mauso=mauso*right.mauso;
return *this;
}
phanso phanso::operator/(phanso right)
{
tuso=tuso*right.mauso;
mauso=mauso*right.tuso;
return *this;
}
phanso phanso::operator+(phanso right)
{
tuso=tuso*right.mauso+mauso*right.tuso;
mauso=mauso*right.mauso;
return *this;
}
phanso phanso::operator-(phanso right)
{
tuso=tuso*right.mauso-mauso*right.tuso;
mauso=mauso*right.mauso;
return *this;
}
int UCNN(int a,int b)
{
while (a!=b)
{
if (a>b) a=a-b;
else b=b-a;
};
return a;
}
phanso phanso::rutgon()
{
phanso tmp;
tmp.tuso=tuso/(UCNN(tuso,mauso));
tmp.mauso=mauso/(UCNN(tuso,mauso));
return *this;
}
bai 25
#include<iostream.h>
class Mang
{
public:
void Hien();
void Nhap();
int& operator[](int);
Mang(int n=50)
{
Size=n;
A=new int[Size];
}
void Delete(int n);
operator int()
{ return A[1];}
private:
int Size;
int*A;
};
int main()
{
Mang M(5),N;
int i,n,k;
for(i=0;i<6;i++)
{
cout<<"Nhap vao phan tu thu "<<i+1<<": ";
cin>>M[i];
};
k=M;
cout<<k;
//M.Delete(2);
//for(i=0;i<5;i++)
//cout<<M[i]<<endl;
return 0;
}
void Mang::Hien()
{
for (i=0;i<Size;i++)
cout<<A[i]<<" ";
}
int& Mang::operator[](int n)
{
if (n>Size)
{
cout<<"Vuot qua chi so cho phep!"<<endl;
return A[Size];
}
return A[n];
}
void Mang::Delete(int n)
{
for(i=n;i<Size;i++)
A[i]=A[i+1];
}
Bai 26
#include <iostream.h>
class sophuc
{
private:
int a;
public:
void show();
void get();
sophuc()
{}
sophuc(int c,int d):a(c),b(d)
{}
sophuc operator+(sophuc right);
};
int main()
{
sophuc a(2,5),b(2,6),c;
c=a+b;
c.show();
return 0;
}
void sophuc::show()
{
cout<<a<<" + "<<b<<'i';
}
void sophuc::get()
{
cout<<"Nhap vao phan thuc: ";
cin>>a;
cout<<"Nhap vao phan ao: ";
cin>>b;
}
sophuc sophuc::operator+(sophuc right)
{
a=a+right.a;
b=b+right.b;
return *this;
}
bai 28
#include<iostream.h>
#include<math.h>
class dagiac
{
protected:
float dt,cv;
public:
virtual void Hien();
virtual void Nhap()
{}
};
class hcnhat:public dagiac
{
private:
float a,b;
public:
void Hien();
void Nhap();
};
class htron:public dagiac
{
private:
float r;
public:
void Hien();
void Nhap();
};
class htgiac:public dagiac
{
private:
float a,b,c;
public:
void Hien();
void Nhap();
};
dagiac* Nhapthongtin();
void Hienthongtin();
void Xoamanhinh(int n);
//CHUONG TRINH CHINH
int main()
{
int n=0,i,menu;
char tt;
dagiac* A[100];
do
{
cout<<" Chuong trinh tinh dien tich va chu vi cac da giac quen thuoc "<<endl
<<" 1.Nhap kich thuoc cac hinh"<<endl
<<" 2.Hien thi thong tin cac hinh da nhap"<<endl
<<" 3.Ket thuc chuong trinh"<<endl
<<"Nhap vao lua chon: ";
Xoamanhinh(5);
cin>>menu;
switch(menu)
{
case 1:
do
{
A[n++]=Nhapthongtin();
cout<<"Ban co muon nhap tiep khong? (c/k) ";
cin>>tt;
}
while ((tt!='k')&&(tt!='K'));
break;
case 2:
for (i=0;i<n;i++)
{
cout<<"Hinh thu "<<i+1<<endl;
A[i]->Hien();
};
cin.get();
cin.ignore();
break;
case 3:
break;
};
}
while(menu!=3);
return 0;
}
//DING NGHIA CAC HAM THONG THUONG
void Xoamanhinh(int n)
{
int i,j;
for(i=0;i<10-n;i++)
{
cout.width(80);
cout<<""<<endl;
};
}
dagiac* Nhapthongtin()
{
dagiac* p;
int chon;
cout<<" 1.Hinh chu nhat"<<endl
<<" 2.Hinh tam giac"<<endl
<<" 3.Hinh tron"<<endl
<<" Nhap vao lua chon: ";
Xoamanhinh(4);
cin>>chon;
switch (chon)
{
case 1:
p=new hcnhat;
break;
case 2:
p=new htgiac;
break;
case 3:
p=new htron;
break;
default:
cout<<"Ban nhap chon sai roi!"<<endl;
break;
};
p->Nhap();
return p;
}
//DINH NGHIA CAC HAM TRONG LOP
void dagiac::Hien()
{
cout.precision(2);
cout.setf(ios::showpoint);
cout<<" Dien tich: "<<dt;
cout<<" Chu vi: "<<cv<<endl;
}
void hcnhat::Nhap()
{
float a,b;
cout<<" Nhap vao hai canh hinh chu nhat: ";
cin>>a>>b;
dt=a*b;
cv=(a+b)*2;
}
void hcnhat::Hien()
{
cout<<"Dien tich va chu vi hinh chu nhat"<<endl;
dagiac::Hien();
}
void htgiac::Nhap()
{
float a,b,c,p;
cout<<"Nhap vao cac canh tam giac ";
cin>>a>>b>>c;
cv=a+b+c;
p=cv/2;
dt=sqrt(p*(p-a)*(p-b)*(p-c));
}
void htgiac::Hien()
{
cout<<"Dien tich va chu vi hinh tam giac"<<endl;
dagiac::Hien();
}
void htron::Nhap()
{
float r;
cout<<"Nhap vao ban kinh hinh tron: ";
cin>>r;
dt=3.14*r*r;
cv=3.14*2*r;
}
void htron::Hien()
{
cout<<"Dien tich va chu vi hinh tron"<<endl;
dagiac::Hien();
}
Bai 29
#include<iostream.h>
#include<string.h>
#include<conio.h>
class nhansu
{
private:
char hoten[30],ngaysinh[15];
public:
virtual void Nhap();
virtual void Hien();
};
class canboql:public nhansu
{
private:
char chucvu[10];
public:
void Nhap();
void Hien();
};
class giaovien:public nhansu
{
private:
char bomon[15];
public:
void Nhap();
void Hien();
};
class nhanvienpv:public nhansu
{
private:
public:
void Hien();
void Nhap();
};
nhansu* Nhapthongtin();
void Hienthongtin(nhansu* p);
int main()
{
int chon,i,n=0;
char tt;
nhansu* A[100];
do
{
clrscr();
cout<<" Chuong trinh quan ly nhan su"<<endl
<<" 1.Nhap thong tin"<<endl
<<" 2.Hien thong tin"<<endl
<<" 3.Ket thuc chuong trinh"<<endl
<<" Nhap vao lua chon cua ban: ";
cin>>chon;
cin.get();
switch (chon)
{
case 1:
do
{
A[n++]=Nhapthongtin();
cout<<"Ban co muon tiep tuc khong? (c/k) ";
cin>>tt;
cin.get();
}
while (tt!='k');
break;
case 2:
for (i=0;i<n;i++)
{
cout<<"Thong tin nhan su "<<i+1<<endl;
Hienthongtin(A[i]);
}
cin.ignore();
break;
case 3:
break;
};
}
while(chon!=3);
return 0;
}
//DINH NGHIA CAC HAM THONG THUONG
nhansu* Nhapthongtin()
{
int lchon;
nhansu* p;
clrscr();
cout<<" 1.Can bo quan ly"<<endl
<<" 2.Giao vien"<<endl
<<" 3.Nhan vien phuc vu"<<endl
<<" Nhap vao lua chon cua ban: ";
cin>>lchon;
cin.get();
switch(lchon)
{
case 1:
p=new canboql;
break;
case 2:
p=new giaovien;
break;
case 3:
p=new nhanvienpv;
break;
};
p->Nhap();
return p;
}
void Hienthongtin(nhansu* pt)
{
pt->Hien();
};
//DINH NGHIA CAC HAM TRONG LOP
void nhansu::Nhap()
{
cout<<" Ho va ten: ";
cin.get(hoten,sizeof(hoten));
cin.get();
cout<<" Ngay sinh: ";
cin.get(ngaysinh,sizeof(ngaysinh));
cin.get();
}
void nhansu::Hien()
{
cout<<" Ho va ten: "<<hoten<<endl
<<" Ngay sinh: "<<ngaysinh<<endl;
}
void canboql::Nhap()
{
cout<<"Nhap thong tin cho can bo quan ly "<<endl;
nhansu::Nhap();
cout<<" Chuc vu: ";
cin>>chucvu;
cin.get();
}
void canboql::Hien()
{
nhansu::Hien();
cout<<" Chuc vu: "<<chucvu<<endl;
}
void giaovien::Nhap()
{
cout<<"Nhap thong tin cho giao vien "<<endl;
nhansu::Nhap();
cout<<" Bo mon giang day: ";
cin>>bomon;
}
void giaovien::Hien()
{
nhansu::Hien();
cout<<" Bo mon giang day: "<<bomon<<endl;
}
void nhanvienpv::Nhap()
{
cout<<"Nhap thong tin cho nhan vien phuc vu "<<endl;
nhansu::Nhap();
}
void nhanvienpv::Hien()
{
nhansu::Hien();
}
Bạn đang đọc truyện trên: Truyen4U.Com