简体   繁体   中英

How to check double *array?

I try to store my data in double *array.

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

I want to store numbers like 124.1546 and operators like '+'. 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. Should I store data somehow else?

UPD: I try to make Reverse Polish notation. For that I get string with fgets from stdin, then parse this string with this function:

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)
  • Characters/operators are stored as bits (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:

x.type

and when you set the value of it, you need to set the new type and the appriopriate field. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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