多项式的运算(c语言实现)
#include "stdio.h"#include "stdlib.h"#include "conio.h"
typedef struct Item{ double coef;//系数 int expn;//指数 struct Item *next; }Item,*Polyn;
#define CreateItem(p) p=(Item *)malloc(sizeof(Item));#define DeleteItem(p) free((void *)p);
/************************************************************//* 判断选择函数 *//************************************************************/int Select(char *str) { char ch; printf("%s\n",str); printf("Input Y or N:"); do{ ch=getch();}while(ch!='Y'&&ch!='y'&&ch!='N'&&ch!='n'); printf("\n"); if(ch=='Y'||ch=='y') return(1); else return(0); }/************************************************************//* 插入位置定位函数 *//**************************************************************/int InsertLocate(Polyn h,int expn,Item **p) { Item *pre,*q; pre=h; q=h->next; while(q&&q->expnnext; } if(!q) { *p=pre; return(1); } else if(q->expn==expn) { *p=q; return(0); } else { *p=pre; return(-1); } }/************************************************************//* 插入结点函数 *//************************************************************/void insert(Item *pre,Item *p){ p->next=pre->next; pre->next=p; }/************************************************************//* 输入多项式 *//************************************************************/Polyn Input(void){ double coef; int expn,flag; Item *h,*p,*q,*pp; CreateItem(h);//产生头结点 h->next=NULL; printf("input coef and expn(if end ,expn=-1)\n"); while(1) { scanf("%lf%d",&coef,&expn); //输入多项式的系数和指数 if(expn==-1) break; //若指数为-,表示输入结束 if(InsertLocate(h,expn,&pp))//返回值非表示插入新结点 { CreateItem(p); p->coef=coef; p->expn