| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 4501 人关注过本帖
标题:本人的一元多项式加减乘
只看楼主 加入收藏
chllcy
Rank: 1
等 级:新手上路
帖 子:26
专家分:0
注 册:2006-10-7
收藏
 问题点数:0 回复次数:13 
本人的一元多项式加减乘

做的比较粗略,希望大家指点一下 还有一个输入x的值输出多项式的结果
#include "stdafx.h"
#include <iostream>
#include <conio.h>
#include "math.h"
using namespace std;
typedef struct
{
double coef;
int expn;
}elemtype;
typedef struct lnode
{
elemtype data;
struct lnode * next;
}lnode,*linklist;
linklist creat() //输入并建立多项式
{
char ch;int i=1;
elemtype _data;
linklist head=new lnode,p,r=head;
ch='y';
while(ch=='y'||ch=='Y')
{
cout<<"请输入第"<<i<<"项系数: ";
cin>>_data.coef;
cout<<"请输入第"<<i<<"项指数: ";
cin>>_data.expn;
p=new lnode;
(p->data).coef=_data.coef;
(p->data).expn=_data.expn;
r->next=p;
r=p;i++;
cout<<"是否还要输入项,要则输入Y,否则按任意其他键退出 ";
cin>>ch;
}
r->next=NULL;
return(head);
}

void del(linklist head)
{
linklist p=head,q=head;
while(q!=NULL)
{
p=q; q=p->next; delete p;
}
}
void swap(linklist &head)//按降序排,并合并指数相同的项
{
lnode m,*p=head->next,*q,*z,*r;
int i,j,count=0;double sum=0;
while(p!=NULL)
{
count++;
p=p->next;

}
for(i=1;i<count;i++)
{
p=head->next;q=p->next;
for(j=0;j<count-i;j++)
{
if(((p->data).expn)<((q->data).expn))
{
(m.data).expn=(q->data).expn;
(m.data).coef=(q->data).coef;
(q->data).expn=(p->data).expn;
(q->data).coef=(p->data).coef;
(p->data).expn=(m.data).expn;
(p->data).coef=(m.data).coef;

}
p=p->next;q=q->next;
}
}
p=head->next;r=head;
while(p!=NULL)
{
q=p->next;z=p;sum=(p->data).coef;
while(q!=NULL)
{
if((p->data).expn==(q->data).expn)
{sum+=(q->data).coef;z->next=q->next;delete q;q=z->next;}
else
{z=q;q=q->next;}
}
if(sum!=0)
{(p->data).coef=sum;r=p;p=p->next;}
else
{r->next=p->next;delete p;p=r->next;}
}

}

void print(const linklist head) //输出多项式,序列按指数降序排列
{
lnode *p=head->next;
if(p!=NULL)
{
if((p->data).coef!=0)
cout<<(p->data).coef<<"x^"<<(p->data).expn;
p=p->next;
while(p!=NULL)
{
if(((p->data).coef)>0)
cout<<'+';
if((p->data).coef!=0)
{
if((p->data).expn>=0)
cout<<(p->data).coef<<"x^"<<(p->data).expn;
else
cout<<(p->data).coef<<"x^("<<(p->data).expn<<')';
}
p=p->next;
}
}cout<<endl;
}
int cmp(int &a,int &b)
{
if(a>b)
return 1;
else if(a==b)
return 0;
else if(a<b)
return -1;
}
linklist add(linklist m,linklist n)
{
linklist head=new lnode;
linklist p=m->next,q=n->next,x,y;//利用x,y创建另一个链表存两个链表的和
x=head;
elemtype a,b; double sum=0;
if(p==NULL)
print(q);
else if(q==NULL)
print(p);
while(p&&q)
{
a=(p->data); b=(q->data);y=new lnode;x->next=y;x=y;
switch(cmp(a.expn,b.expn))
{
case 1:(y->data).expn=(p->data).expn;
(y->data).coef=(p->data).coef;p=p->next;break;
case 0:sum=a.coef+b.coef;
if(sum!=0)
{(y->data).coef=sum;(y->data).expn=(p->data).expn;}
else
{(y->data).expn=0;
(y->data).coef=0;}
q=q->next;p=p->next;
break;
case -1:(y->data).expn=(q->data).expn;
(y->data).coef=(q->data).coef;q=q->next;break;
}
}
while(q!=NULL)
{
y=new lnode;x->next=y;x=y;
(y->data).expn=(q->data).expn;
(y->data).coef=(q->data).coef;
q=q->next;
}
while(p!=NULL)
{
y=new lnode;x->next=y;x=y;
(y->data).expn=(p->data).expn;
(y->data).coef=(p->data).coef;
p=p->next;
}
x->next=NULL;
swap(head);
return head;
}


