我用#CSDN#这个app发现了有技术含量的博客,小伙伴们求同去《喵喵在开发一个新式的社交网络,现在她想要写一个模块来处理文本并计算其中不同标签的数量。》, 一起来围观吧 https://blog.csdn.net/qq_57230742/article/details/122809821?utm_source=app&app_version=5.0.1&code=app_1562916241&uLinkId=usr1mkqgl919blen
供参考:
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
typedef struct node
{
char c[30];
int count;
struct node *next;
}node,*link;
node *firstWord(node *head,char word[])//判断单词是不是第一次出现
{
node *p=head;
while(p->next != NULL)
{
if(strcmp(p->next->c, word) == 0)
return p->next;
p=p->next;
}
return NULL;
}
void print(node *p)//输出单词和次数
{
node *q=p;
int cnt_word=0;
while(q->next){q = q->next;cnt_word++;}
printf("%d\n",cnt_word);
while(p->next != NULL)
{
printf("%s %d\n", p->next->c,p->next->count);
q=p;
p=p->next;
free(q);
}
free(p);
}
int main()
{
char str[1024],word[30]={0};
node *head,*p,*q,*t;
int i,j=0,tag=1,flg=1;
head=(link)malloc(sizeof(node));
head->next=NULL;
p=head;
gets(str);
while(flg)
{
switch(tag){
case 1:
i=0;
if(str[j] == '#')
word[i++]=str[j++] ,tag = 2;
else
tag = 3;
break;
case 2:
if(isalpha(str[j]))
word[i++]=str[j++];
else if(str[j] == '#')
i = 0, tag = 3;
else
tag = 3;
break;
case 3:
if(str[j] == ' ' || str[j] == '\0')
{
if(i > 1){
word[i]='\0';//加入结束符
t=firstWord(head,word);
if(t == NULL)//第一次出现的单词
{
q=(link)malloc(sizeof(node));
q->next=NULL;
q->count=1;
strcpy(q->c,word);
p->next=q;
p=q;
}
else
t->count++;
}
tag = 1;
}
if(str[j] == '\0') flg=0;
j++;
break;
}
}
print(head);
return 0;
}
用C肯定是更复杂啊,C++好歹有个STL,C语言的话很多算法都得手写
厉害,谁会写一个呢
#include <stdlib.h>
#include<string.h>
#include<stdio.h>
int main()
{
char zifu[1000];
char results[100][1000];
gets(zifu);
int num=0,m=0,i=0,j=0,flag;
if(zifu[0]!='#'){
flag=0;
}else if(zifu[1]!=' '&&zifu[0]=='#'){
flag=1;
}
/*else if(zifu[1]!=' '){
flag=1;
}*/
for(i=0;i<strlen(zifu);i++){
if(' '==zifu[i]){
m=0;
if(i<(strlen(zifu)-1)&&zifu[i+1]!='#'){flag=0;
//num++;
}else if(zifu[i+1]=='#'){
num++;flag=1;
}
continue;
} else {
if(flag==1){
results[num][m]=zifu[i];
m++;
}
}
}
int nums=0;
for(int n=0;n<=num;n++){
if(strlen(results[n])>=2){
nums++;
}
}
printf("%d\n",nums);
char shuzu[num][1000];
int flag1=0;
for(j=0;j<=num;j++){//
if(strlen(results[j])>=2){
int sum=1,flag2=0;
if(flag1==0){
strcpy(shuzu[0],results[j]);
flag1++;
}else{
for(int m=0;m<flag1;m++){
if(strcmp(shuzu[m],results[j])==0){
flag2=1;
break;
}
if(m==flag1-1){
strcpy(shuzu[flag1],results[j]);
flag1++;
break;
}
}
}
if(flag2==1)continue;
for(int h=j+1;h<=num;h++){
if(strcmp(results[j],results[h])==0){
sum++;
}
}
printf("%s %d\n",results[j],sum);
}
}
printf("\n\n");
return 0;
}