Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/65.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
Arrays 如何将字符串中存储为字符的十进制数转换为双精度数?_Arrays_C_Loops_Strtod - Fatal编程技术网

Arrays 如何将字符串中存储为字符的十进制数转换为双精度数?

Arrays 如何将字符串中存储为字符的十进制数转换为双精度数?,arrays,c,loops,strtod,Arrays,C,Loops,Strtod,编程新手,我正在循环一个后缀字符串,并试图单独检索数字,一次只需要一个 起初我写这个是为了整数,只是做了“-0”,然而,现在我需要尝试使它与十进制数兼容 下面是一个整数转换的例子,我如何调整它 int i; char postfix[] = "4 3 +"; for (i=0; i<strlen(postfix); i++) { if (isalnum(postfix[i])) { int value=(postfix[i]-'0');

编程新手,我正在循环一个后缀字符串,并试图单独检索数字,一次只需要一个

起初我写这个是为了整数,只是做了“-0”,然而,现在我需要尝试使它与十进制数兼容

下面是一个整数转换的例子,我如何调整它

int i;
char postfix[] = "4 3 +";

for (i=0; i<strlen(postfix); i++) {
    if (isalnum(postfix[i])) {
        int value=(postfix[i]-'0');
        printf("%d\n", value);
    }
}

4
3
将值存储为双精度

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{

    int i;
    double res;
    char *ptr1;
    char *ptr2;
    char postfix[] = "4.5 3.4 4.5 7.6 8.9 3.6 +";

    ptr1 = postfix;
    res = strtod(ptr1, &ptr2);
    
    
    while (res!= 0.0f )  // strtod() return 0.0 if double value is not found.
    {
        printf("Value in decimal is %lf\n",res);
        ptr1 = ptr2;
        res = strtod(ptr1, &ptr2);
    }
    return 0;
}
请查看此链接以了解有关
strtod()函数的信息:

使用
strtod()
解析字符串以获得有效的
双精度

current==endptr
表示转换失败

char postfix[] = "4 3 +";
char *current = postfix;
char *endptr;

double v1 = strtod(current, &endptr);
if (current == endptr) TBD_code_handle_failure();
else current = endptr; 

double v2 = strtod(current, &endptr);
if (current == endptr) TBD_code_handle_failure();
else current = endptr; 

while (isspace((unsigned char) *current)) {  // skip white-space
  current++;
}

if (strchr("+-*/", *current) == NULL) {
  TBD_code_handle_failure(); // unexpected operator
}

printf("%g %g %c\n", v1, v2, *current);

或者

double v1, v2;
char oper;
if (sscanf(postfix, "%lf %lf %c", &v1, &v2, &oper) == 3) {
  ; // Success!
}

如果您只想解“a b op”,可以使用
sscanf(后缀,%f%f%s),&a和&b,c)
,然后在浮点
a
b
c[0]
操作符中获取值。对于更复杂的表达式,我认为您需要一个堆栈数据结构,而更复杂的解析函数可能是我的选择,除非我可以完全依赖于每种情况下的“x y op”形式(我可能会选择[
s
]
scanf
)。另外,请注意,你的整数方法只适用于非负的,一位数的数字,所以我不太愿意称它为整数情况的完整解决方案。然而,代码> > SttoDo()面向整数的兄弟姐妹,你可以考虑。
char postfix[] = "4 3 +";
char *current = postfix;
char *endptr;

double v1 = strtod(current, &endptr);
if (current == endptr) TBD_code_handle_failure();
else current = endptr; 

double v2 = strtod(current, &endptr);
if (current == endptr) TBD_code_handle_failure();
else current = endptr; 

while (isspace((unsigned char) *current)) {  // skip white-space
  current++;
}

if (strchr("+-*/", *current) == NULL) {
  TBD_code_handle_failure(); // unexpected operator
}

printf("%g %g %c\n", v1, v2, *current);
double v1, v2;
char oper;
if (sscanf(postfix, "%lf %lf %c", &v1, &v2, &oper) == 3) {
  ; // Success!
}