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


在我的代码中,我要求用户输入如下形式的多项式:-5x^2+7y^3-19z+5

一切似乎都很好,但是在解码这个多项式并以链表形式存储时存在两个问题:

第一个错误出现在第一个系数在多项式中为正,如17 x^3-13z+5
在这种情况下,一个非常长的整数值(很可能是垃圾值)存储在链表的相关节点中。

第二个错误是当没有像x ^7-18y^3+z-13这样的第一个系数时,
在这种情况下, 0被存储在链表的相关节点中。在多项式的其他术语中,例如上面示例中的z,其中没有系数1存储在节点的系数部分。

因此,第一个系数出现的问题只是“正系数”或“无系数”。

4

1 回答 1

0

您的代码中有很多错误,最重要的是表达式

!value;

你需要分配给它,喜欢

value = !value;

并且下一个token前面sign没有'+'sign的时候没有初始化,需要初始化为1,检查是否有a '-',否则设置为-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*);

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;
}
于 2015-02-25T17:57:25.367 回答