Đề: Viết chương trình thực hiện các yêu cầu sau:
a. Cho phép người dùng nhập một văn bản từ bàn phím và lưu thành tập tin văn bản với tên vanban.txt.
b. Thống kê tần số xuất hiện của các kí tự trong tập tin trên
- Code:
-
#include <iostream>
#include <string>
#include <stdio.h>
#include <conio.h>
using namespace std;
void ghifile(char text[])
{
FILE *f = fopen("vanban.txt", "wt");
if(!f) // tuong tu (f == NULL)
return;
fputs(text, f);
fclose(f);
}
void thongke(int tk[])
{
FILE *f = fopen("vanban.txt","rt");
if(!f)
return;
while(!feof(f))
{
int c = fgetc(f);
tk[c]++;
}
fclose(f);
}
void main()
{
char text[10000];
cout<<"Nhap chuoi ki tu: ";
gets(text);
ghifile(text);
int tk[256] = {0};
thongke(tk);
cout<<endl;
for(int i = 0; i <= 255; i++)
if(tk[i] > 0)
cout<<char(i)<<" xuat hien: "<<tk[i]<<" lan"<<endl;
getch();
}
____ _ +------------------------------------
___ /o) | Admin: Đỗ Văn Minh
__ / ./_ | Email: Nhox_sock_nock_out
_ .( .(__|
_( ( \ \ )-+/o)------------------------------
_(\\ \ \__/. /
__\aa.aaaa/
___\____/