1. Trang chủ
  2. » Công Nghệ Thông Tin

Bài tập lập trình PHẦN IV tệp

6 145 0

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

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

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

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

Trang 1

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<n;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<n)

{if (a[i]%2==0) s=s+a[i];

i++;

}

return s;

Trang 2

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<n)

{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);

Trang 3

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<n;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>

Trang 4

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<n)

{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);

Trang 5

*n=i;

}

float tbc(int a[],int n)

{int i,s=0;

for(i=0;i<n;s=s+a[i],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;

Trang 6

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<n;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 phan tu trong tep = %5d",tong(a,n)); getch();

}

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

TỪ KHÓA LIÊN QUAN

w