简体   繁体   中英

Logical error causing infinite loop in C

I am working on a project in a basic C class that involves reading polynomial equations in from a file, taking the derivatives, and outputting the derivatives back to a different file.

I am doing the parsing of the array that holds the input from the file in my first function (after main), and there is a large while loop that, in theory, should move through the array and basically figure out what everything in that array is, in terms of a polynomial. The goal is to put the coefficients and exponents into one of two arrays, one for positive exponents and one for negative exponents, where the element number would represent the exponent and the value of the coefficient inside of that array element.

The problem that we have run into is that for some reason, the exponent variable never gets assigned a value. We think that this is due to a possible logic error somewhere in the if statements, but we can't seem to determine what it is. Any help would be greatly appreciated.

#include <stdio.h>
#include <string.h>

void function1 (char *, double *, double *);
void function2 (char *, double *, double *);

int main(void)
{
     char input [40];
     double neg_part[11], pos_part[11];
     double cos[11], sin[11], tan[11];
     int i=0;
     for  (i;i<11;i++)
     {
         neg_part[i]=0;
         pos_part[i]=0;
     }

     FILE *ifp = fopen("functions.txt","r+"), *ofp = fopen("derive.txt","w");
     do
     {
         if (ifp)
             while (!feof(ifp))
             {
                  fgets(input, 40, ifp);
                  function1(&input, &neg_part, &pos_part);
                  function2 (&input, &neg_part, &pos_part);
             }

     }while(!feof(ifp));

}


void function1(char *inputptr, double neg_part[], double pos_part[])
{
    int exponent, i=0;
    double xcoef;
    while (*inputptr!='\n')
    {
        if (isdigit(*(inputptr)))
        {
            if (*(inputptr+1)==' '|| *(inputptr+1)=='-' || *(inputptr+1)=='+')
            {
                 xcoef= strtol(inputptr, &inputptr, 10);
                 exponent=0;
                 pos_part[exponent]=xcoef;
            }

            else if (*(inputptr+1)=='x')
            {
                xcoef= strtol(inputptr, &inputptr, 10);
                if (*(inputptr+1)== '^')
                {
                    inputptr+2;
                    exponent=strtol(inputptr, &inputptr, 10);
                }
                else if(*(inputptr+1)==' ' || *(inputptr+1)=='-' || *(inputptr+1)=='+')
                {
                    exponent=1;
                }
            }
        }

        if (!isdigit(*inputptr))
        {
            if (*inputptr=='x')
            {
                xcoef=1;
                if (*(inputptr+1)=='^')
                {
                    exponent= strtol(inputptr, &inputptr, 10);
                }
                else if (*(inputptr+1)=='+'  ||  *(inputptr+1)=='-'  ||  *(inputptr+1)==' ')
                    exponent= 0;
            }
        }
        if (exponent>=0)
        {
            pos_part[exponent]=xcoef;
        }
        else if(exponent<0)
        {
            exponent=exponent*(-1);
            neg_part[exponent]=xcoef;
        }

        i++;
    }

}
while (*inputptr!='\n')

inputptr doesn't move. There are inputptr+2; .Did you mean inputptr+=2 to increment inputptr by 2 ?

You have a statement: inputptr+2; , which does nothing, logically. Perhaps you meant inputptr+=2 , which increments the inputptr by 2?

Good luck. I want to see this code, looks great!

I would go line by line printing out "here" or some other statement to see where your code is actually reaching. If it never prints out "here" in any of the internal if statements, then probably your while condition isn't being met.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM