⑴ 数据结构(c语言)用单链表存储一元多项式,并实现两个多项式的相加运算,怎么做
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
typedef int ElemType;
/*单项链表的声明*/
typedef struct PolynNode{
int coef; // 系数
int expn; // 指数
struct PolynNode *next; }PolynNode,*PolynList;
/*正位序(插在表尾)输入n个元素的值,建立带表头结构的单链线性表*/
/*指数系数一对一对输入*/ void CreatePolyn(PolynList &L,int n)
{
int i;
下载
原文档已转码为如下格式,以便移动设备查看
数据结构(c语言)用单链表存储一元多项式,并实现两个多项式的相加运算【最新】
阅读:1037次 页数:36页 2016-03-21 举报
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
typedef int ElemType;
/*单项链表的声明*/
typedef struct PolynNode{
int coef; // 系数
int expn; // 指数
struct PolynNode *next; }PolynNode,*PolynList;
/*正位序(插在表尾)输入n个元素的值,建立带表头结构的单链线性表*/
/*指数系数一对一对输入*/ void CreatePolyn(PolynList &L,int n)
{
int i;
PolynList p,q;
L=(PolynList)malloc(sizeof(PolynNode)); // 生成头结点
L->next=NULL;
q=L;
printf("成对输入%d个数据 ",n);
for(i=1;i<=n;i++)
{
p=(PolynList)malloc(sizeof(PolynNode));
scanf("%d%d",&p->coef,&p->expn); //指数和系数成对输入
q->next=p;
q=q->next;
}
p->next=NULL;
}
// 初始条件:单链表L已存在
// 操作结果: 依次对L的每个数据元素调用函数vi()。一旦vi()失败,则操作失败
void PolynTraverse(PolynList L,void(*vi)(ElemType, ElemType)) {
PolynList p=L->next;
while(p)
{
vi(p->coef, p->expn);
if(p->next)
{
printf(" + "); //“+”号的输出,最后一项后面没有“+”
}
p=p->next;
}
printf(" ");
}
/*ListTraverse()调用的函数(类型要一致)*/ void visit(ElemType c, ElemType e) {
if(c != 0)
{
printf("%dX^%d",c,e); //格式化输出多项式每一项
}
}
/* 多项式相加,原理:归并 */ /* 参数:两个已经存在的多项式 */ /* 返回值:归并后新的多项式的头结点 */
PolynList MergeList(PolynList La, PolynList Lb) {
PolynList pa, pb, pc, Lc;
pa = La->next;
pb = Lb->next;
Lc = pc = La; // 用La的头结点作为Lc的头结点
while(pa&&pb)
{
if(pa->expn < pb->expn)
{
pc->next = pa; //如果指数不相等,pc指针连上指数小的结
点,
pc = pa;
pa = pa->next; //指向该结点的指针后移
}
else if (pa ->expn > pb->expn )
{
pc->next = pb; //pc指针连上指数小的结点,
pc = pb;
pb = pb->next; //指向该结点的指针后移
}
else //(pa ->expn = pb->expn )
{
pa->coef = pa->coef + pb->coef; //指数相等时,系数相加
pc->next = pa;
pc = pa;
pa = pa->next; //两指针都往后移
pb = pb->next;
}
}
pc->next = pa ? pa:pb; // 插入剩余段
return Lc;
}
void main()
{
PolynList ha,hb,hc;
printf("非递减输入多项式ha, ");
CreatePolyn(ha,5); // 正位序输入n个元素的值
printf("非递减输入多项式hb, ");
CreatePolyn(hb,5); // 正位序输入n个元素的值
⑵ C/C++数据结构与算法,一元多项式问题
给你一个大致的思路(以下是思路的伪代码):
1)链运洞使用链表,链表的每个节点表示多项式的一个项,结点定义如下:
typedef struct
{
double coeff;
int power;
pItem next;
} item, *pItem;
2) 定义链表头指针
pItem head = null;
3) 打开输入文件
4) 从文件棚枯读入,每读入一行,动态生悄消成一个项并加入到链表中
/*申请内存,动态建立一个项*/
pItem p = (pItem)malloc(sizeof(item));
/*假设读入系数为4, 幂为1*/
p->coeff = 4;
p->power = 1;
/*加入到链表中*/
pItem q = head;
while(q!= null)
{
q=q->next;
}
q.next = p;
p.next = null;
5) 输入表达式
pItem r = head;
while(r != null)
{
printf("%fx^%d", r->coeff, r.power);
r = r->next;
}
ps. 上面是伪代码,忽略了诸如读入文件、输出是每个项前的运算符等很多细节。仅供参考
⑶ 用单链表实现一元多项式加法
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
typedef struct _POLYNODE{
int coef;//系数
int exp;//指数
struct _POLYNODE *next;
}polynode,*polyptr;
void createPoly(polynode **P, char ch[]);//建立多项式链表
void polyAdd(polynode *A,polynode *B);/雹搜/多项式加
void polyMinus(polynode *A,polynode *B);//减
void polyMulti(polynode *A,polynode *B);//乘
void polyDiv(polynode *A,polynode *B);//除
void order(polynode **P);//排序
void display(polynode *P);//展示多项式
void destroy(polynode **P);//销毁多项式
void menu();//命令菜单
int isPut(char ch[]);
//菜单
void menu(){
printf("1.输入多项式.\n"
"2.多项式相加.\n"
"3.多项式相减.\n"
"4.多项式相乘.\n"
"5.多项式相除.\n"
"6.显示多项式.\n"
"7.销毁塌贺多项式.\n"
"8.退出.\n");
}
//判断菜单选择
int IsChoice(int choice){
if(0 < choice && 9 > choice)
return 1;
else
return 0;
}
int isPut(char ch[]){
int i,j = 1;
for(i = 0; ch[i] != '\0'; i++){
{if(0 == j && '^' == ch[i])
return 0;
if('^' == ch[i] && 1 == j)
j = 0;
if(('+' ==ch[i] || '-' == ch[i] || '*' == ch[i] || '/' == ch[i]) && 0 == j)
j = 1;
}
if('.' != ch[i] && 'x' != ch[i] && 'X' != ch[i] && '^' != ch[i] && '+' != ch[i] && '-' != ch[i] && '*' != ch[i] && '/' != ch[i] && !isdigit(ch[i]))
return 0;
else{
if('+' ==ch[0] || '*' == ch[0] || '/' == ch[0] || '^' == ch[0] || '.' == ch[0])
return 0;
if('\0' == ch[i+1] && '+' ==ch[0] || '*' == ch[0] || '/' == ch[0] || '^' == ch[0])
return 0;
// 上面是判断字符串首尾是否合格 下面是中间部分
if(0 != i && ch[i+1] != '\源衫历0' ){
if(('X' == ch[i] || 'x' == ch[i]) && !isdigit(ch[i-1]) && '+' != ch[i-1] && '-' != ch[i-1] && '*' != ch[i-1] && '/' != ch[i-1] && '.' != ch[i-1])
return 0;
if(('X' == ch[i] || 'x' == ch[i]) && '^' != ch[i+1] && '+' != ch[i+1] && '-' != ch[i+1] && '*' != ch[i+1] && '/' != ch[i+1])
return 0;
if(('+' == ch[i] || '-' == ch[i] || '*' == ch[i] || '/' == ch[i]) && !isdigit(ch[i-1]) && 'X' != ch[i-1] && 'x' != ch[i-1] && !isdigit(ch[i+1]) && 'X' != ch[i+1] && 'x' != ch[i+1])
return 0;
if('^' == ch[i] && 'X' != ch[i-1] && 'x' != ch[i-1])
return 0;
if('^' == ch[i] && !isdigit(ch[i+1]))
return 0;
if('.' == ch[i] && !isdigit(ch[i+1]) && !isdigit(ch[i-1]))
return 0;
}
}
}
return 1;
}
void createPoly(polynode **P, char ch[]){
char *t = ch;
int i = 0, j = 1;
int iscoef = 1,isminus = 1;
polyptr Q,L;
if('-' == ch[0]){
isminus = -1;
*t++;
}
while('\0' != *t){
Q = (polyptr)malloc(sizeof(polynode));
Q->coef = 1;
Q->exp = 0;
Q->next = NULL;//申请节点,初始化参数为1.
if(-1 == isminus){
Q->coef *= isminus;
isminus = 1;
}
while('+' != *t && '-' != *t && '*' != *t && '/' != *t && '\0' != *t){
if('x' != *t && 'X' != *t){
while(isdigit(*t)){
i =((int)*t - 48) + i*10;
t++;
j *= i;
}//抽取数字
if(1 == iscoef && 0 != i){
Q->coef *= i;
}
if(0 == iscoef){
Q->exp += i;
iscoef = 1;
}
//如果i=0,则
}
else{
iscoef = 0;
t++;
if('^' == *t)
t++;
else
Q->exp = 1;
}
i = 0;
}//while 遍历到加减乘除,则退出循环,到下一新的节点
iscoef = 1;
if('\0' != *t){
if('-' == *t)
isminus = -1;
t++;
}
if(0 == j){
Q->coef = 0;
j = 1;
}
printf("系数:%d,指数:%d\n",Q->coef,Q->exp);
if(NULL == *P){
*P = Q;
}
else{
L->next = Q;
}
L = Q;
}//while遍历整个字符串
}
void polyAdd(polynode *A,polynode *B){
polyptr P = A, Q,L;
polyptr COPYA = NULL,COPYB = NULL;
if(NULL == A || NULL == B){
printf("多项式未被建立,请先输入多项表达式.\n");
return ;
}
while(NULL != P){//复制A
Q = (polyptr)malloc(sizeof(polynode));
Q->coef = P->coef;
Q->exp = P->exp;
Q->next = NULL;
if(NULL == COPYA)
COPYA = Q;
else
L->next = Q;
L = Q;
P = P->next;
}
P = B;
while(NULL != P){//复制B
Q = (polyptr)malloc(sizeof(polynode));
Q->coef = P->coef;
Q->exp = P->exp;
Q->next = NULL;
if(NULL == COPYB)
COPYB = Q;
else
L->next = Q;
L = Q;
P = P->next;
}
L->next = COPYA;//把COPYA,COPYB两个多项式连接起来,整理一下就OK了.
order(©B);
order(©B);
printf("相加结果为:");
display(COPYB);
destroy(©B);
}
void polyMinus(polynode *A,polynode *B){//相减和相加差不多
polyptr P = A, Q,L;
polyptr COPYA = NULL,COPYB = NULL;
if(NULL == A || NULL == B){
printf("多项式未被建立,请先输入多项表达式.\n");
return ;
}
while(NULL != P){//复制A
Q = (polyptr)malloc(sizeof(polynode));
Q->coef = P->coef;
Q->exp = P->exp;
Q->next = NULL;
if(NULL == COPYA)
COPYA = Q;
else
L->next = Q;
L = Q;
P = P->next;
}
P = B;
while(NULL != P){//复制B
Q = (polyptr)malloc(sizeof(polynode));
Q->coef = -(P->coef);
Q->exp = P->exp;
Q->next = NULL;
if(NULL == COPYB)
COPYB = Q;
else
L->next = Q;
L = Q;
P = P->next;
}
L->next = COPYA;//把COPYA,COPYB两个多项式连接起来,整理一下就OK了.
order(©B);
order(©B);
printf("相减结果为:");
display(COPYB);
destroy(©B);
}
void polyMulti(polynode *A,polynode *B){
polyptr R = A, P = B, Q, L = NULL, T;
if(NULL == A || NULL == B){
printf("多项式未被建立,请先输入多项表达式.\n");
return ;
}
if(0 == A->coef || 0 == B->coef){
printf("多项式乘积为:0\n");
return ;
}
while(NULL != R){
while(NULL != P){
Q = (polyptr)malloc(sizeof(polynode));
Q->coef = P->coef * R->coef;
Q->exp = P->exp + R->exp;
Q->next = NULL;
if(NULL == L)
L = Q;
else
T->next = Q;
T = Q;
P = P->next;
}
P = B;
R = R->next;
}
order(&L);
order(&L);
printf("多项式乘积为:\n");
display(L);
destroy(&L);
}
void polyDiv(polynode *A,polynode *B){//多项式除法
polyptr P = A, Q,L,R,T,C,D;
polyptr COPYA = NULL,COPYB = NULL;
if(NULL == A || NULL == B){
printf("多项式未被建立,请先输入多项表达式.\n");
return ;
}
if(A->coef == 0){
printf("0.\n");
return ;
}
if(B->coef == 0){
printf("除数为0,错误!\n");
return ;
}
if(A->coef < B->coef){
printf("商:0,余数为:");
display(A);
return ;
}
while(NULL != P){//复制A
Q = (polyptr)malloc(sizeof(polynode));
Q->coef = P->coef;
Q->exp = P->exp;
Q->next = NULL;
if(NULL == COPYA)
COPYA = Q;
else
L->next = Q;
L = Q;
P = P->next;
}
P = B;
while(NULL != P){//复制B
Q = (polyptr)malloc(sizeof(polynode));
Q->coef = -(P->coef);
Q->exp = P->exp;
Q->next = NULL;
if(NULL == COPYB)
COPYB = Q;
else
L->next = Q;
L = Q;
P = P->next;
}
C = P = Q = L = R = T = NULL;
//------------------开始计算
while(COPYA->exp >= COPYB->exp){
D = COPYA;
while(NULL != D->next)
D = D->next;
R = COPYB;
Q = (polyptr)malloc(sizeof(polynode));
Q->coef = (-COPYA->coef) / R->coef;
Q->exp = COPYA->exp - R->exp;
Q->next = NULL;
if(NULL == L)
L = Q;
else
P->next = Q;
P = Q;
while(NULL != R){
Q = (polyptr)malloc(sizeof(polynode));
Q->coef = P->coef * R->coef;
Q->exp = P->exp + R->exp;
Q->next = NULL;
if(NULL == T)
T = Q;
else
C->next = Q;
C = Q;
R = R->next;
}
D->next = T;
order(©A);
order(©A);
T = NULL;
C = NULL;
}
order(&L);
order(©A);
printf("A除以B,商:");
display(L);
printf("余数:");
display(COPYA);
destroy(&L);
destroy(©A);
destroy(©B);
}
void display(polynode *P){
//考虑情况有系数为1,指数为1,0,一般数;系数为系数不为1,指数为1,0,一般数;
//系数为负数,指数为1,0,一般数,主要考虑中间符号问题.
if(NULL == P){
printf("多项式为空.\n");
return ;
}
if(1 == P->coef){
if(0 == P->exp)
printf("1");
else if(1 == P->exp) printf("x");
else printf("x^%d",P->exp);
}
else{
if(-1 == P->coef){
if(0 == P->exp)
printf("-1");
else if(1 == P->exp) printf("-x");
else printf("-x^%d",P->exp);
}
else if(0 == P->exp)
printf("%d",P->coef);
else if(1 == P->exp) printf("%dx",P->coef);
else
printf("%dx^%d",P->coef,P->exp);
}
P = P->next;
while(NULL != P){
if(0 < P->coef){
if(1 == P->coef){
if(0 == P->exp)
printf("+1");
else if(1 == P->exp) printf("+x");
else printf("+x^%d",P->exp);
}
else{
if(0 == P->exp)
printf("+%d",P->coef);
else if(1 == P->exp) printf("+%dx",P->coef);
else
printf("+%dx^%d",P->coef,P->exp);
}
}
else{
if(-1 == P->coef){
if(0 == P->exp)
printf("-1");
else if(1 == P->exp) printf("-x");
else printf("-x^%d",P->exp);
}
else{
if(0 == P->exp)
printf("%d",P->coef);
else if(1 == P->exp) printf("%dx",P->coef);
else
printf("%dx^%d",P->coef,P->exp);
}
}
P = P->next;
}
printf("\n");
}
void destroy(polynode **P){
polyptr Q = *P;
if(NULL == *P)
return ;
while(*P != NULL){
Q = *P;
*P = (*P)->next;
delete Q;
}
}
void order(polynode **P){
//首先 系数为零的要清掉,其次指数从高到低排序,再者系数相同的要合并.
polyptr prev,curr,OUT,INcurr;//前一节点和当前节点
int temp;
//出去第一节点系数为0的项
while(NULL != *P){
if(0 != (*P)->coef)
break;
else{
if(NULL == (*P)->next)
return;
curr = *P;
(*P) = (*P)->next;
delete curr;
}
}
if(NULL == *P || NULL == (*P)->next)//如果只剩1项或空,则不需要整理,退出函数
return;
//冒泡排序
OUT = INcurr = *P;
while(NULL != OUT->next){//外循环
while(NULL != INcurr->next){//内循环
prev = INcurr;
INcurr = INcurr->next;
if(prev->exp < INcurr->exp){
temp = prev->coef;
prev->coef = INcurr->coef;
INcurr->coef = temp;//交换系数
temp = prev->exp;
prev->exp = INcurr->exp;
INcurr->exp = temp;//交换指数
}
}
OUT = OUT->next;
INcurr = *P;
}
//去除0项
prev = curr = *P;
curr = curr->next;
while(NULL != curr){
if(0 == curr->coef){
prev->next = curr->next;
delete curr;
curr = prev->next;
}
else{
prev = curr;
curr = curr->next;
}
}
//合并同类项
OUT = INcurr = *P;
while(NULL != OUT->next){
while(NULL != INcurr->next){
prev = INcurr;
INcurr = INcurr->next;
if(INcurr->exp == OUT->exp){
OUT->coef += INcurr->coef;
prev->next = INcurr->next;
delete INcurr;
INcurr = prev;
}
}
INcurr = OUT = OUT->next;
if(NULL == OUT)
return;
}
}
void main(){
int choice;
// int i;
char ch[100];
polynode *polyA,*polyB;
polyA = polyB = NULL;
menu();
scanf("%d",&choice);
while(!IsChoice(choice)){
menu();
printf("输入错误,重新输入.\n");
scanf("%d",&choice);
}
while(8 != choice){
switch(choice){
case 1:
if(NULL != polyA || NULL != polyB){
destroy(&polyA);
destroy(&polyB);
printf("原多项式被销毁.\n");
}
printf("多项式输入格式:4x^3+7x^2+x+6--x不分大小写.\n输入多项式A:\n");
scanf("%s",&ch);
while(!isPut(ch)){
printf("输入错误!重新输.\n");
scanf("%s",&ch);
}
createPoly(&polyA,ch);//建立多项式A链表
printf("输入多项式B:\n");
scanf("%s",&ch);
while(!isPut(ch)){
printf("输入错误!重新输.\n");
scanf("%s",&ch);
}
createPoly(&polyB,ch);//建立多项式B链表
order(&polyB);
order(&polyA);//整理排序多项式,默认降幂
printf("建立多项式成功!多项式:\nA为:");
display(polyA);
printf("B为:");
display(polyB);
break;
case 2:
polyAdd(polyA,polyB);
break;
case 3:
polyMinus(polyA,polyB);
break;
case 4:
polyMulti(polyA,polyB);
break;
case 5:
printf("PS:系数只支持整数,计算除法存在误差;\n如果除数所有项系数为1,能得到正确答案,或者某些情况系数刚好被整除.");
polyDiv(polyA,polyB);
break;
case 6:
printf("------显示多项式------\nA :");
display(polyA);
printf("B :");
display(polyB);
break;
case 7:
destroy(&polyA);
destroy(&polyB);
printf("此多项式已被清空.\n");
break;
default:
return ;
}
choice = 0;
menu();
scanf("%d",&choice);
while(!IsChoice(choice) || 0 == choice){
menu();
printf("输入错误,重新输入.\n");
scanf("%d",&choice);
}
}
}
⑷ 一元多项式计算器 (1)设Pn(x)和Qm(x)分别为两个一元多项式,利用单链表
#include <stdio.h>
#include <malloc.h>
typedef int ElemType;
typedef int Status;
typedef struct test{//定义多项式游裂的存储结构
int xs;
int zs;
struct test *next;
}PolyNode,*PolyList;
PolyList p,q,r;
void createPolyList(PolyList *L){//创建多项式
(*L) = (PolyList)malloc(sizeof(PolyNode));
(*L)->next = NULL;
r=(*L);
int xs,zs;
scanf("%d %d",&xs,&zs);
while(xs!=0){
//生成新节点;
p = (PolyList)malloc(sizeof(PolyNode));
p->next = NULL;
p->xs = xs;
p->zs = zs;
//把新节点已尾接法方式插入链表;
r->next=p; r=p;
scanf("%d %d",&xs,&zs);
}
}
void addPolyList(PolyList la,PolyList lb,PolyList *lc){//多项式加法
PolyList pa,pb,pc;
(*lc) = (PolyList)malloc(sizeof(PolyNode));
(*lc)->next = NULL; r=(*lc);
pa=la->next; pb=lb->next;
while((pa!=NULL)&&(pb!=NULL)){
if(pa->zs < pb->zs){
//复制la的当前元局森素节点
pc=(PolyList)malloc(sizeof(PolyNode));
pc->next=NULL;
pc->zs=pa->zs;
pc->xs=pa->xs;
//把新节点加入神腊闭lc
r->next=pc; r=pc;
pa=pa->next;
}else if(pa->zs == pb->zs){
//将两个系数相加和赋值给变量sum
int sum;
sum=pa->xs+pb->xs;
if(sum!=0){
//生成新节点并适当赋值;
pc=(PolyList)malloc(sizeof(PolyNode));
pc->next=NULL;
pc->xs=sum; pc->zs=pa->zs;
//把新节点加入lc
r->next=pc; r=pc;
}
pa=pa->next;pb=pb->next;
}else if(pa->zs > pb->zs){
//复制lb的当前元素节点
pc=(PolyList)malloc(sizeof(PolyNode));
pc->next=NULL;
pc->zs=pb->zs;
pc->xs=pb->xs;
//把新节点加入lc
r->next=pc; r=pc;
pb=pb->next;
}
}
if(pa){
pc->next=pa;
}else if(pb){
pc->next=pb;
}
}
void printList(PolyList l){//打印多项式
p=l->next;
while(p){
printf("\n%2d %2d",p->xs,p->zs);
p=p->next;
}
printf("\n");
}
void oppositePolyList(PolyList *l){//多项式的系数取相反数
PolyList p=(*l)->next;
while(p){
p->xs=-p->xs;
p=p->next;
}
}
void subtractionPolyList(PolyList la,PolyList lb,PolyList *lc){//多项式减法
oppositePolyList(&lb);
addPolyList(la,lb,lc);
oppositePolyList(&lb);
}
void reverse (PolyList *l){//多项式逆置
PolyList p,q,r;
p=*l;
q=p->next;
p->next=NULL;
while(q){
r=q->next;
q->next=p->next;
p->next=q;
q=r;
if(r)
r=r->next;
}
}
int main(){
PolyList la,lb,lc;
createPolyList(&la);
createPolyList(&lb);
subtractionPolyList(la,lb,&lc);
printf("\nla: ");
printList(la);
printf("\nlb: ");
printList(lb);
printf("\nlc: ");
printList(lc);
printf("\nre_lc: ");
reverse(&lc);
printList(lc);
return 0;
}
————————————————
版权声明:本文为CSDN博主“Java功程师”的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_3766/article/details/80648368
⑸ 一个用单链表存储一元多项式的c语言程序,我觉得出错的地方,却改不对。还请大家指正。谢谢!
void CreateList(LinkList *&L,ElemType a[],ElemType b[],int n)
错了,应该是void CreateList(LinkList**l,ElemType a[],ElemTpye b[],int n);
Add()那个函数也错了,是地址错误。我给我写的给你吧:
但是要自己输入数据
#include<stdio.h>
#include<iostream>
using namespace std;
typedef struct ploynode
{
double coef;
int exp;
struct ploynode*link;
}ploynode;
typedef ploynode* NODE;
char Compare(int x,int y)
{
if(x==y) return ('枝丛=');
else if(x>y) return ('>');
else return ('<');
}
NODE Creat()
{
NODE head,p;
head=p=new ploynode;
while(1)
{
cin>>p->coef>>p->exp;
if(p->coef==0) break;
p->link=new ploynode;
p=p->link;
}
p->link=NULL;
return head;
}
void Output(NODE head)
{
NODE p=head;
while(p!=NULL)
{
if(p->coef)
{
if(p->exp!=0)
{
if(p->coef!=1)
cout<笑绝<p->coef;
if(p->exp!=1) cout<<"x^"<<p->exp;
else cout<<"x";
}
else cout<<p->coef;
if(p->coef>0&&p->link&&p->link->coef!=0) cout<<'+';
}
p=p->碰搭姿link;
}
cout<<endl;
}
NODE Attach(double c,int e,NODE d)
{
NODE x;
x=new ploynode;
x->coef=c;
x->exp=e;
d->link=x;
return x;
}
NODE PolyAdd(NODE a,NODE b)//多项式的加法实现
{
NODE p,q,d,c;
int x;
p=a;q=b;
c=new ploynode;d=c;
while((p!=NULL)&&(q!=NULL))
switch(Compare(p->exp,q->exp))
{
case '=':x=p->coef+q->coef;
if(x!=0)
d=Attach(x,p->exp,d);
p=p->link;q=q->link;
break;
case '>':d=Attach(p->coef,p->exp,d);
p=p->link;
break;
case '<':d=Attach(q->coef,q->exp,d);
q=q->link;
break;
}
while(p!=NULL)
{
d=Attach(p->coef,p->exp,d);
p=p->link;
}
while(q!=NULL)
{
d=Attach(q->coef,q->exp,d);
q=q->link;
}
d->link=NULL;
p=c;c=c->link;delete p;
return c;
}
NODE Multi(NODE a,NODE b)
{
NODE ploy[10],p,q,dst[10],Dst,temp;
q=a;p=b;
double c;int e;
int i=0,count;
while(p!=NULL)
{
if(p->coef==0) break;
ploy[i]=p;
p=p->link;
i++;
}
count=i;
for(i=0;i<count;i++)
ploy[i]->link=NULL;
for(i=0;i<count;i++)
dst[i]=new ploynode;
for(i=0;i<count;i++)
{
q=a;temp=dst[i];
while(q!=NULL)
{
if(q->coef==0) break;
c=q->coef*ploy[i]->coef;
e=q->exp+ploy[i]->exp;
temp=Attach(c,e,temp);
q=q->link;
}
temp->link=NULL;
}
for(i=0;i<count;i++)
dst[i]=dst[i]->link;
Dst=dst[0];
for(i=1;i<count;i++)
Dst=PolyAdd(Dst,dst[i]);
return Dst;
}
void main()
{
NODE src1,src2,dst;
src1=new ploynode;
src2=new ploynode;
dst=new ploynode;
src1=Creat();
cout<<"src1:"<<endl;
Output(src1);
src2=Creat();
cout<<"src2:"<<endl;
Output(src2);
dst=PolyAdd(src1,src2);
cout<<"dst:"<<endl;
Output(dst);
}
⑹ C语言:用单链表实现任意两个一元多项式的加、减法运算
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 7
typedef enum { add, nul, sub, div1, yu, l, r }OP;
int a[N][N] = {
{ 0, 0, -1, -1, -1, 1, 2 },
{ 0, 0, -1, -1, -1, 1, 2 },
{ 1, 1, 0, 0, 0, 1, 2 },
{ 1, 1, 0, 0, 0, 1, 2 },
{ 1, 1, 0, 0, 0, 1, 2 },
};
int top;
OP beg;
int b[1024];
OP op[1024];
void init_nu( ){ top = 0; }
void push_nu( int term ){ b[top++] = term; }
int pop_nu( ){ return b[--top]; }
int is_empty_nu( ){ return top == 0; }
void destory_nu( ) { top = 0; }
void init_sign( ){ beg = 0; }
void push_sign( OP sign ){ op[beg++] = sign; }
void destory_sign( ){ beg = 0; }
OP pop_sign( ){ return op[--beg];}
OP get_sign( ){ return op[beg - 1]; }
int is_empty_sign( ){return beg == 0; }
int eval()
{
int i, j;
i = pop_nu();
j = pop_nu();
switch( pop_sign() )
{
case '+': push_nu( j + i ); break;
case '-': push_nu( j - i ); break;
case '*': push_nu( j * i ); break;
case '/': push_nu( j / i ); break;
case '%': push_nu( j & i ); break;
defult: break;
}
}
int change( char *s )
{
int i;
int n = strlen( s );
for( i = 0; i < n; i++ )
{
if( s[i] >= '0' && s[i] <= '9' )
push_nu( 0 );
while( s[i] >= '0' && s[i] <= '9' )
push_nu( 10 * pop_nu() + s[i++] - '0' );
switch( s[i] )
{
case '+': while( a[add][get_sign()] <= 0 )
eval();
push_sign( add ); break;
case '-': while( a[nul][get_sign()] <= 0 )
eval();
push_sign( nul ); break;
case '*': while( a[sub][get_sign()] <= 0 )
eval();
push_sign( sub ); break;
case '/': while( a[div1][get_sign()] <= 0 )
eval();
push_sign( div1 ); break;
case '%': while( a[yu][get_sign()] <= 0 )
eval();
push_sign( yu ); break;
case '(': push_sign( l ); break;
case ')': while( (get_sign()) != l )
eval();
pop_sign();
break;
defult: break;
}
}
return pop_nu();
}
int main( void )
{
char *s = "((5-3)*2+4/2&2+1)";
init_nu();
init_sign();
printf( "%d\n", change( s ));
destory_nu();
destory_sign();
return 0;
}
以前编的,希望对你有帮助。
别忘了给我分。