程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C >> 關於C >> c語言實現一元多項式

c語言實現一元多項式

編輯:關於C
[cpp] 
/*
 * Ploly.c
 *
 *  Created on: 2012-12-3
 *      Author: Administrator
 */ 
#include <stdio.h> 
#include <stdlib.h> 
#define DEV 0.0000002 
#define TRUE 1 
#define OK 1 
#define ERROR 0  www.2cto.com
#define CHECK(t) do{if(!(t)){return 0;}}while(0) 
typedef int Status; 
typedef struct term { 
    double coef; 
    int expn; 
    struct term *next; 
} PolyNode, *Poly; 
Status makeNode(double coef, int expn, Poly *p) { 
    (*p) = (Poly) malloc(sizeof(struct term)); 
    CHECK(*p); 
    (*p)->coef = coef; 
    (*p)->expn = expn; 
    (*p)->next = 0; 
    return OK; 

Status destoryPoly(Poly *head) { 
    Poly p = *head; 
    while (p) { 
        *head = p->next; 
        free(p); 
        p = *head; 
    } 
    *head = 0; 
    return OK; 

Status AddNodePoly(Poly *head, double coef, int expn) { 
    CHECK(*head); 
    if (abs(coef) < DEV) 
        return OK; 
    Poly q = (*head)->next; 
    Poly p = (*head); 
    Poly s; 
    while (q && expn < q->expn) { 
        p = q; 
        q = q->next; 
    } 
    if (q == 0 || expn > q->expn) { 
        CHECK(makeNode(coef, expn,&s)); 
        p->next = s; 
        s->next = q; 
    } else { 
        q->coef += coef; 
        if (q->coef == 0) { 
            p->next = q->next; 
            free(q); 
        } 
    } 
    return OK; 

Status createPoly(Poly *head) { 
    if (*head) 
        destoryPoly(head); 
    CHECK(makeNode(0,0,head)); 
    int expn; 
    double coef; 
    do { 
        scanf("%lf%d", &coef, &expn); 
        if (abs(coef) < DEV && expn == 0) 
            break; 
        if (abs(coef) < DEV 
        ) 
            continue; 
        if (!AddNodePoly(head, coef, expn)) { 
            return ERROR; 
        } 
    } while (TRUE); 
    return OK; 

Status print(Poly head) { 
    CHECK(head); 
    Poly p = head->next; 
    while (p) { 
        printf("%1.1f", p->coef); 
        if (p->expn) 
            printf("*x^%d", p->expn); 
        if (p->next && p->next->coef > 0) 
            printf("+"); 
        p = p->next; 
    } 
    printf("\n"); 
    return OK; 

Status AddPoly(Poly *a, Poly *b) { 
    CHECK(*a&&*b&&*a!=*b); 
    Poly p = *a,p1 = (*a)->next,p2 = (*b)->next,t; 
    while (p1 && p2) { 
        if (p1->expn == p2->expn) { 
            p1->coef = p1->coef + p2->coef; 
            if (p1->coef == 0) { 
                t = p1->next; 
                free(p1); 
                p1 = t; 
            } else { 
                p->next = p1; 
                p1 = p1->next; 
                p = p->next; 
            } 
            t = p2->next; 
            free(p2); 
            p2 = t; 
        } else { 
            t = p1->expn > p2->expn ? p1 : p2; 
            p->next = t; 
            p = t; 
            t = t->next; 
            p1->expn > p2->expn ? p1 : p2 = t; 
        } 
    } 
    p->next = 0; 
    if(p2){ 
        p->next = p2; 
    } 
    free(b); 
    b = 0; 
    return OK; 

Status SubPoly(Poly *a, Poly *b) { 
    CHECK(*a&&*b&&*a!=*b); 
    Poly p = (*b)->next; 
    while(p){ 
        p->coef = -p->coef; 
        p = p->next; 
    } 
    return AddPoly(a,b); 

Status reverse(Poly head){ 
    CHECK(head); 
    Poly e = 0; 
    Poly p = head->next; 
    Poly t; 
    while(p){ 
        t = p->next; 
        p->next = e; 
        e = p; 
        p = t; 
    } 
    head->next = e; 
    return OK; 

Status multiPoly(Poly *a,Poly *b){ 
    CHECK(*a&&*b); 
    Poly head,p1,p2; 
    CHECK(makeNode(0,0,&head)); 
    p1 = (*a)->next; 
    while(p1){ 
        p2 = (*b)->next; 
        while(p2){ 
            AddNodePoly(&head,p1->coef*p2->coef,p1->expn+p2->expn); 
            print(head); 
            p2 = p2->next; 
        } 
        p1 = p1->next; 
    } 
    *a = head; 
    return OK; 

int main() { 
    freopen("c:/in.log", "r", stdin); 
    Poly a = 0, b = 0; 
    if (createPoly(&a)) { 
        print(a); 
    } 
    if (createPoly(&b)) { 
        print(b); 
    } 
    if (SubPoly(&a, &b)) { 
        print(a); 
    } 
    destoryPoly(&a); 
    return 0; 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved