第四半期のプロジェクト多項式の合計

5861 ワード

/*      
Copyright (c)2015,                    
All rights reserved.      
    : 4  --     .cpp      
      :        
    :2015 10 16       
     :v1.0      
      
    :           ,           。
    :    。
    :   A,   B,     A,B,       。
*/
#include <stdio.h>
#include <malloc.h>
#define MAX 20          //       
typedef struct      //            
{
    double coef;        //  
    int exp;            //  
} PolyArray;

typedef struct pnode    //         ,         ,       
{
    double coef;        //  
    int exp;            //  
    struct pnode *next;
} PolyNode;

void DispPoly(PolyNode *L)  //     
{
    bool first=true;        //first true      
    PolyNode *p=L->next;
    while (p!=NULL)
    {
        if (first)
            first=false;
        else if (p->coef>0)
            printf("+");
        if (p->exp==0)
            printf("%g",p->coef);
        else if (p->exp==1)
            printf("%gx",p->coef);
        else
            printf("%gx^%d",p->coef,p->exp);
        p=p->next;
    }
    printf("
"); } void DestroyList(PolyNode *&L) // { PolyNode *p=L,*q=p->next; while (q!=NULL) { free(p); p=q; q=p->next; } free(p); } void CreateListR(PolyNode *&L, PolyArray a[], int n) // { PolyNode *s,*r; int i; L=(PolyNode *)malloc(sizeof(PolyNode)); // L->next=NULL; r=L; //r , for (i=0; i<n; i++) { s=(PolyNode *)malloc(sizeof(PolyNode));// s->coef=a[i].coef; s->exp=a[i].exp; r->next=s; // *s *r r=s; } r->next=NULL; // next NULL } void Sort(PolyNode *&head) // exp { PolyNode *p=head->next,*q,*r; if (p!=NULL) // { r=p->next; //r *p p->next=NULL; // p=r; while (p!=NULL) { r=p->next; //r *p q=head; while (q->next!=NULL && q->next->exp>p->exp) q=q->next; // *p *q p->next=q->next; // *p *q q->next=p; p=r; } } } void Add(PolyNode *ha,PolyNode *hb,PolyNode *&hc) // , { PolyNode *pa=ha->next,*pb=hb->next,*s,*tc; double c; hc=(PolyNode *)malloc(sizeof(PolyNode)); // tc=hc; while (pa!=NULL && pb!=NULL) { if (pa->exp>pb->exp) { s=(PolyNode *)malloc(sizeof(PolyNode)); // s->exp=pa->exp; s->coef=pa->coef; tc->next=s; tc=s; pa=pa->next; } else if (pa->exp<pb->exp) { s=(PolyNode *)malloc(sizeof(PolyNode)); // s->exp=pb->exp; s->coef=pb->coef; tc->next=s; tc=s; pb=pb->next; } else //pa->exp=pb->exp { c=pa->coef+pb->coef; if (c!=0) // 0 { s=(PolyNode *)malloc(sizeof(PolyNode)); // s->exp=pa->exp; s->coef=c; tc->next=s; tc=s; } pa=pa->next; pb=pb->next; } } if (pb!=NULL) pa=pb; // while (pa!=NULL) { s=(PolyNode *)malloc(sizeof(PolyNode)); // s->exp=pa->exp; s->coef=pa->coef; tc->next=s; tc=s; pa=pa->next; } tc->next=NULL; } int main() { PolyNode *ha,*hb,*hc; 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); printf(" : "); DispPoly(hc); DestroyList(ha); DestroyList(hb); DestroyList(hc); return 0; }
 
<img src="http://img.blog.csdn.net/20151019160817592?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
    :
                ,           ,             ,