本文介绍: 【代码】C //练习 6-3 编写一个交叉引用程序,打印文档中所有单词的列表,并且每个单词还有一个列表,记录出现过该单词的行号。对the、and等非实义单词不予考虑。
C程序设计语言 (第二版) 练习 6-3
练习 6-3 编写一个交叉引用程序,打印文档中所有单词的列表,并且每个单词还有一个列表,记录出现过该单词的行号。对the、and等非实义单词不予考虑。
注意:代码在win32控制台运行,在不同的IDE环境下,有部分可能需要变更。
IDE工具:Visual Studio 2010
代码块:
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXWORD 100
#define BUFSIZE 100
char buf[BUFSIZE];
int bufp = 0;
long lineNumber = 1;
struct tnode{
char *word;
struct tnode *left;
struct tnode *right;
int count;
long lineNumbers[];
};
int judge(char *s){
int found = 0;
int giveup = 0;
char *list[] = {"a", "an", "and", "be", "but", "by", "he", "I", "is", "it", "of", "off", "on", "she", "so", "the", "they", "to", "you"};
int top = sizeof list / sizeof list[0] - 1;
int bottom = 0;
int guess = top / 2;
int diff = 0;
if(s != NULL){
while(!found && !giveup){
diff = strcmp(list[guess], s);
if(0 == diff){
found = 1;
}
else if(0 < diff){
top = guess - 1;
}
else{
bottom = guess + 1;
}
if(top < bottom){
giveup = 1;
}
else{
guess = (top + bottom) / 2;
}
}
}
return found;
}
struct tnode *talloc(void) {
return (struct tnode *) malloc(sizeof(struct tnode));
}
char *_strdup(char *s) {
char *p;
p = (char *) malloc(strlen(s) + 1);
if (p != NULL)
strcpy(p, s);
return p;
}
struct tnode *addtree(struct tnode *p, char *w, long lineNumber) {
int i, cond;
if(p == NULL){
p = talloc();
p->word = _strdup(w);
p->count = 1;
p->lineNumbers[p->count - 1] = lineNumber;
p-> left = p->right = NULL;
}
else{
cond = strcmp(w, p->word);
if(cond == 0){
for(i = 0; i < p->count; i++){
if(lineNumber == p->lineNumbers[i]){
return p;
}
}
p->lineNumbers[p->count] = lineNumber;
p->count += 1;
}
else if(cond < 0){
p->left = addtree(p->left, w, lineNumber);
}
else{
p->right = addtree(p->right, w, lineNumber);
}
}
return p;
}
void treeprint(struct tnode *p) {
int i;
if(p != NULL){
treeprint(p->left);
if(p->count > 1 && strlen(p->word) > 1){
printf("%12s:t", p->word);
for(i = 0; i < p->count; i++){
printf("%lu%s", p->lineNumbers[i], (i == p->count - 1) ? "n" : ", ");
}
}
treeprint(p->right);
}
}
int getch(void){
return (bufp > 0) ? buf[--bufp] : getchar();
}
void ungetch(int c){
if (bufp >= BUFSIZE){
printf("ungetch: too many charactersn");
}
else{
buf[bufp++] = c;
}
}
int getword(char *word, int lim){
int c;
char *w = word;
while(isspace(c = getch())){
if (c == 'n'){
lineNumber++;
}
}
if(c != EOF){
*w++ = c;
}
if(!isalpha(c)){
*w = '';
return c;
}
for(;--lim > 0; w++){
if(!isalpha(*w = getch())){
ungetch(*w);
break;
}
}
*w = '';
return word[0];
}
int main(){
struct tnode *root;
char word[MAXWORD];
root = NULL;
while (getword(word, MAXWORD) != EOF){
if (isalpha(word[0]) && !judge(word)){
root = addtree(root, word, lineNumber);
}
}
treeprint(root);
system("pause");
return 0;
}
原文地址:https://blog.csdn.net/navicheung/article/details/135643762
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.7code.cn/show_58304.html
如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除!
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。