c++ - 如何检查科学记数法是否为 double C++

标签 c++ arrays char double scientific-notation

我基本上想检查 line 是否是有效的科学 double 。

所以基本上,如果一行有一个像 a 这样的字符,或者只有几个像 help0.a 这样的字符,那么它将是被认为不是科学替身并被拒绝,但是像 1.234E+92.468E9 这样的东西会被存储,因为它是一个可接受的值

我已经编写了一些代码来处理这个问题,但是我需要一些帮助……区分科学替身和一些字符

char *temp;
int u=0;
int arrayLen = strlen(temp);
for(int i=0; i<len; i++)
{
  if(isalpha(temp[i]))
  {
    if((temp[i] == 'e') && (!isalpha(temp[i-1])))
    {
      break;
    }
    u++;
  }
}

if(u > 0)
{
   temp[0] = 0;
   break;
}

最佳答案

作为 T.C.建议,使用strtod。但是检查返回指针,看看它是否读取了所有内容。

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

double convert_number( const char * num )
{
   char * endptr = 0;
   double retval;
   retval = strtod( num, &endptr );
   return ( !endptr || ( *endptr != '\0' ) ) ? 0 : retval;
}


int main( int argc, char ** argv )
{
    int index;
    for( index = 0; index < argc; ++index )
        printf( "%30s --> %f\n", argv[index], convert_number( argv[index] ) );
    return 0;
}

例子:

./a.out 13.2425 99993.3131.1134  13111.34e313e2  1313e4 1 324.3 "2242e+3"
                       ./a.out --> 0.000000
                       13.2425 --> 13.242500
               99993.3131.1134 --> 0.000000
                13111.34e313e2 --> 0.000000
                        1313e4 --> 13130000.000000
                             1 --> 1.000000
                         324.3 --> 324.300000
                       2242e+3 --> 2242000.000000

--------------------根据请求的替代方案------------------------ ----

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

int convert_number( const char * num, double * retval )
{
   char * endptr = 0;
   *retval = strtod( num, &endptr );
   return ( !endptr || ( *endptr != '\0' ) ) ? 0 : 1;
}


int main( int argc, char ** argv )
{
    int index;
    double dvalue;
    for( index = 0; index < argc; ++index )
        if( convert_number( argv[index], &dvalue ) )
            printf( "%30s --> %f\n", argv[index], dvalue );
        else
            printf( "%30s --> Rejected\n", argv[index], dvalue );
    return 0;
}

结果:

./a.out 13.2425 99993.3131.1134  13111.34e313e2  1313e4 1 324.3 "2242e+3" 0.0
                       ./a.out --> Rejected
                       13.2425 --> 13.242500
               99993.3131.1134 --> Rejected
                13111.34e313e2 --> Rejected
                        1313e4 --> 13130000.000000
                             1 --> 1.000000
                         324.3 --> 324.300000
                       2242e+3 --> 2242000.000000
                           0.0 --> 0.000000

新手版:

int convert_number( const char * num, double * retval )
{
   char * endptr = 0; /* Prepare a pointer for strtod to inform us where it ended extracting the double from the string. */
   *retval = strtod( num, &endptr );  /* Run the extraction of the double from the source string (num) and give us the value so we can store it in the address given by the caller (retain the position in the string where strtod stopped processing). */
   if( endptr == NULL )
       return 0;  /* endptr should never be NULL, but it is a defensive programming to prevent dereferencing null in the next statement. */
   else if( *endptr != '\0 ) /* If we are pointing to a non-null character, then there was an invalid character that strtod did not accept and stopped processing.  So it is not only a double on the line.  So we reject. */
       return 0; /* Not strictly the double we are looking for. */ 
   /* else */
   return 1;  /* Return 1 to the caller to indicate truth (non-zero) that the value in *retval has valid double value to use. */
}

关于c++ - 如何检查科学记数法是否为 double C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27452954/

相关文章:

c++ - std::is_trivially_copyable 错了吗?

arrays - 从字符分组创建新数组?

Java程序从行中删除换行符

java - 在java中添加两个char数据类型

c++ - 如何获取PRO文件中的目标目录

c++ - 将多维数组加载到 C++ 中的数据文件中

javascript - 使用 javascript 连接两个变量并查找数组值位置

TDD session 期间出现 java.lang.AssertionError

c++ - 转换(const char*)var出错

c++ - 使用 MSVC++ 编译器打包枚举