Today, the result of a question about judging the character type was wrong. The inspection found that it was an expression error. Using
0<=x<=9 is equivalent to (0<=x) <= 9
(0<=x) is a comparison, and the result is true ( Generally, it is 1) or 0. Both 1 and 0 are <=9, so the result of this expression is true.
And 0<=x && x <= 9 is equivalent to (0<=x) && (x <=9) if x is in the range of [0,9], the expression is true (1), otherwise it is 0.
Subject content:
Write a program, enter a character, determine whether it is a number, uppercase letter, lowercase letter or other, and display 0, 1, 2 or -1 respectively.
Input: an ASCII character
Output: Number -1,0,1 or 2
Sample 1 input:
3
Sample 1 output:
0
Sample 2 input:
Sample 2 output:
-1
Sample 3 input:
A
Sample 3 output:
1
Time limit: 500ms Memory limit: 32000kb
Idea: The ASCII code value of 0 is 48, the ASCII code value of A is 65, and the ASCII code value of a is 97. You can compare characters directly or numerically.
#include <iostream>
using namespace std;
int main()
{
char c;
cin>>c;
if('0'<=c&&c<='9')
{
cout<<"0";
}
else if('A'<=c&&c<='Z')
{
cout<<"1"<<endl;
}
else if('a'<=c&&c<='z')
{
cout<<"2"<<endl;
}
else cout<<"-1";
return 0;
}