有人知道为什么这道题错了吗(c++)?

图片说明
图片说明

#include <iostream>

using namespace std;

int main()

{

char str[1024];

cin.get(str, 1024);

bool flag = true;

for (int i = 0; i < strlen(str); i++)

{

if ((str[0] == '0' || str[0] == '1' || str[0] == '2' || str[0] == '3' || str[0] == '4' || str[0] == '5' || str[0] != '6' || str[0] == '7' || str[0] == '8' || str[0] == '9') || (str[i] != '0' && str[i] != '1' && str[i] != '2' && str[i] != '3' && str[i] != '4' && str[i] != '5' && str[i] != '6' && str[i] != '7' && str[i] != '8' && str[i] != '9' && str[i] != 'a' && str[i] != 'b' && str[i] != 'c' && str[i] != 'd' && str[i] != 'e' && str[i] != 'f' && str[i] != 'g' && str[i] != 'h' && str[i] != 'i' && str[i] != 'j' && str[i] != 'k' && str[i] != 'l' && str[i] != 'm' && str[i] != 'n' && str[i] != 'o' && str[i] != 'p' && str[i] != 'q' && str[i] != 'r' && str[i] != 's' && str[i] != 't' && str[i] != 'u' && str[i] != 'v' && str[i] != 'w' && str[i] != 'x' && str[i] != 'y' && str[i] != 'z' && str[i] != 'A' && str[i] != 'B' && str[i] != 'C' && str[i] != 'D' && str[i] != 'E' && str[i] != 'F' && str[i] != 'G' && str[i] != 'H' && str[i] != 'I' && str[i] != 'J' && str[i] != 'K' && str[i] != 'L' && str[i] != 'M' && str[i] != 'N' && str[i] != 'O' && str[i] != 'P' && str[i] != 'Q' && str[i] != 'R' && str[i] != 'S' && str[i] != 'T' && str[i] != 'U' && str[i] != 'V' && str[i] != 'W' && str[i] != 'X' && str[i] != 'Y' && str[i] != 'Z'))

{

flag = false;

break;

}

}

if (flag == true)

{

cout << "yes" << endl;

}

else

{

cout << "no" << endl;

}

return 0;

}

回答

#include <stdio.h>

#include <string.h>

int main()

{

char str[20];

memset(str,0x0,20);

gets(str);

int i,flag;

flag = 1;

for(i=0;i<strlen(str);i++){

if(((str[i]>='A')&&(str[i]<='Z')) || ((str[i]>='a')&&(str[i]<='z')) || ((str[i]>='0')&&(str[i]<='9')) || (str[i] == '_'))

{

if(((str[i]>='0')&&(str[i]<='9')) && i==0)

{

flag = 0;

break;

}

}

else {

flag = 0;

break;

}

}

if(flag)

printf("yesn");

else

printf("non");

return 0;

}

以上是 有人知道为什么这道题错了吗(c++)? 的全部内容, 来源链接: utcz.com/a/29114.html

回到顶部