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

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

Đang tải... (xem toàn văn)

Thông tin tài liệu

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; return toigian(p); } 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; return toigian(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

Ngày đăng: 25/03/2018, 21:23

Từ khóa liên quan

Tài liệu cùng người dùng

Tài liệu liên quan