Ví dụ : Cho một mảng các phân số (PHANSO) gồm n phần tử (n≤50). Hãy viết chương trình nhập và xuất danh sách các phân số sau đó tìm phân số có giá trị lớn nhất, tổng và tích các phân số và nghịch đảo giá trị các phân số trong mảng.
#define MAX 100
typedef struct PHANSO
{
int tu, mau;
};
void NhapPS(PHANSO &ps);
void XuatPS(PHANSO ps);
void NhapMangPS(PHANSO dsps[], int &n);
void XuatMangPS(PHANSO dsps[], int n);
PHANSO TimMax(PHANSO dsps[], int n);
int KiemTra(PHANSO ps);
//Tra ve 1: Neu hop le
int USCLN(int a, int b);
PHANSO RutGon(PHANSO ps);
PHANSO NghichDao(PHANSO ps);
PHANSO Nhan(PHANSO ps1, PHANSO ps2);
PHANSO Chia(PHANSO ps1, PHANSO ps2);
PHANSO Tru(PHANSO ps1, PHANSO ps2);
PHANSO Cong(PHANSO ps1, PHANSO ps2);
int SoSanh(PHANSO ps1, PHANSO ps2);
//Tra ve 0: ps1=ps2
//Tra ve 1: ps1>ps2
//Tra ve -1: ps1<ps2
PHANSO TongCacPS(PHANSO dsps[], int n);
PHANSO TichCacPS(PHANSO dsps[], int n);
void NghichDaoCacPS(PHANSO dsps[], int n);
void main()
{
int n;
PHANSO a[MAX], max, s, p;
clrscr();
NhapMangPS(a, n);
printf("\nMang cac phan so vua nhap: ");
XuatMangPS(a, n);
max=TimMax(a, n);
printf("\nPhan so co gia tri lon nhat: ");
XuatPS(max);
s=TongCacPS(a, n);
printf("\nTong gia tri cac phan so co trong mang: ");
XuatPS(s);
p=TichCacPS(a, n);
printf("\nTich gia tri cac phan so co trong mang: ");
XuatPS(p);
NghichDaoCacPS(a, n);
printf("\nMang phan so sau khi nghich dao cac phan tu: ");
XuatMangPS(a, n);
getch();
}
void NhapPS(PHANSO &ps)
{
do
{
printf("\nNhap tu so: ");
scanf("%d", &ps.tu);
printf("\nNhap mau so: ");
scanf("%d", &ps.mau);
if(!KiemTra(ps))
printf("\nMau so khong duoc bang 0, nhap lai phan so\n");
else
break;
} while(1);
ps=RutGon(ps);
}
void XuatPS(PHANSO ps)
{
printf("%d", ps.tu);
if(ps.tu&&ps.mau!=1)
printf("/%d", ps.mau);
}
void NhapMangPS(PHANSO dsps[], int &n)
{
printf("\nNhap so luong phan so: ");
scanf("%d", &n);
for(int i=0; i<n; i++)
{
printf("\nNhap vao phan so thu %d: ", i+1);
NhapPS(dsps[i]);
}
}
void XuatMangPS(PHANSO dsps[], int n)
{
for(int i=0; i<n; i++)
{
XuatPS(dsps[i]);
printf("\t");
}
}
int KiemTra(PHANSO ps)
{
if(ps.mau==0)
return 0;
return 1;
}
int USCLN(int a, int b)
{
a=abs(a);
b=abs(b);
while(a!=b)
{
if(a>b)
a=a-b;
else
b=b-a;
}
return a;
}
PHANSO RutGon(PHANSO ps)
{
int us;
if(ps.tu==0)
return ps;
us=USCLN(ps.tu, ps.mau);
ps.tu=ps.tu/us;
ps.mau=ps.mau/us;
return ps;
}
PHANSO NghichDao(PHANSO ps)
{
PHANSO kq;
kq.tu=ps.mau;
kq.mau=ps.tu;
return kq;
}
PHANSO Nhan(PHANSO ps1, PHANSO ps2)
{
PHANSO kq;
kq.tu=ps1.tu*ps2.tu;
kq.mau=ps1.mau*ps2.mau;
kq=RutGon(kq);
return kq;
}
PHANSO Chia(PHANSO ps1, PHANSO ps2)
{
PHANSO kq;
kq=Nhan(ps1, NghichDao(ps2));
return kq;
}
PHANSO Tru(PHANSO ps1, PHANSO ps2)
{
PHANSO kq;
kq.tu=ps1.tu*ps2.mau-ps1.mau*ps2.tu;
kq.mau=ps1.mau*ps2.mau;
kq=RutGon(kq);
return kq;
}
PHANSO Cong(PHANSO ps1, PHANSO ps2)
{
PHANSO kq;
kq.tu=ps1.tu*ps2.mau+ps1.mau*ps2.tu;
kq.mau=ps1.mau*ps2.mau;
kq=RutGon(kq);
return kq;
}
int SoSanh(PHANSO ps1, PHANSO ps2)
{
ps1=RutGon(ps1);
ps2=RutGon(ps2);
if(ps1.tu==ps2.tu&&ps1.mau==ps2.mau)
return 0;
if(ps1.tu*ps2.mau>ps2.tu*ps1.mau)
return 1;
return -1;
}
PHANSO TimMax(PHANSO dsps[], int n)
{
PHANSO max;
max=dsps[0];
for(int i=1; i<n; i++)
if(SoSanh(dsps[i], max)==1)
max=dsps[i];
return max;
}
PHANSO TongCacPS(PHANSO dsps[], int n)
{
PHANSO s=dsps[0];
for(int i=1; i<n; i++)
{
s=Cong(s, dsps[i]);
}
return s;
}
PHANSO TichCacPS(PHANSO dsps[], int n)
{
PHANSO p=dsps[0];
for(int i=1; i<n; i++)
{
p=Nhan(p, dsps[i]);
}
return p;
}
void NghichDaoCacPS(PHANSO dsps[], int n)
{
for(int i=0; i<n; i++)
{
dsps[i]=NghichDao(dsps[i]);
}
}
0 nhận xét:
Đăng nhận xét