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

相关文章

Java实现将HTML文件与字符串转换为图片

《Java实现将HTML文件与字符串转换为图片》在Java开发中,我们经常会遇到将HTML内容转换为图片的需求,本文小编就来和大家详细讲讲如何使用FreeSpire.DocforJava库来实现这一功... 目录前言核心实现:html 转图片完整代码场景 1:转换本地 HTML 文件为图片场景 2:转换 H

C++统计函数执行时间的最佳实践

《C++统计函数执行时间的最佳实践》在软件开发过程中,性能分析是优化程序的重要环节,了解函数的执行时间分布对于识别性能瓶颈至关重要,本文将分享一个C++函数执行时间统计工具,希望对大家有所帮助... 目录前言工具特性核心设计1. 数据结构设计2. 单例模式管理器3. RAII自动计时使用方法基本用法高级用法

GO语言中函数命名返回值的使用

《GO语言中函数命名返回值的使用》在Go语言中,函数可以为其返回值指定名称,这被称为命名返回值或命名返回参数,这种特性可以使代码更清晰,特别是在返回多个值时,感兴趣的可以了解一下... 目录基本语法函数命名返回特点代码示例命名特点基本语法func functionName(parameters) (nam

Python Counter 函数使用案例

《PythonCounter函数使用案例》Counter是collections模块中的一个类,专门用于对可迭代对象中的元素进行计数,接下来通过本文给大家介绍PythonCounter函数使用案例... 目录一、Counter函数概述二、基本使用案例(一)列表元素计数(二)字符串字符计数(三)元组计数三、C

Java使用正则提取字符串中的内容的详细步骤

《Java使用正则提取字符串中的内容的详细步骤》:本文主要介绍Java中使用正则表达式提取字符串内容的方法,通过Pattern和Matcher类实现,涵盖编译正则、查找匹配、分组捕获、数字与邮箱提... 目录1. 基础流程2. 关键方法说明3. 常见场景示例场景1:提取所有数字场景2:提取邮箱地址4. 高级

Python中的filter() 函数的工作原理及应用技巧

《Python中的filter()函数的工作原理及应用技巧》Python的filter()函数用于筛选序列元素,返回迭代器,适合函数式编程,相比列表推导式,内存更优,尤其适用于大数据集,结合lamb... 目录前言一、基本概念基本语法二、使用方式1. 使用 lambda 函数2. 使用普通函数3. 使用 N

MySQL中REPLACE函数与语句举例详解

《MySQL中REPLACE函数与语句举例详解》在MySQL中REPLACE函数是一个用于处理字符串的强大工具,它的主要功能是替换字符串中的某些子字符串,:本文主要介绍MySQL中REPLACE函... 目录一、REPLACE()函数语法:参数说明:功能说明:示例:二、REPLACE INTO语句语法:参数

Python 字符串裁切与提取全面且实用的解决方案

《Python字符串裁切与提取全面且实用的解决方案》本文梳理了Python字符串处理方法,涵盖基础切片、split/partition分割、正则匹配及结构化数据解析(如BeautifulSoup、j... 目录python 字符串裁切与提取的完整指南 基础切片方法1. 使用切片操作符[start:end]2

基于Python编写自动化邮件发送程序(进阶版)

《基于Python编写自动化邮件发送程序(进阶版)》在数字化时代,自动化邮件发送功能已成为企业和个人提升工作效率的重要工具,本文将使用Python编写一个简单的自动化邮件发送程序,希望对大家有所帮助... 目录理解SMTP协议基础配置开发环境构建邮件发送函数核心逻辑实现完整发送流程添加附件支持功能实现htm

python中update()函数的用法和一些例子

《python中update()函数的用法和一些例子》update()方法是字典对象的方法,用于将一个字典中的键值对更新到另一个字典中,:本文主要介绍python中update()函数的用法和一些... 目录前言用法注意事项示例示例 1: 使用另一个字典来更新示例 2: 使用可迭代对象来更新示例 3: 使用