linklist sub(linklist m,linklist n)
{
linklist p=m->next,q=n->next,x,y;//利用x,y创建另一个链表存两个链表的和
linklist head=new lnode;x=head;
elemtype a,b; double sum=0;
if(p==NULL)
print(q);
else if(q==NULL)
print(p);
while(p&&q)
{
a=(p->data); b=(q->data);y=new lnode;x->next=y;x=y;
switch(cmp(a.expn,b.expn))
{case 1:(y->data).expn=(p->data).expn;
(y->data).coef=(p->data).coef;p=p->next;break;
case 0:sum=a.coef-b.coef;
if(sum!=0)
{(y->data).coef=sum;(y->data).expn=(p->data).expn;}
else
{(y->data).expn=0;
(y->data).coef=0;}
q=q->next;p=p->next;
break;
case -1:(y->data).expn=(q->data).expn;
(y->data).coef=-(q->data).coef;q=q->next;break;
}
}

while(q!=NULL)
{
y=new lnode;x->next=y;x=y;
(y->data).expn=(q->data).expn;
(y->data).coef=-(q->data).coef;
q=q->next;
}
while(p!=NULL)
{
y=new lnode;x->next=y;x=y;
(y->data).expn=(p->data).expn;
(y->data).coef=(p->data).coef;
p=p->next;
}
x->next=NULL;swap(head);
return head;
}
void result(linklist &head,const double& x)
{
double sum=0,temp=0;
linklist p=head->next;
while(p!=NULL)
{
temp=((p->data).coef)*pow(x,(p->data).expn);
sum+=temp;
p=p->next;
}
cout<<"第1个多项式结果为: "<<sum<<endl;
}
linklist mul(linklist m,linklist n)
{
linklist head=new lnode;
linklist p=m->next,q=n->next,x,y,z,r=head;
x=head;int count=0,i,j;
double sum=0;
while(p!=NULL)
{
for(q=n->next;q!=NULL;q=q->next)
{
// count++;
y=new lnode;x->next=y;x=y;
(y->data).coef=(p->data).coef*(q->data).coef;
(y->data).expn=(p->data).expn+(q->data).expn;
}
p=p->next;
}
x->next=NULL;
swap(head);
return head;
}
int _tmain(int argc, _TCHAR* argv[])
//int main(int argc, char *argv[])
{
linklist x1,x2;double x;
cout<<"请输入第一个多元二项式"<<endl;
x1=creat();
swap(x1);
print(x1);
cout<<"请输入第二个多元二项式"<<endl;
x2=creat();
swap(x2);
print(x2);
print(add(x1,x2));
del(add(x1,x2));
print(sub(x1,x2));
del(sub(x1,x2));
cout<<"请输入x的值:";
cin>>x;
result(x1,x);
print(mul(x1,x2));
del(mul(x1,x2));
del(x1);del(x2);//删除建立起来存储一元多项式的链表
getch();
return 0;
}

搜索更多相关主题的帖子: 多项式 
2006-10-17 18:25
tang1000000
Rank: 1
等 级:新手上路
帖 子:23
专家分:0
注 册:2006-9-23
收藏
得分:0 

第一个程序我 运行了一下 有一个错误! 大家看看是那里错了!
--------------------Configuration: tang - Win32 Debug--------------------
Compiling...
a.cpp
c:\program files\microsoft visual studio\myprojects\tang\a.cpp(1) : fatal error C1083: Cannot open include file: 'stdafx.H': No such file or directory
Error executing cl.exe.

tang.exe - 1 error(s), 0 warning(s)

[此贴子已经被作者于2006-10-22 23:33:30编辑过]

2006-10-17 20:58
~兜兜~
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2006-10-22
收藏
得分:0 
LZ能否帮忙做一个算术表达式求值的算法呀???
2006-10-22 11:57
yuki
Rank: 2
等 级:新手上路
威 望:5
帖 子:508
专家分:0
注 册:2005-2-4
收藏
得分:0 

我也做过一个,顺便贴一贴,呵呵~
/* Polynomial.h */
#ifndef __POLYNOMIAL_H
#define __POLYNOMIAL_H

