Bài tập đề cương turbo C: Tệp

#include<conio.h>

#include<stdio.h>

#include<string.h>

void nhapmt(float pa[][100],int n,int m,char x)

{

int i,j;float tg;

for(i=1;i<=n;i++)

for(j=1;j<=m;j++)

{

printf("\n %c[%d,%d] = ",x,i,j);

scanf("%f",&tg);pa[i][j]=tg;

}

}

void inmt(float pa[][100],int n,int m,char x)

{

FILE *f;char s[]="CONG_MT.C";

int i,j;

if ((x=='a')||(x=='A')) f=fopen(s,"w");

else f=fopen(s,"a");

fprintf(f,"ma tran %c : \n",x);

for(i=1;i<=n;i++)

{

for(j=1;j<=m;j++)

fprintf(f,"%6.1f",pa[i][j]);

fprintf(f,"\n");

}

fclose(f);

}

void main()

{

float a[100][100],b[100][100],c[100][100],tg;int n,m,i,j,k;

printf("\n nhap n,m : ");scanf("%d%d",&n,&m);

nhapmt(a,n,m,'a');

nhapmt(b,n,m,'b');

for(i=1;i<=n;i++)

for(j=1;j<=m;j++)

{

c[i][j]=a[i][j]+b[i][j];

}

inmt(a,n,m,'a');

inmt(b,n,m,'b');

inmt(c,n,m,'c');

getch();

}

 

