2

我的 ctype.h 中有函数 isnumber()。我在http://www.cplusplus.com/reference/cctype/中也没有的书籍中找不到此功能的参考资料以供参考。我打算通过一个简单的例子来检测并显示 isdigit() 和 isnumber() 之间的区别,但没有(此外,函数无法检测到 U+0C6A、U+0ED2 和 ½)。有什么区别吗?我该如何解决?

int main(int, char *[])
{
    string text("Hello½123, Poly౪ ໒girl0.5!!!");
    decltype(text.size()) punct = 0, alpha = 0, number = 0, space = 0, digit = 0, graf = 0;
    for(auto i : text) {
        if(ispunct(i)){
            cout << i << "<punct  ";
            ++punct;
        }
        if(isalpha(i)) {
            cout << i << "<alpha  ";
            ++alpha;
        }
        if(isnumber(i)) {
            cout << i << "<number  ";
            ++number;
        }
        if(isdigit(i)) {
            cout << i << "<digit  ";
            ++digit;
        }
        if(isgraph(i)) {
            cout << i << "<graph  ";
            ++graf;
        }
        if(isspace(i))
            ++space;
    }
    cout << endl << "There is " << endl;
    cout << punct <<  " puncts," << endl;
    cout << alpha << " alphas," << endl;
    cout << number << " numbers," << endl;
    cout << digit << " digits," << endl;
    cout << graf << " graphs," << endl;
    cout << space << " spaces" << endl;
    cout << "in " << text << endl;

    return 0;
}

结果的一部分:

...

5 numbers,
5 digits,
23 graphs,
2 spaces
in Hello½123, Poly౪ ໒girl0.5!!!
4

3 回答 3

6

isnumber()可能是 Apple 特有的 C++ 方法(我手头没有 Mac 可以检查)。您可以在Apple 开发指南中看到它:

isnumber()函数的行为类似于isdigit(),但可能会识别其他字符,具体取决于当前的语言环境设置。


此外,isnumber()未在 Linux 上声明:我在 Linux 4.7.2 上使用 g++ 6.1.1 并得到错误:

g++ a.cpp
a.cpp: In function 'int main(int, char**)':
a.cpp:20:17: error: 'isnumber' was not declared in this scope
   if (isnumber(i)) {
                 ^

我也用clang3.8.1来测试:

clang++ a.cpp --std=c++11
a.cpp:20:7: error: use of undeclared identifier 'isnumber'
                if (isnumber(i)) {
                    ^
于 2016-08-29T10:44:23.050 回答
4

isdigit()仅适用于 0-9。

isnumber()允许其他数值,例如分数。一些“数字”而非数字的字符包括上标 2 和 3(“²”和“³”)的 0x00b2 和 0x00b3,以及诸如“¼”、“½”和“¾”等分数的字形。

于 2016-08-29T10:38:58.720 回答
2

isnumber()功能是Apple添加的,因此您需要使用Apple文档。

这是来自iOS †手册页ctype.h引用:

STANDARDS
     These functions, except for digittoint(), isascii(), ishexnumber(),
     isideogram(), isnumber(), isphonogram(), isrune(), isspecial() and
     toascii(), conform to ISO/IEC 9899:1990 (``ISO C90'').

同一页面实际上并没有链接到手册页isnumber(),但是我从标准函数的手册页的链接中推断出它的 URL,并发现了这个

The isnumber() function behaves similarly to isdigit(), but may recognize
additional characters, depending on the current locale setting.

谷歌上的结果 #4 为isnumber ctype……

于 2016-08-29T11:01:12.423 回答