#define TRUE 0x01
#define FALSE 0x00

/* 错误代码 */
#define ERR_ON_SUCCESS 0x00 /* 没有错误 */
#define ERR_STACK_OVERRUN 0x01 /* 堆栈溢出 */
#define ERR_ITEM_EXIST 0x02 /* 欲添加项已存在 */
#define ERR_PTR_INVAILD 0x04 /* 无效的指针传入 */
#define ERR_LNKLST_INVAILD 0x08 /* 无效的链表 */
#define ERR_ACTCODE_INVAILD 0x10 /* 无效的操作码 */
#define ERR_ADD_IMPOSSIBLE 0x20 /* 无法做加运算 */
#define ERR_SUB_IMPOSSIBLE 0x40 /* 无法做减运算 */
#define ERR_EMPTY_POLY 0x80 /* 多项式链表为空 */
#define ERR_UNKNOWN 0xff /* 位置错误 */

/* 操作码 */
#define ACT_ADD 0x01 /* 多项式加 */
#define ACT_SUB 0x02 /* 多项式减 */
#define ACT_MUP 0x03 /* 多项式乘 */

/* 嵌入式功能配置常数 */
#define AUTO_DESTORY_EXIST FALSE /* 欲插入的项已存在时,自动删除创建的项 */
#define CHK_PARAMETER_PTR TRUE /* 检查作为参数传入的指针是否有效 */
#define RAISE_ERR TRUE /* 错误报告函数 */
#define CRITICAL_ABORT TRUE /* 错误异常中断 */
#define COPY_LNKLST TRUE /* 链表复制函数 */
#define SIMPLE_OUTPUT TRUE /* 简单的多项式输出函数 */
#define SIMPLE_INPUT TRUE /* 简单的多项式输入函数 */

#define EPSILON 1E-6 /* 极小值定义,用于浮点数趋近比较 */

#include "Polynomial.cpp"

#endif


我们都在命运湖上荡舟划桨,波浪起伏使我们无法逃离孤行;如果我们迷失方向,波浪将指引我们穿过另一天曙光
2006-10-22 13:17
yuki
Rank: 2
等 级:新手上路
威 望:5
帖 子:508
专家分:0
注 册:2005-2-4
收藏
得分:0 

/* Polynomial.cpp */
typedef struct Polynomial {
float p;
int e;
struct Polynomial *next;
struct Polynomial *prev;
} poly;

typedef struct {
poly *head;
poly *tail;
int length;
} poly_lnklst;

typedef unsigned int uint;

#if RAISE_ERR == TRUE
inline void
raise_err(uint err) {
switch(err) {
case ERR_STACK_OVERRUN:
printf("Error %#x : Stack overrun\n", err);
break;
case ERR_ITEM_EXIST:
printf("Error %#x : Item is already exist\n",err);
break;
case ERR_PTR_INVAILD:
printf("Error %#x : Invaild pointer\n",err);
break;
case ERR_LNKLST_INVAILD:
printf("Error %#x : Invaild Linked-List\n",err);
break;
case ERR_ACTCODE_INVAILD:
printf("Error %#x : Invaild Action Code\n",err);
break;
case ERR_ADD_IMPOSSIBLE:
printf("Error %#x : Item cannot be added\n",err);
break;
case ERR_SUB_IMPOSSIBLE:
printf("Error %#x : Item cannot be subtracted\n",err);
break;
case ERR_EMPTY_POLY:
printf("Error %#x : Polynomial grows empty!\n",err);
break;
case ERR_UNKNOWN:
printf("Error %#x : Unknown error\n",err);
break;
}
}
#endif

inline uint
destory_polynomial(poly_lnklst *plst) {
poly *p = (poly *)0;
poly *tmp = (poly *)0;

#if CHK_PARAMETER_PTR == TRUE
if(!plst)
return ERR_LNKLST_INVAILD;
#endif

p = plst->head;
/* 逐一释放链表项目 */
while(p) {
tmp = p->next;
free(p);
p = tmp;
}

/* 释放两表容器 */
free(plst);
plst = (poly_lnklst *)0;
return ERR_ON_SUCCESS;
}

inline uint
init_poly_lnklst(poly_lnklst *plst) {
#if CHK_PARAMETER_PTR == TRUE
if(!plst)
return ERR_LNKLST_INVAILD;
#endif

plst->head = (poly *)0;
plst->tail = (poly *)0;
plst->length = (int)0;

return ERR_ON_SUCCESS;
}

