isalpha(< mychar>)== true计算结果为false? [英] isalpha(<mychar>) == true evaluates to false?

查看:189
本文介绍了isalpha(< mychar>)== true计算结果为false?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

string temp等于我的调试器中的ZERO:\t.WORD\t1。 (我的文件的第一行)

  string temp = RemoveWhiteSpace 
int i = 0;
if(temp.length()> 0&& isalpha(temp [0]))
cout< 没有真正工作< endl;
if(temp.length()> 0&& isalpha(temp [0])== true)
cout< 真正工作< endl;

这是我的代码,用于检查temp的第一个字符是否为a-z,A-Z。第一个if语句将求值为true,第二个为false。为什么?!?!?!我试过这个,即使没有temp.length()> 0&&它仍然评估为false。它只是讨厌== true。我唯一能想到的是isalpha()返回!= 0和true == 1.然后,你可以得到isalpha()== 2!= 1。但是,我不知道如果C ++是...奇怪。



BTW,我不需要知道== true在逻辑上是无意义的。我知道。



输出为

  b 

使用GNU GCC在Ubuntu 9.10上编译CodeBlock(如果这是重要的)


<如果为true,那么is *函数只能保证返回一个非零值,而不一定是1.一个典型的实现是基于表的,其中一个条目在每个字符值的表,以及定义哪个位意味着什么的一组位。 is *函数将正确的位掩码与表值进行AND操作,并返回它,对于任何类型发生位置为0的任何类型,它只有值1.



例如:

  #define __digit 1 
#define __lower 2
#define __upper 4
extern int __type_table [];

int isdigit(int c){
return __type_table [c + 1]& __数字;
}

int isalpha(int c){
return __type_table [c + 1]& (__lower | __upper);
}

int islower(int c){
return __type_table [c + 1]& __降低;
}

int isupper(int c){
return __type_table [c + 1]& __上;
}

其中 __ type_table 定义为 int __type_table [UINT_MAX + 1]; ,并将初始化为(例如) __ type_table ['0'+ 1] == __digit __ type_table ['A'+ 1] == __upper



case你关心,'+1'部分是在表的开始为 EOF (通常定义为-1)。


string temp is equal to "ZERO:\t.WORD\t1" from my debugger. (the first line of my file)

string temp = RemoveWhiteSpace(data);
int i = 0;
if ( temp.length() > 0 && isalpha(temp[0]) )
    cout << "without true worked" << endl;
if ( temp.length() > 0 && isalpha(temp[0]) == true )
    cout << "with true worked" << endl;

This is my code to check if first character of temp is a a-z,A-Z. The first if statement will evaluate to true and the 2nd to false. WHY?!?!?! I have tried this even without the "temp.length() > 0 &&" and it still evaluates false. It just hates the "== true". The only thing I can think of is that isalpha() returns != 0 and true == 1. Then, you could get isalpha() == 2 != 1. But, I have no idea if C++ is that ... weird.

BTW, I dont need to know that the "== true" is logically pointless. I know.

output was

without true worked

Compiled with CodeBlock using GNU GCC on Ubuntu 9.10 (if this matters any)

解决方案

The is* functions are only guaranteed to return a non-zero value if true, NOT necessarily a 1. A typical implementation is table based, with one entry in the table for each character value, and a set of bits defining which bit means what. The is* function will just AND the right bitmask with the table value, and return that, which will only be the value 1 for whichever type happens to have been given bit position 0.

E.g.:

#define __digit 1
#define __lower 2
#define __upper 4
extern int __type_table[];

int isdigit(int c) { 
    return __type_table[c+1] & __digit;
}

int isalpha(int c) { 
    return __type_table[c+1] & (__lower | __upper);
}

int islower(int c) { 
    return __type_table[c+1] & __lower;
}

int isupper(int c) { 
    return __type_table[c+1] & __upper;
}

Where __type_table is defined as something like int __type_table[UINT_MAX+1]; and would be initialized so (for example) __type_table['0'+1] == __digit and __type_table['A'+1] == __upper.

In case you care, the '+1' part is to leave a spot at the beginning of the table for EOF (which is typically defined as -1).

这篇关于isalpha(&lt; mychar&gt;)== true计算结果为false?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