1
I’m almost done with my expression interpreter. But I have no idea how to do the most important part : The result
I’d learn a lot from any idea.
main.cpp
#include <string>
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <cmath>
#include <algorithm>
//defines
#define space ' '
//disables any deprecation warning
#pragma warning(disable : 4996)
//usings
using std::vector;
using std::string;
using std::cout;
using std::endl;
long double nabs(long double dub) {
return -abs(dub);
}
string remove_char(string str, char c = space) {
str.erase(std::remove(str.begin(), str.end(), c), str.end());
return str;
}
long double parse(string str) {
return std::stold(str);
}
bool try_parse(string str)
{
char* end = 0;
double val = strtod(str.c_str(), &end);
return end != str.c_str() && val != HUGE_VAL;
}
char first_char(string str) {
return *str.c_str();
}
bool is_opr(string str) {
return first_char(str) == '&';
}
string &first_item(vector<string> vec) {
return vec[0];
}
vector<string> get_types(vector<string> vec) {
for (int i = 0; i < vec.size(); i++) {
string &s = vec[i];
bool doubly = try_parse(s);
bool amp = is_opr(s);
//if unknown
if (!doubly && !amp) {
s = "<unk> " + s;
continue;
}
//if operator
else if (!doubly && amp) {
s = "<opr> " + s;
continue;
}
//if number
else if (doubly && !amp) {
s = "<dub> " + s;
continue;
}
}
return vec;
}
/*
|
|
|
V
*/
long double get_result(vector<string> vec) {
long double val;
for (int i = 0; i < vec.size(); i++) {
//código...
}
return val;
}
vector<string> split(string s, const char c = space)
{
string buff{ "" };
vector<string> v;
for (auto n : s)
{
if (n != c) buff += n; else
if (n == c && buff != "") { v.push_back(buff); buff = ""; }
}
if (buff != "") v.push_back(buff);
return v;
}
string simplify(string expr) {
string iexpr = expr;
for (int i = 0; i < iexpr.length(); i++) {
char& c = iexpr[i];
if (c == '+')
iexpr.replace(i, 1, " &ad ");
else if (c == '-')
iexpr.replace(i, 1, " &sb ");
else if (c == '*')
iexpr.replace(i, 1, " &mp ");
else if (c == '/')
iexpr.replace(i, 1, " &dv ");
}
return iexpr;
}
int main() {
vector<string> sep_rep = get_types(split(simplify("-21 + 32 - 3 * 2")));
for (auto str : sep_rep) {
cout << str << endl;
}
std::cin.get();
return 0;
}
It depends. To get the result, you will have to do the syntactic analysis of this
vector<string>
and then interpret the result. If you just need something silly that doesn’t have parentheses or operator precedence, it’s not very difficult, but it won’t be very functional either. If you need something that you have or will have after parentheses and precedence, then it is a very laborious thing and the result will be a code significantly larger than what you did for the lexical analysis.– Victor Stafusa
What you’re trying to do is syntactic analysis. If you’re not just looking for something simple and silly, I recommend reading this to get started: https://answall.com/q/181635/132 and then read this too: https://answall.com/q/180927/132. It’s worth seeing this one too: https://answall.com/q/2044/132
– Victor Stafusa
Related question (not duplicate): https://answall.com/q/308306/132
– Victor Stafusa
I’ll start with the silly one then think about seeing something bigger.
– Maria Cristina
Vitor Stafusa, This related question is mine
– Maria Cristina
I know. I usually put this kind of comment on serial questions so that other users who bump into your question don’t think it’s duplicate of the other. Or else those who don’t understand something, who see your other question to understand.
– Victor Stafusa