多项式相加C语言
来源:学生作业帮 编辑:作业帮 分类:综合作业 时间:2024/10/03 07:21:44
多项式相加C语言
#include <stdio.h>
#include <malloc.h>
struct SLL
{
\x09// ax^r
\x09int a;
\x09int r;
\x09struct SLL* next;
};
struct SLL *add(struct SLL *head, int a, int r)
{
\x09struct SLL *t;
\x09struct SLL *tt;
\x09for(t=head, tt=NULL; t; tt = t, t = t->next) if(t->r <= r) break;
\x09if(!t)
\x09{
\x09\x09struct SLL *node = (struct SLL*)malloc(sizeof(struct SLL));
\x09\x09node->a = a;
\x09\x09node->r = r;
\x09\x09node->next = NULL;
\x09\x09if(!tt) return node;
\x09\x09tt->next = node;
\x09\x09return head;
\x09}
\x09if(t->r == r)
\x09{
\x09\x09t->a += a;
\x09\x09if(t->a == 0)
\x09\x09{
\x09\x09\x09if(!tt)
\x09\x09\x09{
\x09\x09\x09\x09tt = head;
\x09\x09\x09\x09head = head -> next;
\x09\x09\x09\x09free(tt);
\x09\x09\x09\x09return head;
\x09\x09\x09}
\x09\x09\x09tt->next = t->next;
\x09\x09\x09free(t);
\x09\x09\x09return head;
\x09\x09}
\x09}
\x09if(t->r < r)
\x09{
\x09\x09struct SLL *node = (struct SLL*)malloc(sizeof(struct SLL));
\x09\x09node->a = a;
\x09\x09node->r = r;
\x09\x09node->next = t;
\x09\x09if(!tt) return node;
\x09\x09tt->next = node;
\x09}
\x09
\x09return head;
}
void print(struct SLL *head)
{
\x09struct SLL *t = head;
\x09for(; t; t=t->next)
\x09{
\x09\x09printf("%d %d\n", t->a, t->r);
\x09}
}
int main()
{
\x09struct SLL *head = NULL;
\x09int m, n, a, r, i;
\x09scanf("%d%d", &m, &n);
\x09for(i=0; i<m+n; i++)
\x09{
\x09\x09scanf("%d %d", &a, &r);
\x09\x09head = add(head, a, r);
\x09}
\x09print(head);
\x09return 0;
}
#include <malloc.h>
struct SLL
{
\x09// ax^r
\x09int a;
\x09int r;
\x09struct SLL* next;
};
struct SLL *add(struct SLL *head, int a, int r)
{
\x09struct SLL *t;
\x09struct SLL *tt;
\x09for(t=head, tt=NULL; t; tt = t, t = t->next) if(t->r <= r) break;
\x09if(!t)
\x09{
\x09\x09struct SLL *node = (struct SLL*)malloc(sizeof(struct SLL));
\x09\x09node->a = a;
\x09\x09node->r = r;
\x09\x09node->next = NULL;
\x09\x09if(!tt) return node;
\x09\x09tt->next = node;
\x09\x09return head;
\x09}
\x09if(t->r == r)
\x09{
\x09\x09t->a += a;
\x09\x09if(t->a == 0)
\x09\x09{
\x09\x09\x09if(!tt)
\x09\x09\x09{
\x09\x09\x09\x09tt = head;
\x09\x09\x09\x09head = head -> next;
\x09\x09\x09\x09free(tt);
\x09\x09\x09\x09return head;
\x09\x09\x09}
\x09\x09\x09tt->next = t->next;
\x09\x09\x09free(t);
\x09\x09\x09return head;
\x09\x09}
\x09}
\x09if(t->r < r)
\x09{
\x09\x09struct SLL *node = (struct SLL*)malloc(sizeof(struct SLL));
\x09\x09node->a = a;
\x09\x09node->r = r;
\x09\x09node->next = t;
\x09\x09if(!tt) return node;
\x09\x09tt->next = node;
\x09}
\x09
\x09return head;
}
void print(struct SLL *head)
{
\x09struct SLL *t = head;
\x09for(; t; t=t->next)
\x09{
\x09\x09printf("%d %d\n", t->a, t->r);
\x09}
}
int main()
{
\x09struct SLL *head = NULL;
\x09int m, n, a, r, i;
\x09scanf("%d%d", &m, &n);
\x09for(i=0; i<m+n; i++)
\x09{
\x09\x09scanf("%d %d", &a, &r);
\x09\x09head = add(head, a, r);
\x09}
\x09print(head);
\x09return 0;
}
C语言写多项式相加怎么写?
数据结构(C语言)用单链表存储一元多项式,并实现两个多项式的相加运算
数据结构:用链表实现两个多项式相加,用C++或者C语言实现多项式相加,求完整代码
数据结构(C语言) 如何分别用顺序和链式 实现一元多项式的相加?
如何用C语言实现两个一元多项式的相加和相乘?
在C语言实现的一元多项式的相加操作中,怎样同时实现合并一个多项式中的同类项?
多项式计算 C语言编程
c语言 计算多项式的程序
一元多项式的实现(C语言)
用C语言编程多项式计算
C程序设计 急啊多项式计算输入并建立多项式;输出多项式;两个多项式相加,建立并输出和多项式两个多项式
matlab 多项式函数相加