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

统计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;
}

原文地址:https://www.cnblogs.com/lingr7/p/9824223.html

时间: 2025-01-17 05:02:55

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

李洪强iOS开发之OC[016]C语言关键字

// //  main.m //  04 - C语言关键字 // //  Created by vic fan on 16/7/12. //  Copyright © 2016年 李洪强. All rights reserved. // C语言关键字 A -  数据相关 1) 基本数据类型(5个) void    空   没有的意思 char    字符 占 1个字节(char的本质也是数字) int     整数  4个字节 float   浮点数  (小数点)保留7位有效数字 double 

读书笔记-C语言关键字

001 关键字 C语言一共32个关键字 1. 声明和定义 在开始认识关键字前,必须要明白什么是声明,什么事定义: 定义:(编译器)创建一个对象,为这个对象分配一段内存并给他取上一个名字.在一个作用域内,一个变量或者对象只能定义一次,并且定以后为它分配的内存不可变: 声明:1.告诉编译器这个名字已经和一片内存匹配上了,并且这个内存是在其他地方定义的,声明可以多次:2.告诉编译器,这个名字已经占用,不能再用来定义其他变量或者对象了. 备注:定义创建了对象,并为它分配了内存,声明没有分配内存 2. 关

C语言关键字

常用c语言关键字 C 语言标准定义的32 个关键字:auto 声明自动变量,缺省时编译器一般默认为autoint 声明整型变量double 声明双精度变量long 声明长整型变量char 声明字符型变量float 声明浮点型变量short 声明短整型变量signed 声明有符号类型变量unsigned 声明无符号类型变量struct 声明结构体变量union 声明联合数据类型enum 声明枚举类型static 声明静态变量switch 用于开关语句case 开关语句分支default 开关语句中

如何统计局域网内的邮件收发次数和流量?

使用WFilter上网行为管理软件,您可以很直观的监控到局域网内的邮件收发,并且对其进行统计. 本文,我们将简单介绍WFilter的邮件统计功能. 如果需要了解邮件监控,审计,收发限制的其他方案,请参考:WFilter邮件监控方案 1. 在"分类统计"中,选择需要查看的邮件统计报表. 支持"邮件次数统计","邮件大小统计",和"邮件类型统计" 每个报表都可以选择不同的统计字段 设置不同的日期范围 可以设置饼图.柱状图.折线图.

第0004道练习题_Python统计文本里单词出现次数

Python练习题第 0004 题 https://github.com/Show-Me-the-Code/show-me-the-code 第 0004 题:任一个英文的纯文本文件,统计其中的单词出现次数. Talk is cheap, show you my code. #! /usr/bin/env python #! -*- coding: utf-8 -*- from collections import OrderedDict __author__ = 'Sophie' class

C语言关键字、标示符与注释

一. 关键字 : 关键字就是已被C语言本身使用,不能作其它用途使用的字. 例如关键字不能用作变量名.函数名等 C语言关键字一共32个关键字,比如常用的int  struct  break enum等等 关键字主要分成两类:一类是数据类型关键字 ,另一类是流程控制语句关键字 例如: 二, 标示符 标识符使用来标识源程序中的某个对象的名字的,这些对象可以是语句.数据类型.函数. 变量.常量.数组等. 标示符的命名规则 (必须遵守的法则): 只能有字母.数字.下划线构成或者是$符号,并且只能以字母或者

读取一个文件中的字符,统计每个字符出现的次数

1 //统计每个字符出现的次数 2 #include<stdio.h> 3 #include<stdlib.h> 4 #include<string.h> 5 6 int main() 7 { 8 FILE *fp_read; 9 char ch; 10 int count[26]; 11 int index; 12 fopen_s(&fp_read,"E:\\first.txt","r"); 13 memset(coun

&quot;分拣&quot; 思路 统计每个单词出现的次数

package collection.map; public class Letter { public String name; public int count; } package collection.map; /* * 统计每个单词出现的次数 * "分拣" 思路 * 1.为所有key创建容器 * 之后容器中存放对应value * 2.第一次创建容器,并存放值value * 第二次之后,直接使用容器存放值 */ import java.util.HashMap; import

利用servlet转发技术实现统计form表单中字母次数

需求是利用servlet转发技术,实现对html网页中用户输入的内容进行统计,统计每个字母出现的次数,忽略大小写.其中统计功能在一个servlet中,转发功能在另一个servlet中. 1.新建dynamic web project,命名为CounterCharacter 2.在webContent目录中新建index.html文件,设置form表单 <!DOCTYPE html> <html> <head> <meta charset="UTF-8&q