练习 6-4 编写一个程序,根据单词的出现频率按降序打印输入的各个不同单词,并在 每个单词的前面标上它的出现次数
来源:互联网 发布:同步推苹果助手 mac 编辑:程序博客网 时间:2024/05/16 12:31
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#define MAXWORD 100
/*Author Stat: founded 2017 11 26*/
static size_t word_count = 0;
struct line{
char *word;
size_t count;
struct line *next;
};
int getword(char *,int);
struct line *additem(struct line *,char *);
void swapcontent(struct line *,struct line *);
void showitem(struct line *);
struct line *talloc(void);
char *strdup(char *);
int main(void)
{
struct line *head;
char word[MAXWORD];
head = NULL;
while(getword(word,MAXWORD) != EOF)
if(isalpha(word[0]))
head = additem(head,word);
printf("Total word: %lu\n",word_count);
showitem(head);
return 0;
}
int getword(char *word,int n)
{
int c,getch(void);
void ungetch(int);
char *w = word;
while(isspace(c = getch()))
;
if(c != EOF)
*w++ = c;
if(!isalpha(c)){
*w = '\0';
return c;
}
for(;--n > 0; w++)
if(!isalpha(*w = getch())){
ungetch(*w);
break;
}
*w = '\0';
return word[0];
}
struct line *additem(struct line *p,char *w)
{
if(p == NULL){
if((p = talloc()) == NULL){
printf("Out of memory!\n");
exit(EXIT_FAILURE);
}
if((p->word = strdup(w)) == NULL){
printf("Out of memory!\n");
exit(EXIT_FAILURE);
}
p->count = 1;
p->next = NULL;
word_count++;
}else if(strcmp(p->word,w) == 0)
p->count++;
else
p->next = additem(p->next,w);
if(p->next != NULL)
swapcontent(p,p->next);
return p;
}
void swapcontent(struct line *p,struct line *pn)
{
size_t temp;
char *temps;
if(p->count < pn->count){
temp = p->count;
p->count = pn->count;
pn->count = temp;
temps = p->word;
p->word = pn->word;
pn->word = temps;
}
}
void showitem(struct line *p)
{
if(p != NULL){
printf("%4lu %s\n",p->count,p->word);
showitem(p->next);
free(p->word);
p->word = NULL;
free(p);
p = NULL;
}
}
struct line *talloc(void)
{
return (struct line *)malloc(sizeof(struct line));
}
char *strdup(char *s)
{
char *p;
if((p = (char *)malloc(strlen(s)+1)) != NULL)
strcpy(p,s);
return p;
}
#define BUFSIZE 100
static char buf[BUFSIZE];
static int ps = 0;
int getch(void)
{
return (ps > 0) ? buf[--ps] : getchar();
}
void ungetch(int c)
{
if(ps > BUFSIZE)
printf("stack too full!\n");
else
buf[ps++] = c;
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#define MAXWORD 100
/*Author Stat: founded 2017 11 26*/
static size_t word_count = 0;
struct line{
char *word;
size_t count;
struct line *next;
};
int getword(char *,int);
struct line *additem(struct line *,char *);
void swapcontent(struct line *,struct line *);
void showitem(struct line *);
struct line *talloc(void);
char *strdup(char *);
int main(void)
{
struct line *head;
char word[MAXWORD];
head = NULL;
while(getword(word,MAXWORD) != EOF)
if(isalpha(word[0]))
head = additem(head,word);
printf("Total word: %lu\n",word_count);
showitem(head);
return 0;
}
int getword(char *word,int n)
{
int c,getch(void);
void ungetch(int);
char *w = word;
while(isspace(c = getch()))
;
if(c != EOF)
*w++ = c;
if(!isalpha(c)){
*w = '\0';
return c;
}
for(;--n > 0; w++)
if(!isalpha(*w = getch())){
ungetch(*w);
break;
}
*w = '\0';
return word[0];
}
struct line *additem(struct line *p,char *w)
{
if(p == NULL){
if((p = talloc()) == NULL){
printf("Out of memory!\n");
exit(EXIT_FAILURE);
}
if((p->word = strdup(w)) == NULL){
printf("Out of memory!\n");
exit(EXIT_FAILURE);
}
p->count = 1;
p->next = NULL;
word_count++;
}else if(strcmp(p->word,w) == 0)
p->count++;
else
p->next = additem(p->next,w);
if(p->next != NULL)
swapcontent(p,p->next);
return p;
}
void swapcontent(struct line *p,struct line *pn)
{
size_t temp;
char *temps;
if(p->count < pn->count){
temp = p->count;
p->count = pn->count;
pn->count = temp;
temps = p->word;
p->word = pn->word;
pn->word = temps;
}
}
void showitem(struct line *p)
{
if(p != NULL){
printf("%4lu %s\n",p->count,p->word);
showitem(p->next);
free(p->word);
p->word = NULL;
free(p);
p = NULL;
}
}
struct line *talloc(void)
{
return (struct line *)malloc(sizeof(struct line));
}
char *strdup(char *s)
{
char *p;
if((p = (char *)malloc(strlen(s)+1)) != NULL)
strcpy(p,s);
return p;
}
#define BUFSIZE 100
static char buf[BUFSIZE];
static int ps = 0;
int getch(void)
{
return (ps > 0) ? buf[--ps] : getchar();
}
void ungetch(int c)
{
if(ps > BUFSIZE)
printf("stack too full!\n");
else
buf[ps++] = c;
}
阅读全文
0 0
- 练习 6-4 编写一个程序,根据单词的出现频率按降序打印输入的各个不同单词,并在 每个单词的前面标上它的出现次数
- go语言之map练习(二):编写一个程序wordfreq程序,统计输入文本中每个单词出现的频率(次数)
- 统计每个单词在输入中出现的次数
- 练习 6-3 编写一个交叉引用程序,打印文档中所有单词的列表,并且每个单词还有一 个列表,记录出现过该单词的行号。对 the、and 等非实义单词不予考虑。
- 统计单词出现的次数并按单词出现的次数顺序输出单词及其次数
- 获取每个单词出现的频率
- 单词的出现次数
- 编写一个程序,统计给定文件中包含的每个单词出现的频率,并按单词表的顺序显示统计结果
- 统计TXT文档中各个单词出现的频率,并将前十个打印输出的程序性能分析
- 查找一个字符串中每个单词的出现次数--Java
- Java实现 统计单词出现的次数并按照单词频率从高到低输出
- c++统计输入中不同单词所出现的次数
- c++统计输入中不同单词所出现的次数
- 计算单词出现的频率
- java编程题:输入一段英文,获取每个单词出现的次数跟位置,并排序
- 分析一个文本文件中各个单词出现的频率,把频率最高的10个词打印出来
- 分析一个文本文件中各个单词出现的频率,把频率最高的10个词打印出来
- 分析一个文本文件中各个词出现的频率,并把频率最高的十个单词打印出来。
- Merge k Sorted Lists
- C#特性(attribute)
- HDU 5690 All X(快速幂)
- “自顶向下,逐步求精”——Top-down Design
- Online Multi-Object Tracking via Structural Constraint Event Aggregation
- 练习 6-4 编写一个程序,根据单词的出现频率按降序打印输入的各个不同单词,并在 每个单词的前面标上它的出现次数
- The Solution Path Algotithm for Identity-Aware Multi-Object Tracking
- 基于tomcat集群会话保持(DeltaManager)
- 软工文档
- 调用天气查询webservice服务的两种基本方法
- CCF 1018 打电话
- 汇编程序设计--学生成绩管理系统
- Groupwise Tracking of Crowded Similar-Appearance Targets from Low-Continuity Image Sequences
- Fastjson 使用指南