数据结构中实现一元多项式简单计算:
设计一个一元多项式简单的计算器。
基本要求:
一元多项式简单计算器的基本功能为:
(1)输入并建立多项式;
(2)输出多项式;
(3)两个多项式相加,建立并输出和多项式;
(4)两个多项式相减,建立并输出差多项式;
#include
#include
#define MAX 20
typedef struct
{
float coef ; int exp ; //系数 //指数 //多项式最多项数 //定义存放多项式的数组类型
} PolyArray [MAX ];
typedef struct pnode //定义单链表结点类型
{
float coef ; int exp ; //系数 //指数 struct pnode *next ;
//输出多项式 } PolyNode ; void DispPoly (PolyNode *L )
{
}
void CreateListR (PolyNode *&L , PolyArray a , int n ) //尾插法建表 {
PolyNode *s ,*r ; int i ; L =(PolyNode *)malloc (sizeof (PolyNode )); L ->next =NULL ; r =L ; { s =(PolyNode *)malloc (sizeof (PolyNode )); //创建新结点 //r始终指向终端结点, 开始时指向头结点 for (i =0;i next ; while (p !=NULL ) { } printf ("\n"); printf ("%gX^%d ", p ->coef , p ->exp ); p =p ->next ;
} } s ->exp =a [i ].exp ; r ->next =s ; r =s ;
//终端结点next 域置为NULL //按exp 域递减排序 //将*s插入*r之后 r ->next =NULL ; void Sort (PolyNode *&head )
{
} PolyNode *p =head ->next ,*q ,*r ; if (p !=NULL ) { } r =p ->next ; p =r ; while (p !=NULL ) { } r =p ->next ; q =head ; while (q ->next !=NULL && q ->next ->exp >p ->exp ) q =q ->next ; //在有序表中找插入*p的前驱结点*q //将*p插入到*q之后 p ->next =q ->next ; q ->next =p ; p =r ; //r保存*p结点后继结点的指针 //r保存*p结点后继结点的指针 //构造只含一个数据结点的有序表 p ->next =NULL ; //若原单链表中有一个或以上的数据结点
void Add (PolyNode *ha , PolyNode *hb , PolyNode *&hc ) //求两有序集合的并 {
PolyNode *pa =ha ->next ,*pb =hb ->next ,*s ,*tc ; float c ; hc =(PolyNode *)malloc (sizeof (PolyNode )); tc =hc ; while (pa !=NULL && pb !=NULL ) { if (pa ->exp >pb ->exp ) { } else if (pa ->exp exp ) { s =(PolyNode *)malloc (sizeof (PolyNode )); s ->exp =pa ->exp ; s ->coef =pa ->coef ; tc ->next =s ; tc =s ; pa =pa ->next ; //复制结点 //创建头结点
} } } s ->exp =pb ->exp ; s ->coef =pb ->coef ; tc ->next =s ; tc =s ; pb =pb ->next ; //pa->exp=pb->exp else { } c =pa ->coef +pb ->coef ; if (c !=0) { } pa =pa ->next ; pb =pb ->next ; s =(PolyNode *)malloc (sizeof (PolyNode )); s ->exp =pa ->exp ; s ->coef =c ; tc ->next =s ; tc =s ; //复制结点 //系数之和不为时创建新结点 if (pb !=NULL ) pa =pb ; //复制余下的结点 while (pa !=NULL ) { } tc ->next =NULL ; s =(PolyNode *)malloc (sizeof (PolyNode )); s ->exp =pa ->exp ; s ->coef =pa ->coef ; tc ->next =s ; tc =s ; pa =pa ->next ; //复制结点
void Cut (PolyNode *ha , PolyNode *hb , PolyNode *&hd ) //求两有序集合的并 {
PolyNode *pa =ha ->next ,*pb =hb ->next ,*s ,*td ; float d ; hd =(PolyNode *)malloc (sizeof (PolyNode )); while (pa !=NULL && pb !=NULL ) { if (pa ->exp >pb ->exp ) { } else if (pa ->exp exp ) s =(PolyNode *)malloc (sizeof (PolyNode )); s ->exp =pa ->exp ; s ->coef =pa ->coef ; td ->next =s ; td =s ; pa =pa ->next ; //复制结点 //创建头结点 td =hd ;
} } } else { } d =pa ->coef -pb ->coef ; if (d !=0) { } pa =pa ->next ; pb =pb ->next ; s =(PolyNode *)malloc (sizeof (PolyNode )); s ->exp =pa ->exp ; s ->coef =d ; td ->next =s ; td =s ; //复制结点 //系数之和不为时创建新结点 //pa->exp=pb->exp s =(PolyNode *)malloc (sizeof (PolyNode )); s ->exp =pb ->exp ; s ->coef =-(pb ->coef ); td ->next =s ; td =s ; pb =pb ->next ; //复制结点 if (pb !=NULL ) pa =pb ; //复制余下的结点 while (pa !=NULL ) { } td ->next =NULL ; s =(PolyNode *)malloc (sizeof (PolyNode )); s ->exp =pa ->exp ; s ->coef =pa ->coef ; td ->next =s ; td =s ; pa =pa ->next ; //复制结点
void main () {
PolyNode *ha ,*hb ,*hc ,*hd ,*he ; PolyArray a ={{1.2,0},{2.5,1},{3.2,3},{-2.5,5}}; PolyArray b ={{-1.2,0},{2.5,1},{3.2,3},{2.5,5},{5.4,10}}; CreateListR (ha , a ,4); CreateListR (hb , b ,5); printf (" 原多项式A: " ); DispPoly (ha ); printf (" 原多项式B: " ); DispPoly (hb ); Sort (ha ); Sort (hb ); printf (" 有序多项式A: "); DispPoly (ha ); printf (" 有序多项式B: "); DispPoly (hb ); Add (ha , hb , hc );
} Cut (ha , hb , hd ); printf (" 多项式相减: " ); DispPoly (hd );
数据结构中实现一元多项式简单计算:
设计一个一元多项式简单的计算器。
基本要求:
一元多项式简单计算器的基本功能为:
(1)输入并建立多项式;
(2)输出多项式;
(3)两个多项式相加,建立并输出和多项式;
(4)两个多项式相减,建立并输出差多项式;
#include
#include
#define MAX 20
typedef struct
{
float coef ; int exp ; //系数 //指数 //多项式最多项数 //定义存放多项式的数组类型
} PolyArray [MAX ];
typedef struct pnode //定义单链表结点类型
{
float coef ; int exp ; //系数 //指数 struct pnode *next ;
//输出多项式 } PolyNode ; void DispPoly (PolyNode *L )
{
}
void CreateListR (PolyNode *&L , PolyArray a , int n ) //尾插法建表 {
PolyNode *s ,*r ; int i ; L =(PolyNode *)malloc (sizeof (PolyNode )); L ->next =NULL ; r =L ; { s =(PolyNode *)malloc (sizeof (PolyNode )); //创建新结点 //r始终指向终端结点, 开始时指向头结点 for (i =0;i next ; while (p !=NULL ) { } printf ("\n"); printf ("%gX^%d ", p ->coef , p ->exp ); p =p ->next ;
} } s ->exp =a [i ].exp ; r ->next =s ; r =s ;
//终端结点next 域置为NULL //按exp 域递减排序 //将*s插入*r之后 r ->next =NULL ; void Sort (PolyNode *&head )
{
} PolyNode *p =head ->next ,*q ,*r ; if (p !=NULL ) { } r =p ->next ; p =r ; while (p !=NULL ) { } r =p ->next ; q =head ; while (q ->next !=NULL && q ->next ->exp >p ->exp ) q =q ->next ; //在有序表中找插入*p的前驱结点*q //将*p插入到*q之后 p ->next =q ->next ; q ->next =p ; p =r ; //r保存*p结点后继结点的指针 //r保存*p结点后继结点的指针 //构造只含一个数据结点的有序表 p ->next =NULL ; //若原单链表中有一个或以上的数据结点
void Add (PolyNode *ha , PolyNode *hb , PolyNode *&hc ) //求两有序集合的并 {
PolyNode *pa =ha ->next ,*pb =hb ->next ,*s ,*tc ; float c ; hc =(PolyNode *)malloc (sizeof (PolyNode )); tc =hc ; while (pa !=NULL && pb !=NULL ) { if (pa ->exp >pb ->exp ) { } else if (pa ->exp exp ) { s =(PolyNode *)malloc (sizeof (PolyNode )); s ->exp =pa ->exp ; s ->coef =pa ->coef ; tc ->next =s ; tc =s ; pa =pa ->next ; //复制结点 //创建头结点
} } } s ->exp =pb ->exp ; s ->coef =pb ->coef ; tc ->next =s ; tc =s ; pb =pb ->next ; //pa->exp=pb->exp else { } c =pa ->coef +pb ->coef ; if (c !=0) { } pa =pa ->next ; pb =pb ->next ; s =(PolyNode *)malloc (sizeof (PolyNode )); s ->exp =pa ->exp ; s ->coef =c ; tc ->next =s ; tc =s ; //复制结点 //系数之和不为时创建新结点 if (pb !=NULL ) pa =pb ; //复制余下的结点 while (pa !=NULL ) { } tc ->next =NULL ; s =(PolyNode *)malloc (sizeof (PolyNode )); s ->exp =pa ->exp ; s ->coef =pa ->coef ; tc ->next =s ; tc =s ; pa =pa ->next ; //复制结点
void Cut (PolyNode *ha , PolyNode *hb , PolyNode *&hd ) //求两有序集合的并 {
PolyNode *pa =ha ->next ,*pb =hb ->next ,*s ,*td ; float d ; hd =(PolyNode *)malloc (sizeof (PolyNode )); while (pa !=NULL && pb !=NULL ) { if (pa ->exp >pb ->exp ) { } else if (pa ->exp exp ) s =(PolyNode *)malloc (sizeof (PolyNode )); s ->exp =pa ->exp ; s ->coef =pa ->coef ; td ->next =s ; td =s ; pa =pa ->next ; //复制结点 //创建头结点 td =hd ;
} } } else { } d =pa ->coef -pb ->coef ; if (d !=0) { } pa =pa ->next ; pb =pb ->next ; s =(PolyNode *)malloc (sizeof (PolyNode )); s ->exp =pa ->exp ; s ->coef =d ; td ->next =s ; td =s ; //复制结点 //系数之和不为时创建新结点 //pa->exp=pb->exp s =(PolyNode *)malloc (sizeof (PolyNode )); s ->exp =pb ->exp ; s ->coef =-(pb ->coef ); td ->next =s ; td =s ; pb =pb ->next ; //复制结点 if (pb !=NULL ) pa =pb ; //复制余下的结点 while (pa !=NULL ) { } td ->next =NULL ; s =(PolyNode *)malloc (sizeof (PolyNode )); s ->exp =pa ->exp ; s ->coef =pa ->coef ; td ->next =s ; td =s ; pa =pa ->next ; //复制结点
void main () {
PolyNode *ha ,*hb ,*hc ,*hd ,*he ; PolyArray a ={{1.2,0},{2.5,1},{3.2,3},{-2.5,5}}; PolyArray b ={{-1.2,0},{2.5,1},{3.2,3},{2.5,5},{5.4,10}}; CreateListR (ha , a ,4); CreateListR (hb , b ,5); printf (" 原多项式A: " ); DispPoly (ha ); printf (" 原多项式B: " ); DispPoly (hb ); Sort (ha ); Sort (hb ); printf (" 有序多项式A: "); DispPoly (ha ); printf (" 有序多项式B: "); DispPoly (hb ); Add (ha , hb , hc );
} Cut (ha , hb , hd ); printf (" 多项式相减: " ); DispPoly (hd );