C语言实现的一元多项式的表示及相减

#include<stdio.h>#include<malloc.h>typedefstructPolynode{intcoef;intexp;structPolynod... #include<stdio.h>
#include<malloc.h>
typedef struct Polynode

{
int coef;
int exp;
struct Polynode *next;
} Polynode;

Polynode* polycreate(/*Polynode *head*/)
{
Polynode *h, *rear, *s,*temp;
int c,e;
h=(Polynode *)malloc(sizeof(Polynode)); /*建立多项式的头结点*/
h->next = NULL;
scanf("%d%d",&c,&e);/*键入多项式的系数和指数项*/
while(c!=0) /*若c=0,则代表多项式的输入结束*/
{
rear = h;//rear不再指向尾节点,而是从头结点开始遍历
temp = rear->next;
while(temp!=NULL && temp->exp <e)
{
rear = temp;
temp = rear->next;
}
if(temp!='\0'&&temp->exp==e) {temp->coef+=c;}
else{s=(Polynode*)malloc(sizeof(Polynode)); /*申请新的结点*/
s->coef=c ;
s->exp=e ;
s->next = temp;//插入
rear->next = s;
}
scanf("%d%d",&c,&e);
}
return(h);
}

void polylist_sub(Polynode *head1,Polynode *head2)
{
int sum;Polynode *temp;
Polynode *p=head1->next,*q=head2->next,*r=head1;
while(p!=NULL&&q!=NULL)
{
if(p->exp<q->exp) {r->next=p;r=r->next;p=p->next;}

else if(p->exp>q->exp) {r->next=q;r=r->next;q=q->next;}

else {sum=(p->coef)-(q->coef);

if(sum!=0) {p->coef=sum;r->next=p;r=r->next;p=p->next;temp=q;q=q->next;free(temp);}

else {temp=p;free(p);p->next=temp;

temp=q;free(q);q->next=temp;}
}
if(p!=NULL) r->next=p;
else
r->next=q;
}}

void printf_list(Polynode *head)
{
Polynode *p=head->next;
while(p)
{if(p->coef<0)
{printf("%dx^%d",p->coef,p->exp);if(p->next)
printf("+");p=p->next;}

else {printf("%dx^%d",p->coef,p->exp);if(p->next)
printf("-");p=p->next;}
}}
int main()
{
Polynode *head1,*head2;
//clrscr();
head1=polycreate(/*head1*/);
head2=polycreate(/*head2*/);
printf_list(head1);
printf("\n");
printf_list(head2);
printf("\n");
polylist_sub(head1,head2);
printf_list(head1);
getchar();
getchar();
return 0;
}
这是由加法的基础上写再来的,有个问题就是怎样对相减后输出时的系数前的符号进行控制呢,究竟要在哪一部分来实现这个功能呢?好像上面的这个程序,编译后,当输入
2 3 4 5 0 0
4 3 5 6 0 0
时,程序输出的结果是

那么要怎样改变被减多项式中的各项系数前的符号呢?
展开
 我来答
头永迎8279
推荐于2016-08-09 · TA获得超过383个赞
知道答主
回答量:263
采纳率:0%
帮助的人:152万
展开全部

#include<stdio.h>
#include<stdlib.h>

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");
}
王博宇87王之行
2011-09-12 · 超过35用户采纳过TA的回答
知道答主
回答量:144
采纳率:100%
帮助的人:45.6万
展开全部
不好意思啊,别选我,我只是做任务
{
一、0-5x^6
如果上面那个多项式没有六次方项,而下面一个有正的六次方项,相减的结果应该是负的,我觉得好像代码里没有体现这个吧。

二、"-2x^3-" || "4x^5+"
你会疑惑为什么我写这么奇怪的式子
你仔细看看这段代码(摘自printf_list):
if(p->coef<0)
{
printf("%dx^%d",p->coef,p->exp);
if(p->next) printf("+");
p=p->next;
}
else
{
printf("%dx^%d",p->coef,p->exp);
if(p->next) printf("-");
p=p->next;
}
简单的说就是当前项是负的,那么先输出当前项,在下一项存在的情况下输出一个负号。。。
}

最后我想说~~代码贴的太长而且花括号啊,断行啊太乱了~~看着实在是累~~这样不太容易吸引人来关注这个问题~~ 要不是为了这个悬赏的五分,我看我也是不会来看的,呵呵,最后一句说笑~~
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
百度网友106f88e
2011-09-12 · TA获得超过664个赞
知道小有建树答主
回答量:379
采纳率:0%
帮助的人:419万
展开全部
两个疑惑:
{
一、0-5x^6
如果上面那个多项式没有六次方项,而下面一个有正的六次方项,相减的结果应该是负的,我觉得好像代码里没有体现这个吧。

二、"-2x^3-" || "4x^5+"
你会疑惑为什么我写这么奇怪的式子
你仔细看看这段代码(摘自printf_list):
if(p->coef<0)
{
printf("%dx^%d",p->coef,p->exp);
if(p->next) printf("+");
p=p->next;
}
else
{
printf("%dx^%d",p->coef,p->exp);
if(p->next) printf("-");
p=p->next;
}
简单的说就是当前项是负的,那么先输出当前项,在下一项存在的情况下输出一个负号。。。
}

最后我想说~~代码贴的太长而且花括号啊,断行啊太乱了~~看着实在是累~~这样不太容易吸引人来关注这个问题~~ 要不是为了这个悬赏的五分,我看我也是不会来看的,呵呵,最后一句说笑~~
本回答被网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
矫情的兰二瓜
2016-02-06 · 超过24用户采纳过TA的回答
知道答主
回答量:45
采纳率:0%
帮助的人:27.2万
展开全部
这个要用到字符串来实现
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 1条折叠回答
收起 更多回答(2)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式