练习 6-3 编写一个交叉引用程序,打印文档中所有单词的列表,并且每个单词还有一 个列表,记录出现过该单词的行号。对 the、and 等非实义单词不予考虑。
来源:互联网 发布:网络大电影人群分析 编辑:程序博客网 时间:2024/04/29 22:04
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define MAXWORD 100
/*Author Stat: founded 2017 11 26*/
static int count_line = 1;
struct line{
int line;
struct line *next;
};
struct tnode{
char *word;
struct line *start;
struct tnode *next;
};
struct tnode *addItem(struct tnode *,char *);
struct line *addlines(struct line *);
void showItem(struct tnode *);
void showLine(struct line *);
struct tnode *Ialloc(void);
struct line *Lalloc(void);
char *walloc(char *);
int getword(char *,int);
int Nonrealword(char *);
int main(void)
{
struct tnode *head;
char word[MAXWORD];
head = NULL;
while(getword(word,MAXWORD) != EOF)
if(Nonrealword(word))
if(isalpha(word[0]))
head = addItem(head,word);
printf("%33s\n\n","Words Line Number");
showItem(head);
return 0;
}
struct tnode *addItem(struct tnode *p,char *w)
{
if(p == NULL){
if((p = Ialloc()) == NULL){
printf("Out of memory!\n");
exit(EXIT_FAILURE);
}
if((p->word = walloc(w)) == NULL){
printf("Out of memory!\n");
exit(EXIT_FAILURE);
}
if((p->start = Lalloc()) == NULL){
printf("Out of memory!\n");
exit(EXIT_FAILURE);
}
p->start->line = count_line;
p->start->next = NULL;
p->next = NULL;
}else if(strcmp(p->word,w) == 0)
p->start = addlines(p->start);
else
p->next = addItem(p->next,w);
return p;
}
struct line *addlines(struct line *p)
{
if(p == NULL){
if((p = Lalloc()) == NULL){
printf("Out of memory!\n");
exit(EXIT_FAILURE);
}
p->line = count_line;
p->next = NULL;
}else if(p->line == count_line)
;
else
p->next = addlines(p->next);
return p;
}
void showItem(struct tnode *p)
{
if(p != NULL){
printf("%20s ",p->word);
showLine(p->start);
printf("\n");
showItem(p->next);
free(p);
}
}
void showLine(struct line *p)
{
if(p != NULL){
printf("%d ",p->line);
showLine(p->next);
free(p);
}
}
struct tnode *Ialloc(void)
{
return (struct tnode *)malloc(sizeof(struct tnode));
}
struct line *Lalloc(void)
{
return (struct line *)malloc(sizeof(struct line));
}
char *walloc(char *w)
{
char *p;
if((p = (char *)malloc(strlen(w)+1)) != NULL)
strcpy(p,w);
return p;
}
int getword(char *word,int max)
{
int c,getch(void);
void ungetch(int);
char *w = word;
while(isspace(c = getch()))
if(c == '\n')
count_line++;
if( c != EOF)
*w++ = c;
if(!isalpha(c)){
*w = '\0';
return c;
}
for(;--max; w++)
if(!isalnum(*w = getch()))
break;
if(*w != '\'')
ungetch(*w);
else{
w++;
for(;--max;w++)
if(!isalnum(*w = getch())){
ungetch(*w);
break;
}
}
*w = '\0';
return word[0];
}
int Nonrealword(char *s)
{
int i;
static char *list[] = {
"a","an","and","be","but","by","he","I","is","off","on",
"she","so","the","they","you",NULL
};
i = 0;
while(list[i]){
if(strcmp(list[i],s) == 0)
return 0;
i++;
}
return 1;
}
#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 <stdlib.h>
#include <string.h>
#include <ctype.h>
#define MAXWORD 100
/*Author Stat: founded 2017 11 26*/
static int count_line = 1;
struct line{
int line;
struct line *next;
};
struct tnode{
char *word;
struct line *start;
struct tnode *next;
};
struct tnode *addItem(struct tnode *,char *);
struct line *addlines(struct line *);
void showItem(struct tnode *);
void showLine(struct line *);
struct tnode *Ialloc(void);
struct line *Lalloc(void);
char *walloc(char *);
int getword(char *,int);
int Nonrealword(char *);
int main(void)
{
struct tnode *head;
char word[MAXWORD];
head = NULL;
while(getword(word,MAXWORD) != EOF)
if(Nonrealword(word))
if(isalpha(word[0]))
head = addItem(head,word);
printf("%33s\n\n","Words Line Number");
showItem(head);
return 0;
}
struct tnode *addItem(struct tnode *p,char *w)
{
if(p == NULL){
if((p = Ialloc()) == NULL){
printf("Out of memory!\n");
exit(EXIT_FAILURE);
}
if((p->word = walloc(w)) == NULL){
printf("Out of memory!\n");
exit(EXIT_FAILURE);
}
if((p->start = Lalloc()) == NULL){
printf("Out of memory!\n");
exit(EXIT_FAILURE);
}
p->start->line = count_line;
p->start->next = NULL;
p->next = NULL;
}else if(strcmp(p->word,w) == 0)
p->start = addlines(p->start);
else
p->next = addItem(p->next,w);
return p;
}
struct line *addlines(struct line *p)
{
if(p == NULL){
if((p = Lalloc()) == NULL){
printf("Out of memory!\n");
exit(EXIT_FAILURE);
}
p->line = count_line;
p->next = NULL;
}else if(p->line == count_line)
;
else
p->next = addlines(p->next);
return p;
}
void showItem(struct tnode *p)
{
if(p != NULL){
printf("%20s ",p->word);
showLine(p->start);
printf("\n");
showItem(p->next);
free(p);
}
}
void showLine(struct line *p)
{
if(p != NULL){
printf("%d ",p->line);
showLine(p->next);
free(p);
}
}
struct tnode *Ialloc(void)
{
return (struct tnode *)malloc(sizeof(struct tnode));
}
struct line *Lalloc(void)
{
return (struct line *)malloc(sizeof(struct line));
}
char *walloc(char *w)
{
char *p;
if((p = (char *)malloc(strlen(w)+1)) != NULL)
strcpy(p,w);
return p;
}
int getword(char *word,int max)
{
int c,getch(void);
void ungetch(int);
char *w = word;
while(isspace(c = getch()))
if(c == '\n')
count_line++;
if( c != EOF)
*w++ = c;
if(!isalpha(c)){
*w = '\0';
return c;
}
for(;--max; w++)
if(!isalnum(*w = getch()))
break;
if(*w != '\'')
ungetch(*w);
else{
w++;
for(;--max;w++)
if(!isalnum(*w = getch())){
ungetch(*w);
break;
}
}
*w = '\0';
return word[0];
}
int Nonrealword(char *s)
{
int i;
static char *list[] = {
"a","an","and","be","but","by","he","I","is","off","on",
"she","so","the","they","you",NULL
};
i = 0;
while(list[i]){
if(strcmp(list[i],s) == 0)
return 0;
i++;
}
return 1;
}
#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-3 编写一个交叉引用程序,打印文档中所有单词的列表,并且每个单词还有一 个列表,记录出现过该单词的行号。对 the、and 等非实义单词不予考虑。
- 练习6-3 编写一个交叉引用程序,打印文档中所有单词的列表(答案程序)
- 打印文本中的所有单词,并且打印每个单词出现的行号,非实义单词不考虑(TCPL,练习6-3)
- 练习 6-4 编写一个程序,根据单词的出现频率按降序打印输入的各个不同单词,并在 每个单词的前面标上它的出现次数
- List列表实现统计文档中单词出现的次数
- Eclipse中鼠标左键点中一个单词,自动出现该单词的所有位置
- 编写一个程序,打印输入中单词长度的直方图
- 编写一个程序,打印输入中单词长度的直方图
- 统计一TXT文档中单词出现频率,输出频率最高的10个单词
- 给出一个2D板和字典中的单词列表,找到棋盘上的所有单词。每个单词必须由顺序相邻单元格的字母构成。不能重复使用
- 练习1-13 编写一个程序,打印输入中单词长度的直方图
- 练习1-13 编写一个程序,打印输入中单词长度的直方图(垂直)
- 【第六周:列表与元组】#根据单词的长度对一个单词列表进行排序
- 计算出一字符串(字符串中每个单词之间有一个或多个空格)中每个单词的 出现的次数
- 有一个string,写一个函数,要求输出这个string中每个单词出现的次数的列表
- 找到一个单词的所有相似单词
- 用户输入一个单词,要求在一个字典中找出该单词的所有兄弟单词,并输出
- 用户输入一个单词,要求在一个字典中找出该单词的所有兄弟单词,并输出
- Windows服务器IIS搭建网站
- AttributeError: '_csv.reader' object has no attribute 'next' 使用pyhon3.2以上运行以下代码时报错
- SRM 719 div1 Medium (树形dp)
- C++变量名 命名规则
- QT自定义槽
- 练习 6-3 编写一个交叉引用程序,打印文档中所有单词的列表,并且每个单词还有一 个列表,记录出现过该单词的行号。对 the、and 等非实义单词不予考虑。
- 周末训练笔记+POJ 1811 Prime Test【大素数判定】【素因子分解】
- 模板-并查集
- 工作周报-2017年11月26日
- 超链接实现post方式提交
- 1.使用canvas实现一个简单的时钟
- 简单的文件操作
- 2017年11月26日训练笔记
- 怎么利用qq来获取好友ip地址