Let me explain what I want to do:

I want to change a expression like this: x * (2 + 3) - (y + 1)

to this: 5 * (2 + 3) - (6 + 1)

using this:

instVar('x', 5);
instVar('y', 6);

The problem is I get the following output:

5 * (2 + 3) - (y + 1)
x * (2 + 3) - (6 + 1)

Here is my code:

    size_t pos = 0;
    while ((pos = originalExpr.find(var, pos)) != std::string::npos)
    {
        modifiedExpr = originalExpr;
        char a_val = '0' + val;
        std::replace( modifiedExpr.begin(), modifiedExpr.end(), var, a_val);
        pos++;
    }


    //Testing the output if it works
    cout << modifiedExpr << endl;

How can I change it so that the output can give me this:

5 * (2 + 3) - (y + 1)
5 * (2 + 3) - (6 + 1)

modifiedExpr = originalExpr; needs to be moved outside the loop. The way you have it now is every time the loop is executed modifiedExpr becomes the original again erasing what you just did.

Yeah, I tried that. Still gives me the same output.

When the function exits are you updating the original expression to be the modified? If not then this will always happen. I think you need to store the modified expression in the class and when you first set the expression set the modified expression to be the same as the original. then whenever you call a function that modifies the expression use the modified expression.

Member Avatar for iamthwee

Don't understand the fuss

#include <iostream>
#include <string>
#include <vector>

using namespace std;


string  replace(string expression, char a, char b)
{

   for (int i=0; i<expression.length(); i++)
   {
    if(expression[i]==a)
    {
      expression[i] = b;
    }
   }
   return expression;

}


int main()
{

  string var = "x *(2+3)-(y+1)";

  string tmp = replace(var,'x','5');
  cout << replace(tmp,'y','1');




}
Member Avatar for iamthwee

Or OOP

#include <iostream>
#include <string>
#include <vector>

using namespace std;

class Expression
{
  public:
   string my_expression;

   void set(string k)
   {
     my_expression = k;
   }

   void set_string(char a, char b)
   {
      for (int i=0; i<my_expression.length(); i++)
     {
      if(my_expression[i]==a)
      {
        my_expression[i] = b;
      }
     }

   }

   void print()
   {
    cout << my_expression;
   }



};





int main()
{

  Expression one;
  one.set("x *(2+3)-(y+1)");

  one.set_string('x','5');
  one.set_string('y','7');

  one.print();

  return 0;

}

Yeah that could work, but still, the problem remains getting to change a previous instantiated variable. If you add another one.set_string('x','3') then it will unfortunately still remain as:

5 * (2 + 3) - (7 + 1)

I appreciate your efforts so far. I really appreciate it!

Member Avatar for iamthwee

OK this should fix it...

Expression one;
  one.set("x *(2+3)-(y+1)");

  one.set_string('x','5');
  one.set_string('y','7');

  one.print();

  Expression two;
  two.set("x *(2+3)-(y+1)");
  two.set_string('x','3');
  two.set_string('y','7');

  two.print();

Woah! So close. But I am not allowed to add another object of the class. I must only work with one object and be able to change it like this:

Expression expr("x * (2 + 3) - (y + 1)");

//Assigning values to the variables
expr.instantiateVariable('x',5); // -> 5 * (2 + 3) - (y + 1) = Output
expr.instantiateVariable('y',6); // -> 5 * (2 + 3) - (6 + 1) = Updated output
expr.instantiateVariable('y',7); // -> 5 * (2 + 3) - (7 + 1) = Updated output

Which should give the output such as:

5 * (2 + 3) - (y + 1)
5 * (2 + 3) - (6 + 1)
5 * (2 + 3) - (7 + 1)
Member Avatar for iamthwee

Hmm let me think about that one, can I ask will the variables that you replace be only one digit long or could it be more than one digit or even a decimal:

e.g x = 324 or x = 1.4323

Member Avatar for iamthwee

Actually, I wanted to ask are you 100% sure you're not allowed to instantiate another object, the reason I ask is this seems more complicated than it should be for an assignment.

The only way I can see you doing this is to store the index positions of all the letter variables in an array, but it gets hairy real quick if you are substituting letters for integers larger than 9 or decimal numbers.

Additionally, what if your expression has ab + (5-9)

You would have to go through and put a start inbetween the a and b making a*b and what about a(2-3) that would make a*(2-3) If I was setting this assignment I would definitely not expect something like this, I would just be looking to understand if you get the shunting algorithmn working which, in itself is more than complicated by itself.

Well we are working with ints and not doubles (Thank goodness). Yeah I expect that we must be able to to "ab = a*b". It's a heavy assignment... :(

Member Avatar for iamthwee

Better go clarify that with your teacher as I suspect you won't need to convert ab to a*b or 2abc^2 to 2*a*b*c*c

Don't assume anything otherwise you'll end up wasting time. Also clarify by ints will those ints be larger than 9, i.e. more than one character long - this is important as it will affect the complexity of your program.

If your teacher is any good he'll point you in the right direction.

If you need to be able to change a variable at any time why not store everything as tokens? Then the token for a variable can hold onto the variable and the value of it. When you read in the expression you would tokenize the string and create a stack, list or vector of tokens. Then you could change variables at any time.

NVM guys. I managed with the instantiation. I will share my code with you.

I might have more questions so stay tuned! ^^

void Expression::instantiateVariable(char var, int val)
{
    string valA;
    valA = numberToString(val);
    for(int i = 0; i < originalExpr.length();i++)
    {
        if(originalExpr[i] == var)
        {
            modifiedExpr.replace(i, 1, valA);
        }
    }
}

string Expression::numberToString(int number)
{
    ostringstream ss;
    ss << number;
    return ss.str();
}
Member Avatar for iamthwee

In your function it is probably not a good idea to call your two parameters the same name.

I would go for function(char val1, int val2) that way you know exactly which one is which.

@iamthewee The OP's function uses var and val. I don't think it would compile if they had the same name. I think that would be a type redeclaration error.

Member Avatar for iamthwee

Oh God you're right, my eyes were playing tricks on me.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.