【改】软件工程分词软件

来源:互联网 发布:87红楼梦 台湾 知乎 编辑:程序博客网 时间:2024/06/05 03:48


#include<iostream>

#include<windows.h>
#include<conio.h>
#include<time.h>
#include<stdlib.h>
#include<iomanip>
#include<time.h>
using namespace std;
#define M 20000
typedef struct
{
char danci[20];
int sum;
}SumPoint;
void setpos(int i,int j)
{
COORD pos={i-1,j-1};
HANDLE Out=GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleCursorPosition(Out,pos);
}
void SetGameBack()
{
system("color 2F");
system("cls");
setpos(14,8);
cout<<"*                      词  频  统  计               *"<<endl;
setpos(14,10);
cout<<"*                 按任意键开始                      *"<<endl;
setpos(50,20);
    _getch();//等待键盘输入
}
void main()
{
SetGameBack();
system("cls");
SumPoint word[M];
SumPoint words;
int K,n=0,i,j;
char filename[10];
cout << "***********请输入文件路径:***********" << endl;
cin >> filename;    
FILE *fp;
char ch;
if ((fp = fopen(filename, "r")) == NULL)
{
cout << "无法打开文件!" << endl;
exit(0);
}
while (!feof(fp))
{
ch= getc(fp);
if (ch ==' ' ||ch==10)
{
continue;
}
if ((ch>='a'&&ch<='z') || (ch>='A'&&ch<='Z'))              
{
K = 0;
words.sum = 1;
while ((ch >= 'a'&&ch <= 'z') || (ch >= 'A'&&ch <= 'Z') || (ch == '\''))
{
if (ch >= 'A'&&ch <= 'Z')
ch += 32;
words.danci[K++] = ch;
ch = getc(fp);
}
words.danci[K++] = '\0';
j = n;
for (i = 0; i<j; i++) 
{
if (strcmp(words.danci, word[i].danci) == 0)
{
word[i].sum++;
break;
}
}
if (n == 0 || i == j)
{
word[n] = words;
n++;
}
}
}
SumPoint max[11];
SumPoint temp;
for (i = 0; i<10; i++)
{
max[i] = word[i];
}
for (j = 0; j<10; j++)
for (i = 0; i<10 - j; i++)
if (max[i].sum<max[i + 1].sum)
{
temp =max[i];
max[i] =max[i + 1];


max[i + 1] = temp;
}
for (i = 10; i<n; i++)
{
if (max[9].sum<word[i].sum)
{
int a = 8;
while (max[a].sum<word[i].sum&&a >= 0)
{
a--;
}
for (j = 9; j>a + 1; j--)
{
max[j] =max[j - 1];
}
if (a<0)
max[0] = word[i];
else
max[j] = word[i];
}
}
for (i = 0; i<10; i++)
{
cout << setiosflags(ios::left) << setw(10) <<max[i].danci << max[i].sum<< endl;
}

}


0 0
原创粉丝点击