inline poly *
create_poly_item(float p, int e, uint *err) {
poly *item =
(poly *)malloc(sizeof(poly));
if(!item) {
*err = ERR_STACK_OVERRUN;
return (poly *)0;
}
item->p = p;
item->e = e;
item->next = (poly *)0;
item->prev = (poly *)0;
*err = ERR_ON_SUCCESS;
return item;
}

#if COPY_LNKLST == TRUE
inline poly_lnklst *
copy_polynomial(poly_lnklst *plstSrc,uint *err) {
poly *pSrc = (poly *)0;
poly *pDes = (poly *)0;
poly *tmp = (poly *)0;
poly_lnklst *plstDes = (poly_lnklst *)0;

#if CHK_PARAMETER_PTR == TRUE
if(!plstSrc) {
*err = ERR_LNKLST_INVAILD;
return (poly_lnklst *)0;
}
#endif

plstDes = (poly_lnklst *)malloc(sizeof(poly_lnklst));
if(!plstDes) {
*err = ERR_STACK_OVERRUN;
#if RAISE_ERR == TRUE
raise_err(*err);
#endif
#if CRITICAL_ABORT == TRUE
exit(1);
#else
return (poly_lnklst *)0;
#endif
}

/* 复制表首 */
pSrc = plstSrc->head;
if(!pSrc) {
free(plstDes);
*err = ERR_EMPTY_POLY;
return (poly_lnklst *)0;
}
pDes = create_poly_item(pSrc->p,pSrc->e,err);
if(*err != ERR_ON_SUCCESS) {
#if RAISE_ERR == TRUE
raise_err(*err);
#endif
#if CRITICAL_ABORT == TRUE
exit(1);
#else
return (poly_lnklst *)0;
#endif
}
plstDes->head = plstDes->tail = pDes;
plstDes->length = plstSrc->length;
/* 逐一复制余下的项目 */
pSrc = pSrc->next;
while(pSrc) {
tmp = create_poly_item(pSrc->p,pSrc->e,err);
if(*err != ERR_ON_SUCCESS) {
#if RAISE_ERR == TRUE
raise_err(*err);
#endif
#if CRITICAL_ABORT == TRUE
exit(1);
#else
return (poly_lnklst *)0;
#endif
}
/* 创建链表的节点连接 */
pDes->next = tmp;
tmp->prev = pDes;
pDes = tmp;
plstDes->tail = tmp;
pSrc = pSrc->next;
}
return plstDes;
}
#endif

inline uint
connect_to_lst(poly_lnklst *plst, poly *item) {
poly *p = (poly *)0,
*tmp = (poly *)0;

#if CHK_PARAMETER_PTR == TRUE
if(!plst)
return ERR_LNKLST_INVAILD;
if(!item)
return ERR_PTR_INVAILD;
#endif

if(!plst->head) {
plst->head = plst->tail = item;
item->next = item->prev = (poly *)0;
plst->length = (int)1;
}
else {
p = plst->head;
while(p) {
tmp = p->next;
#if AUTO_DESTORY_EXIST == TRUE
if(p->e == item->e) {
free(item);
item = (poly *)0;
return ERR_ITEM_EXIST;
}
#else
if(p->e == item->e)
return ERR_ITEM_EXIST;
#endif
if(tmp) {
if((p->e < item->e) && (tmp->e > item->e)) {
item->prev = p;
item->next = tmp;
p->next = item;
tmp->prev = item;
++plst->length;
break;
}
}
else {
if(p->e > item->e && !p->prev) {
item->prev = (poly *)0;
item->next = p;
p->prev = item;
plst->head = item;
++plst->length;
break;
}
else {
item->prev = p;
item->next = (poly *)0;
p->next = item;
plst->tail = item;
++plst->length;
break;
}
}
p = tmp;
}
}
return ERR_ON_SUCCESS;
}

