Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/firebase/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
检查输入字符是否为数字的C程序?_C - Fatal编程技术网

检查输入字符是否为数字的C程序?

检查输入字符是否为数字的C程序?,c,C,我试图通过模式检查来检查输入的字符是否为数字 我编写了以下程序 这个程序没有为下面的测试用例提供完美的输出 谁能告诉我我的逻辑哪里出了问题 /* 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) {

我试图通过模式检查来检查输入的字符是否为数字

我编写了以下程序

这个程序没有为下面的测试用例提供完美的输出 谁能告诉我我的逻辑哪里出了问题

/*

 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');
}
/*
输出测试用例
234234=这是一个数字。
a3434a=它不是一个数字。
33aa3a=它不是一个数字。
*/
#定义是1
#定义0
#包括
内部主(空)
{
charc[30];
int arr_size,result,i=0,state;
printf(“输入您的数字:=”;
scanf(“%s”、&c);
arr_size=(sizeof(c)/sizeof(c[0]);
对于(i;ireturn(c>='0'&&c您的
for
循环在数组设置的每个字符上运行
state
每次迭代。每次设置
state
时,都会忘记以前的值

因此,您的
for
循环实际上相当于编写

state = is_digit(c[29]);
您需要进行两项更改:

  • 仅迭代用户输入的字符数组。因此,当遇到以null结尾的字符时,请停止迭代
  • 仅当所有字符都是数字时,才将状态设置为
    TRUE

  • 由于这看起来像是家庭作业,我不会为您完整地写出代码!

    您总是覆盖
    状态,因此它仅在最后一个元素是数字时才进行检查

        if(check_digit(c[i]))
        state = yes;
        else
        state = no;
    
    改为使用:
    state=(state_digit(c[i])&&state)
    [初始状态为
    yes
    ]:这意味着您正在寻找一个“无数字”条目,一旦找到它,您就给出答案

    另一种可能是,一旦你找到一个非数字,
    break
    ing

  • 您正在处理一个字符串。只需迭代,直到找到“\0”。无需对
    sizeof
    感到奇怪
  • 阅读
    manisdigit
  • 如果你真的想传递一个指向scanf的指针,它必须是
    &c[0]
    。然而,
    c
    仅仅是一个指针,所以你可以说
    scanf(“%s”,c);
  • 然后是
    状态
    变量。试着在纸上手动运行一些值,看看会发生什么
  • arr_size=(sizeof(c)/sizeof(c[0]);
    //arr_尺寸=30/1
    对于(i;i
    如果您输入的数字少于30位,状态不是“否”吗。。。 如果您输入这样一个30位数字:
    12234****************8877
    ,数字的最后一位数字“7”不是表示“是”吗

    使用
    if(isdigit())
    (i;i

    当状态变为“否”时,会出现中断。

    我如何迭代用户输入的字符?任何内置函数?在for循环测试中,
    i
    作为终止测试
    ,而(c[i]!='\0'&&check_digit(c[i]){if(!c[i])state=yes;else state=no;i++}
    我修改了我的循环,但仍然没有得到我想要的输出。1.提取修改后的代码是不够的。2.你没有告诉我们你的输入是什么。3.你没有告诉我们你的输出是什么。4.你没有告诉我们你期望的输出是什么。你所谓的“数字”其他人都叫“数字”。(0到9是数字。)和
    system(“\npause”)
    将不起作用,因为没有名为“\npause”的命令。请改用
    system(“pause”)
    ,或者最好不要从GUI而是从命令行启动程序。可能会重复
    arr_size=(sizeof(c)/sizeof(c[0]));
        //arr_size=30/1
    
    for(i; i < arr_size; i++)
        //for(i; i < 30; i++)