Вопрос

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;
}
Это было полезно?

Решение

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];

Другие советы

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)
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top