inline uint
remove_item(poly_lnklst *plst, poly *item) {
poly *tmp_prev = (poly *)0;
poly *tmp_next = (poly *)0;

#if CHK_PARAMETER_PTR == TRUE
if(!plst)
return ERR_LNKLST_INVAILD;
if(!plst->head || !item)
return ERR_PTR_INVAILD;
#endif

tmp_prev = item->prev;
tmp_next = item->next;

if(tmp_prev) {
tmp_prev->next = tmp_next;
if(tmp_next)
tmp_next->prev = tmp_prev;
else
plst->tail = tmp_prev;
}
else {
plst->head = tmp_next;
if(!tmp_next)
return ERR_EMPTY_POLY;
if(!tmp_next->next)
plst->tail = tmp_next;
tmp_next->prev = (poly *)0;
}

--plst->length;
free(item);
item = (poly *)0;
return ERR_ON_SUCCESS;
}

inline uint
merge_item(poly *des, poly *src, uint code) {
poly *tmp = (poly *)0;

poly *tmp_prev = (poly *)0;
poly *tmp_next = (poly *)0;

#if CHK_PARAMETER_PTR == TRUE
if(!des || !src)
return ERR_PTR_INVAILD;
#endif

switch(code) {
case ACT_ADD:
if(des->e == src->e) {
des->p += src->p;
return ERR_ON_SUCCESS;
}
else
return ERR_ADD_IMPOSSIBLE;
case ACT_SUB:
if(des->e == src->e) {
des->p -= src->p;
return ERR_ON_SUCCESS;

}
else
return ERR_SUB_IMPOSSIBLE;
case ACT_MUP:
des->p *= src->p;
des->e += src->e;
return ERR_ON_SUCCESS;
default:
return ERR_ACTCODE_INVAILD;
}
}

inline uint
find_and_merge(poly_lnklst *plst) {
poly *tmp = (poly *)0;
poly *tmp_next = (poly *)0;
poly *tmp_prev = (poly *)0;
poly **container = (poly **)0;
register int i = 0;
register int j;
uint err;

#if CHK_PARAMETER_PTR == TRUE
if(!plst)
return ERR_LNKLST_INVAILD;
if(!plst->head)
return ERR_PTR_INVAILD;
#endif

tmp = plst->head;
container = (poly **)malloc(sizeof(poly *) * plst->length);

container[0] = tmp;
tmp = tmp->next;
while(tmp) {
for(j = i; j >=0; --j) {
if(!container[j])
continue;
if(container[j]->e == tmp->e) {
err = merge_item(tmp,container[j],ACT_ADD);
if(err != ERR_ON_SUCCESS) {
#if RAISE_ERR == TRUE
raise_err(err);
#endif
#if CRITICAL_ABORT == TRUE
exit(1);
#else
return err;
#endif
}

err = remove_item(plst,container[j]);
if(err != ERR_ON_SUCCESS) {
#if RAISE_ERR == TRUE
raise_err(err);
#endif
#if CRITICAL_ABORT == TRUE
exit(1);
#else
return err;
#endif
}

/* 如果系数为0 */
if(tmp->p <= EPSILON) {
tmp_next = tmp->next;
err = remove_item(plst,tmp);

if(err != ERR_ON_SUCCESS) {
#if RAISE_ERR == TRUE
raise_err(err);
#endif
#if CRITICAL_ABORT == TRUE
exit(1);
#else
return err;
#endif
}

tmp = tmp_next;
}
else
tmp = tmp->next;
break;
}
}
/* 如果没有合并,则存入容器 */
if(j < 0) {
container[++i] = tmp;
tmp = tmp->next;
}
}
free(container);
return ERR_ON_SUCCESS;
}


我们都在命运湖上荡舟划桨,波浪起伏使我们无法逃离孤行;如果我们迷失方向,波浪将指引我们穿过另一天曙光
2006-10-22 13:19
yuki
Rank: 2
等 级:新手上路
威 望:5
帖 子:508
专家分:0
注 册:2005-2-4
收藏
得分:0 

