C语言编程问题,求高手解答遇到输入非法字符时,怎么处理。
答案:2 悬赏:50 手机版
解决时间 2021-03-21 20:10
- 提问者网友:温柔港
- 2021-03-21 13:02
C语言编程问题,求高手解答遇到输入非法字符时,怎么处理。
最佳答案
- 五星知识达人网友:慢性怪人
- 2021-03-21 13:16
可以采用如下语句
while((scanf("%d",&x))==1)
{}
只有输入一个数字时,才进入主体语句
while((scanf("%d",&x))==1)
{}
只有输入一个数字时,才进入主体语句
全部回答
- 1楼网友:忘川信使
- 2021-03-21 14:36
#include
#include
#include
#include "regex.h"
const int MAX_LEN = 1000;
char * trimString(char guessString[])
{
int head = 0 ,rear = strlen(guessString)-1;
char result[MAX_LEN];
int i,k;
while( guessString[head] != '\0' &&
(guessString[head] == ' ' || guessString[head] == ' ' )
)
{
head ++;
}
while( rear >= 0 &&
(guessString[rear] == ' ' || guessString[rear] == ' ' )
)
{
rear --;
}
for(i = head ,k = 0; i <= rear ; i++ , k++)
{
result[k] = guessString[i];
}
result[k] = '\0';
return result;
}
int transStringToNum(char guessString[])
{
int i;
int head = 0 ,rear = strlen(guessString)-1;
char trimGuessString[MAX_LEN];
int num = 0;
// 去前后空格
strcpy(trimGuessString,trimString(guessString));
// 检查是否空串
if( 0== strlen(trimGuessString) )
{
return -1;
}
// 检查是否有非数字字符
for(i = 0 ; trimGuessString[i] != '\0' ; i ++)
{
if(trimGuessString[i] < '0' || trimGuessString[i] > '9')
{
return -2;
}
}
// 字符串转换成数字
for(i = 0 ,num = 0 ; trimGuessString[i] != '\0' ; i++)
{
num = num * 10 + (trimGuessString[i] - '0');
}
return num;
}
int main()
{
int num,guessNum,n,count=0;
char guessString[MAX_LEN];
char reply;
srand(time(NULL));
do
{
//产生一个随即数
num=rand()%100+1;
printf("你有10次机会,请猜数字并按回车。\n");
for(n=1;n<=10;n++)
{
// 以字符串形式读入一个整数
gets(guessString);
guessNum = transStringToNum(guessString);
if(guessNum == -1 )
{
printf("请输入一个数字(必须是在1-100 之间)!\n");
n--;
continue;
}
if(guessNum == -2 )
{
printf("输入的不是一个合法数字(必须是在1-100 之间)!\n");
n--;
continue;
}
if(guessNum>num)
{
printf("It's too high!\n");
}
else if(guessNum {
printf("It's too low!\n");
}
else if(guessNum == num)
{
printf("You are right!\n");
printf("你共用了%d次机会猜对数字。\n",n);
break;
}
}
printf("Do you want to continue(Y/N or y/n)?");
fflush(stdin);
reply = getch();
}while((toupper(reply)=='Y'));
printf("The game is over!\n");
return 0;
}
#include
#include
#include "regex.h"
const int MAX_LEN = 1000;
char * trimString(char guessString[])
{
int head = 0 ,rear = strlen(guessString)-1;
char result[MAX_LEN];
int i,k;
while( guessString[head] != '\0' &&
(guessString[head] == ' ' || guessString[head] == ' ' )
)
{
head ++;
}
while( rear >= 0 &&
(guessString[rear] == ' ' || guessString[rear] == ' ' )
)
{
rear --;
}
for(i = head ,k = 0; i <= rear ; i++ , k++)
{
result[k] = guessString[i];
}
result[k] = '\0';
return result;
}
int transStringToNum(char guessString[])
{
int i;
int head = 0 ,rear = strlen(guessString)-1;
char trimGuessString[MAX_LEN];
int num = 0;
// 去前后空格
strcpy(trimGuessString,trimString(guessString));
// 检查是否空串
if( 0== strlen(trimGuessString) )
{
return -1;
}
// 检查是否有非数字字符
for(i = 0 ; trimGuessString[i] != '\0' ; i ++)
{
if(trimGuessString[i] < '0' || trimGuessString[i] > '9')
{
return -2;
}
}
// 字符串转换成数字
for(i = 0 ,num = 0 ; trimGuessString[i] != '\0' ; i++)
{
num = num * 10 + (trimGuessString[i] - '0');
}
return num;
}
int main()
{
int num,guessNum,n,count=0;
char guessString[MAX_LEN];
char reply;
srand(time(NULL));
do
{
//产生一个随即数
num=rand()%100+1;
printf("你有10次机会,请猜数字并按回车。\n");
for(n=1;n<=10;n++)
{
// 以字符串形式读入一个整数
gets(guessString);
guessNum = transStringToNum(guessString);
if(guessNum == -1 )
{
printf("请输入一个数字(必须是在1-100 之间)!\n");
n--;
continue;
}
if(guessNum == -2 )
{
printf("输入的不是一个合法数字(必须是在1-100 之间)!\n");
n--;
continue;
}
if(guessNum>num)
{
printf("It's too high!\n");
}
else if(guessNum
printf("It's too low!\n");
}
else if(guessNum == num)
{
printf("You are right!\n");
printf("你共用了%d次机会猜对数字。\n",n);
break;
}
}
printf("Do you want to continue(Y/N or y/n)?");
fflush(stdin);
reply = getch();
}while((toupper(reply)=='Y'));
printf("The game is over!\n");
return 0;
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