0

I am working on a project that carries out an operation between two polynomials. The polynomials are read in from a text file, which I named "functions.txt", and are in the form of "(2*x^2+5*x^4-3*x)+(6*x+2*x^3+x^5)". There are an unknown amount of these equations and the operation can be '-', '+' or ''. I have managed to read in the file and store each character into a character array. At this point, I am simply having trouble trying to find the math operator ('', '-', or '+'). I figured find the ')' in the string and take the character immediately after it and store it into the mathOperator; if the character after ')' is not '\0'. However, this does not seem to work as it returns " ". Any suggestions and help are greatly appreciated. Here is where the problem is:

if(polyFile.good())
{
    while(!polyFile.eof() && counter < MAX_SIZE)
    {
        polyFile.get(polyArray[counter]);
        ++counter;
    }

    polyArray[counter - 1] = '\0';

    for(int i = 0; i < polyFile.eof(); ++i)
    {
        if(polyArray[i] = ')')
        {
            polyArray[i + 1] = mathOperator;
            cout << mathOperator;
        }
    }

}
else
{
    cout << "The file could not be opened." << endl;
}
Costigan555
  • 11
  • 1
  • 6
  • One question - did you come up with this way to process polynomials? If you did, then I'll just let you know that the way this is usually done is writing a grammar/parser, and not trying to pick off characters one at a time in an ad-hoc fashion. – PaulMcKenzie May 07 '14 at 02:15
  • I originally had the information put into a string array, but I figured for this project I should put each of the characters into a character array because I will need to compare each of the values later to carry out the operation. I also have very limited experience with C++ and programming in general. – Costigan555 May 07 '14 at 02:28
  • The issue isn't a character array or a C++ issue. The real issue is that your approach will fail to work for anything that is slightly different than the test case you're using. What if the parentheses are nested? What about numbers that are more than 1 digit? How about order of operations, where multiplication and division are done before addition and subtraction? Please see here for expression parsing and what is involved in doing this correctly: http://stackoverflow.com/questions/11703082/parsing-math-expression-in-c-c – PaulMcKenzie May 07 '14 at 08:13
  • Also here: http://www.codeproject.com/Articles/88435/Simple-Guide-to-Mathematical-Expression-Parsing Basically, you can be an ace C++ programmer and get the approach wrong in solving these problems. The right approach is to have a grammar, and then parse according to that grammar. – PaulMcKenzie May 07 '14 at 08:22
  • Thanks everybody for all of the help. I have completely changed my approach to this program and am almost finished with it. However, the program seems to be outputting the wrong answer and I can't seem to figure out why. I have re-posted if anyone is interested. http://stackoverflow.com/questions/23531571/c-equations-with-polynomials – Costigan555 May 08 '14 at 02:35

2 Answers2

1

There're some issues in this block

for(int i = 0; i < polyFile.eof(); ++i)
{
    if(polyArray[i] = ')')
    {
        polyArray[i + 1] = mathOperator;
        cout << mathOperator;
    }
}

1/ In the for loop, you want to use i < counter rather than polyFile.eof()

2/ In the if statement, you probably want to use if(!strcmp(polyArray[i], ")")); "=" is an assignment operator, not a comparison one

3/ This line:

polyArray[i + 1] = mathOperator;

mean you're assigning mathOperator to polyArray[i+1], not storing whatever in polyArray[i+1] to mathOperator. This is what you desired:

mathOperator = polyArray[i + 1];
tmlai
  • 372
  • 1
  • 8
  • Thanks. This helped a lot. Silly mistakes on my part, but it is picking up the " after the last ')' in every line. Does the compiler see this as a regular character? – Costigan555 May 07 '14 at 02:30
  • Never mind, I managed to fix it. Thanks again for your help. Much appreciated. – Costigan555 May 07 '14 at 02:32
0

This line

for(int i = 0; i < polyFile.eof(); ++i)

should be using counter to go through the array

for(int i = 0; i < counter - 1; ++i)
The Dark
  • 8,453
  • 1
  • 16
  • 19