【发布时间】:2012-02-22 07:00:21
【问题描述】:
我正在尝试通过模式检查来检查输入的字符是否为数字。
我编写了以下程序。
这个程序没有给我以下测试用例的完美输出 谁能告诉我我的逻辑哪里出了问题。
/*
Output test case
234234 = It's a digit.
a3434a = It's not a digit.
33aa3a = It' not a digit.
*/
#define yes 1
#define no 0
#include <stdio.h>
int main(void)
{
char c[30];
int arr_size, result, i=0, state;
printf("Enter your digit:= ");
scanf("%s",&c);
arr_size=(sizeof(c)/sizeof(c[0]));
for(i; i < arr_size; i++)
{
if(check_digit(c[i]))
state = yes;
else
state = no;
}
if(!state)
printf("It's not a digit\n");
else
printf("It's a digit\n");
system("\npause");
return 0;
}
int check_digit(char c)
{
return (c>='0' && c<='9');
}
【问题讨论】:
-
你所谓的“数字”,其他人都称之为“数字”。 (0 到 9 是数字)。而
system("\npause")将不起作用,因为没有名为“\npause”的命令。请改用system("pause"),或者更好的是,不要从 GUI 而是从命令行启动程序。
标签: c