#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <iostream>
#include <windows.h>
#include <string.h>
using namespace std;
FILE *fp=fopen("数据文件.txt","r");
#define MAX 1024
char line[MAX];
int main()
{
HWND TheWindow;
SetConsoleTitle("文本统计器");
system("color f0");
char stop[3]="。";
int data = 0,chinese = 0, sentence = 0,blank = 0;
int english_ch = 0,passages = 0,rows = 0;
unsigned char temp[2];
while(fgets( line , MAX , fp) != NULL)
{
rows++;
if(line[0] == '\n') continue;
for(int i = 0; i < 100;i++)
{
if(line[i] == '\n')
{
passages++;
continue;
}
temp[0] = line[i];
if (temp[0] >= 0XB0 )
{
i++;
chinese++;
continue;
}
if(line[i] >= 'a'&&line[i] <= 'z'||line[i] >= 'A'&&line[i] <= 'Z')
{
english_ch++;
continue;
}
if(line[i] >= '0' && line[i] <= '9')
{
data++;
continue;
}
if(line[i] == ' ')
{
blank++;
continue;
}
if(line[i] == stop[0] && line[i+1] == stop[1]||line[i] == '.')
{
sentence++;
continue;
}
}
}
fp=fopen("文本统计结果文件.txt","w");
fprintf(fp,"中文个数为:%2d\n",chinese);
fprintf(fp,"英文个数为:%2d\n",english_ch);
fprintf(fp,"数字个数为:%2d\n",data);
fprintf(fp,"空格个数为:%2d\n",blank);
fprintf(fp,"句子数为:%2d\n",sentence);
fprintf(fp,"行数为:%2d\n",rows);
fprintf(fp,"段落数为:%2d\n",passages);
fclose(fp);
ShellExecute(NULL,"open","文本统计结果文件.txt",NULL,NULL,SW_SHOWNORMAL);
TheWindow = ::FindWindow( NULL, "文本统计器" );
SendMessage(TheWindow,WM_SYSCOMMAND,SC_CLOSE,NULL);
return 0;
}