inline uint
polynomial_opr(poly_lnklst *plstDes, poly_lnklst *plstSrc, uint oprcode) {
poly *pSrc = (poly *)0;
poly *pDes = (poly *)0;
poly *tmp = (poly *)0;
poly_lnklst *plstOpr = (poly_lnklst *)0;
poly_lnklst *plstRet = (poly_lnklst *)0;

uint err;

#if CHK_PARAMETER_PTR == TRUE
if(!plstDes || !plstSrc)
return ERR_LNKLST_INVAILD;
if(!plstDes->head || !plstSrc->head)
return ERR_PTR_INVAILD;
#endif

pSrc = plstSrc->head;

switch(oprcode) {
case ACT_ADD:
case ACT_SUB:
while(pSrc) {
pDes = plstDes->head;
while(pDes) {
if(pSrc->e == pDes->e) {
err = merge_item(pDes,pSrc,oprcode);
if(err != ERR_ON_SUCCESS) {
#if RAISE_ERR == TRUE
raise_err(err);
#endif
#if CRITICAL_ABORT == TRUE
exit(1);
#else
return err;
#endif
}

/* 如果相加后的系数为0,则删除此项 */
if(pDes->p <= EPSILON) {
tmp = pDes->next;
err = remove_item(plstDes,pDes);
if(err != ERR_ON_SUCCESS) {
#if RAISE_ERR == TRUE
raise_err(err);
#endif
#if CRITICAL_ABORT == TRUE
exit(1);
#else
return err;
#endif
}
pDes = tmp;
}
break;
}
else {
if(pDes->next) {
if((pSrc->e > pDes->e) && (pSrc->e < pDes->next->e)) {
tmp = create_poly_item(pSrc->p,pSrc->e,&err);
if(err != ERR_ON_SUCCESS) {
#if RAISE_ERR == TRUE
raise_err(err);
#endif
#if CRITICAL_ABORT == TRUE
exit(1);
#else
return err;
#endif
}
tmp->prev = pDes;
tmp->next = pDes->next;
pDes->next->prev = tmp;
pDes->next = tmp;
++plstDes->length;
break;
}
}
else {
tmp = create_poly_item(pSrc->p,pSrc->e,&err);
if(err != ERR_ON_SUCCESS) {
#if RAISE_ERR == TRUE
raise_err(err);
#endif
#if CRITICAL_ABORT == TRUE
exit(1);
#else
return err;
#endif
}
if(pDes->e > tmp->e && !pDes->prev) {
tmp->prev = (poly *)0;
tmp->next = pDes;
pDes->prev = tmp;
plstDes->head = tmp;
++plstDes->length;
break;
}
else {
plstDes->tail->next = tmp;
tmp->prev = plstDes->tail;
tmp->next = (poly *)0;
plstDes->tail = tmp;
++plstDes->length;
break;
}
}
}
pDes = pDes->next;
}
pSrc = pSrc->next;
}
return ERR_ON_SUCCESS;
case ACT_MUP:
/* 复制旧链表 */
plstOpr = copy_polynomial(plstDes,&err);
if(err != ERR_ON_SUCCESS) {
#if RAISE_ERR == TRUE
raise_err(err);
#endif
#if CRITICAL_ABORT == TRUE
exit(1);
#else
return err;
#endif
}
/* 首先计算第一项,保存到返回链表 */
pDes = plstOpr->head;
while(pDes) {
err = merge_item(pDes,pSrc,ACT_MUP);
pDes = pDes->next;
}
plstRet = copy_polynomial(plstOpr,&err);
if(err != ERR_ON_SUCCESS) {
#if RAISE_ERR == TRUE
raise_err(err);
#endif
#if CRITICAL_ABORT == TRUE
exit(1);
#else
return err;
#endif
}
/* 删除plstOpr */
err = destory_polynomial(plstOpr);
if(err != ERR_ON_SUCCESS) {
#if RAISE_ERR == TRUE
raise_err(err);
#endif
#if CRITICAL_ABORT == TRUE
exit(1);
#else
return err;
#endif
}
/* 计算其余项 */
pSrc = pSrc->next;
while(pSrc) {
plstOpr = copy_polynomial(plstDes,&err);
if(err != ERR_ON_SUCCESS) {
#if RAISE_ERR == TRUE
raise_err(err);
#endif
#if CRITICAL_ABORT == TRUE
exit(1);
#else
return err;
#endif
}
pDes = plstOpr->head;
while(pDes) {
err = merge_item(pDes,pSrc,ACT_MUP);
pDes = pDes->next;
}
/* 两链表相加 */
err = polynomial_opr(plstRet,plstOpr,ACT_ADD);
if(err != ERR_ON_SUCCESS) {
#if RAISE_ERR == TRUE
raise_err(err);
#endif
#if CRITICAL_ABORT == TRUE
exit(1);
#else
return err;
#endif
}
/* 删除plstOpr */
err = destory_polynomial(plstOpr);
if(err != ERR_ON_SUCCESS) {
#if RAISE_ERR == TRUE
raise_err(err);
#endif
#if CRITICAL_ABORT == TRUE
exit(1);
#else
return err;
#endif
}
/* 计算下一项 */
pSrc = pSrc->next;
}
/* 清除原表达式 */
pDes = plstDes->head;
while(pDes) {
pSrc = pDes->next;
free(pDes);
pDes = pSrc;
}
plstDes->head = plstRet->head;
plstDes->tail = plstRet->tail;
plstDes->length = plstRet->length;

free(plstRet);

return ERR_ON_SUCCESS;
default:
return ERR_ACTCODE_INVAILD;
}
}

