# Bài tập lập trình PHẦN III cấu trúc

Phần III : Cấu Trúc
Câu 1:
#include
#include
struct ps{int tu,mau;};
int ucln(int a,int b)
{if (a==b) return a;
if (a>b) return ucln(a-b,b);
return ucln(a,b-a);
}
ps tao(int t, int m)
{ps p;
p.tu=t;
p.mau=m;
return p;
}
ps toigian(ps p)
{int d=ucln(p.tu,p.mau);
p.tu=p.tu/d;
p.mau=p.mau/d;
return p;

}
void in(ps p)
{if ((p.tu==0)||(p.mau==1))
printf("%d",p.tu);
else
if(p.tu==p.mau) printf("1");
else printf("%d/%d",p.tu,p.mau);
}
ps tong(ps p1,ps p2)
{ps p;
p.tu=p1.tu*p2.mau+p2.tu*p1.mau;
p.mau=p1.mau*p2.mau;
}
void main()
{ps p,p1,p2;
int t1,t2,m1,m2;
clrscr();
printf("\n nhap tu, mau cua phan so 1: ");
scanf("%d%d",&t1,&m1);
printf("\n nhap tu, mau cua phan so 2: ");

scanf("%d%d",&t2,&m2);
p1=tao(t1,m1); in(p1);
p2=tao(t2,m2); printf(" + "); in(p2);
p=tong(p1,p2);
printf(" = "); in(p);
getch();
}
Câu 2:
#include
#include
struct ps{int tu,mau;};
int ucln(int a,int b)
{if (a==b) return a;
if (a>b) return ucln(a-b,b);
return ucln(a,b-a);
}
ps tao(int t, int m)

{ps p;
p.tu=t;
p.mau=m;
return p;
}
ps toigian(ps p)
{int d=ucln(p.tu,p.mau);
p.tu=p.tu/d;
p.mau=p.mau/d;
return p;
}
void in(ps p)
{if ((p.tu==0)||(p.mau==1))
printf("%d",p.tu);
else
if(p.tu==p.mau) printf("1");
else printf("%d/%d",p.tu,p.mau);
}
ps hieu(ps p1,ps p2)
{ps p;
p.tu=p1.tu*p2.mau-p2.tu*p2.mau;
p.mau=p1.mau*p2.mau;
if (p.tu==0) return p;
}

void main()
{ps p,p1,p2;
int t1,t2,m1,m2;
clrscr();
nhap: printf("\n nhap tu, mau cua phan so 1: ");
scanf("%d%d",&t1,&m1);
printf("\n nhap tu, mau cua phan so 2: ");
scanf("%d%d",&t2,&m2);
if ((m1==0)||(m2==0))
{printf("\n mau so khong duoc bang 0, moi nhap lai:");
goto nhap;
}
p1=tao(t1,m1); in(p1);
p2=tao(t2,m2); printf(" - "); in(p2);
p=hieu(p1,p2);
printf(" = "); in(p);
getch();
}
Câu 3:
#include
#include
struct sp{float thuc,ao;};
sp tao(float t, float a)
{struct sp p;
p.thuc=t;
p.ao=a;
return p;
}
void in(sp p)
{if (p.thuc==0) printf("i*%0.1f",p.ao);
else
if(p.ao==0) printf("%0.1f",p.thuc);
else
if (p.ao<0) printf("(%0.1f - i*%0.1f)",p.thuc,-p.ao);
else printf("(%0.1f + i*%0.1f)",p.thuc,p.ao);
}
sp tong(sp p1,sp p2)
{struct sp p;
p.thuc=p1.thuc+p2.thuc;
p.ao=p1.ao+p2.ao;
return p;
}

void main()
{struct sp p,p1,p2;
float t1,t2,a1,a2;
clrscr();
printf("\n nhap phan thuc, phan ao cua so phuc 1: ");
scanf("%f%f",&t1,&a1);
printf("\n nhap phan thuc, phan ao cua so phuc 2: ");
scanf("%f%f",&t2,&a2);
p1=tao(t1,a1); in(p1);
p2=tao(t2,a2); printf(" + "); in(p2);
p=tong(p1,p2);
printf(" = "); in(p);
getch();
}
Câu 4:
#include
#include
struct sp{float thuc,ao;};
sp tao(int t, int a)
{sp p;
p.thuc=t;
p.ao=a;
return p;
}
void in(sp p)
{if (p.thuc==0) printf("i*%0.1f",p.ao);
else
if(p.ao==0) printf("%0.1f",p.thuc);
else
if (p.ao<0) printf("(%0.1f - i*%0.1f)",p.thuc,-p.ao);
else printf("(%0.1f + i*%0.1f)",p.thuc,p.ao);
}
sp hieu(sp p1,sp p2)
{sp p;
p.thuc=p1.thuc-p2.thuc;
p.ao=p1.ao-p2.ao;
return p;
}
void main()
{sp p,p1,p2;
float t1,t2,a1,a2;
clrscr();

printf("\n nhap phan thuc, phan ao cua so phuc 1: ");
scanf("%f%f",&t1,&a1);
printf("\n nhap phan thuc, phan ao cua so phuc 2: ");
scanf("%f%f",&t2,&a2);
p1=tao(t1,a1); in(p1);
p2=tao(t2,a2); printf(" - "); in(p2);
p=hieu(p1,p2);
printf(" = "); in(p);
getch();
}
Câu 5:
#include
#include
#include
#include
struct sv
{char hoten[30];
int tuoi;
float diemky1,diemky2,diemcanam;
};
void nhap(sv a[],int n)
{float tg; int i;
for (i=0;i{printf("\n moi nhap thong tin cua sinh vien thu %d: ",i);
printf("\n nhap ho va ten: "); fflush(stdin); gets(a[i].hoten);
printf("\n nhap tuoi: "); scanf("%d",&a[i].tuoi);
printf("\n nhap diem ky 1: "); scanf("%f",&tg); a[i].diemky1=tg;
printf("\n nhap diem ky 2: "); scanf("%f",&tg); a[i].diemky2=tg;
a[i].diemcanam=(a[i].diemky1+a[i].diemky2*2)/3;
}
}
void xem(sv a[],int n)
{int i;
for (i=0;iprintf("\n %s %10d %5.1f",a[i].hoten,a[i].tuoi,a[i].diemcanam);
}
void main()
{struct sv a[50];
int n;
clrscr();
nhap: printf("\n moi nhap so luong sinh vien: ");
scanf("%d",&n);

if(n<=0) goto nhap;
nhap(a,n);
printf("\n xem thong tin cac sinh vien vua nhap:");
xem(a,n);
getch();
}

### Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tải bản đầy đủ ngay

×