简体   繁体   English

如何检查double *数组?

[英]How to check double *array?

I try to store my data in double *array. 我尝试将数据存储在double *数组中。

double* parse_string(char *string)
...
double *parsed_string = parse_string(str);

I want to store numbers like 124.1546 and operators like '+'. 我想存储像124.1546这样的数字和像'+'这样的运算符。 The problem is: when this data is in array, I can't detect which one is number and which is operator. 问题是:当这个数据在数组中时,我无法检测哪个是数字,哪个是运算符。 I have tried isdigit() and isalpha(), but in works only with char *array. 我尝试过isdigit()和isalpha(),但只适用于char *数组。 Should I store data somehow else? 我应该以其他方式存储数据吗?

UPD: I try to make Reverse Polish notation. UPD:我尝试制作反向波兰表示法。 For that I get string with fgets from stdin, then parse this string with this function: 为此,我从stdin获取fgets的字符串,然后使用此函数解析此字符串:

double* parse_string(char *string)
{
char result_digits[50][50];
char result_operators[50];
static double result_string[100];

int i = 0;
int digits_cnt = 0;
int digit_string = 0;
int operators_cnt = 0;
while (string[i] != '\n') {
    if (detect_symbol_type(string[i]) == sym_digit || string[i] == '.') {
        result_digits[digit_string][digits_cnt] = string[i];
        digits_cnt++;
    } else {
        if (detect_symbol_type(string[i]) == sym_operator) {
            result_operators[operators_cnt] = string[i];
            result_digits[digit_string][digits_cnt] = '\n';
            operators_cnt++;
            digit_string++;
            digits_cnt = 0;
        }
    }
    i++;
}
result_operators[operators_cnt] = '\n';

double result_numbers[100];
for (int i = 0; i <= digit_string; i++) {
    double result = 0;
    sscanf(result_digits[i], "%lf", &result);
    result_numbers[i] = result;
    printf("Parse result: %lf\n", result);
}

int k = 0;
for (int i = 0; i <= (digit_string + operators_cnt); i++) {
    result_string[i] = result_numbers[k];
    printf("%lf", result_string[i]);
    i++;
    result_string[i] = result_operators[k];
    printf("%c", (char)result_string[i]);
    k++;
}

return result_string;
}

So I can receive double array with all numbers and operators or two arrays separately. 所以我可以分别使用所有数字和运算符或两个数组接收双数组。 And I start to think that two arrays in my case is not such a bad idea... 我开始认为在我的情况下两个阵列并不是一个坏主意......

On a computer: 在电脑上:

  • Numbers are stored as bits (001110101010101110001) 数字存储为位(001110101010111110001)
  • Characters/operators are stored as bits (000011101111010101101) 字符/运算符存储为位(000011101111010101101)

There is no inherent property allowing to distinguish one from another. 没有固有的属性可以区分彼此。 It is you as a programmer who specifies how a given data should be interpreted. 这是作为一个程序员谁规定一个给定的数据应该如何解释。 If you lose that information, eg by storing numbers and characters together at the same location, it cannot be recovered. 如果丢失该信息,例如通过在同一位置存储数字和字符,则无法恢复。

What you can do however, is to store that information (how the data should be interpreted) explicitly, in a separate bit value. 但是,您可以做的是在单独的位值中显式地存储该信息(如何解释数据)。 A typical low-level construct is a union and a flag: 典型的低级构造是联合和标志:

typedef enum {
    Number,
    Operator,
    SomethingElse,
    ...
} MyDataType;

typedef struct {
    MyDataType type;
    union {
        double number;
        char op;
    };
} MyData;

Now, if you have an object of type MyData , say x you can check what it is by accessing: 现在,如果你有一个MyData类型的对象,比如x你可以通过访问来检查它是什么:

x.type

and when you set the value of it, you need to set the new type and the appriopriate field. 当你设置它的值时,你需要设置新类型和appriopriate字段。 For example, assigning a number it would look as: 例如,指定一个数字,它看起来像:

x.type = Number;
x.number = 124.1546

If you are into object-oriented programming in C++ this a bit cumbersome mechanic can be hidden with access members. 如果您使用C ++进行面向对象编程,可以使用访问成员隐藏一些麻烦的机制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM