bai3_socp_f
#include"stdio.h"
#include"conio.h"
#include"iostream.h"
#include"iomanip.h"
#include"math.h"
class dayso
{
private:
float a[50];
int n;
public:
friend void doc(char *tentep,dayso &ds);
friend void ghi(char *tentep);
void nhap();
void xuat();
void nguyento();
void chinhphuong();
void hoanhao();
void sapxep();
void chen();
};
void dayso::nhap()
{
int i;
cout<<"
Nhap so phan tu cho day n="; cin>>n;
for(i=1;i<=n;i++)
{
cout<<"
a["<<i<<"]=";
cin>>a[i];
}
}
void doc(char *tentep,dayso &ds)
{
FILE *f;
f=fopen(tentep,"rb");
fread(&ds,sizeof(dayso),1,f);
fclose(f);
}
void taofile(char *tentep)
{
FILE *f;
dayso tg;
f=fopen(tentep,"wb");
cout<<"
Nhap day so:";
tg.nhap();
fwrite(&tg,sizeof(dayso),1,f);
fclose(f);
}
void dayso::xuat()
{
int i;
for(i=1;i<=n;i++)
cout<<setw(5)<<a[i];
}
void xuatfile(char *tentep)
{
FILE *f;
dayso tg;
f=fopen(tentep,"rb");
fread(&tg,sizeof(dayso),1,f);
cout<<"
Day so la:";
tg.xuat();
fclose(f);
}
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()
{
char *tentep;
dayso ds;
clrscr();
cout<<"
Nhap ten file:"; gets(tentep);
taofile(tentep);
cout<<"
Du lieu tu file:";
xuatfile(tentep);
cout<<"
";
doc(tentep,ds);
cout<<"
Du lieu tu lop:";
cout<<"
Day so 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 khi chen la:";
ds.xuat();
getch();
}
Bạn đang đọc truyện trên: Truyen247.Pro