I've been working on this assignment for over a week now, and it's due this Friday. I need to make a class Evaluator that will take a string and perform two methods on it: getExpression(), which is to simply return the string with any spaces and preceding zeroes removed, and it will put negative values in parentheses. I haven't gotten the getExpression() quite done yet, but it shouldn't be too hard.
The other method is reduce(), which will perform the next operation in the expression. So after getExpression() is called and for example returns: "1*6+30", then I would want to evaluate the 1*6 first, then getExpression() would be called again to verify it now reads "6+30", then call reduce() again so that it will evaluate the addition part.
My prof's hints says that we should split the expression into two arrays; one int array of the values, and one char array of the operators. I've done that. Then when reduce() is called, I searched the operators array for a multiplication, saved its index, and replaced the corresponding value in the values array with values[index] * values[index+1]. But now, according to his hints, we must delete the operator from the operator array and move all following opreators down one position, and after replacing the first value with the result of the operation, I must move all the following values down one position. How would I go about doing this? I was able to move all the values down one position, except that the last position would get duplicated. So in essence, I would like to shrink the size of the array, even though I know you can't (and I initialized both the values array and operators array with room for 20)
edit: Well I should clarify that currently when I move all of the following values down, what used to be the last value in the array is now a zero. When the int array is created, it is given all zeroes by default. I suppose I could put an if statement to determine if what follows is a zero then don't display it, but how would I differentiate between zeroes which are intentional (such as 1+200) and which are just unused spaces in the array that by default are zero.
As I add operators and values to their arrays, I have a counter increment with each addition, so that I have two int variables that will indicate the "filled" size of the array.
edit 2: So do you think I could get by with decrementing my counters to sort of fake that the array is getting smaller, but really I'm just iterating to one less after each reduce().
The other method is reduce(), which will perform the next operation in the expression. So after getExpression() is called and for example returns: "1*6+30", then I would want to evaluate the 1*6 first, then getExpression() would be called again to verify it now reads "6+30", then call reduce() again so that it will evaluate the addition part.
My prof's hints says that we should split the expression into two arrays; one int array of the values, and one char array of the operators. I've done that. Then when reduce() is called, I searched the operators array for a multiplication, saved its index, and replaced the corresponding value in the values array with values[index] * values[index+1]. But now, according to his hints, we must delete the operator from the operator array and move all following opreators down one position, and after replacing the first value with the result of the operation, I must move all the following values down one position. How would I go about doing this? I was able to move all the values down one position, except that the last position would get duplicated. So in essence, I would like to shrink the size of the array, even though I know you can't (and I initialized both the values array and operators array with room for 20)
edit: Well I should clarify that currently when I move all of the following values down, what used to be the last value in the array is now a zero. When the int array is created, it is given all zeroes by default. I suppose I could put an if statement to determine if what follows is a zero then don't display it, but how would I differentiate between zeroes which are intentional (such as 1+200) and which are just unused spaces in the array that by default are zero.
As I add operators and values to their arrays, I have a counter increment with each addition, so that I have two int variables that will indicate the "filled" size of the array.
edit 2: So do you think I could get by with decrementing my counters to sort of fake that the array is getting smaller, but really I'm just iterating to one less after each reduce().