My program reduce expression to one value.
I have a problem with changing “char sign” as a character of action. Could you show me some simple solution or idea how to do it?
I tried:
(tab[i]-'0') 'sign' (tab[i+1]-'0');
This is the full code:
#include <cstdlib>
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
char* tab = "12+";
int b = sizeof (tab);
char* tmp = new char[b] ;
tmp [b-1] = '\0';
int k = b/3;
for(int i=0; i<k; i++){
if(isdigit(tab[i]) && isdigit(tab[i+1]) ){
if(tab[i+2]=='+' || tab[i+2]=='-' || tab[i+2]=='*'){
char sign = tab[i+2];
int n = (tab[i]-'0') + (tab[i+1]-'0'); //here is a problem, i want to replice + as a char sign which will be recognized
tmp[i] = n+'0';
}
else goto LAB;
}
else if (isdigit(tab[i]) && isdigit(tab[i+2])){
}
else if (isdigit(tab[i+1]) && isdigit(tab[i+2])){
}
else
LAB:
tmp[i]= tab[i];
}
cout<<"Import "<<tmp[0]-'0'<<endl;
system("PAUSE");
return EXIT_SUCCESS;
}
You cannot replace an operator, or function name with a symbol which is given by user, because compiler has to know which function should be called and there is no mechanism to convert a ‘+’ character to
operator+(int, int)call embedded in a language. You have to write it by yourself.The simplest solution is to write explicitly every operator you want to support: