單詞詞頻統計c語言
1. c語言高分求助 統計詞頻
「keefo」的答案不錯
不過有一個地方可以完善,單詞統計,使用定長的數組不好,還是改成鏈表來存儲。
要是單詞超過了數組的長度,就會溢出,那可就不好了。
需要的話,一會兒寫出來^_^
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
const int WORD_LENGTH = 256; //使用const int 比用define要好
struct wordNode
{
char word[WORD_LENGTH]; //存放單詞
int iWordCount; //單詞出現次數
wordNode *pNext; //結構體的指針
};
wordNode *pHeader = NULL; //鏈表的頭指針
//聲明需要的函數
void CountWord(char *current);
wordNode * SearchWord(char *current);
void PrintResult();
void Release();
int main()
{
//臨時存放單詞的片語
char temp[WORD_LENGTH];
//打開要讀取的文件
FILE *fp;
if( NULL == (fp=fopen("123.txt", "r")) ) //把NULL寫在前面是一種編程習慣,不用覺得奇怪
{
printf("Open file failed!!\n");
exit(1);
}
//循環讀取文本中的內容
while( EOF != (fscanf(fp,"%s",temp)) )
{
CountWord(temp);
}
//關閉文件
fclose(fp);
//輸出統計結果
PrintResult();
//釋放內存,養成好習慣
Release();
return 0;
}
//單詞統計
void CountWord(char *current)
{
wordNode *pNode = NULL;
pNode = SearchWord(current);
if(NULL == pNode)
{
return;
}
else
{
pNode->iWordCount++;
}
}
//查找單詞所在節點
wordNode * SearchWord(char *current)
{
//當鏈表為空的時候,也就統計第一個單詞時
if( NULL == pHeader)
{
pHeader = new wordNode;
strcpy(pHeader->word, current);
pHeader->iWordCount = 0;
pHeader->pNext = NULL;
return pHeader;
}
//搜索現有的鏈表
wordNode *pCurr = pHeader;
wordNode *pPre = NULL;
while( (NULL != pCurr) && (0 != strcmp(pCurr->word, current)) )
{
pPre = pCurr;
pCurr = pCurr->pNext;
}
//該單詞不存在
if(NULL == pCurr)
{
pCurr = new wordNode;
strcpy(pCurr->word, current);
pCurr->iWordCount = 0;
pCurr->pNext = NULL;
pPre->pNext = pCurr;
}
return pCurr;
}
//輸出結果
void PrintResult()
{
if(NULL == pHeader)
{
printf("No Word!!\n");
}
else
{
wordNode *pCurr = pHeader;
while(NULL != pCurr)
{
printf("%s\t%d\n", pCurr->word, pCurr->iWordCount);
pCurr = pCurr->pNext;
}
}
}
void Release()
{
if(NULL == pHeader)
{
return;
}
wordNode *pCurr = pHeader;
while(NULL != pCurr)
{
pHeader = pCurr->pNext;
delete pCurr;
pCurr = pHeader;
}
}
字母的統計工作,和單詞一樣,其實更簡單一些。
2. c語言問題 統計單詞的詞頻①編寫函數void count(char a[],char w[][10
對w進行循環與字元串a比對
比如"is"與a進行比對,找到a中所有有a地方然後判斷該位置是否為一個單詞是則在對應b中增加數字
如果代碼不想自己寫的話
我可以有償代勞
3. c語言用鏈表實現,統計一個英文文本文件中每個單詞的出現次數(詞頻統計),結果按單詞詞典序輸出到屏幕
#include <stdio.h>
#include <string.h>
int main(void)
{
int a = 0, b = 0, c = 0;
char buf[128];
FILE *fp;
/* 打開文件,文件名必須大寫 */
fp= fopen("DATA5610.TXT", "r");
if (!fp) {
printf("No 'DATA5610.TXT' found.\n");
return -1;
}
/* 逐次讀取單詞,空格或回車分割 */
while (fscanf(fp, "%s", buf) > 0) {
/* 如讀取到的單詞是 if,則a自增 1 */
if (strcmp(buf, "if") == 0)
a++;
else if (strcmp(buf, "while") == 0)
b++;
else if (strcmp(buf, "for") == 0)
c++;
}
printf("if: %d, while: %d, for: %d\n", a, b, c);
fclose(fp);
return 0;
}
4. c語言程序設計:1,統計英文文本中單詞個數。2,統計某一特定單詞出現的頻度。
1、統計英文文本中單詞個數。
if((a[i]>='a'&&a[i]<='z')||(a[i]>='A'&&a[i]<='Z')) sum++;
2、統計某一特定單詞出現的頻度。
for(i=0;i!='/0';i++)
{
if(a[i]=='特定單詞')
sum++;
}
(4)單詞詞頻統計c語言擴展閱讀:
if語句的一般形式如下:
if(表達式)語句1
[else語句2]
if語句中的「表達式」可以是關系表達式、邏輯表達式,甚至是數值表達式。其中最直觀、最容易理解的是關系表達式。所謂關系表達式就是兩個數值進行比較的式子。
for循環小括弧里第一個「;」號前為一個為不參與循環的單次表達式,其可作為某一變數的初始化賦值語句, 用來給循環控制變數賦初值。
5. 使用C語言編寫一個詞頻(限英文文章)統計程序
不知道你說的意思是從文件中讀出 還是直接從屏幕輸入單詞 下面是我做的直接從屏幕輸入單詞的一個程序 你可以看看噢
#include <stdio.h>
#include<string.h>
void main()
{
char str[100][26];
const char str1[5]="1234"; const char str2=' ';
int num[100];
int m,k,i,n,x,q;
int max,max1,max2;
double pl;
again:
max=0;max1=0;max2=0;
m=0;k=0;i=0;n=0;x=0;q=0;
pl=0;
for(m=0 ;m<100;m++)
{num[m]=0;
}
for(m=0 ;m<100;m++)
{strcpy( str[m],&str2);
}
printf(" 請輸入單詞(100個單詞以 內 每個不大於25個字元)\n");
printf(" 輸入 1234 結束輸入 \n");
for(m=0 ;m<100;m++)
{
scanf("%s",str[m]);
if( strcmp(str[m],str1 )==0 )
break;
}
for(i=0 ; i<m;i++)
{
for(k=0; k<m;k++)
{
if(strcmp(str[i],str[k] )==0)
{
num[i]++;
}
}
}
max=num[0];
while(max>=2)
{
max=num[0];max1=0;max2=0;
for(n=0;n<m;n++)
{
if(max<num[n+1])
{
max=num[n+1];
max1=n+1;
}
}
num[max1]=0;
pl=(double)max/m;
printf("%8s",str[max1]);
printf(" 出現%8d次",max);
printf(" 位置 第%8d個 ",max1+1);
printf("頻率%8lf\n",pl);
for(n=0;n<m;n++)
{
if(max==num[n+1] && max1!=n+1)
{max2=n+1;
num[max2]=0;
printf("%8s",str[max2]);
printf(" 出現%8d次",max);
printf(" 位置 第%8d個 ",max2+1);
printf("頻率%8lf\n",pl);
}
}
printf(" 。第%d位 。\n\n\n",q+1);
q++;
}
printf("重新輸入 按 1 其它鍵退出程序");
scanf("%d",&x);
switch(x)
{case 1:
goto again;
break;
default:
return;
}
}
6. 如何用c語言完成統計一個句子中每個單詞出現的次數啊是用c語言,不要用c++,謝謝大家了
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<string.h>
typedefstructWord{
charw[20];
intk;
structWord*next;
}pWord;
intmain(intargc,char*argv[])
{
FILE*fp=fopen("input.txt","r");
structWord*Head=NULL;
while(!feof(fp)){
char*p=(char*)malloc(20*sizeof(char));
fscanf(fp,"%s",p);
if(Head==NULL){
structWord*temp=(structWord*)malloc(sizeof(structWord));
strcpy(temp->w,p);
temp->k=1;
temp->next=NULL;
Head=temp;
}else{
structWord*pp=Head;
while(pp!=NULL){
if(strcasecmp(pp->w,p)==0){
++pp->k;
break;
}else{
pp=pp->next;
}
}
if(pp==NULL){
structWord*temp=(structWord*)malloc(sizeof(structWord));
strcpy(temp->w,p);
temp->k=1;
temp->next=Head;
Head=temp;
}
}
}
structWord*q=Head;
while(q!=NULL){
printf("%s",q->w);
printf("%d ",q->k);
q=q->next;
}
return0;
}/*------endofmain------*
我是把要讀寫的東西放進了一個input.txt中這樣好讀寫一下你看看行不行吧
7. 用C語言編寫(單詞詞頻統計)用Vc++6.0環境
判斷一個單詞的條件:單詞前一個字元是空格後一個字元也是空格
根據這個你就可以提取出各個單詞了,統計出現次數就不難了
還有就是c語言在VC6.0下不能繪圖的 所以柱狀圖無法做 除非用第三方庫
8. 用C語言實現英文單詞詞頻統計
你這樣存單詞也太浪費空間了,你可以把一個單詞放到一個變數里。可以這樣解決:用一個變數(word)記單詞,另一個變數(ch)去接收文章的每個字元,當if(ch>='a'&&ch<='z') &&(ch>='A'&&ch<='Z')word+=ch; else { ch=nextchar();並且這時變數word中就是一個完整的單詞了,你是怎麼存儲你就隨意了.};
你在存儲是可以記數,當有一定的數量是你可以刪除什麼的都可以.也可以放到文件里.
9. C語言單詞詞頻統計
柱狀圖 橫著顯示可以嗎?還有txt文件裡面只是單詞吧
列印效果
shao ***
he ****
這樣可以嗎,可以我就幫你寫