zoukankan      html  css  js  c++  java
  • K&R练习题6-1统计关键词出现的次数

    这道练习题训练了:

    1.结构体数组

    2.二分查找

    3.指针操作

    ----

    都不难。但非常基础,我认为非常好,做完了记到博客上来,题目见k&R,实现例如以下:

    /*
     * Practice of struct array. K&R 6-1
     * @author : wusuopubupt
     * @date   : 2014-09-18
     */
    
    #include <stdio.h>
    #include <ctype.h>
    #include <string.h>
    
    #define MAXWORD 100
    
    typedef struct key_{
    	char *word;
    	int count;
    } key;
    
    key key_tab[] = {
    	{"auto", 0},
    	{"break", 0},
    	{"case", 0},
    	{"char", 0},
    	{"const", 0},
    	{"continue", 0},
    	{"default", 0},
    	{"for", 0},
    	{"int", 0},
    	{"void", 0},
    	{"while", 0}
    };
    
    int getword(char *word, int n);
    int binary_search(key key_tab[], char *word, int n);
    key *binary_search2(key *key_tab, char *word, int n);
    
    int getword(char *word, int n) {
    	int c;
    	char *w = word;
    
    	while(isspace(c = getchar())) {
    		;
    	}
    	if(c != EOF) {
    		*w++ = c;
    	}
    	if(!isalpha(c)) {
    		*w = '';
    		return c;
    	}
    	while(n > 0) {
    		c = getchar();
    		if(isalnum(c)) {
    			*w++ = c;
    		}
    		else {
    			break;
    		}
    		n--;
    	}
    
    	*w = '';
    	return w[0];
    }
    
    int binary_search(key key_tab[], char *word, int n) {
    	int low = 0;
    	int high = n-1;
    	int mid;
    	int result;
    	while(low <= high) {
    		mid = (low+high) / 2;
    		result = strcmp(word, key_tab[mid].word);
    		if(result < 0) {
    			high = mid-1;
    		}
    		else if(result > 0) {
    			low = mid + 1;
    		}
    		else {
    			return mid;
    		}
    	}
    	return -1;
    }
    
    /* implemented with pointer */
    key *binary_search2(key *key_tab, char *word, int n) {
    	key *low = key_tab;
    	key *high = key_tab + n-1;
    	key *mid;
    	int result;
    	while(low <= high) {
    		//mid = (low+high) / 2; /* error: can not use pointer+pointer d*/
    		mid = low + (high-low) / 2; /* pointer + num */
    		result = strcmp(word, mid->word);
    		if(result < 0) {
    			high = mid-1;
    		}
    		else if(result > 0) {
    			low = mid + 1;
    		}
    		else {
    			return mid;
    		}
    	}
    	return NULL;
    }
    
    int main1() {
    	int i;
    	int n_keys = sizeof(key_tab) / sizeof(key_tab[0]);
    	char word[MAXWORD];
    
    	while(getword(word, MAXWORD) != EOF) {
    		if(isalpha(word[0])) {
    			if((i = binary_search(key_tab, word, n_keys)) >= 0) {
    				key_tab[i].count++;
    			}
    		}
    	}
    
    	i = 0;
    	while(i < n_keys) {
    		printf("%s : %d
    ", key_tab[i].word, key_tab[i].count);
    		i++;
    	}
    
    	return 0;
    }
    
    int main() {
    	int n_keys = sizeof(key_tab) / sizeof(key_tab[0]);
    	char word[MAXWORD];
    	key *k = NULL;
    
    	while(getword(word, MAXWORD) != EOF) {
    		if(isalpha(word[0])) {
    			if((k = binary_search2(key_tab, word, n_keys)) != NULL) {
    				k->count++;
    			}
    		}
    	}
    
    	int i = 0;
    	while(i < n_keys) {
    		printf("%s : %d
    ", key_tab[i].word, key_tab[i].count);
    		i++;
    	}
    
    	return 0;
    }
    



    github:https://github.com/wusuopubupt/LearningC/blob/master/K%26R/chp6/keyword_count.c



  • 相关阅读:
    CSS中一个冒号和两个冒号有什么区别
    伪类元素实现可伸缩时间轴
    Gulp实现css、js、图片的压缩以及css、js文件的MD5命名
    for 循环进化史
    细谈sass和less中的变量及其作用域
    Vue2.0源码阅读笔记--双向绑定实现原理
    你所不知道的setTimeout
    前端COOKIE与SESSION的区别
    js移动端向左滑动出现删除按钮
    推荐几款屏幕录制工具(可录制GIF)
  • 原文地址:https://www.cnblogs.com/bhlsheji/p/5211331.html
Copyright © 2011-2022 走看看