线性表的应用----多项式操作

来源:互联网 发布:伊藤园和三得利 知乎 编辑:程序博客网 时间:2024/04/28 05:45

/**********************************************
线性表的应用----多项式操作
BY YQG.06.3.9 (网页排版较乱,可下载源码(点击右键,目标另存为))
(注:可将输入转为多项式整体输入)
***********************************************/

#include "stdio.h" #include "stdlib.h"#include"string.h"#define LENGTH sizeof(struct node)typedef struct node{  int exp;  float codf;  struct node*next;}Node;/***************************************** 将字串转换为链表形式******************************************/Node*transpoly(char str[]){  int length=0,flag=0,i=0;  char stemp[6]={' '};  Node*head=(Node*)malloc(LENGTH),*lp,*lq=head;  head->next=NULL;  length=strlen(str);      while(flagnext=NULL;           while(str[flag]!='x')               stemp[i++]=str[flag++];               flag++;              /* 此时str[flag]应为'^'*/   if(str[flag++]!='^')               printf("ERROR!");           lp->codf=atof(stemp);           i=0;   while((str[flag]!='+')&&(flagexp=atoi(stemp);           lq->next=lp;           lq=lp;       }    return head;}/*******************************************  输出链表 ******************************************/void outpoly(Node*head){  Node*lp=head->next;  while(lp->next!=NULL)  {if(lp->next->codf>0)                    printf("%.2fX^%d + ",lp->codf,lp->exp);                else                    printf("%.2fX^%d",lp->codf,lp->exp);lp=lp->next;  }  printf("%.2fX^%d",lp->codf,lp->exp);/*输出最后一个结点*/}/*****************************************************  两个多项式相加*******************************************************/Node*addpoly(Node*headf,Node*heads){  Node*head=(Node*)malloc(LENGTH),*lp,*lq=head;  Node*temp;/*临时结点*/  head->next=NULL;  headf=headf->next;  heads=heads->next;     while((headf!=NULL) && (heads!=NULL))/*两个链表中有一个为空退出循                                               环*/     {       if(headf->exp != heads->exp)         {           lp=(Node*)malloc(LENGTH);           lp->next=NULL;   temp=(headf->exp > heads->exp)? heads:headf;           lp->exp=temp->exp;           lp->codf=temp->codf;           lq->next=lp;           lq=lp;           if(temp==heads) /*注意此时不能用temp=temp->next,                     这将不会使heads或headf移动*/   heads=heads->next;           else   headf=headf->next;          }        else            {                if((headf->codf - heads->codf)!=0)                   {                      lp=(Node*)malloc(LENGTH);                      lp->next=NULL;                      lp->codf=headf->codf + heads->codf;                      lp->exp=headf->exp;                      lq->next=lp;                      lq=lp;                     }                  /*else continue;系数相等,且指数相等,忽略它*/                   heads=heads->next;                   headf=headf->next;              }    }/*其中一个链表扫描完*/   temp=(heads==NULL)?headf:heads;/*取得没扫描完的链,因为没有在不同的                                    链表中切换,因此TEMP没问题*/      while(temp!=NULL)         {            lp=(Node*)malloc(LENGTH);            lp->next=NULL;            lp->codf=temp->codf;            lp->exp=temp->exp;            lq->next=lp;            lq=lp;        temp=temp->next;          }  return head;}void main(){ Node*head,*head1,*head2; char str[20]; printf("Input the first poly:"); gets(str); head1=transpoly(str); printf("/n"); outpoly(head1); printf("/n"); printf("Input the second poly:"); gets(str); head2=transpoly(str); printf("/n"); outpoly(head2); printf("/n"); head=addpoly(head1,head2); printf("/nthe addpoly is:"); outpoly(head); getch();}
原创粉丝点击