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;
}