统计C语言关键字出现次数

《C程序设计语言》K&R版本第6章结构6.3结构数组内容

/*
Name: 统计c语言关键字出现次数
Copyright:
Author: lingr7
Date: 21/10/18 10:05
Description: 完全根据《C程序设计语言》K&R版本6.3结构数组内容编写。在dev c++5.11中编译运行成功
一个项目文件.dev,内含calc.h,getch.c,getop.c,keytab.h,tongjimain.c.4个子文件
关键字结构数组在keytab.h中定义,可以自行修改该表,但要注意,这个数组里顺序必须是字典序升序。
*/ /*tongjimain.c*/
#include <stdio.h>
#include <ctype.h>
#include <string.h> #include "calc.h"
#include "keytab.h" #define MAXWORD 100 int getword(char *, int);
int binsearch(char *, struct key *, int);
/*统计输入中c语言关键字的出现次数*/
main(){
int n;
char word[MAXWORD];
printf("%s %s %s", keytab[0].word, keytab[1].word, keytab[2].word);
while (getword(word,MAXWORD) != EOF)/*因为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);
return 0;
}
/*折半查找*/
/*2018年10月20日
lingr7*/
/* binsearch函数 :在tab[0]到tab[n-1]中查找单词 */
int binsearch(char *word, struct key tab[], int n){
int cond;
int low, high, mid; low = 0;
high = n - 1;
while (low <= high){
mid = (high+low) / 2;
if ((cond = strcmp(word, tab[mid].word)) < 0)
high = mid - 1;
else if (cond > 0)
low = mid + 1;
else /*找到了匹配的值*/
return mid;
}
return -1; /*没有匹配的值*/
}
/* getword函数:从输入中读取下一个单词或字符*/
int getword(char *word, int lim){
int c, getch(void);
void ungetch(int);
char *w = word; while (isspace(c = getch()))
;
if (c != EOF)
*w++ = c;
if (!isalpha(c)) {
*w = '\0';
return c;
}
for ( ; --lim > 0; w++)
if (!isalnum(*w = getch())){
ungetch(*w);/**/
break;
}
*w = '\0';
return word[0];
}
/*calc.h*/
#define NUMBER '0' /*void push(double);*/
/*double pop(void);*/ int getop(char []); int getch(void);
void ungetch(int);
/*keytab.h*/
#define NKEYS ( sizeof keytab / sizeof(struct key))
/*结构初始化*/
/*最好声明为外部变量*/
struct key {
char *word;
int count;
} keytab[] ={
"auto", 0,
"break", 0,
"case", 0,
"char", 0,
"const", 0,
"continue", 0,
"default", 0,
"main", 0,
"unsigned", 0,
"void", 0,
"volatile", 0,
"while", 0,
};
/*getch.c*/
#include <stdio.h>
#define BUFSIZE 100 char buf[BUFSIZE]; /* buffer for ungetch */
int bufp = 0; /* next free position in buf */ int getch(void) /* get a (possibly pushed-back) character */
{
return (bufp > 0) ? buf[--bufp] : getchar();
} void ungetch(int c) /* push character back on input */
{
if (bufp >= BUFSIZE)
printf("ungetch: too many characters\n");
else
buf[bufp++] = c;
}
/*getop.c*/
#include <stdio.h>
#include <ctype.h>
#include "calc.h" /* getop: get next character or numeric operand */
int getop(char s[])
{
int i, c; while ((s[0] = c = getch()) == ' ' || c == '\t')
;
s[1] = '\0';
if (!isdigit(c) && c != '.')
return c; /* not a number */
i = 0;
if (isdigit(c)) /* collect integer part */
while (isdigit(s[++i] = c = getch()))
;
if (c == '.') /* collect fraction part */
while (isdigit(s[++i] = c = getch()))
;
s[i] = '\0';
if (c != EOF)
ungetch(c);
return NUMBER;
}

统计C语言关键字出现次数的更多相关文章

  1. 李洪强漫谈iOS开发[C语言-009] - C语言关键字

    // //  main.m //  04 - C语言关键字 // //  Created by vic fan on 16/7/12. //  Copyright © 2016年 李洪强. All r ...

  2. FILE不是C语言关键字

    FILE不是C语言关键字,只是标准C中的标准输入输出中定义的一个新的数据类型 stdio.htypedef struct _iobuf{ char* _ptr; int _cnt; char* _ba ...

  3. Linux 统计某个字符串出现的次数

    要统计一个字符串出现的次数,这里现提供自己常用两种方法: 1. 使用vim统计 用vim打开目标文件,在命令模式下,输入 :%s/objStr//gn 即可 2. 使用grep: grep -o ob ...

  4. C语言关键字-volatile

    1.C语言关键字volatile     C 语言关键字volatile(注意它是用来修饰变量而不是上面介绍的__volatile__)表明某个变量的值可能在外部被改变,因此对这些变量的存取 不能缓存 ...

  5. c语言关键字总结

    1.关键字变更历史 1999年12月16日,ISO推出了C99标准,该标准新增了5个C语言关键字: inline restrict _Bool _Complex _Imaginary(注意bool 从 ...

  6. Java利用正则表达式统计某个字符串出现的次数

    //统计某个字符出现的次数 private void countSubString(){ String string1="香蕉.玉米.面粉"; String string2=&qu ...

  7. 1.C语言关键字(auto break case char const swtich)

    ANSI C标准C语言共有32个关键字,分别为: auto break case char const continue default do double else enum extern floa ...

  8. 统计C语言程序行数

    补充前一篇中统计C语言程序行数的程序 写得比较匆忙,可能有些失误,等弄明白GitHub的用法并完善程序后再补充完整代码链接 没有写成函数,但经过简单修改可以作为一个计算或判断函数使用 判断算法主要为以 ...

  9. C/C++ 知识点---C语言关键字(32个)

    C/C++ 知识点 1.C语言关键字(32个) <1>.基本数据类型 [5] void :声明函数无返回值或无参数,声明空类型指针 char :声明字符型变量 int :声明整形变量 fl ...

随机推荐

  1. ubuntu 无法应用原保存的显示器配置

    打开ubuntu之后的开启页面出现: 所选模式均不匹配可能的模式: 为 CRTC 63 尝试模式 CRTC 63:尝试 800x600@60Hz 模式输出在 1366x768@60Hz (通过 0) ...

  2. Python list, dict, set, tuple

    list方法 append: 添加一个新的元素到末尾 extend: 扩展元素 insert: 在任何位置插入元素 pop: 弹出末尾的元素 remove: remove first occurren ...

  3. javascript之 JavaScript 工具库

    javascript之 JavaScript 工具库jQuery 目录: 一.查找标签和事件绑定以及操作标签的对比 二.DOM对象和jquery的转换 三.$(document).ready( )  ...

  4. 编程中的多字节和Unicode

    在编译许多程序的时候,我们常常会出现诸如指针转换错误或者const char[] 不能转换成XX的错误,这时很可能就是项目编码的问题了,如果您使用的是VS编程环境,那么打开工程属性,里面就有个选项是给 ...

  5. Unity中的输入

    目录 移动平台的输入 触摸 触摸相关的函数 触摸的一个示例 重力加速器 在Unity中访问重力加速器的信息 重力加速器示例 虚拟键盘 其他输入 传统的输入 鼠标,键盘,控制杆,手柄 虚拟控制轴(Vir ...

  6. HDU4302 线段树

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4302 , 可以用线段树,也可以STL中的map,multiset,优先队列中的任何一个解决(可我只会线 ...

  7. animation写动画

    最近,接到项目需求,需要写大量的动画,那么怎么写呢? 动画是使元素从一种样式逐渐变化为另一种样式的效果.可以用百分比来规定变化发生的时间,或用关键词 "from" 和 " ...

  8. Hadoop集群批量命令执行

    ./pdsh -R ssh -w node-10-0[0-5] hostname -R:指定传输方式,默认为rsh,本例为ssh,如果希望ssh传输需要另行安装pdsh-rcmd-ssh,如果希望ss ...

  9. Bootstrap历练实例:响应式导航(带有表单)

    <!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...

  10. C# 理解FileInfo类的Open()方法

    我们在对文件进行读写操作之前必须打开文件,FileInfo类为我们提供了一个Open()方法,该方法包含了两个枚举类型值的参数,一个为FileMode枚举类型值,另一个为FileAccess枚举类型值 ...