C语言实现一元多项式相加(源代码)

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

signal=1; }//若新结点中指数比最后一个结点即 now 中的指数大,则插入 now 之前 else//若指数相等则需合并为一个结点,若相加后指数为 0 则释放该结点 {
now->coef=now->coef+input->coef; signal=1; free(input); if(now->coef==0) {
else {
pre=pre->next; input=(PLOYList *)malloc(sizeof(PLOYList));//申请空间 input->coef=pre->coef; input->expn=pre->expn; input->next=NULL; insert(head,input); // 把 g(x)插入到 f(x)中,相当于两者相加,结果保存于 f(x) } } return head; }
}PLOYList;
void insert(PLOYList *head,PLOYList *input) //查找位置插入新链节的函数,且让输入的多项式呈降序排 列 {
PLOYList *pre,*now; int signal=0; pre=head; if(pre->next==NULL) {pre->next=input;} //如果只有一个头结点,则把新结点直接连在后面 else {
} printf("\n"); }
void start() //用户选择界面 {
printf(" *
1.两个一元多项式相加
*\n");
printf(" *
0.退出系统
*\n");
printf("
\n");
printf(" 注:输入多项式格式为:系数 1 指数 1 系数 2 指数 2 …… ,并以 0 0 结束:\n");
C 语言实现一元多项式相加(源代码)
#include<stdio.h> #include<malloc.h>//动态申请空间的函数的头文件
typedef struct node //定义节点类型
{
float coef;
//多项式的系数
int expn;
//多项式的指数
struct node * next; //结点指针域
void print(PLOYList *fun) //输出多项式,fun 指要输出的多项式链表的表头 {
PLOYList *printing; int flag=0; printing=fun->next; if(fun->next==NULL)//若为空表,则无需输出 {
printf("0\n"); return; } while(flag==0) {
input=(PLOYList *)malloc(sizeof(PLOYList)); //创建新链节 input->coef=x; input->expn=y; input->next=NULL; insert(head,input); //每输入一项就将其排序,是的链表中多项式呈降序排列 scanf("%f %d",&x,&y); } return head; }
printf("
\n");
printf(" 请选择操作: ");
}
void main() {
PLOYList *f,*g,*pf,*hf,*p; int sign=-1; start(); while(sign!=0) {
scanf("%d",&sign); switch(sign) {
case 0: break; case 1://多项式相加 { printf(" 你选择的操作是多项式相加:\n"); printf(" 请输入第一个多项式 f(x):"); f=creat('f'); printf(" 第一个多项式为:f(x)="); print(f); printf(" 请输入第二个多项式 g(x):"); g=creat('g'); printf(" 第二个多项式为:g(x)="); print(g); printf(" 结果为:F(x)=f(x)+g(x)="); f=add(f,g); print(f); printf("\n\n"); printf(" 继续请选择相应操作,退出请按 0. "); break;
}
} } }
pre->next=now->next; free(now); } }//else } //while }//else }//void
PLOYList *creat(char ch) //输入多项式 {
PLOYList *head,*input; float x; int y; head=(PLOYList *)malloc(sizeof(PLOYList)); //创建链表头 head->next=NULL; scanf("%f %d",&x,&y);//实现用户输入的第一个项,包括其指数和系数 while(x!=0)//当用户没有输入结束标志 0 时可一直输入多项式的项,且输入一个创建一个结点 {
PLOYList *add(PLOYList *head,PLOYList *pre) 为第二个多项式建立的链表表头 {
PLOYList *input; int flag=0;
//多项式相加,head 为第一个多项式建立的链表表头,pre
Hale Waihona Puke Baidu
while(flag==0) {
if(pre->next==NULL) flag=1; //若该链表为空,则无需进行加法运算,跳出循环
now=pre->next;//如果不是只有一个头结点,则设置 now 指针 while(signal==0) { if(input->expn < now->expn) {
if(now->next==NULL) {
now->next=input; signal=1; } else { pre=now; now=pre->next;//始终让新输入的数的指数与最后一个结点中的数的指数比较,小于则插在其 后面 } } else if( input->expn > now->expn ) { input->next=now; pre->next=input;
if(printing->coef>0&&fun->next!=printing) printf("+"); if(printing->coef==1); else if(printing->coef==-1) printf("-"); else printf("%f",printing->coef); if(printing->expn!=0) printf("x^%d",printing->expn); else if((printing->coef==1)||(printing->coef==-1)) printf("1"); if(printing->next==NULL) flag=1; else printing=printing->next;
相关文档
最新文档