Phần IV : Tệp
Câu 1:
#include <conio.h>
#include <stdio.h>
void main()
{clrscr();
int i,n;
float a[100];
FILE *f;
printf("ban muon nhap bao nhieu so thuc? ");
scanf("%d",&n);
f=fopen("stduong.txt","wt");
for (i=0;i{printf("nhap mot so thuc tu ban phim vao! ");
scanf("%f",&a[i]);
if (a[i]>0) fprintf(f,"%5.1f",a[i]);
}
fclose(f);
getch();
}
Câu 2:
#include <conio.h>
#include <stdio.h>
void doc_tep(FILE *f,int a[],int *n)
{int i=0;
while (feof(f)==0)
{fscanf(f,"%d",&a[i]);
i++;
}
fclose(f);
*n=i;
}
int tong(int a[], int n)
{int i=0,s=0;
while (i
{if (a[i]%2==0) s=s+a[i];
i++;
}
return s;
}
void main()
{clrscr();
int n,a[50];
FILE *f;
f=fopen("songuyen.txt","rt");
doc_tep(f,a,&n);
printf("\n tong cac gia tri chan co trong tep = %5d",tong(a,n));
getch();
}
Câu 3:
#include <conio.h>
#include <stdio.h>
void doc_tep(FILE *f,int a[],int *n)
{int i=0;
while (feof(f)==0)
{fscanf(f,"%d",&a[i]);
i++;
}
fclose(f);
*n=i;
}
int dem(int a[],int n)
{int i=0,d=0;
while (i
{if (a[i]>0) d++;
i++;
}
return d;
}
void main()
{clrscr();
int n,a[50];
FILE *f;
f=fopen("songuyen.txt","rt");
doc_tep(f,a,&n);
printf("\n so phan tu duong co mat trong tep = %5d",dem(a,n));
getch();
}
Câu 4:
#include <conio.h>
#include <stdio.h>
void main()
{clrscr();
int i,n,a[100];
FILE *f;
printf("ban muon nhap bao nhieu so nguyen? ");
scanf("%d",&n);
f=fopen("snduong.txt","wt");
for (i=0;i
{printf("nhap mot so nguyen tu ban phim vao! ");
scanf("%d",&a[i]);
if (a[i]>0) fprintf(f,"%5d",a[i]);
}
fclose(f);
getch();
}
Câu 6:
#include <conio.h>
#include <stdio.h>
void main()
{clrscr();
int d=0; char s;
FILE *f;
f=fopen("xauhoa.txt","rt");
while (feof(f)==0)
{s=getc(f);
if ((s<='Z')&&(s>='A')) d++;
}
fclose(f);
printf("\n so chu cai in hoa co mat trong tep la: %5d",d);
getch();
}
Câu 7:
#include <conio.h>
#include <stdio.h>
void doc_tep(FILE *f,int a[],int *n)
{int i=0;
while (feof(f)==0)
{fscanf(f,"%d",&a[i]);
i++;
}
fclose(f);
*n=i;
}
int tong(int a[], int n)
{int i=0,s=0;
while (i
{if (a[i]%2!=0) s=s+a[i];
i++;
}
return s;
}
void main()
{clrscr();
int n,a[50];
FILE *f;
f=fopen("songuyen.txt","rt");
doc_tep(f,a,&n);
printf("\n tong cac so co gia tri le trong tep = %5d",tong(a,n));
getch();
}
Câu 8:
#include <conio.h>
#include <stdio.h>
void doc_tep(FILE *f,int a[],int *n)
{int i=0;
while (feof(f)==0)
{fscanf(f,"%d",&a[i]);
i++;
}
fclose(f);
*n=i;
}
float tbc(int a[],int n)
{int i,s=0;
for(i=0;i
return ((float)s/n);
}
void main()
{clrscr();
int n,a[50];
FILE *f;
f=fopen("songuyen.txt","rt");
doc_tep(f,a,&n);
printf("\n trung binh cong cua cac phan tu trong tep = %5.2f",tbc(a,n));
getch();
}
Câu 9:
#include <conio.h>
#include <stdio.h>
void main()
{clrscr();
int d=0; char s;
FILE *f;
f=fopen("xauhoa.txt","rt");
while (feof(f)==0)
{s=getc(f);
if (s=='A') d++;
}
fclose(f);
printf("\n so chu cai 'A' co mat trong tep la: %5d",d);
getch();
}
Câu 10:
#include <conio.h>
#include <stdio.h>
void doc_tep(FILE *f,int a[],int *n)
{int i=0;
while (feof(f)==0)
{fscanf(f,"%d",&a[i]);
i++;
}
fclose(f);
*n=i;
}
int tong(int a[],int n)
{int i,s=0;
for(i=0;i
return s;
}
void main()
{clrscr();
int n,a[50];
FILE *f;
f=fopen("songuyen.txt","rt");
doc_tep(f,a,&n);
printf("\n tong cac phan tu trong tep = %5d",tong(a,n));
getch();
}