作業八
請輸入任何一篇英文文章(a text file)
統計此文章中出現的字(word)及次數.
PS:(1) 大小寫不一致算不同word
ex: zzz , Zzz , ZzZ
這三個算不同的word
(2) 底下這些算同一個word
ex: zzz,
zzz ,
zzz
這三個zzz算同一個word,就是zzz
(3) NCTU-CIS 這算一個 word,不是兩個
-------------------------source code------------------------------------
#include<iostream>
#include<stdio.h>
#include<cstring>
#include<fstream>
using namespace std;
int i;
struct str_rec
{
char str[20];
int repeat;
}word[10000];
int main()
{
int p,q,k,m,n,tmp,r;
char temp[21];
for(i=0;i<10000;i++)
{
word[p].repeat=0;
if(scanf("%s",word[i].str)==EOF)
break;
}
for(p=0;p<i;p++)
{
for(q=1;q<i;q++)
if(strcmp(word[p].str,word[q].str)==0)
{
(word[p].repeat)++;
}
}
for(m=0;m<i;m++)
for(n=0;n<i;n++)
{
if(word[m].repeat>word[n].repeat)
{
tmp=word[n].repeat;
strcpy(temp,word[n].str);
word[n].repeat=word[m].repeat;
strcpy(word[n].str,word[m].str);
word[m].repeat=tmp;
strcpy(word[m].str,temp);
}
}
ofstream ofile;
ofile.open("result.txt",ios::out);
for(k=0;k<i;k++)
{
ofile<<word[k].str<<":"<<word[k].repeat<<endl;
}
ofile.close();
}
--------------------------------------------------------------------------
As you can see,這樣輸出的話會把每一個陣列裡的字串通通輸出..
變成:the 18
the 18
the 18
.
.
.
這樣重複...
想了很久不知道要怎麼解決..
也就是每一個不同的字只要輸出一次
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.113.122.120