0

I read strings from a text file in my c++ script, those strings saved in string words[3] are in this form :

abc[1]
abc[2]
abc[3]*abc[2]

After reading those strings I want to convert string words[3] to double feature[3] in order to be able to replace those strings with the values of double abc[3] = {1,2,3}. So I want to get a double feature[3] = {abc[1],abc[2],abc[3]*abc[2]} How can I do this conversion in C++?

Maklaizer
  • 27
  • 1
  • 7
  • Seems like you have to evaluate basic mathematical expressions to do the conversion. – drescherjm Aug 19 '20 at 14:55
  • what do you mean by evaluating mathematical expressions? – Maklaizer Aug 19 '20 at 14:57
  • `abc[3]*abc[2]` is a mathematical expression – drescherjm Aug 19 '20 at 14:58
  • Related but does not use variables: [https://stackoverflow.com/questions/9329406/evaluating-arithmetic-expressions-from-string-in-c](https://stackoverflow.com/questions/9329406/evaluating-arithmetic-expressions-from-string-in-c) – drescherjm Aug 19 '20 at 14:59
  • too complicated for me as a C++ beginner, I would appreciate a simpler solution – Maklaizer Aug 19 '20 at 15:07
  • 2
    @Maklaizer if you have e.g. `"abc[3]*abc[2]+abc[1]"` string, you parse it similarly as in linked question (though presumably not absolutely the same), so your code at the end would calculate `get("abc[3]") * get("abc[2]")` and then `+ get("abc[1]")` to the result. `get` function would take a string argument `"abc[3]"` and parse array name (`"abc"`) and index (`"3"` which then you can convert to a number `3`). You can have a map to get data from the appropriate array: `map m {{"abc", abc}}; ` so `get` function would access this map to get the array object with data – Alexey S. Larionov Aug 19 '20 at 15:08
  • @AlexLarionov can you write a code example? – Maklaizer Aug 19 '20 at 15:13
  • 2
    It's not a simple task. If you don't yet have the knowledge to use the above info to write some code, you might want to start with something easier. – Asteroids With Wings Aug 19 '20 at 15:29
  • 1
    If this is an assignment for a beginner I expect the assignment limits the scope of the problem to only several different operations on several different variables. The more general problem is likely too complicated for a beginner. – drescherjm Aug 19 '20 at 15:31

1 Answers1

2

What I meant in my comment is something like this. Playground

Note that I omit the hardest part, which is parsing an arbitrary expression into basic operations.

#include <iostream>
#include <string>
#include <map>
#include <string_view>
#include <algorithm>

double abc[] { 
  /*0*/ 5.0, 
  /*1*/ 2.0, 
  /*2*/ 3.0,
  /*3*/ 4.0
};

std::map<std::string, double *> arrays_names {
    {"abc", abc}    
};

double get_value(std::string_view value_str) {
    auto name_end_pos = value_str.find('[');
    auto name = value_str.substr(0, name_end_pos);
    
    auto index_end_pos = value_str.find(']'); 
    auto index_len = index_end_pos - name_end_pos - 1;
    std::string index_str{value_str.substr(name_end_pos + 1, index_len)}; 
    auto index = std::stoi(index_str);

    return arrays_names[std::string(name)][index];
}

double evaluate_expression(const std::string& expr) {
  // I omit all the dirty code that you would need to make yourself
  // to parse the expression
   
  // so I just assume you parsed "abc[3]*abc[2]+abc[1]" to call this:
  return 
    get_value(expr.substr(0, 6)) * 
    get_value(expr.substr(7, 6)) + 
    get_value(expr.substr(14, 6));
}

int main() {
    std::string expr{"abc[3]*abc[2]+abc[1]"};
    std::cout << expr << std::endl;
    std::cout << evaluate_expression(expr) << std::endl;
}

Alexey S. Larionov
  • 6,555
  • 1
  • 18
  • 37
  • If the assignment has only 1 array `abc` the `get_value()` can be simplified to not use a map. This is not meant to be critical of the answer but to help a beginner avoid a more complicated solution if not necissary. – drescherjm Aug 19 '20 at 16:16
  • 1
    @drescherjm yes, and it would also spare making new instances of `string`s, by doing `std::string(name)` in `get_value` – Alexey S. Larionov Aug 19 '20 at 16:21