C //练习 6-1 上述getword函数不能正确处理下划线、字符串常量、注释及预处理控制指令。请编写一个更完善的getword函数。

本文主要是介绍C //练习 6-1 上述getword函数不能正确处理下划线、字符串常量、注释及预处理控制指令。请编写一个更完善的getword函数。,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

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

练习 6-1 上述getword函数不能正确处理下划线、字符串常量、注释及预处理控制指令。请编写一个更完善的getword函数。

注意:代码在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'char buf[BUFSIZE];
int bufp = 0;struct key{char *word;int count;
};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; /* 1 at beginning of a new line */static int after_slash = 0; /* 1 after '\' */int after_star = 0; /* 1 after '*' */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) { /* Preprocessor directive */while((c = getch()) != '\n' && c != EOF) /* Go to end of line */;return getword(word, lim); /* Start over */}line_beg = 0;if(c == '\\') /* Set after_slash flag */after_slash = after_slash ? 0 : 1; /* Ignore '\\' comment */else if(c == '/' ) {if((c = getch()) == '*' && !after_slash) { /* Begin comment */while((c = getch()) != EOF) {if(c == '/') {if(after_star) /* End comment */return getword(word, lim); /* Start over */}else if(c == '*' && !after_slash)after_star = 1;else if(c == '\\')after_slash = after_slash ? 0 : 1; /* Ignore '\\' comments */else {after_star = 0;after_slash = 0;}}} /* End comment */after_slash = 0; /* Not after slash anymore */if(c != EOF)ungetch(c);}else if(c == '\"') {if(!after_slash) { /* String literal */--w; /* Reset w */while((c = getch()) != EOF) {if(c == '\"' && !after_slash)break;else if(c == '\\')after_slash = after_slash ? 0 : 1; /* Ignore '\\' comments */elseafter_slash = 0;*w++ = c;}*w = '\0';if(c == EOF)return EOF;elsereturn getword(word, lim); /* Start over. */}after_slash = 0; /* Not after a slash anymore. */}if(!isalpha(c) && c != '_') { /* It's a symbol. */*w = '\0';if(c != '\\')after_slash = 0;return c;}/* Reset this flag since a slash would have just returned. */after_slash = 0;for( ; --lim > 0; ++w) /* It's a word or letter. */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;
}int main(){struct key keytab[] = {{"auto", 0}, {"break", 0}, {"case", 0}, {"char", 0}, {"const", 0}, {"continue", 0}, {"default", 0},{"do", 0}, {"double", 0}, {"else", 0}, {"enum", 0}, {"extern", 0}, {"float", 0}, {"for", 0}, {"goto", 0}, {"if", 0}, {"int", 0},{"long", 0}, {"register", 0}, {"return", 0}, {"short", 0}, {"signed", 0}, {"sizeof", 0}, {"static", 0}, {"struct", 0}, {"switch", 0},{"typedef", 0}, {"unsigned", 0}, {"union", 0}, {"void", 0}, {"volatile", 0}, {"while", 0}};int n;char word[MAXWORD];while(getword(word, MAXWORD) != EOF){if(isalpha(word[0])){if((n = binsearch(word, keytab, NKEYS)) >= 0){keytab[n].count++;}}}for(n = 0; n < NKEYS; n++){if(keytab[n].count > 0){printf("%4d %s\n", keytab[n].count, keytab[n].word);}}system("pause");return 0;
}

这篇关于C //练习 6-1 上述getword函数不能正确处理下划线、字符串常量、注释及预处理控制指令。请编写一个更完善的getword函数。的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/619100

相关文章

Python函数作用域与闭包举例深度解析

《Python函数作用域与闭包举例深度解析》Python函数的作用域规则和闭包是编程中的关键概念,它们决定了变量的访问和生命周期,:本文主要介绍Python函数作用域与闭包的相关资料,文中通过代码... 目录1. 基础作用域访问示例1:访问全局变量示例2:访问外层函数变量2. 闭包基础示例3:简单闭包示例4

Python实现字典转字符串的五种方法

《Python实现字典转字符串的五种方法》本文介绍了在Python中如何将字典数据结构转换为字符串格式的多种方法,首先可以通过内置的str()函数进行简单转换;其次利用ison.dumps()函数能够... 目录1、使用json模块的dumps方法:2、使用str方法:3、使用循环和字符串拼接:4、使用字符

Python中isinstance()函数原理解释及详细用法示例

《Python中isinstance()函数原理解释及详细用法示例》isinstance()是Python内置的一个非常有用的函数,用于检查一个对象是否属于指定的类型或类型元组中的某一个类型,它是Py... 目录python中isinstance()函数原理解释及详细用法指南一、isinstance()函数

python中的高阶函数示例详解

《python中的高阶函数示例详解》在Python中,高阶函数是指接受函数作为参数或返回函数作为结果的函数,下面:本文主要介绍python中高阶函数的相关资料,文中通过代码介绍的非常详细,需要的朋... 目录1.定义2.map函数3.filter函数4.reduce函数5.sorted函数6.自定义高阶函数

Python 常用数据类型详解之字符串、列表、字典操作方法

《Python常用数据类型详解之字符串、列表、字典操作方法》在Python中,字符串、列表和字典是最常用的数据类型,它们在数据处理、程序设计和算法实现中扮演着重要角色,接下来通过本文给大家介绍这三种... 目录一、字符串(String)(一)创建字符串(二)字符串操作1. 字符串连接2. 字符串重复3. 字

Python中的sort方法、sorted函数与lambda表达式及用法详解

《Python中的sort方法、sorted函数与lambda表达式及用法详解》文章对比了Python中list.sort()与sorted()函数的区别,指出sort()原地排序返回None,sor... 目录1. sort()方法1.1 sort()方法1.2 基本语法和参数A. reverse参数B.

docker编写java的jar完整步骤记录

《docker编写java的jar完整步骤记录》在平常的开发工作中,我们经常需要部署项目,开发测试完成后,最关键的一步就是部署,:本文主要介绍docker编写java的jar的相关资料,文中通过代... 目录all-docker/生成Docker打包部署文件配置服务A的Dockerfile (a/Docke

Java 字符串操作之contains 和 substring 方法最佳实践与常见问题

《Java字符串操作之contains和substring方法最佳实践与常见问题》本文给大家详细介绍Java字符串操作之contains和substring方法最佳实践与常见问题,本文结合实例... 目录一、contains 方法详解1. 方法定义与语法2. 底层实现原理3. 使用示例4. 注意事项二、su

Python函数的基本用法、返回值特性、全局变量修改及异常处理技巧

《Python函数的基本用法、返回值特性、全局变量修改及异常处理技巧》本文将通过实际代码示例,深入讲解Python函数的基本用法、返回值特性、全局变量修改以及异常处理技巧,感兴趣的朋友跟随小编一起看看... 目录一、python函数定义与调用1.1 基本函数定义1.2 函数调用二、函数返回值详解2.1 有返

Python Excel 通用筛选函数的实现

《PythonExcel通用筛选函数的实现》本文主要介绍了PythonExcel通用筛选函数的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着... 目录案例目的示例数据假定数据来源是字典优化:通用CSV数据处理函数使用说明使用示例注意事项案例目的第一