重码 ctype

该博客主要探讨了C++标准库中的ctype函数,通过引用cppreference的资料,介绍了如何使用ctype定义并操作TYPE_FLAG数组。内容涉及istype.h头文件及相应的cpp实现。
摘要由CSDN通过智能技术生成

http://www.cplusplus.com/reference/cctype/

链接为cpp reference 中的ctype 定义,参考这里来写的代码。

首先用这个代码段,生产一个TYPE_FLAG的数组。

// Generate Type Flag array
void gen_flag(void){
	unsigned short flag_type[128] = {};
	int i;
    // 0x00 ~ 0x08
    for(i=0; i<=8; i++)
        flag_type[i] |= FLAG_CONTROL;

    // 0x09
    flag_type[0x09] |= FLAG_CONTROL | FLAG_BLANK | FLAG_SPACE;

    // 0x0a ~ 0x0d
    for(i=0x0a; i<=0x0d; i++)
        flag_type[i] |= FLAG_CONTROL | FLAG_SPACE;

    // 0x0e ~ 0x1f
    for(i=0x0e; i<= 0x1f; i++)
        flag_type[i] |= FLAG_CONTROL;

    // 0x20
    flag_type[0x20] |= FLAG_BLANK | FLAG_SPACE;

    // 0x21 ~ 0x2f
    for(i=0x21; i<= 0x2f; i++)
        flag_type[i] |= FLAG_PUNCT;

    // 0x30 ~ 0x39
    for(i=0x30; i<= 0x39; i++)
        flag_type[i] |= FLAG_DIGIT | FLAG_HEX;

    // 0x3A ~ 0x40
    for(i=0x3A; i<= 0x40; i++)
        flag_type[i] |= FLAG_PUNCT;

    // 0x41 ~ 0x46
    for(i=0x41; i<= 0x46; i++)
        flag_type[i] |= FLAG_UPPER | FLAG_ALPHA | FLAG_HEX;

    // 0x47 ~ 0x5A
    for(i=0x47; i<= 0x5A; i++)
        flag_type[i] |= FLAG_UPPER | FLAG_ALPHA;

    // 0x5B ~ 0x60
    for(i=0x5b; i<= 0x60; i++)
        flag_type[i] |= FLAG_PUNCT;

    // 0x61 ~ 0x66
    for(i=0x61; i<= 0x66; i++)
        flag_type[i] |= FLAG_LOWER | FLAG_ALPHA | FLAG_HEX;

    // 0x67 ~ 0x7A
    for(i=0x67; i<= 0x7A; i++)
        flag_type[i] |= FLAG_ALPHA | FLAG_LOWER;

    // 0x7b ~ 0x7e
    for(i=0x7b; i<= 0x7f; i++)
        flag_type[i] |= FLAG_PUNCT;

    flag_type[0x7f] = FLAG_CONTROL;


	for(i=0;i<=0x7f; i++){
		printf("0x%04x",(int)flag_type[i]);
		if(7 == (i%8)){
            printf(",\n");
		}else{
            printf(", ");
		}
	}

}

test.cpp

#include <iostream>
#include<cstdio>
#include "istype.h"
using namespace std;


int main(int agrc, char* argv[]){
    char test[] = {0x0, 'a', '9', ',', '\\'};
    for(int i=0; i< sizeof(test)/sizeof(char); i++){
        if (is_alpha(test[i])){
            cout << hex << test[i] << " = ";
            cout.put(test[i]);
            cout << " is alpha.\n";
        }else if (is_digit(test[i])){
            cout << hex << test[i] << " = ";
            cout.put(test[i]);
            cout << " is digit.\n";
        }else if (is_punct(test[i])){
            cout << hex << test[i] << " = ";
            cout.put(test[i]);
            cout << " is punctuation.\n";
        }else if (is_cntrl(test[i])){
            cout << hex << test[i] << " = ";
            cout.put(test[i]);
            cout << " is control.\n";
        }

    }

    return 0;
}





istype.h 

/*
 * Copyright (c) 2015. 美细耐斯(上海)电子有限公司
 * All rights reserved.
 * @author heafei@aliyun.com
 * @date 20151231
 */

#ifndef ISTYPE_H
#define ISTYPE_H

bool  is_alpha(int ch);
bool  is_upper(int ch);
bool  is_lower(int ch);
bool is_digit(int ch);
bool is_xdigit(int ch);
bool is_cntrl(int ch);
bool is_punct(int ch);
bool is_print(int ch);
bool is_graph(int ch);
char to_upper(int ch);
char to_lower(int ch);
#endif // ISTYPE_H


// istype.cpp

/*
 * Copyright (c) 2015.
 * All rights reserved.
 * @author heafei@aliyun.com
 * @date 20151231
 */
#include <cstdio>
#include "istype.h"
using namespace std;
enum TypeFlag{
	FLAG_UPPER	 =	0x0001,
	FLAG_LOWER	 =	0x0002,
	FLAG_ALPHA	 =	0x0103,
	FLAG_DIGIT	 =	0x0004,
	FLAG_SPACE	 =	0x0008, /* HT  LF  VT  FF  CR  SP */
	FLAG_PUNCT	 =	0x0010,
	FLAG_CONTROL =	0x0020,
	FLAG_BLANK	 =	0x0040, /* this is SP only, not SP and HT as in C99  */
	FLAG_HEX	 =	0x0080,
	FLAG_LEADBYTE =	0x8000,
};