#if SIMPLE_OUTPUT == TRUE
inline void
print_polynomial(poly_lnklst *plst) {
poly *p = plst->head;
printf("P = %c%.1fx^%d",p->p > 0 ? '\0' : '-',p->p,p->e);
p = p->next;
while(p) {
printf("%c%.1fx^%d",p->p > 0 ? '+' : '-',p->p,p->e);
p = p->next;
}
printf("\n");
}
#endif

#if SIMPLE_INPUT == TRUE
inline poly_lnklst *
input_polynomial(uint *err) {
poly *item = (poly *)0;
poly_lnklst *plst = (poly_lnklst *)malloc(sizeof(poly_lnklst));

int item_count = 0;
int i = 0;

*err = init_poly_lnklst(plst);
if(*err != ERR_ON_SUCCESS) {
#if RAISE_ERR == TRUE
raise_err(*err);
#endif
#if CRITICAL_ABORT == TRUE
exit(1);
#else
return (poly_lnklst *)0;
#endif
}

printf("Input how many items do you want to constructure a polynomial? ");
scanf("%d",&item_count);

while(item_count > 0) {
item = (poly *)malloc(sizeof(poly));
if(!item) {
*err = ERR_STACK_OVERRUN;
#if RAISE_ERR == TRUE
raise_err(*err);
#endif
#if CRITICAL_ABORT == TRUE
exit(1);
#else
free(plst);
return (poly_lnklst *)0;
#endif
}
printf("(p,e) ? ");
scanf("%f,%d",&item->p,&item->e);
*err = connect_to_lst(plst,item);
if(*err != ERR_ON_SUCCESS) {
#if RAISE_ERR == TRUE
raise_err(*err);
#endif
#if AUTO_DESTORY_EXIST == FALSE
free(item);
#endif
continue;
}
--item_count;
}
*err = ERR_ON_SUCCESS;
return plst;
}
#endif


我们都在命运湖上荡舟划桨,波浪起伏使我们无法逃离孤行;如果我们迷失方向,波浪将指引我们穿过另一天曙光
2006-10-22 13:19
yuki
Rank: 2
等 级:新手上路
威 望:5
帖 子:508
专家分:0
注 册:2005-2-4
收藏
得分:0 

/* main.cpp */
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>

#include "Polynomial.h"

int main() {
poly_lnklst *plstA, *plstB;
uint err;
/* inputs */
printf("Input polynomial A\n");
plstA = input_polynomial(&err);
printf("Input polynomial B\n");
plstB = input_polynomial(&err);
/* mutiply */
err = polynomial_opr(plstA,plstB,ACT_MUP);
print_polynomial(plstA);
getch();
err = destory_polynomial(plstA);
err = destory_polynomial(plstB);
return 0;
}


我们都在命运湖上荡舟划桨,波浪起伏使我们无法逃离孤行;如果我们迷失方向,波浪将指引我们穿过另一天曙光
2006-10-22 13:20
SunShining
Rank: 7Rank: 7Rank: 7
等 级:贵宾
威 望:31
帖 子:2215
专家分:0
注 册:2006-2-17
收藏
得分:0 
  yuki 这不是你特意写的吧?

[glow=255,violet,2]闭关修炼ing...[/glow] [FLASH=360,180]http://www./chinaren.swf[/FLASH]
2006-10-22 17:14
yuki
Rank: 2
等 级:新手上路
威 望:5
帖 子:508
专家分:0
注 册:2005-2-4
收藏
得分:0 

不是啊,放假时自学数据结构时写的,这里路过看见了,顺便贴一贴吧,呵呵~


我们都在命运湖上荡舟划桨,波浪起伏使我们无法逃离孤行;如果我们迷失方向,波浪将指引我们穿过另一天曙光
2006-10-23 15:25
云中鹤
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2006-11-27
收藏
得分:0 
呵呵  写得不错啊
2006-12-18 15:19
快速回复:本人的一元多项式加减乘
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.020660 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved