-1
#include<stdio.h>
#include<stdlib.h>
#define TRUE 1
#define FALSE 0

typedef struct polynomial{
    int coeff;
    char var;
    int exp;
    struct polynomial *link;
} poly;

poly* decode(char*);

main()
{
    char polynomial[100];
    poly *first, *second;

    printf("\n---Enter 1st polynomial---\n\n");
    scanf("%s",polynomial);
    first=decode(polynomial);

    printf("\n---Enter 2nd polynomial---\n\n");
    scanf("%s",polynomial);
    second=decode(polynomial);

    //More statements further

return 0;
}

/*--- Decoding Polynomial ---*/

poly* decode(char *polynomial)
{
    poly *p=NULL, *q=NULL, *temp=NULL;
    int i=0, sign;
    short coeff_entry=TRUE, exp_entry=FALSE, var_visited=FALSE, exp_visited=FALSE, coeff_visited=FALSE;

    while(polynomial[i]!='\0') 
    {

        temp=(poly*)malloc(sizeof(poly));
        if(!temp)
        {
            printf("Error! Memory not allocated\n");
            exit(1);
        }

        if(polynomial[i]==43) {i++; sign=1;}
        if(polynomial[i]==45) {i++; sign=-1;}
        while(1)
        {
            if((polynomial[i]>=48&&polynomial[i]<=57)&&coeff_entry==TRUE)
            {
                temp->coeff=10*(temp->coeff)+(polynomial[i]-48);
                coeff_visited=TRUE;
            }
            else if((polynomial[i]>=65&&polynomial[i]<=90)||(polynomial[i]>=97&&polynomial[i]<=122))
            {
                temp->var=polynomial[i];
                coeff_entry=FALSE;
                exp_entry=TRUE;
                var_visited=TRUE;
            }
            else if((polynomial[i]>=48&&polynomial[i]<=57)&&exp_entry==TRUE)
            {
                temp->exp=10*(temp->exp)+(polynomial[i]-48);
                exp_visited=TRUE;
            }
            else if(polynomial[i]==43||polynomial[i]==45||polynomial[i]=='\0')
            {
                exp_entry=FALSE;
                coeff_entry=TRUE;
                if(var_visited&&!exp_visited)
                {
                    temp->exp=1;
                    !var_visited;
                    !exp_visited;
                }
                if(!coeff_visited)
                {
                    !coeff_visited;
                    temp->coeff=1;
                }
                temp->coeff*=sign;              
                break;
            }
            i++;
        }

        //These lines are for debugging purpose only
        printf("\nCoefficient of the term is: %d\n",temp->coeff);
        printf("Variable of the term is: %c\n",temp->var);
        printf("Exponent of the term is: %d\n",temp->exp);

        temp->link=NULL;
        if(p==NULL)     p=q=temp;
        else
        {
            q->link=temp;
            q=q->link;
        }
    }

return p;
}


In my code I am asking user to input a polynomial in the form like: -5x^2+7y^3-19z+5

Everything seems fine but two problems in decoding this polynomial and storing in the linked list form:

First bug comes when the first-most coefficient is positive in polynomial like 17x^3-13z+5
In this case a very long integer value (most probably garbage value) gets store in the respected node of linked list.

Second bug is when there is no first coefficient like x^7-18y^3+z-13
In this case 0 gets stored in the respected node of linked list. In other terms of polynomial, like z in example above, where there is no coefficient 1 gets stored in the coeffient part of the node.

So problem arises with the first coefficient only that too a 'positive coeff' or 'no coefficient' at all.

