De Bai Va Loi Giai C

51
Câu 1: Viết chương trình chuyển đổi số thập phân 256<n<65535 sang số nhị phân dùng toán tử dịch bít (bitwise). Trong đó n được nhập vào từ bàn phím. Bài làm: #include<conio.h> #include<stdio.h> long n; void nhap() { printf("ban hay nhap vao mot so 256< =n<=65536\n"); while(n<256||n>65535) { printf("n= ");scanf("%d",&n); } } /***************************************************************** **********/ void doi(long n) { int i,b; for(i=15;i>=0;--i) { b=n>>i; if(0x01&b) printf("1"); else printf("0"); } printf("\n"); }

Transcript of De Bai Va Loi Giai C

  • Cu 1: Vit chng trnh chuyn i s thp phn 256>i; if(0x01&b) printf("1"); else printf("0"); } printf("\n"); }

  • /***************************************************************************/ void main() { long b; clrscr(); printf("nhap vao so can doi\n"); nhap(); doi(n); getch(); } Cu 2: Vit chng trnh chuyn i s thp phn n=0;--i) {

  • b=n>>i; if(0x01&b) printf("1"); else printf("0"); } printf("\n"); } /***************************************************************************/ void main() { long b; clrscr(); printf("nhap vao so can doi \n"); nhap(); doi(n); getch(); } Cu 3: Vit chng trnh tnh gi tr biu thc logic Z: (vit di dng hm)

    Bi lm: #include #include void nhap(int*a) { int t; while(t1) { scanf("%d",&t); if(t1) printf("ban nhap co loi\n");

  • else *a=t; } } /***************************************************************************/ int z(int a,int b,int c,int d) { int t; t=!((a&&b)||(c||d)&&((a^b)&&(!(c&&d)))); return t; } /***************************************************************************/ void main() { int a,b,c,d; clrscr(); printf("a= "); nhap(&a); printf("b= "); nhap(&b); printf("c= "); nhap(&c); printf("d= "); nhap(&d); printf("bieu thuc logic co gia tri la:\n \nZ= %d",z(a,b,c,d)); getch();

  • } Z= ((A.B)+(C+D).((A + B).(C.D)) trong , A,B,C,D l cc s 0,1 nhp vo t bn phm Cu 4: Vit chng trnh tnh gi tr biu thc sau 1) Z1 = ~(((a & b)^(c|d))
  • #include #include #include char m[10][8]={ {'d','e','n'}, {'n','a','u'}, {'d','o'}, {'c','a','m'}, {'v','a','n','g'}, {'l','u','c'}, {'l','a','m'}, {'x','a','m'}, {'t','i','m'}, {'t','r','a','n','g'}, } ; int ss(char*a) { int i; for (i=0;i
  • r*=pow(10,ss(m3)); printf("nhap vao vach mau thu 4: ");gets(m4); if(stricmp(m4,"nau")==0) s=1; if(stricmp(m4,"do")==0) s=2; if(stricmp(m4,"nhu vang")==0) s=10; if(stricmp(m4,"nhu bac")==0) s=20; printf("gia tri cua dien tro la %d +- %d% ",r,s); getch(); } Cu 6: Vit chng trnh thc hin:

    1. Tm max bn s a,b,c, d dng ton t iu kin e1?e2:e3 2. Tnh gi tr mch khuch i logarit sau: Ura=-UTln(Uvo/R.IS)

    Trong UT=25.5mV, c IS=10-6A. Uvo v R c nhp vo t bn phm. Bi lm:

    #include #include #include double max(double a,double b,double c,double d) { double t; t=a>b?a:b; t=t>c?t:c; t=d>t?d:t; return t; } double kd(double uv,double r) { double ur,ta,tb,tc; double ut=25.5*pow(10,-3); double is=pow(10,-6);

  • ur=-ut*log(uv/r*is); return ur; } void main() { clrscr(); int i; double uv,r,a,b,c,d; printf("ban hay chon cac tien ich sau\n"); printf("1: tim max\n"); printf("2: tim dau ra bo khyech dai\n"); scanf("%d",&i); switch (i) { case 2: { printf("ban hay nhap vao cac thong so cua mach\n"); printf("Uv= ");scanf("%lf",&uv); printf("R= ");scanf("%lf",&r); printf("gia tri dau ra cua bo khuyech dai la:

    %lg\n",kd(uv,r) ); } break; case 1: { printf("ban hay nhap bon so can bat ky\n"); printf("a= ");scanf("%lf",&a); printf("b= ");scanf("%lf",&b); printf("c= ");scanf("%lf",&c); printf("d= ");scanf("%lf",&d); printf("Gia tri lon nhat cua bon so

    %lg\n",max(a,b,c,d)); } break;

  • } getch(); }

    Cu 7: Cho mch logic nh di y, hy lp trnh hin th bn chn l ca mch Trong : X1, X2, X3, X4, Y1, Y2 c nhp vo t bn phm #include #include void nhap(int *a) { while(*a1) { scanf("%d",a); if(*a1) printf(" co loi de nghi nhap lai!\n"); } } void main() { clrscr(); int x1,x2,x3,x4,y1,y2,z; printf("Ban hay nhap vao cac thong so dau vao cua mach logic\n"); printf("x1= ");nhap(&x1); printf("x2= ");nhap(&x2); printf("x3= ");nhap(&x3); printf("x4= ");nhap(&x4); printf("y1= ");nhap(&y1);

    Y2

    X2

    X3

    Y1

    X1

    X4 Z

  • printf("y2= ");nhap(&y2); printf("\t\t\t\tBANG CHAN MACH LOGIC\n\n\n"); printf("\t\tx1\tx2\tx3\tx4\ty1\ty2\tz\n"); printf("\t\t%d\t%d\t%d\t%d\t%d\t%d\t%d\n",x1,x2,x3,x4,y1,y2,!(((x1&&x2)||(x3&&x4))^(y1&&y2))); getch(); } Cu 8: Cho mch logic nh di y, hy lp trnh hin th bng chn l ca mch Trong : X1, X2, X3, X4, Y1, Y2 c nhp vo t bn phm

    Bi lm: #include #include void nhap(int *a) { while(*a1) { scanf("%d",a); if(*a1) printf(" co loi de nghi nhap lai!\n"); } } void main()

    Y2

    X2

    X3

    Y1

    X1

    X4 Z

  • { clrscr(); int x1,x2,x3,x4,y1,y2,z; printf("Ban hay nhap vao cac thong so dau vao cua mach logic\n"); printf("x1= ");nhap(&x1); printf("x2= ");nhap(&x2); printf("x3= ");nhap(&x3); printf("x4= ");nhap(&x4); printf("y1= ");nhap(&y1); printf("y2= ");nhap(&y2); printf("\t\t\t\tBANG CHAN MACH LOGIC\n\n\n"); printf("\t\tx1\tx2\tx3\tx4\ty1\ty2\tz\n"); printf("\t\t%d\t%d\t%d\t%d\t%d\t%d\t%d\n",x1,x2,x3,x4,y1,y2,!(((!(x1&&x2))||(!(x3&&x4)))^(!(y1&&y2)))); getch(); } Cu 9: Cho h s khuch i ca mch lc tn s thp c tnh

    fRC

    K2

    1

    Hy vit chng trnh c th in ra mt bng tn s v h s khuch i tng ng vi di tn t 1Hz1KHz theo tng bc hn km nhau 20, vi R=1K , C=0.1 F.

    Bi lm: #include #include #include void main() { clrscr(); float r=1000,c=0.0001,k; int i; printf(" he so khuyech dai tan so vao tan so ra\n"); for(i=0;i

  • getch(); } Cu 10: Vit chng trnh nhp ch s mu th nht ca in tr, sau chuyn thnh s ca mu tng ng va nhp. Chng trnh s kt thc khi bm Y khi c li hi Bn mun chuyn tip na khng Y/N? Cu 11: Vit chng trnh hin th bng chn l ca hm NAND, hm XOR v hm OR.

    Bi lm: Cu#include #include void nand() { int i,j,n; for(i=0;i

  • void main() { clrscr(); printf("\t\tA\t\tB\t\tY\n\n"); printf(" cong nand\n"); nand(); printf(" cong xor\n"); xor(); getch(); } BAI11_12 #include #include char nhap() { char c; while((c'5')&&c!=62) { c=getch(); if(c'5'||c!=62) printf("co loi de nghi ban nhap lai\n"); } return c; } void and() { int i,j; for(i=0;i
  • } } void nand() { int i,j,n; for(i=0;i
  • int i,j; for(i=0;i
  • #include void and() { int i,j; for(i=0;i
  • getch(); } Cu 13: Vit chng trnh nhp vo mt khu v tn ng nhp (Pass, Username). Nu mt khu l DTVT, tn ng nhp l K2 th thot khi chng trnh, ngc li th yu cu nhp li.

    Bi lm: #include #include #include void main() { clrscr(); int i,a,b; char *npass,*nuse; while(a!=0||b!=0) { printf("\nusename: ");gets(nuse); printf("password: "); for(i=0;i

  • )...( 22

    11

    Vnn

    NV

    NV

    Nr UR

    RURRU

    RRU

    trong : n, RN, R1.Rn, UV1.UVn c nhp t bn phm #include #include void nhap(float *a,int n) { int i; for(i=0;i

  • Cu 15: Vit chng trnh thc hin chc nng so snh 2 s 4 bit A3A2A1A0 vi B3B2B1B0. Trong , A3A1, B3B1 l nhng s nh phn c nhp vo t bn phm.

    Bi lm: #include #include void nhap(int *a) { int i,j; for(i=3;i>=0;--i) { printf("a[%d]=",i); scanf("%d",&j); a[i]=j; } } void main() { clrscr(); int a[4],b[4],i,m,l; printf("nhap vao hai so nhi phan 4 bit\n"); printf("ban hay nhap so thu nhat\n"); nhap(a); printf("ban hay nhap so thu hai\n"); nhap(b); printf("ket qua sau khi so sanh la\n"); for(i=3;i>=0;i--) { m=!a[i]&&b[i]; l=a[i]&&(!b[i]); if(l

  • { printf("%d%d%d%d < %d%d%d%d",a[3],a[2],a[1],a[0],b[3],b[2],b[1],b[0]); break; } if(l>m) { printf("%d%d%d%d > %d%d%d%d",a[3],a[2],a[1],a[0],b[3],b[2],b[1],b[0]); break; } if(l==m&&i==0) printf("%d%d%d%d = %d%d%d%d",a[3],a[2],a[1],a[0],b[3],b[2],b[1],b[0]); } getch(); } Cu 16: Vit chng trnh hin th bng chn l ca mch hiu ton phn (Full Subtractor FS) D = iBBA ii BBBABAB 0 Trong : A,B,Bi l s nh phn c nhp vo t bn phm

    Bi lm: #include #include void main() { clrscr(); int a,b,bi,b0,d; printf("a= "); while(a!=0&&a!=1)

  • scanf("%d",&a); printf("b= "); while(b!=0&&b!=1) scanf("%d",&b); printf("Bi= "); while(bi!=0&&bi!=1) scanf("%d",&bi); d=a^b^bi; b0=((!a)&&b)||(b&&bi)||((!a)&&bi); printf("hieu cua hai so %d va %d la %d va con du %d\n",a,b,d,b0); getch(); } Cu 17: Vit chng trnh hin th bng chn l s bin i m nh phn sang m Gray: Trong A0, A1, A3 l u vo, G0, G1, G3 l u ra 100 AAG 211 AAG 322 AAG 33 AG

    Bi lm: /* in ra man hinh bang chan ly ma gray*/ #include #include void main() { clrscr(); int a0,a1,a2,a3,g0,g1,g2,g3; printf("\n \t\t\tbang chan ly ma gray\n\n"); printf("\t\t\ta0a1a2a3\tg0g1g2g3\n\n"); for (a0=0;a0

  • g0=a0^a1; g1=a1^a2; g2=a2^a3; printf("\t\t\t%d%d%d%d\t %c\t %d%d%d%d\n",a0,a1,a2,a3,157,g0,g1,g2,g3); } getch(); } Cu 18: Vit chng trnh hin th bng chn l ca b phn knh 1 ng vo v 4 ng ra: trong D l hng s c gi tr bng 1; A, B l tn hiu vo, Y0...Y3 l tn hiu ra.

    DBAY ..0 DBAY ..1 BDAY .2 DBAY ..3 Bi lm:

    /*de bai: In ra man hinh bang chan ly cua b phan kenh 1 dau vao 4 dau ra */ #include #include void main() { clrscr(); int a,b,d=1; printf("Bang chan ly bo phan kenh 1 dau vao 4 dau ra\n\n"); printf("\tD\tA\tB\tY0\tY1\tY2\tY3\n\n"); for(a=0;a

  • Trong : mi in tr c cu trc nh sau: Struct Dien_Tro

    { char TenDT[5]; float Gia_tri; };

    #include #include #include typedef struct { char tenDT[5]; float Gia_tri; }Dien_Tro; void hv(Dien_Tro *a,Dien_Tro *b) { Dien_Tro t; t=*a; *a=*b; *b=t; } void doi(Dien_Tro *a,int n) { int i,j; for(i=0;i

  • float dt; printf("ten dien tro: "); gets(h.tenDT); printf("R = ");scanf("%f%*c",&dt); h.Gia_tri=dt; *t=h; } void main() { clrscr(); char name[5]; Dien_Tro r[1000]; int n,i; float dt,t; printf("ban nhap vao so dien tro cua danh sach:\n "); scanf("%d%*c",&n); printf("ban nhap thong tin ve cac dien tro cua danh sach\n"); for(i=0;i
  • Struct Dien_Tro { char TenDT[5]; float Gia_tri; };

    Bi lm: #include #include #include typedef struct { char tenDT[5]; float Gia_tri; }Dien_Tro; void hv(Dien_Tro *a,Dien_Tro *b) { Dien_Tro t; t=*a; *a=*b; *b=t; } void doi(Dien_Tro *a,int n) { int i,j; for(i=0;i

  • Dien_Tro h; float dt; printf("ten dien tro: "); gets(h.tenDT); printf("R = ");scanf("%f%*c",&dt); h.Gia_tri=dt; *t=h; } void main() { clrscr(); char name[5]; Dien_Tro r[1000]; int n,i; float dt,t; printf("ban nhap vao so dien tro co trong dang sach: "); scanf("%d%*c",&n); printf("ban nhap thong tin ve cac dien tro so trong danh sach\n"); for(i=0;i
  • Cu 21: Vit chng trnh nhp vo mt dy s di c gi tr 0 hoc 1 sau p dng cng thc bin i DAC (Digital Analog Converter) theo cng thc sau:

    )22...22(2

    00

    11

    22

    110 dddd

    Uu nn

    nnn

    ref

    Trong : Uref, n, di (i=0->n) c nhp vo t bn phm. U0 l in p c chuyn i.

    Bi lm: #include #include #include void nhap(int *d) { int i,t; while(t!=0&&t!=1) scanf("%d",&t); *d=t; } void main() { clrscr(); int n,d[100],i,t; float ur,u0=0; printf("ban hay nhap vao cac thong so dau vao cua mach bien doi DAC\n\n"); printf("n=");scanf("%d",&n); printf("Uref=");scanf("%f",&ur); for(i=0;i

  • u0*= ur/pow(2,n); printf("Gia tri cua tin hieu sau khi qua bo bien doi DAC la: %f",u0); getch(); } Cu 22: Vit chng trnh nhp vo mt mng Dien_Tro_Chung gm c n in tr c cu trc nh di y. Sau lu nhng in tr c gi tr chn sang mng Dien_Tro_Chan, v nhng in tr c gi tr l sang mng Dien_Tro_Le.

    Struct Dien_Tro { char TenDT[5]; float Gia_tri; };

    Bi lm:

    #include #include #include typedef struct { char tendt[5]; float giatri; } dientro; void nhap(dientro *t) { dientro h; float dt; printf("ten dien tro: "); gets(h.tendt); printf("R = ");scanf("%f%*c",&dt); h.giatri=dt; *t=h; } void hien(dientro a[],int n)

  • { int i; for(i=0;i
  • printf("\n"); getch(); } Cu 23: Vit chng trnh thc hin php ton hon v ca hai bin a, b c kiu

    Struct Dien_Tro { char TenDT[5]; float Gia_tri; };

    Chng trnh c vit theo hng chng trnh con Cu 24: Vit chng trnh nhp vo mt danh sch lin kt theo chiu thun cc in tr c cu trc nh di y. Sau thc hin chc nng thm in tr v hin th danh sch in tr nhp

    Struct Dien_Tro { char TenDT[5]; float Gia_tri; };

    Bi lm: #include #include #include #include typedef struct pp { char tendt[5]; float giatri; struct pp *next; } dientro; void nhap(dientro **pp,dientro **pd,char *ht) { dientro *p=*pp;

  • dientro *pdau=*pd; float t; if(pdau==NULL) { pdau=(dientro*)malloc(sizeof(dientro)); p=pdau; } else { p->next=(dientro*)malloc(sizeof(dientro)); p=p->next; } strcpy(p->tendt,ht); printf("gia tri: "); scanf("%f%*c",&t); p->giatri=t; p->next=NULL; *pd=pdau; *pp=p; } void main() { char ht[5]; dientro *pdau,*p,*pl; clrscr(); printf("ban hay nhap thong tin ve cac dien tro vao danh sach\n\n"); pdau=NULL; while(1) { printf("ten dien tro: "); gets(ht); if(ht[0]==0) break; nhap(&p,&pdau,ht); }

  • printf("nhap ten dien tro can chen\n"); printf("ten ten tro: "); gets(ht); nhap(&p,&pdau,ht); printf("danh sach dien tro la: \n"); pl=pdau; while(pl!=NULL) { printf(" dien tro %s =%g\n",(*pl).tendt,(*pl).giatri); pl=pl->next; } getch(); }

    Cu 25: Vit chng trnh nhp vo mt danh sch lin kt theo chiu ngc cc in tr c cu trc nh di y. Sau thc hin chc nng thm in tr v hin th danh sch in tr nhp

    Struct Dien_Tro { char TenDT[5]; float Gia_tri; };

    Bi lm: #include #include #include #include typedef struct pp { char tendt[5]; float giatri; struct pp *next;

  • } dientro; void solieu(dientro **p,char *ht) { float t; dientro *pp; pp=(dientro *)malloc(sizeof(dientro)); strcpy(pp->tendt,ht); printf("gia tri cua dien tro la: ");scanf("%f%*c",&t); pp->giatri=t; pp->next=NULL; *p=pp; } void nhap(dientro **pp,dientro **pd,char *ht) { dientro *p=*pp,*pl; dientro *pdau=*pd; float t; if(pdau==NULL) { p=(dientro *)malloc(sizeof(dientro)); p=pdau; solieu(&pdau,ht); } else { solieu(&pl,ht); pl->next=pdau; pdau=pl; } *pd=pdau; *pp=p; }

  • void main() { char ht[5]; dientro *pdau,*p,*pl; clrscr(); printf("ban hay nhap thong tin ve cac dien tro vao danh sach\n\n"); pdau=NULL; while(1) { printf("ten dien tro: "); gets(ht); if(ht[0]==0) break; nhap(&p,&pdau,ht); } printf("nhap ten dien tro can chen\n"); printf("ten ten tro: "); gets(ht); nhap(&p,&pdau,ht); printf("danh sach dien tro la: \n"); pl=pdau; while(pl!=NULL) { printf(" dien tro %s =%g\n",(*pl).tendt,(*pl).giatri); pl=pl->next; } getch(); }

    Cu 26: Vit chng trnh ci t thut ton rt gn dng ba Cc l cho bi ton gm 3 u vo. Cc gi tr ban u l s 0 hoc 1 c nhp vo bng Cc l

  • Cu 27: Vit chng trnh nhp vo mt danh sch lin kt theo chiu thun cc in tr c cu trc nh di y. Sau thc hin chc nng xa in tr v tr bt k v hin th danh sch in tr nhp

    Struct Dien_Tro { char TenDT[5]; float Gia_tri; };

    Bi lm: #include #include #include #include typedef struct pp { char tendt[5]; float giatri; int vt; struct pp *next; } dientro; void nhap(dientro **pp,dientro **pd,char *ht) { dientro *p=*pp; dientro *pdau=*pd; float t; if(pdau==NULL) { pdau=(dientro*)malloc(sizeof(dientro)); p=pdau; } else { p->next=(dientro*)malloc(sizeof(dientro));

  • p=p->next; } strcpy(p->tendt,ht); printf("gia tri: "); scanf("%f%*c",&t); p->giatri=t; p->next=NULL; *pd=pdau; *pp=p; } void main() { char ht[5]; dientro *pdau,*p,*pl,*tam; int vtri,vit=0; clrscr(); printf("ban hay nhap thong tin ve cac dien tro vao danh sach\n\n"); pdau=NULL; while(1) { printf("ten dien tro: "); gets(ht); if(ht[0]==0) break; nhap(&p,&pdau,ht); vit=vit+1; p->vt=vit; } clrscr(); pl=pdau; while(pl!=NULL) { printf(" %d: dien tro %s =%g\n",(*pl).vt,(*pl).tendt,(*pl).giatri); pl=pl->next; }

  • pl=pdau; printf("Ban hay nhap vi tri cua dien tro can xoa\n"); scanf("%d",&vtri); if(vtri==1) { tam=pdau; pdau=pdau->next; free(tam); } if(vtri==2) { pdau->next=pdau->next->next; } else { while(1) { pl=pl->next; if(pl->next->next==NULL) break; if(pl->next->vt==vtri) break; } tam=pl->next; pl->next=pl->next->next; free(tam); } printf("Danh sach cac dien tro sau khi bi xoa la:\n\n"); pl=pdau; while(pl!=NULL) { printf(" %d: dien tro %s =%g\n",(*pl).vt,(*pl).tendt,(*pl).giatri); pl=pl->next; }

  • getch(); } Cu 28: Vit chng trnh nhp vo mt danh sch lin kt theo chiu ngc cc in tr c cu trc nh di y. Sau thc hin chc nng xa in tr v tr bt k v hin th danh sch in tr nhp

    Struct Dien_Tro { char TenDT[5]; float Gia_tri; };

    Bi lm:

    #include #include #include #include typedef struct pp { char tendt[5]; float giatri; int vt; struct pp *next; } dientro; void solieu(dientro **p,char *ht) { float t; dientro *pp; pp=(dientro *)malloc(sizeof(dientro)); strcpy(pp->tendt,ht); printf("gia tri cua dien tro la: ");scanf("%f%*c",&t); pp->giatri=t; pp->next=NULL; *p=pp;

  • } void nhap(dientro **pp,dientro **pd,char *ht) { dientro *p=*pp,*pl; dientro *pdau=*pd; int vit; if(pdau==NULL) { p=(dientro *)malloc(sizeof(dientro)); p=pdau; solieu(&pdau,ht); vit=1; pdau->vt=vit; } else { solieu(&pl,ht); vit++; pl->vt=vit; pl->next=pdau; pdau=pl; } *pd=pdau; *pp=p; } void main() { char ht[5]; dientro *pdau,*p,*pl,*tam; int vtri; clrscr(); printf("ban hay nhap thong tin ve cac dien tro vao danh sach\n\n");

  • pdau=NULL; while(1) { printf("ten dien tro: "); gets(ht); if(ht[0]==0) break; nhap(&p,&pdau,ht); } pl=pdau; printf("Ban hay nhap vi tri cua dien tro can xoa\n"); scanf("%d",&vtri); if(vtri==1) { tam=pdau; pdau=pdau->next; free(tam); } if(vtri==2) { pdau->next=pdau->next->next; } else { while(1) { pl=pl->next; if(pl->next->next==NULL) break; if(pl->next->vt==vtri) break; } tam=pl->next; pl->next=pl->next->next; free(tam); }

  • printf("danh sach dien tro la: \n"); pl=pdau; while(pl!=NULL) { printf(" dien tro %s =%g\n",(*pl).tendt,(*pl).giatri); pl=pl->next; } getch(); } Cu 29: Vit chng trnh nhp vo hai danh sch a,b lin kt theo chiu thun cc in tr c cu trc nh di y. Sau thc hin vic cng gp gi tr ca 2 danh sch a,b vo danh sch c. Hin th danh sch in tr nhp

    Struct Dien_Tro { char TenDT[5]; float Gia_tri; };

    Cu 30: Vit chng trnh hin th bng chn l ca B m thp phn ng b, vi gi thit l 00001234 nnnn QQQQ . Cho phng trnh trng thi nh sau:

    nn QQ 11

    1

    nnnnnn QQQQQQ 212141

    2

    nnnnnnn QQQQQQQ 3123121

    3

    nnnn

    nnnn QQQQQQQ 411231

    4

    Trong cc nnnn QQQQ 1234 l nhng trng thi trc ca trig JK, c cc gi tr 0 hoc 1.

    Bi lm: #include #include void main() {

  • int q1,q2,q3,q4; printf("bang chan ly cua bo dem thap phan dong bo \n\n"); for(q1=0;q1
  • A=n>>i; c=0x01&A; B=g>>i; d=0x01&B; m=!c&&d; l=c&&(!d); if(lm) { printf("%d > %d",n,g); break; } if(l==m&&i==0) printf("%d = %d",n,g); } printf("\n"); } void main() { int a,b; clrscr(); printf("nhap vao so can doi\n"); nhap(&a); nhap(&b); ss(a,b); getch(); }

  • Cu 32: Vit chng trnh nhp vo bn bt A, B, C, D (c gi tr nh phn) sau minh ha chng trnh hin th n LED 7, cho n khi ngi dng bm phm N th kt thc. Cho phng trnh ca trng thi ca n LED 7 on nh sau:

    ACCABDa ABBACb

    ABCc ABCACBCDd ABACe ABACBCDf ABBCBCDg

    Bi lm: #include #include void nhap(int *d) { int t; while(t!=0&&t!=1) scanf("%d",&t); *d=t; } void led(int a,int b,int c,int d,int e,int f,int g) { if(a==0) printf("\t\t %c%c%c%c\n",205,205,205,205); else printf("\n"); if(f==0&&b==0) printf("\t\t%c %c\n\t\t%c %c\n\t\t%c %c\n",186,186,186,186,186,186,186,186); if(f==0&&b==1) printf("\t\t%c \n\t\t%c \n\t\t%c \n",186,186,186); if(f==1&&b==0) printf("\t\t %c\n\t\t %c\n\t\t %c\n",186,186,186);

  • if(f==1&&b==1) printf("\t\t \n\t\t \n\t\t \n"); if(g==0) printf("\t\t %c%c%c%c\n",205,205,205,205); else printf("\n"); if(e==0&&c==0) printf("\t\t%c %c\n\t\t%c %c\n\t\t%c %c\n",186,186,186,186,186,186,186,186); if(e==0&&c==1) printf("\t\t%c \n\t\t%c \n\t\t%c \n",186,186,186); if(e==1&&c==0) printf("\t\t %c\n\t\t %c\n\t\t %c\n",186,186,186); if(e==1&&c==1) printf("\t\t \n\t\t \n\t\t \n"); if(d==0) printf("\t\t %c%c%c%c\n",205,205,205,205); else printf("\n"); } void main() { char t,y; int A,B,C,D,a,b,c,d,e,f,g; do { clrscr(); printf("Ban hay nhap vao mot so nhi phan 4 bit\n"); printf("D=");nhap(&D); printf("C=");nhap(&C); printf("B=");nhap(&B);

  • printf("A=");nhap(&A); clrscr(); do { printf("hien thi led 7 doan cua so %d%d%d%d (nhap n de thoi hien)\n",D,C,B,A); a=!(D||B||(C&&A)||(!C&&!A)); b=!(!C||(B&&A)||(!B&&!A)); c=!(C||(!B)||A); d=!(D||(!C&&B)||(!C&&!A)||(C&&!B&&A)); e=!((!C&&!A)||(B&&!A)); f=!(D||(C&&!B)||(C&&!A)||(!B&&!A)); g=!(D||(C&&!B)||(!C&&B)||(B&&!A)); led(a,b,c,d,e,f,g); t=getch(); clrscr(); } while(t!='n'); printf("Ban co muon tiep tuc khong c/n\n"); y=getch(); } while(y!='n'); } Cu 33: Vit chng trnh tm in tr c gi tr ln nht trong mng in tr gm n in tr c nhp vo t bn phm. Sau hin th thng tin ca in tr c gi tr ln nht . Trong mi in tr c cu trc nh sau:

    Struct Dien_Tro { char TenDT[5]; float Gia_tri; };

    Bi lm: #include

  • #include #include #include typedef struct pp { char tendt[5]; float giatri; struct pp *next; } dientro; void nhap(dientro **pp,dientro **pd,char *ht) { dientro *p=*pp; dientro *pdau=*pd; float t; if(pdau==NULL) { pdau=(dientro*)malloc(sizeof(dientro)); p=pdau; } else { p->next=(dientro*)malloc(sizeof(dientro)); p=p->next; } strcpy(p->tendt,ht); printf("gia tri: "); scanf("%f%*c",&t); p->giatri=t; p->next=NULL; *pd=pdau; *pp=p; } void main() {

  • char ht[5]; dientro *pdau,*p,*pl; clrscr(); float max,to; printf("ban hay nhap thong tin ve cac dien tro vao danh sach\n\n"); pdau=NULL; while(1) { printf("ten tro: "); gets(ht); if(ht[0]==0) break; nhap(&p,&pdau,ht); } pl=pdau; max=pl->giatri; while(pl!=NULL) { to=pl->giatri; max=max>to?max:to; pl=pl->next; } printf("dien tro co gia tri lon nhat la: \n"); pl=pdau; while(pl!=NULL) { if(pl->giatri==max) printf(" dien tro %s =%g\n",(*pl).tendt,(*pl).giatri); pl=pl->next; } getch(); }

  • Cu 34: Vit chng trnh tm in tr c gi tr nh nht trong mng in tr gm n in tr c nhp vo t bn phm. Sau hin th thng tin ca in tr c gi tr nh nht . Trong mi in tr c cu trc nh sau:

    Struct Dien_Tro { char TenDT[5]; float Gia_tri; };

    Bi lm: #include #include #include #include typedef struct pp { char tendt[5]; float giatri; struct pp *next; } dientro; void nhap(dientro **pp,dientro **pd,char *ht) { dientro *p=*pp; dientro *pdau=*pd; float t; if(pdau==NULL) { pdau=(dientro*)malloc(sizeof(dientro)); p=pdau; } else { p->next=(dientro*)malloc(sizeof(dientro)); p=p->next;

  • } strcpy(p->tendt,ht); printf("gia tri: "); scanf("%f%*c",&t); p->giatri=t; p->next=NULL; *pd=pdau; *pp=p; } void main() { char ht[5]; dientro *pdau,*p,*pl; clrscr(); float min,to; printf("ban hay nhap thong tin ve cac dien tro vao danh sach\n\n"); pdau=NULL; while(1) { printf("ten tro: "); gets(ht); if(ht[0]==0) break; nhap(&p,&pdau,ht); } pl=pdau; min=pl->giatri; while(pl!=NULL) { to=pl->giatri; min=minnext; } printf("dien tro co gia tri nho nhat la: \n");

  • pl=pdau; while(pl!=NULL) { if(pl->giatri==min) printf(" dien tro %s =%g\n",(*pl).tendt,(*pl).giatri); pl=pl->next; } getch(); }