#include
#include
typedef struct Node
{
int data;
struct Node *next;
}Node;//链表结点
typedef struct LinkList//定义链表数据类型
{
Node *head;//表头结点
void PrintMform()//输出多项式
{
Node *p=head->next;
int exp = head->data-1;
while(p!=NULL)
{
if(exp==head->data-1)
printf("%dX^%d",p->data,exp);
else
{
if(p->data>0)
printf("+%dX^%d",p->data,exp);
if(p->data==0)
printf("+0");
if(p->data<
0)
printf("%dX^%d",p->data,exp);
}
exp--;
p=p->next;
}
printf("\n");
}
void CreateByInput(int length)//通过输入元素建立链表
{
head=(Node *)malloc(sizeof(Node));
head->data=0;//表头节点存储链表真实长度
head->next=NULL;
int i,temp;
Node *p,*cur = head;
for(i=1;i<=length;i++)
{
printf("structing LinkList,Please input the value:\n");
scanf("%d",&temp);
p = (Node *)malloc(sizeof(Node));
p->data=temp;
p->next=cur->next;
cur->next=p;
cur = cur->next;
head->data++;
}
}
}LinkList;
void main()
{
LinkList L1,L2;
int length;//就是多项式的项数
printf("Please input the first LinkList's length:\n");
scanf("%d",&length);
printf("begin to struct the first LinkList\n");//开始构造第一个多项式
L1.CreateByInput(length);
printf("begin to struct the second LinkList\n");//开始构造第二个多项式
L2.CreateByInput(length);
printf("the first duoxiangshi is:\n");
L1.PrintMform();//输出第一个多项式
printf("the second duoxiangshi is:\n");
L2.PrintMform();//输出第二个多项式
Node *p = L1.head->next;/////////////////从这里开始
Node *q = L2.head->next;//是计算多项式L1-L2,结果存入L1
while(p!=NULL)
{
p->data-=q->data;
p=p->next;
q=q->next;
}/////////////////////////////////////到这里结束
printf("the substract is:\n");
L1.PrintMform();
system("pause");
}
用无进位的高精度加法来算