简答题 C++
题号 #2656974

定义内联函数,判断一个字符是否为数字字符。

进入在线模考

参考答案

暂无

答案解析

解:数字字符ASCII码值是连在一起的,可用ch>='0'&&ch<='9'来判断。
#include<iostream.h>
inline bool IfDigitChar(char ch){
    if(ch>='0'&&ch<='9') return 1;
    else return 0;
}
void main(){
    char ch;
    cout<<"请输入一个字符(输入“!”停止)"<<endl;
    cin>>ch;
    while(ch!='!'){    
        if(IfDigitChar(ch)) cout<<ch<<" is a digit char"<<endl;
        else cout<<ch<<" is not a digit char"<<endl;
        cout<<"请输入一个字符(输入“!”停止)"<<endl;
        cin>>ch;
    }
}

你可能感兴趣的试题

找试题 · 找答案 · 找资料 在线模考