1. ## Adding multiplication signs to a math formula...

Hey,

I'm wrapping up a project for school and for the life of me I cant figure out how to place multiplication signs in the correct places of a math formula.

The basic idea of the project is to multiply matrices in the most optimal way. So, Ive written the code to find the optimal parenthesization and Ive written a function to multiply two matrices together.

I am left with (A1((A2A3)(((A4A5)A6)A7))) where each A is a matrix. I need to place multiplication signs in the correct places so I can put it into an expression tree and evaluate it.

So I need to get from (A1((A2A3)(((A4A5)A6)A7))) to (A1*((A2*A3)*(((A4*A5)*A6)*A7)))

But how?!? Everything I have tried doesn't work!

Any suggestions?! Thanks...

BC

2. Originally Posted by BC2210
Hey,

So I need to get from (A1((A2A3)(((A4A5)A6)A7))) to (A1*((A2*A3)*(((A4*A5)*A6)*A7)))

But how?!? Everything I have tried doesn't work!

BC
I would have suggested a stack for this, and then use Reverse Polish Notation perhaps, but...

First blush looks like you only want the * after a digit, unless:

1) The digit is the last digit in the equation: like A7)))
or
2) the digit is followed by a closing parenthesis, in which case the digit goes immediately after the closing parenthesis.

Load them doggies up into a char array, and add 'em in, rawhide!

Edit: Well, that may not be really as helpful as I'd like. Tell you what, why don't you post up some sample strings - may 10 or 20, so we'll have something to play with. Trying to work from one example is not good.