C //练习 6-2 编写一个程序,用以读入一个C语言程序,并按字母表顺序分组打印变量名,要求每一组内各变量名的前6个字符相同,其余字符不同。字符串和注释中的单词不予考虑。请将6作为一个可在命令行

发布时间:2024年01月19日

C程序设计语言 (第二版) 练习 6-2

练习 6-2 编写一个程序,用以读入一个C语言程序,并按字母表顺序分组打印变量名,要求每一组内各变量名的前6个字符相同,其余字符不同。字符串和注释中的单词不予考虑。请将6作为一个可在命令行中设定的参数。

注意:代码在win32控制台运行,在不同的IDE环境下,有部分可能需要变更。
IDE工具:Visual Studio 2010

?

代码块:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#define MAXWORD 100
#define NKEYS (sizeof keytab / sizeof(keytab[0]))
#define MAXOP 100
#define NUMBER '0'
#define MAXVAL 100
#define BUFSIZE 100
#define VAR '1'
#define DEFAULTCOMPARISON 6
#define PRINTPREVIOUS_YES 1
#define PRINTPREVIOUS_NO 0
#define PRINTPREVIOUS_NEWGROUP 2


char buf[BUFSIZE];
int bufp = 0;


struct key{
	char *word;
	int count;
};

struct tnode{
	char *word;
	int count;
	struct tnode *left;
	struct tnode *right;
};

int getch(void){
	return (bufp > 0) ? buf[--bufp] : getchar();
}

void ungetch(int c){
	if(bufp >= BUFSIZE){
		printf("Ungetch! Too many characters!\n");
	}
	else{
		buf[bufp++] = c;
	}
}

int getword(char *word, int lim) {
    int c;
    char *w = word;
    static int line_beg = 1;
    static int after_slash = 0;
    int after_star = 0;

    if(isspace(c = getch())){
        after_slash = 0;
	}
    while(isspace(c)){
        if(c == '\n'){
            line_beg = 1;
		}
        c = getch();
    }

    if(c != EOF){
        *w++ = c;
	}

    if(c == '#' && line_beg == 1){
        while((c = getch()) != '\n' && c != EOF)
            ;
        return getword(word, lim);
    }

    line_beg = 0;

    if(c == '\\'){
        after_slash = after_slash ? 0 : 1;
	}
    else if(c == '/' ){
        if((c = getch()) == '*' && !after_slash){
            while((c = getch()) != EOF){
                if(c == '/'){
                    if(after_star){
                        return getword(word, lim);
					}
                }
                else if(c == '*' && !after_slash){
                    after_star = 1;
				}
                else if(c == '\\'){
                    after_slash = after_slash ? 0 : 1;
				}
                else{
                    after_star = 0;
                    after_slash = 0;
                }
            }
        }

        after_slash = 0;
        if(c != EOF){
            ungetch(c);
		}
    }

    else if(c == '\"'){
        if(!after_slash){
            --w;
            while((c = getch()) != EOF){
                if(c == '\"' && !after_slash){
                    break;
				}
                else if(c == '\\'){
                    after_slash = after_slash ? 0 : 1;
				}
                else{
                    after_slash = 0;
				}
                *w++ = c;
            }
            *w = '\0';
            if(c == EOF){
                return EOF;
			}
            else{
                return getword(word, lim);
			}
        }
        after_slash = 0;
    }

    if(!isalpha(c) && c != '_'){
        *w = '\0';
        if(c != '\\'){
            after_slash = 0;
		}
        return c;
    }

    after_slash = 0;

    for( ; --lim > 0; ++w)
        if(!isalnum(*w = getch()) && *w != '_'){
            ungetch(*w);
            break;
        }
    *w = '\0';
    return word[0];
}

int binsearch(char *word, struct key tab[], int n){
	int cond;
	int low, high, mid;

	low = 0;
	high = n - 1;
	while(low <= high){
		mid = (low + high) / 2;
		if((cond = strcmp(word, tab[mid].word)) < 0){
			high = mid - 1;
		}
		else if(cond > 0){
			low = mid + 1;
		}
		else{
			return  mid;
		}
	}
	return -1;
}

struct tnode *talloc(void){
	return (struct tnode*)malloc(sizeof(struct tnode));
}

char *strup(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){
	int cond;

	if(p == NULL){
		p = talloc();
		p->word = strup(w);
		p->count = 1;
		p->left = p->right = NULL;
	}
	else if((cond = strcmp(w, p->word)) == 0){
		p->count++;
	}
	else if(cond < 0){
		p->left = addtree(p->left, w);
	}
	else{
		p->right = addtree(p->right, w);
	}
	return p;
}

void treeprint(struct tnode *p, int n) {
    static int printPrevious = 1;
    static struct tnode *previous;

    if (p != NULL) {
        treeprint(p->left, n);

        if (n == 0){
            printf("%4d %s\n", p->count, p->word);
		}
        else{
            if(previous != NULL){
                if (strncmp(previous->word, p->word, n) == 0){
                    if(printPrevious == PRINTPREVIOUS_NEWGROUP){
                        printf("\n%4d %s\n", previous->count, previous->word);
                        printPrevious = PRINTPREVIOUS_NO;
                    }
					else if(printPrevious == PRINTPREVIOUS_YES){
                        printf("%4d %s\n", previous->count, previous->word);
                        printPrevious = PRINTPREVIOUS_NO;
                    }
                    printf("%4d %s\n", p->count, p->word);
                }
				else{
                    printPrevious = PRINTPREVIOUS_NEWGROUP;
				}
            }
            previous = p;
        }
        treeprint(p->right, n);
    }
}

int main(int argc, char *argv[]){
    struct tnode *root;
    char word[MAXWORD];
    int numCharactersMatch;

    if(argc == 1){
        numCharactersMatch = DEFAULTCOMPARISON;
	}
    else if(argc == 2){
        numCharactersMatch = atoi(argv[1]);
	}
    else{
        printf("Incorrect number of arguments.\n");
        return 1;
    }

    root = NULL;
    while(getword(word, MAXWORD) != EOF){
        if (isalpha(word[0])){
            root = addtree(root, word);
		}
	}
    treeprint(root, numCharactersMatch);

	system("pause");
	return 0;
}
文章来源:https://blog.csdn.net/navicheung/article/details/135643153
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。