1. Trang chủ
  2. » Giáo án - Bài giảng

Lap trinh C

19 110 0

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

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 19
Dung lượng 84,5 KB

Nội dung

/* thong tin cua moi sinh vien: masv,ten,diem. lap chuong trinh thuc hien viec nhap va xuat thong tin theo mang. */ #include"stdio.h" #include"math.h" typedef struct bg { char masv[5]; char ten[20]; float diem; } tt; tt *p; int n; void ndl(); void ndl() { int i; float x; printf("\n nhap vao so phan tu can nhap: "); scanf("%d",&n); for(i=1;i<=n;i++) { printf("\n nhap vao thong tin cua nguoi thu %d : ",i); fflush(stdin); printf("\n masv: "); gets(p[i].masv); printf("\n hoten: "); gets(p[i].ten); printf("\n diem: "); scanf("%f",&x); p[i].diem=x; }; }; void xdl(); void xdl() { int i; printf("\n danh sach vua nhap la: "); for(i=1;i<=n;i++) printf("\n %s %s %3.1f",p[i].masv,p[i].ten,p[i].diem); }; main() { ndl(); xdl(); getch(); } /* bien cau truc: cau truc 1 ban ghi: masv,hoten, quequan, truong, tuoi, sbd, diem. lap chuong trinh thuc hien theo yeu cau sau: a) nhap ndl cho ds lop. b) in ra ds vua nhap. c) sap xep danh sach theo thu tu tang dan cua diem. d) tim kiem thong tin khi biet diem (diem nhap vao tu ban phim). d) in ra nhung nguoi co diem cao nhat. */ #include"stdio.h" #include"math.h" typedef struct ct { char masv[5]; char ht[25]; char qq[25]; int tuoi; char truong[15]; int sbd; float diem; } bg; bg a[40]; int n; void ndl(); void ndl() { int i; float x; printf("\n nhap vao so thanh vien cua lop: "); scanf("%d",&n); printf("\n nhap vao thong tin cua cac thanh vien: "); for(i=1;i<=n;i++) { printf("\n nhap vao thanh vien thu %d ",i); fflush(stdin); printf("\n nhap masv: "); gets(a[i].masv); /* kiem tra tinh nhap trung masv */ printf("\n nhap ho ten: "); gets(a[i].ht); printf("\n nhap que quan:"); gets(a[i].qq); printf("\n nhap tuoi: "); scanf("%d",&a[i].tuoi); fflush(stdin); printf("\n nhap truong: "); gets(a[i].truong); printf("\n nhap vao sbd: "); scanf("%d",&a[i].sbd); printf("\n nhap vao diem: "); scanf("%f",&x); a[i].diem=x; }; }; void xdl(); void xdl() { int i; printf("\n cac thanh vien trong lop vua nhap la:"); for(i=1;i<=n;i++) { printf("\n %s %s %s %d %s %d %3.1f",a[i].masv,a[i].ht,a[i].qq,a[i].tuoi,a[i].truong,a[i].sbd,a[i].diem); }; }; void sx(); void sx() { int i,j; bg tg; for(i=1;i<n;i++) for(j=n;j>i;j ) if(a[j].diem<a[j-1].diem) { tg=a[j]; a[j]=a[j-1]; a[j-1]=tg; }; printf("\n danh sach lop sau khi sap xep tang dan theo diem: "); for(j=1;j<=n;j++) printf("\n %s %s %s %d %s %d %3.1f",a[j].masv,a[j].ht,a[j].qq,a[j].tuoi,a[j].truong,a[j].sbd,a[j].diem); }; void tk(); void tk() { int i,k; printf("\n nhap vao diem can tim: "); scanf("%d",&k); printf("\ nhung nguoi co diem= %d la: ",k); for(i=1;i<=n;i++) if(a[i].diem==k) printf("\n %s %s %s %d %s %d %3.1f",a[i].masv,a[i].ht,a[i].qq,a[i].tuoi,a[i].truong,a[i].sbd,a[i].diem); }; void ttcn(); void ttcn() { int i; float cn; cn=a[1].diem; for(i=2;i<=n;i++) if(cn<a[i].diem) cn=a[i].diem; printf("\n nhung nguoi co diem cao nhat la:"); for(i=1;i<=n;i++) if(a[i].diem==cn) printf("\n %s %s %s %d %s %d %3.1f",a[i].masv,a[i].ht,a[i].qq,a[i].tuoi,a[i].truong,a[i].sbd,a[i].diem); }; main() { bg tv; ndl(); xdl(); sx(); tk(); ttcn(); getch(); } /* mang_cautruc_tep: bien cau truc: cau truc 1 ban ghi: masv,hoten, quequan, truong, tuoi, sbd, diem. lap chuong trinh thuc hien theo yeu cau sau: a) nhap ndl (hay nhap bo sung) va dl duoc luu vao 1 tep. b) in ra du lieu cau tep vua nhap. c) sap xep danh sach theo thu tu tang dan cua diem. d) tim kiem thong tin khi biet diem (diem nhap vao tu ban phim). d) in ra nhung nguoi co diem cao nhat. */ #include"stdio.h" #include"math.h" typedef struct ct { char masv[5]; char ht[25]; char qq[25]; int tuoi; char truong[15]; int sbd; float diem; } bg; void ndl(); void ndl() { FILE *f; bg tv,a[50]; float x; int i,n; f=fopen("vd.txt","at"); printf("\n nhap vao so ban ghi can bo sung :"); scanf("%d",&n); for(i=1;i<=n;i++) { printf("\n nhap vao thanh vien thu %d: ",i); fflush(stdin); printf("\n nhap masv: "); gets(a[i].masv); printf("\n nhap ho ten: "); gets(a[i].ht); printf("\n nhap que quan:"); gets(a[i].qq); printf("\n nhap tuoi: "); scanf("%d",&a[i].tuoi); fflush(stdin); printf("\n nhap truong: "); gets(a[i].truong); printf("\n nhap vao sbd: "); scanf("%d",&a[i].sbd); printf("\n nhap vao diem: "); scanf("%f",&x); a[i].diem=x; fwrite(&a[i],sizeof(tv),1,f); }; fclose(f); }; void xdl(); void xdl() { FILE *f; bg tv,tv1,a[50]; float x; int i; f=fopen("vd.txt","rt"); i=1; while(fread(&tv,sizeof(tv1),1,f)>0) { x=tv.diem; printf("\n %s %s %s %d %s %d %3.1f",tv.masv,tv.ht,tv.qq,tv.tuoi,tv.truong,tv.sbd,x); i+=1; }; fclose(f); }; void sx(); void sx() { FILE *f; bg tg,tv,tv1,a[50];int n,i,j; f=fopen("vd.txt","rt"); i=0; while(fread(&tv,sizeof(tv1),1,f)>0) { i+=1; a[i]=tv; }; n=i; for(i=1;i<=n-1;i++) for(j=n;j>=i+1;j ) if(a[j].diem<a[j-1].diem) { tg=a[j]; a[j]=a[j-1]; a[j-1]=tg; }; printf("\n danh sach sau khi sap xep tang dan theo diem la: "); for(j=1;j<=n;j++) printf("\n %s %s %s %d %s %d %3.1f",a[j].masv,a[j].ht,a[j].qq,a[j].tuoi,a[j].truong,a[j].sbd,a[j].diem); }; void tk(); void tk() { FILE *f;bg tv,tv1; float x; f=fopen("vd.txt","rt"); printf("\n nhap vao diem can tim: "); scanf("%f",&x); while(fread(&tv,sizeof(tv1),1,f)>0) if(tv.diem==x) printf("\n %s %s %s %d %s %d %3.1f",tv.masv,tv.ht,tv.qq,tv.tuoi,tv.truong,tv.sbd,tv.diem); }; void cn(); void cn() { FILE *f; bg tv,tv1,a[50]; float x; int n,i; f=fopen("vd.txt","rt"); fread(&tv,sizeof(tv1),1,f); x=tv.diem; i=0; while(fread(&tv,sizeof(tv1),1,f)>0) { i+=1; a[i]=tv; if(tv.diem>x) x=tv.diem; }; printf("\n danh sach nhung nguoi co diem cao nhat la: "); n=i; for(i=1;i<=n;i++) if(x==a[i].diem) printf("\n %s %s %s %d %s %d %3.1f",a[i].masv,a[i].ht,a[i].qq,a[i].tuoi,a[i].truong,a[i].sbd,a[i].diem); }; main() { ndl(); xdl(); sx(); tk(); cn(); getch(); } /* cho danh sach lien ket kieu gom cau truc kieu ht: struct hs { char ht[25]; int ns; struct hs *tiep; }; cho biet con tro tro toi dau danh sach. lap doan chuong trinh thuc hien cac Y/C: 1) in cac sinh vien co nam sinh >=1972. 2) xoa khoi danh sach cac hoc sinh sinh nam 1974. */ #include"stdio.h" #include"math.h" #include"conio.h" typedef struct hs { char ht[25]; int ns; struct hs *tiep; } person; main() /* {ctrlscr(); */ { int i; char ht[25]; int ns; person *pdau,*p,*p1; pdau=NULL; while(1) { fflush(stdin); printf("\n Ho ten: "); gets(ht); if(ht[0]==0) break; if(pdau==NULL) { pdau=(person*)malloc(sizeof(person)); p=pdau; } else { p->tiep=(person*)malloc(sizeof(person)); p=p->tiep; }; strcpy(p->ht,ht); printf("\n nam sinh "); scanf("%d%*c",&ns); p->ns=ns; p->tiep=NULL; }; p=pdau; while(p!=NULL) { printf("\n Ho ten: %-25s , tuoi: %d",(*p).ht,(*p).ns); p=p->tiep; }; /* nhung nguoi co nam sinh>1972 */ while(1) { printf("\n nam sinh:"); scanf("%d",ns); if(ns==0) break; /* duyet tu dau ds */ p=pdau; while(p!=NULL) { if(strcmp(p->ns,ns)==0) printf("\n Ho ten %-25s , nam sinh: %d",(*p).ht,(*p).ns); p=p->tiep; }; }; printf("\n Loai phan tu dau danh sach"); getch(); } /* co tep "sn.txt": gom cac phan tu nguyen duong duoc cach nhau boi 1 dau phay hay xuong dong lap chuong trinh thuc hien cac yeu cau sau: a) dua ra cac phan tu cua tep. b) dua ra cac phan tu cua tep da duoc sap xep theo thu tu tang dan. c) dua ra man hinh cac so trong tep la so chinh phuong. [...]... fprintf(f2,"%s"," "); }; }; main() { xdl(); sx(); scp(); snt(); ddl(); getch(); } /* lap chuong trinh thuc hien cac chuc nang sau: a) nhap vao mang 2 chieu (cac phan tu la so nguyen) b) in cac phan tu vua nhap c) tin phan tu lon nhat, va phan tu am cuoi cung tren hang thu k (k duoc nhap vao tu ban phim) d) neu ma tran vua nhap la vuong thi tinh tong cac phan tu nam tren duong chao chinh e) sap xep theo day tang dan... hien theo cac yeu cau sau: a) nhap (chen) vao tep vd1.txt cac phan tu la so nguyen b) in ra man hinh cac phan tu cua tep c) dua ra man hinh cac phan tu cua tep theo thu tu tang dan d) dua ra man hinh phan tu lon nhat trong tep e) dua ra man hinh cac phan tu am cua day f) lay cac phan tu chia het cho 2 tu tep vd1.txt de dua vao tep vd2.txt */ #include"stdio.h" #include"math.h" int n; void ndl(); void... cua ma tran: "); scanf("%d",&n); printf("\n nhap vao so cot cua ma tran: "); scanf("%d",&m); ndl(n,m); xdl(n,m); printf("\n nhap vao so hang can tim: "); scanf("%d",&k); tpt(k,m); if(n==m) tt(n,n); else printf("\n ma tran vua nhap khong phai la ma tran vuong"); printf("\n nhap vao so hang can sap xep: "); scanf("%d",&k); sx(k,m); getch(); } /* tep nhi phan: lap chuong trinh thuc hien theo cac yeu cau... }; }; void tpt(int c, int d); void tpt(int c, int d) { int i,l; l=a [c] [1]; for(i=1;i=1)) i-=1; if(i>=1) printf("\n phan tu am cuoi cuni cung la: %d va nam o vi tri %d %d",a [c] [i] ,c, i); else printf("\n trong hang %d khong co so am" ,c) ; }; void tt(int c, int d); void tt(int c, int d) { int t,i,j;... tim cac so chinh phuong, so nguyen to trong ma tran vua nhap */ #include"stdio.h" #include"math.h" #include"conio.h" int a[10][10],n,m; void ndl(int c, int d); void ndl(int c, int d) { int i,j; for(i=1;i< =c; i++) for(j=1;j . sach"); getch(); } /* co tep "sn.txt": gom cac phan tu nguyen duong duoc cach nhau boi 1 dau phay hay xuong dong lap chuong trinh thuc hien cac yeu cau sau: a) dua ra cac. xdl(); sx(); scp(); snt(); ddl(); getch(); } /* lap chuong trinh thuc hien cac chuc nang sau: a) nhap vao mang 2 chieu. (cac phan tu la so nguyen) b) in cac phan tu vua nhap. c) tin phan. tk(); cn(); getch(); } /* cho danh sach lien ket kieu gom cau truc kieu ht: struct hs { char ht[25]; int ns; struct hs *tiep; }; cho biet con tro tro toi dau danh sach. lap doan chuong trinh

Ngày đăng: 01/07/2014, 08:37

Xem thêm

w