Ashish Tomer
  • 55
  • 1
  • 8
  • `decode a polynomial`? parse it? [Do not cast the result of `malloc()`](http://stackoverflow.com/a/605858/1983495) – Iharob Al Asimi Feb 25 '15 at 17:49
  • what do you think this `!coeff_visited;` does? You probably mean `coeff_visited = !coeff_visited;`? – Iharob Al Asimi Feb 25 '15 at 17:50
  • did you know that the compiler ignores white space characters? – Iharob Al Asimi Feb 25 '15 at 17:53
  • 1
    If the leading term has no leading sign declaration (such as your `x^7` leading term vs `+x^7` or `-x^7`) the value of `sign` is *indeterminate*. Thusly, evaluation of indeterminate values (such as using said value in a multiplication) invokes *undefined behavior*. It needs to be declared and iniitialized *inside* the `while-loop` with a default value of `1` and changed to `-1` on detection of a leading `-` on the current term. – WhozCraig Feb 25 '15 at 17:54
  • temp is no initialized, temp->coeff should be set to 0 Or all the struct should be set to 0 memset(temp, 0, sizeof(poly)); – Ôrel Feb 25 '15 at 18:00
  • 1
    You can use '0' and '9' instead of 48 and 57, it will be easiest to read – Ôrel Feb 25 '15 at 18:01
  • @AurélienLAJOIE `can` -> `should`, and the same applies to `'+`' and `'-`` and all the other characters... – Iharob Al Asimi Feb 25 '15 at 18:03

1 Answers1

0

There are many errors in your code, the most important is the expresions like

!value;

you need to assign to it, lik

value = !value;

And you are not initializing sign when there is not '+' sign in front of the next token, you need to initialize it to 1, check if there is a '-' and set it to -1 otherwise ignore the '+'.

Now I appended the fixed code, try it

#include <stdio.h>
#include <stdlib.h>
#define TRUE 1
#define FALSE 0

typedef struct _polynomial {
    int coeff;
    char var;
    int exp;
    struct _polynomial *link;
} poly;

poly *decode(char*);

int
main()
{
    char string[100];

    printf("\n---Enter 1st polynomial---\n\n");
    scanf("%99s", string);
    decode(string);

    printf("\n---Enter 2nd polynomial---\n\n");
    scanf("%99s", string);
    decode(string);

    //More statements further

    return 0;
}

/*--- Decoding Polynomial ---*/

poly* decode(char *polynomial)
{
    poly *p = NULL, *q = NULL, *temp = NULL;
    int   i = 0, sign;
    short coeff_entry=TRUE, exp_entry=FALSE, var_visited=FALSE, exp_visited=FALSE, coeff_visited=FALSE;

    while (polynomial[i] != '\0')
    {
        temp = malloc(sizeof(poly));
        if (temp == NULL)
        {
            printf("Error! Memory not allocated\n");
            exit(1);
        }

        sign = 1;
        if (polynomial[i] == 43)
            i++;
        if (polynomial[i] == 45)
        {
            i++;
            sign=-1;
        }

        while (1)
        {
            if ((polynomial[i] >= 48 && polynomial[i] <= 57) && coeff_entry == TRUE)
            {
                temp->coeff   = 10 * temp->coeff + polynomial[i] - 48;
                coeff_visited = TRUE;
            }
            else if ((polynomial[i] >= 65 && polynomial[i] <= 90) || (polynomial[i] >= 97 && polynomial[i] <= 122))
            {
                temp->var   = polynomial[i];
                coeff_entry = FALSE;
                exp_entry   = TRUE;
                var_visited = TRUE;
            }
            else if ((polynomial[i] >= 48 && polynomial[i] <= 57) && exp_entry == TRUE)
            {
                temp->exp=10*(temp->exp)+(polynomial[i]-48);
                exp_visited=TRUE;
            }
            else if (polynomial[i] == 43 || polynomial[i] == 45 || polynomial[i] == '\0')
            {
                exp_entry=FALSE;
                coeff_entry=TRUE;
                if (var_visited && !exp_visited)
                {
                    temp->exp   = 1;
                    var_visited = !var_visited;
                    exp_visited = !exp_visited;
                }
                if (!coeff_visited)
                {
                    coeff_visited = !coeff_visited;
                    temp->coeff   = 1;
                }
                temp->coeff *= sign;

                break;
            }
            i++;
        }

        printf("\nCoefficient of the term is: %d\n", temp->coeff);
        printf("Variable of the term is: %c\n", temp->var);
        printf("Exponent of the term is: %d\n", temp->exp);

        temp->link = NULL;
        if (p == NULL)
            p = q = temp;
        else
        {
            q->link = temp;
            q       = q->link;
        }
    }

    return p;
}
Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
  • `!value;` is not incorrect. The `!` operator performs a logical negation of its operand. This operator actually produces an integer result, either zero or one. And that's what i'm playing with `TRUE==1` and `FALSE==0`
    And the `sign` is being initialized on every iteration, after checking whether the current character in `polynomial` string is `-` or `+` `if(polynomial[i]==43) {i++; sign=1;}` `if(polynomial[i]==45) {i++; sign=-1;}`
    – Ashish Tomer Feb 25 '15 at 18:17
  • 1
    `!value;` does nothing, it's useless expression because `!value`'s result is not stored anywhere, it's not incorrect, but it's useless. You initilize `sign` if and only if a `+` is found otherwise it's uninitizlied and you can check that by executing the program after fixing the `!value;` problem. Not initializing the `sign` variable when there is no `+` is a problem for `x^2 + 2x + 3` for example. – Iharob Al Asimi Feb 25 '15 at 18:35
  • Yes you are right. And I have many logical errors too, I'll rectify them. – Ashish Tomer Feb 26 '15 at 05:29