c++中如何判断输入的数时字符还是数字?请举例谢谢!
答案:2 悬赏:0 手机版
解决时间 2021-03-03 16:14
- 提问者网友:却不属于对方
- 2021-03-02 16:23
c++中如何判断输入的数时字符还是数字?请举例谢谢!
最佳答案
- 五星知识达人网友:摆渡翁
- 2021-03-02 16:49
通过字符的字母和数字的ASCII递增的特点来判断。
例如:
//参考代码如下:
#include "iostream"
#include "stdio.h"
#include
using namespace std;
int main()
{
char ch;
cin>>ch;
if(ch>='0'&&ch<='9')
cout<<"该字符为数字"<
else if((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z')) //字符区分大小写
cout<<"该字符为字母"<
return 0;
}
例如:
//参考代码如下:
#include "iostream"
#include "stdio.h"
#include
using namespace std;
int main()
{
char ch;
cin>>ch;
if(ch>='0'&&ch<='9')
cout<<"该字符为数字"<
cout<<"该字符为字母"<
}
全部回答
- 1楼网友:毛毛
- 2021-03-02 17:51
#include
using namespace std;
int main()
{
while(true)
{
char temp;
cin >> temp;
if ( (temp <= 'z' && temp >= 'a') || (temp <= 'z' && temp >= 'a') )
cout << temp << " is character" << endl;
else if (temp >= '0' && temp <= '9')
cout << temp << " is number" << endl;
}
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