bai3_matran
#include"stdio.h"
#include"conio.h"
#include"iostream.h"
#include"iomanip.h"
#include"math.h"
class dayso
{
private:
float a[50];
int n;
public:
void nhap(char ten);
void xuat();
void nguyento();
void chinhphuong();
void hoanhao();
void sapxep();
void chen();
};
void dayso::nhap(char ten)
{
int i;
cout<<"
Nhap so phan tu cua day n=";
cin>>n;
cout<<"
Nhap day"<<ten<<":
";
for(i=1;i<=n;i++)
{
cout<<ten<<"["<<i<<"]=";
cin>>a[i];
}
}
void dayso::xuat()
{
int i;
for(i=1;i<=n;i++)
cout<<setw(5)<<a[i];
}
int nt(int a)
{
int i,d;
if(a==1)
return 0;
if(a==2)
return 1;
if(a>2)
d=0;
for(i=2;i<=a/2;i++)
if(a%i==0)
d=d+1;
if(d==0)
return 1;
else
return 0;
}
void dayso::nguyento()
{
int i;
for(i=1;i<=n;i++)
if(nt(a[i]))
cout<<setw(5)<<a[i];
}
int cp(int a)
{
if(sqrt(a)==int(sqrt(a)))
return 1;
else
return 0;
}
void dayso::chinhphuong()
{
int i;
for(i=1;i<=n;i++)
if(cp(a[i]))
cout<<setw(5)<<a[i];
}
int hh(int a)
{
int i,d;
d=0;
for(i=1;i<=a/2;i++)
if(a%i==0)
d=d+i;
if(d==a)
return 1;
else
return 0;
}
void dayso::hoanhao()
{
int i;
for(i=1;i<=n;i++)
if(hh(a[i]))
cout<<setw(5)<<a[i];
}
void dayso::sapxep()
{
int i,j,tg;
for(i=1;i<=n;i++)
for(j=i+1;j<=n;j++)
if(a[i]>a[j])
{
tg=a[i];
a[i]=a[j];
a[j]=tg;
}
}
void dayso::chen()
{
int i,k,m;
cout<<"
Nhap gia tri can chen m=";
cin>>m;
k=0;
while(a[k]<=m)
k++;
if(k>n)
a[n+1]=m;
for(i=n+1;i>k;i--)
a[i]=a[i-1];
a[i]=m;
n++;
}
void main()
{
dayso ds;
clrscr();
ds.nhap('A');
cout<<"
Day so vua nhap la:
";
ds.xuat();
cout<<"
Cac so nguyen to la:
";
ds.nguyento();
cout<<"
Cac so chinh phuong la:
";
ds.chinhphuong();
cout<<"
Cac so hoan hao la:
";
ds.hoanhao();
cout<<"
Day sau khi sap xep tang la:
";
ds.sapxep();
ds.xuat();
ds.chen();
cout<<"
Day so sau hki chen la:";
ds.xuat();
getch();
}
Bạn đang đọc truyện trên: AzTruyen.Top