doc6 trang | Chia sẻ: oanh_nt | Lượt xem: 1693 | Lượt tải: 0download
Nội dung tài liệu Bài tập đề cương turbo C: Tệp, để tải tài liệu về máy bạn click vào nút DOWNLOAD ở trên
TỆP Bài 1: #include #include #include void nhapmt(float pa[][100],int n,int m,char x) { int i,j;float tg; for(i=1;i<=n;i++) for(j=1;j<=m;j++) { printf("\n %c[%d,%d] = ",x,i,j); scanf("%f",&tg);pa[i][j]=tg; } } void inmt(float pa[][100],int n,int m,char x) { FILE *f;char s[]="CONG_MT.C"; int i,j; if ((x=='a')||(x=='A')) f=fopen(s,"w"); else f=fopen(s,"a"); fprintf(f,"ma tran %c : \n",x); for(i=1;i<=n;i++) { for(j=1;j<=m;j++) fprintf(f,"%6.1f",pa[i][j]); fprintf(f,"\n"); } fclose(f); } void main() { float a[100][100],b[100][100],c[100][100],tg;int n,m,i,j,k; printf("\n nhap n,m : ");scanf("%d%d",&n,&m); nhapmt(a,n,m,'a'); nhapmt(b,n,m,'b'); for(i=1;i<=n;i++) for(j=1;j<=m;j++) { c[i][j]=a[i][j]+b[i][j]; } inmt(a,n,m,'a'); inmt(b,n,m,'b'); inmt(c,n,m,'c'); getch(); } Bài 2 : #include #include char sa[50],sb[50],sc[]="Ma tran C : "; void nhapmt(float pa[][100],float pb[][100],int *n,int *p,int *m) { int i,j;float tg; FILE *f; f=fopen("TICH_MT.C","r"); fscanf(f,"%d%d%d",n,p,m); fgets(sa,50,f);fgets(sa,50,f); for(i=1;i<=*n;i++) for(j=1;j<=*p;j++) { fscanf(f,"%f",&tg);pa[i][j]=tg; } fgets(sb,50,f);fgets(sb,50,f); for(i=1;i<=*p;i++) for(j=1;j<=*m;j++) { fscanf(f,"%f",&tg);pb[i][j]=tg; } fclose(f); } void inmt(float pa[][100],int n,int m) { FILE *f;char s[]="TICH_MT.C"; int i,j; f=fopen(s,"a"); fprintf(f,"ma tran C : \n"); for(i=1;i<=n;i++) { for(j=1;j<=m;j++) fprintf(f,"%6.1f",pa[i][j]); fprintf(f,"\n"); } fclose(f); } void main() { float a[100][100],b[100][100],c[100][100],tg;int n,m,p,i,j,k; nhapmt(a,b,&n,&p,&m); for(i=1;i<=n;i++) for(j=1;j<=m;j++) { c[i][j]=0; for(k=1;k<=p;k++) c[i][j]+=a[i][k]*b[k][j]; } inmt(c,n,m); getch(); } Bài 3 : #include #include #include char s[]="SO_LIEU.C"; typedef struct slbh { int dg;int sl;char ten[30];long int tien; }; long int tt=0; int n; struct slbh a[50],swap; void nhapdl() { int i;FILE *f; clrscr(); printf("\n nhap vao so hang n = ");scanf("%d",&n); f=fopen(s,"wb"); for(i=0;i<=n-1;i++) { printf("\n nhap mat hang thu %d : ",i+1); printf("\n Ten hang : ");fflush(stdin);gets(a[i].ten); printf(" Don gia : ");scanf("%d",&a[i].dg); printf(" So luong : ");scanf("%d",&a[i].sl); a[i].tien=(long int)(a[i].dg)*(long int)(a[i].sl); tt+=a[i].tien; } fwrite(a,sizeof(slbh),n,f); fclose(f); printf("\n\t Nhap xong. Press any key...");getch(); } void indl() { int i;FILE *f;slbh b[50]; char *g[]={"STT","tenhang","Don gia","So luong","Thanh tien"}; clrscr(); f=fopen(s,"rb"); printf("\n\t\t\t SO LIEU BAN HANG"); printf("\n %3s %30s %10s %10s %10s \n",g[0],g[1],g[2],g[3],g[4]); for(i=0;i<n;i++) { fread(b,sizeof(slbh),n,f); printf("%3d %30s %10d %10d %10ld \n",i+1,b[i].ten,b[i].dg,b[i].sl,b[i].tien); } printf("\n\t\t\t\t\t Tong tien : %ld",tt); fclose(f); printf("\n\t press any key..."); getch(); } void main() { int k; clrscr(); nhapdl(); indl(); } Bài 4 : #include #include #include #include struct dssv { char ho_ten[30];int tuoi;float dtb; }a[50] ; int n; long size = sizeof(dssv); char ss[]="DANHSACH.C"; void nhap() { dssv x;int i=-1;FILE *f;float dtb1; f=fopen(ss,"ab"); do { i++; printf("\n Bo sung sinh vien : "); printf("\n Ho ten : ");fflush(stdin);gets(x.ho_ten); printf(" Tuoi : ");scanf("%d",&x.tuoi); printf(" Diem trungbinh : ");scanf("%f",&x.dtb); printf("\n Bam ESC de dung nhap..."); fwrite(&x,size,1,f);a[i]=x; } while (getch()!=27); n=i+1; fclose(f); } void in() { FILE *f;int i=0;dssv x; char *s[]={"STT","HO TEN","TUOI","DIEM TB"}; f=fopen(ss,"rb"); if (f==NULL) {printf("\n khong co tep ");getch();} else { printf("\n\t\t\t DANH SACH SINH VIEN "); printf("\n%4s%30s%10s%10s",s[0],s[1],s[2],s[3]); while (fread(&x,size,1,f)>0) { i++; printf("\n%4d%30s%10d%10.1f",i+1,x.ho_ten,x.tuoi,x.dtb); } } fclose(f); getch(); } void sua() { dssv x;FILE *f;char ten[30];int i=0; printf("\n Nhap ten sinh vien muon sua diem : "); gets(ten); for(i=0;i<n;i++) if (strcmpi(a[i].ho_ten,ten)==0) break; if (strcmpi(a[i].ho_ten,ten)!=0) printf("\n khong tim thay sinh vien %s ",ten); else { printf("\n Sua tuoi : ");scanf("%d",&a[i].tuoi); printf("\n sua diem tb : ");scanf("%f",&a[i].dtb); f=fopen(ss,"wb"); fwrite(a,sizeof(dssv),n,f); fclose(f); printf("\n Da sua xong...");getch(); } } void main() { int k; remove("DANHSACH.C"); do { clrscr(); printf("\n 1. Bo sung."); printf("\n 2. Sua."); printf("\n 3. In."); printf("\n 4. Exit."); printf("\n Moi chon cac chuc nang...(^-^)"); scanf("%d%*c",&k); if (k==1) nhap(); if (k==2) sua(); if (k==3) in(); if (k>4) {printf("\n Nhap sai chuc nang.Press any key...^^!");getch();} } while (k!=4); } To be continue…bài tập về màn hình và đồ hoạ.

Các file đính kèm theo tài liệu này:

  • docbt_tep_du_lieu_3908.doc
Tài liệu liên quan