我必须编写一个程序来求和两个多项式。出于此目的,首先,我正在编写一个程序来获取两个多项式并打印它们。
我的程序是这样的:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct node
{
int power;
int coef;
struct node *link;
};
int input( struct node *Phead);
int display( struct node *Phead);
int main()
{
struct node *Phead= malloc(sizeof(struct node)),*Qhead = malloc(sizeof(struct node));
input(Phead);
display(Phead);
input(Qhead);
display(Qhead);
return 0;
}
int input( struct node *Phead)
{
//Phead
//Qhead = malloc(sizeof(struct node));
int value;
printf("\n\t\tEntry of polynomial:\n");
printf("Enter the coefficient: ");
scanf("%d",&Phead->coef);
printf("Enter the power: ");
scanf("%d",&Phead->power);
Phead->link = NULL ;
printf("Enter the coefficient,( 0 to break ): ");
scanf("%d", &value);
while ( value != 0 )
{
struct node *new_node = malloc(sizeof(struct node ));
new_node -> coef = value;
printf("Enter the power: ");
scanf("%d",&new_node->power);
new_node->link = Phead;
Phead = new_node;
//printf("%d",Phead->power);
printf("Enter the coefficient,( 0 to break ): ");
scanf("%d", &value);
}
return 0;
}
int display( struct node *Phead)
{
struct node *temp = Phead;
//printf("I am in display.\n");
while ( temp != NULL )
{
//printf("I am in while.\n");
printf("%d * x ^ %d + ",temp->coef,temp->power);
temp=temp->link;
}
//printf("%d * x ^ %d + ",temp->coef,temp->power);
//printf("0");
return 0;
}
我无法打印多项式。问题在于temp变量。请帮我解决这个问题。
答案 0 :(得分:-2)
问题在于你的这部分代码:
new_node->link = Phead;
Phead = new_node;
按如下方式解决:
Phead->link = new_node;
Phead = new_node;