【问题标题】:How do you convert a string to double?如何将字符串转换为双精度?
【发布时间】:2022-01-16 17:50:51
【问题描述】:

如何编写一个程序来读取输入字符串并将该字符串转换为浮点数。

我对 double convert_to_double (char *) 的功能有点卡住了:

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

double convert_to_double (char *);

int main(void)
{
    char *s;
    s = malloc(10 * sizeof(char));
    printf("Enter text: ");
    fgets(str, 10, stdin);
    printf("The number is %lf", convert_to_double (str));
    return 0;
}

double convert_to_double (char *str)
{
    char *s;
    double result;
    result = strtod(str, &s);


    if (s != NULL)
    {
        char *anotherEnd;
        double anotherResult = strtod(s, &anotherEnd);
    }
    if (isalnum(s) == 0)
    {  
        printf("Wrong digit entered..");
    }

    return result;
}

【问题讨论】:

  • 你没有在函数的任何地方使用str。你只是用两个未初始化的变量调用strtod(s, &amp;ptr);
  • 我可以建议使用scanf("%lf", &amp;myDouble) 之类的东西吗?检查返回值?
  • @phuclv 我改了,你是这个意思吗?
  • 什么是带有符号和运算符的示例输入?您对此输入的期望输出是什么?

标签: c pointers


【解决方案1】:

如果要严格验证输入字符串,请尝试:

double convert_to_double(char *str)
{
    char *e;
    double d = strtod(str, &e);
    if (*e != '\n') {
        fprintf(stderr, "input error %c\n", *e);
        exit(1);
    }
    return d;
}

字符*e是有效数字后剩余的第一个字符 表示已转换;在这种用法中通常是换行符。 如果您不必如此严格地检查输入字符串,只需说:

double convert_to_double(char *str)
{
    double d = strtod(str, (char **)NULL);
    return d;
}

您可能不必启用它。

【讨论】:

  • 如果str 只是"\n",第一个convert_to_double() 不会将其检测为错误。 无转化的最佳测试是if (str == e) ...,对于添加到尾随的垃圾文本很有用。
【解决方案2】:

你为 strtod 做错了:

double convert_to_double (char *str) {
{
    char *s;
    double result;
    // first parameter is the string you want to convert
    // second pram is a pointer to the first char which was not converted.
    // this can be used if you have more than one data to convert in the same string
    result = strtod(str, &s);
    // example of using returned pointer :
    if (s != NULL) {
        char *anotherEnd;
        double anotherResult = strtod(s, &anotherEnd);
    }
    return result;
}

【讨论】:

  • 这个问题可以不使用 strtod() 函数吗?如果是,你是怎么做的?就像我们说只使用 strlen() 或者只是一些没有任何字符串函数的普通方法......
  • @hamburgerwhoselflearns,一切皆有可能。您可以按字符分析输入字符串 char 并做任何您想做的事情。想想字符串“12”:它的意思是 1*10+2。如果字符串为“123”,则为 1*100+2*10+3。这对你有什么建议吗?从 result=0 开始,然后逐个字符获取 char,将结果乘以 10 并添加新数字,依此类推。
  • @hamburgerwhoself 了解strtod 有什么问题?这是要使用的函数。
  • @hamburgerwhoselflearns : 你也有 atof 功能。
【解决方案3】:

如何将字符串转换为double?

if (s != NULL) 不是一个有用的测试。尝试转换字符串时,结束指针s永远不会是NULL。

使用strtod()。测试潜在的错误。

double convert_to_double (const char *str) {
  // Optional test of str, which suppose to point to a string
  if (str == NULL) {
    fprintf(stderr, "Null pointer\n");
    return NAN;
  }

  char *endptr;  // Location of end of conversion.
  errno = 0;
  double result = strtod(str, &endptr);

  if (str == endptr) {
    fprintf(stderr, "No conversion of <%s>\n", str);
    return NAN;
  }

  // Consume trailing white-space. 
  while (isspace((unsigned char) *endptr)) {
    endptr++;
  }
  if (*endptr) {
    fprintf(stderr, "Trailing junk <%s>\n", str);
    return result;
  }

  if (errno) {
    perror("Error:");
    return result;
  }
  return result;
}

最后的测试应该更宽容,因为 underflow 通常不是真正的问题。

  if (errno) {
    if (errno != ERANGE && fabs(result > DBL_MIN)) { 
      perror("Error:");
      return result;
    }
  }

double 可能是文本形式的 hundreds of digits。缓冲区大小要大方。

// s = malloc(10 * sizeof(char));
#define DBL_STR_SIZE 2000
str = malloc(sizeof *str * DBL_STR_SIZE);
if (str) {
  printf("Enter text: ");
  if (fgets(str, DBL_STR_SIZE, stdin)) {
    // process str

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-08-11
    • 1970-01-01
    • 2014-07-24
    • 2013-03-23
    相关资源
    最近更新 更多