如何处理从复杂输入(字符串)中提取双精度类型数字的操作 [英] How to handle extraction of double type numbers from a complex input (string)

查看:129
本文介绍了如何处理从复杂输入(字符串)中提取双精度类型数字的操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

祝大家有美好的一天,

我对C编程还是有点陌生​​,我偶然发现了一个问题,该问题现在试图适当解决几天,但仍然没有令人满意的结果.

I am kinda new to C programming and I've stumbled upon a problem that am trying to address properlly for a few days now but still with no satisfying results.

我正在尝试从用户输入的某些格式为{[ 5.1 ; 4 ], [15.3 ; -3.2] }的用户中提取一些double值.我尝试了许多方法,到目前为止,最好的方法是使用fgets()将用户输入读取为字符串,并循环使用for,如果isdigit()看到数字,则将其存储在另一个字符串中,然后检查是否存在是数字后面的点,如果有的话,我将其放在数字后面的字符串上,依此类推.

I am trying to extract some double values from a user given input in this form {[ 5.1 ; 4 ], [15.3 ; -3.2] }. I tried many approaches, the best one so far is that I read user input as a string using fgets(), loop through with for and if isdigit() sees a digit I store it in another string, then I check if there is a dot right after the digit, if there is then I put it to the string right behind the digit and so on and so forth.

真正的问题是,当我想输出结果字符串并用strtod()将其转换为所需的double时,它仅在用户首先输入一些数字时才起作用,但是如果输入看起来像{ [ 2.5 ; 1 ] }代码只是不在乎,什么也不做.任何帮助或见解将不胜感激,也许我采用了错误的方法?谢谢.

The real problem is when I want to output the resultant string and convert it to my desired double with strtod() it works only if the user inputs some digits first, but if the input looks something like { [ 2.5 ; 1 ] } the code just don't care and does nothing. Any help or insights would be much appreciated, maybe I am taking a wrong approach? Thank you.

我的代码

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

#define STR_LEN 256

int main() {

char t1[STR_LEN], digits[STR_LEN], *dpt, per[2] = ".";
int i, digit, period;
double x;

fgets(t1, STR_LEN, stdin);

for (i = 0; i < strlen(t1); i++) {
    digit = isdigit(t1[i]);
    period = (t1[i + 1] == '.') ? 1 : 0;
    if (digit) {
        digits[i] = t1[i];
    }
    if (period == 1) {
        strcpy(digits, digits);
        strcat(digits, per);
    }
}
x = strtod(digits, &dpt);
int testOutput = strlen(digits);
printf("%s %d %lf", digits, testOutput, x);

return 0;
}

推荐答案

索引到digits时需要一个单独的索引变量(与i不同).这是因为如果您输入的是(例如; 37.5)i将在digits

You need a separate index variable (different from i) when indexing into digits. This is because if your input is (e.g. ; 37.5) i will be misaligned on digits

此外,当您将一个数字复制到digits中时,必须添加EOS终止符以保持字符串完整[以使strcat不会超出digits字符串的末尾].

Also, when you copy over a digit into digits, you have to add an EOS terminator to keep the string whole [so that the strcat would not run off the end of the digits string].

您不想在循环中使用t1[i + 1].它可能会超出输入的结尾,因此请使用t1[i](即该周期将在 next 循环迭代中找到)

You don't want to use t1[i + 1] in the loop. It could go beyond the end of the input, so use t1[i] (i.e. the period will be found on the next loop iteration)

这是您的代码的有效版本[请原谅免费的样式清理]:

Here is a working version of your code [please pardon the gratuitous style cleanup]:

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

#define STR_LEN 256

int
main()
{

    char t1[STR_LEN],
     digits[STR_LEN],
    *dpt,
     per[2] = ".";
    int i, idig,
     digit,
     period;
    double x;

    fgets(t1, STR_LEN, stdin);

    idig = 0;
    for (i = 0; i < strlen(t1); i++) {
        digit = isdigit(t1[i]);
        period = (t1[i] == '.') ? 1 : 0;
        if (digit) {
            digits[idig++] = t1[i];
            digits[idig] = 0;
        }
        if (period == 1) {
            //strcpy(digits, digits);
            strcat(digits, per);
            idig += 1;
        }
    }
    x = strtod(digits, &dpt);
    int testOutput = strlen(digits);

    printf("%s %d %lf\n", digits, testOutput, x);

    return 0;
}


我对代码进行了一些简化:


I've simplified the code a bit:

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

#define STR_LEN 256

int
main()
{
    char t1[STR_LEN];
    char digits[STR_LEN];
    char *dpt;
    int isrc, idig;
    int len;
    int chr;
    double x;

    fgets(t1, STR_LEN, stdin);

    len = strlen(t1);

    idig = 0;
    for (isrc = 0;  isrc < len;  ++isrc) {
        chr = t1[isrc];

        if (isdigit(chr)) {
            digits[idig++] = chr;
            continue;
        }

        if (chr == '.') {
            digits[idig++] = chr;
            continue;
        }
    }

    digits[idig] = 0;

    x = strtod(digits, &dpt);
    int testOutput = strlen(digits);

    printf("%s %d %lf\n", digits, testOutput, x);

    return 0;
}


这是使用char *指针而不是索引变量的第三个版本:


Here's a third version that uses char * pointers instead of index variables:

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

#define STR_LEN 256

int
main()
{
    char t1[STR_LEN];
    char digits[STR_LEN];
    char *dpt;
    char *src;
    char *dst;
    int isrc, idig;
    int len;
    int chr;
    double x;

    fgets(t1, sizeof(t1), stdin);

    src = t1;
    dst = digits;

    for (chr = *src++;  chr != 0;  chr = *src++) {
        if (isdigit(chr)) {
            *dst++ = chr;
            continue;
        }

        if (chr == '.') {
            *dst++ = chr;
            continue;
        }
    }

    *dst = 0;

    x = strtod(digits, &dpt);
    int testOutput = strlen(digits);

    printf("%s %d %lf\n", digits, testOutput, x);

    return 0;
}

这篇关于如何处理从复杂输入(字符串)中提取双精度类型数字的操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