// ASCII -- American Standard Code for Information Interchange
// TYPE_FLAG_ARRY[0] is flag of 0x0 in ASCII
const unsigned short TYPE_FLAG_ARRY[128] = {
    0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020,
    0x0020, 0x0068, 0x0028, 0x0028, 0x0028, 0x0028, 0x0020, 0x0020,
    0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020,
    0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020,
    0x0048, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010,
    0x0010, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010,
    0x0084, 0x0084, 0x0084, 0x0084, 0x0084, 0x0084, 0x0084, 0x0084,
    0x0084, 0x0084, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010,
    0x0010, 0x0183, 0x0183, 0x0183, 0x0183, 0x0183, 0x0183, 0x0103,
    0x0103, 0x0103, 0x0103, 0x0103, 0x0103, 0x0103, 0x0103, 0x0103,
    0x0103, 0x0103, 0x0103, 0x0103, 0x0103, 0x0103, 0x0103, 0x0103,
    0x0103, 0x0103, 0x0103, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010,
    0x0010, 0x0183, 0x0183, 0x0183, 0x0183, 0x0183, 0x0183, 0x0103,
    0x0103, 0x0103, 0x0103, 0x0103, 0x0103, 0x0103, 0x0103, 0x0103,
    0x0103, 0x0103, 0x0103, 0x0103, 0x0103, 0x0103, 0x0103, 0x0103,
    0x0103, 0x0103, 0x0103, 0x0010, 0x0010, 0x0010, 0x0010, 0x0020,
};

bool is_lower(int ch){
    return TYPE_FLAG_ARRY[ch]&FLAG_LOWER;
}

bool is_upper(int ch){
    return TYPE_FLAG_ARRY[ch]&FLAG_UPPER;
}

bool is_alpha(int ch){
    return TYPE_FLAG_ARRY[ch]&FLAG_ALPHA;
}

bool is_digit(int ch){
    return TYPE_FLAG_ARRY[ch] & FLAG_DIGIT;
}

bool is_xdigit(int ch){
    return TYPE_FLAG_ARRY[ch] & FLAG_HEX;
}

bool is_alnum(int ch){
    return TYPE_FLAG_ARRY[ch] & (FLAG_DIGIT | FLAG_ALPHA);
}

bool is_blank(int ch){
    return TYPE_FLAG_ARRY[ch] & FLAG_BLANK;
}

bool is_cntrl(int ch){
    return TYPE_FLAG_ARRY[ch] & FLAG_CONTROL;
}

bool is_punct(int ch){
    return TYPE_FLAG_ARRY[ch] & FLAG_PUNCT;
}

bool is_print(int ch){
    return (ch <= 0x7e) && (ch >= 0x20);
}

bool is_graph(int ch){
    return (ch <= 0x7e) && (ch >= 0x21);
}

char to_upper(int ch){
    #if defined(DEBUG_OLIVER)
    printf("[%s] %c upper -> %c", __func__, ch, (is_alpha(ch) ? (ch-32) : ch));
    #endif // defined(DEBUG_OLIVER)
    return is_alpha(ch) ? (ch-32) : ch;
}

char to_lower(int ch){
    #if defined(DEBUG_OLIVER)
    printf("[%s] %c upper -> %c", __func__, ch, (is_alpha(ch) ? (ch+32) : ch));
    #endif // defined(DEBUG_OLIVER)
    return is_alpha(ch) ? (ch+32) : ch;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
VB.NET(Visual Basic .NET)是Microsoft开发的一种基于.NET框架的编程语言,主要用于Windows应用程序、Web应用和桌面软件的开发。其中,哈希表(Hash Table 或 Dictionary)是一个用于存储键值对的数据结构,提供快速查找的能力,常用于查找重码或数据去重。 如果你想使用VB.NET中的哈希表来判断字符串是否有重码,你可以这样做: ```vb.net Imports System.Collections.Generic Module Module1 Sub Main() Dim hashTable As New Dictionary(Of String, Integer) ' 创建一个哈希表,用String作为键,Integer作为默认值,用来记录每个字符出现的次数 ' 待检查的字符串列表 Dim strList = {"hello", "world", "hello again"} For Each str In strList Dim hashValue = 0 ' 初始化计数器 ' 遍历字符串中的每一个字符 For i As Integer = 0 To str.Length - 1 If Not hashTable.ContainsKey(str(i)) Then ' 如果字符不在哈希表中,则添加并设置值为1 hashTable.Add(str(i), 1) Else ' 如果字符已经在哈希表中,增加计数值 hashValue += hashTable(str(i)) End If Next ' 判断是否有重码,如果所有字符的出现次数加起来大于strList长度,说明有重码 If hashValue <> str.Length Then Console.WriteLine("字符串 '{0}' 有重码", str) Else Console.WriteLine("字符串 '{0}' 没有重码", str) End If Next Console.ReadKey() ' 主程序暂停,等待用户按下回车键 End Sub End Module ``` 在这个例子中,我们遍历输入的字符串,并将每个字符及其出现次数存储到哈希表中。如果某个字符出现的总次数超过其在原始字符串中的位置数量,那么就认为存在重码
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值