【问题标题】:How can i make a string that only accepts numbers and the character "." in c?我怎样才能制作一个只接受数字和字符“。”的字符串。在 c?
【发布时间】:2021-04-02 14:52:15
【问题描述】:

我试图用 c 编写一个程序来检测一个数字是整数还是浮点数,如果是浮点数,则计算小数位数。

但是我在这里遇到了一个问题,当我插入一个浮点数时,因为“。”该程序说它是一个“单词”而不是一个数字,因为我让它只接受数字并且我陷入了 while 循环。

我的代码:

#include<stdio.h>
#include <string.h>
#define BASE 10

main()
{
    int length, number;
    char str[10];
    char ch = '.';
    char *ret;
    char *endptr;

    do{
    printf("Enter string: ");
    scanf("%s", str);

     number = strtol(str, &endptr, BASE);

    }while (*endptr != '\0' || endptr == str);

   ret = strchr(str, ch);

    if(ret > 0){

    length = strlen(ret);
    printf("decimal places: %d\n", length - 1);
    }
    else {

         printf("the number is integrer\n");
    }

    return 0;

}

【问题讨论】:

  • 你试过 strtod 吗?如果您只想测试数字和点,则应执行 strtol 一次,而不是测试字符串结尾或 '.' ,如果是“.”,则使用 strtol 测试“.”后面的字符。
  • 注意:避免溢出问题char str[10]; ... scanf("%s", str); --> char str[80]; ... scanf("%79s", str);
  • seixo paulo,“如果是浮点数,则计算小数位数。”:输入如“123.456e2”,是小数点后 1 位还是 3 位? "123.456e3" 是整数吗?
  • @chux-ReinstateMonica 有字母是我试图避免的,因为程序会将“123.456e2”计为小数点后 5 位,我们都知道这是错误的。
  • @PtitXav 这听起来是个好主意,但我该怎么做呢?对不起,我没有想象我怎么能做到这一点

标签: c string char


【解决方案1】:

只有当您知道输入是整数时,使用strtol 才有意义。如果您知道输入是浮点数,则可以改用strtod。没有可以处理这两种类型的函数,除非您想将整数作为浮点数处理。

为了确定输入是整数、浮点还是无效输入,最好自己检查输入字符串,例如:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <stdbool.h>

int main( void )
{
    char str[100];

retry_input:

    //prompt user for input
    printf( "Enter number: " );

    //read one line of input
    if ( fgets( str, sizeof str, stdin ) == NULL )
    {
        fprintf( stderr, "input error!\n" );
        exit( EXIT_FAILURE );
    }

    //if newline character exists, remove it
    str[strcspn(str,"\n")] = '\0';

    //this variable keeps track of the number of digits encountered
    int num_digits = 0;

    //this variable specifies whether we have encountered a decimal point yet
    bool found_decimal_point = false;

    //inspect the string one character at a time
    for ( char *p = str; *p!='\0'; p++ )
    {
        if ( *p == '.' )
        {
            if ( found_decimal_point )
            {
                printf( "encountered multiple decimal points!\n" );
                goto retry_input;
            }

            found_decimal_point = true;
        }
        else if ( isdigit( (unsigned char)*p ) )
        {
            num_digits++;
        }
        else if ( *p != '+' && *p != '-' )
        {
            printf( "encountered unexpected character in input!\n" );
            goto retry_input;
        }
        else if ( p - str != 0 )
        {
            printf(
                "sign characters (+/-) are only permitted at the start "
                "of the string!\n"
            );
            goto retry_input;
        }
    }

    if ( found_decimal_point )
    {
        //input is floating-point

        printf( "The input is float and has %d digits.\n", num_digits );
    }
    else
    {
        //input is integer

        printf( "The input is integer and has %d digits.\n", num_digits );
    }

    return EXIT_SUCCESS;
}

请注意,此程序还将计算遇到的位数,并在程序结束时打印总数。既然你在问题中说你想计算“小数位数”,这可能就是你想要的。

另请注意,此程序不接受指数表示法的浮点数。

【讨论】:

    猜你喜欢
    • 2015-04-13
    • 1970-01-01
    • 1970-01-01
    • 2019-12-29
    • 1970-01-01
    • 2021-10-01
    • 2015-12-02
    • 2011-01-10
    相关资源
    最近更新 更多