数据结构多项式乘积

#include<stdio.h>
#include<math.h>
#include<iostream>
#include<malloc.h>
#include<stdlib.h>
#include<conio.h>
using namespace std;
#define Listsize 20
#define OK 1
#define ERROR 0

typedef int Status;
     //一元多项式是一个有序链表,每一项都是LinkList的数据元素
typedef struct {
    float coef;   //系数
    int expn;     //指数
}ElemType;  //ElemType为LinkList的数据对象名

typedef struct LNode{       //结点类型
    ElemType data;
    struct LNode *next;
}*Link, *Position;

typedef struct {            //链表类型
    Link head,tail;  //分别指向线性链表的头结点和最后一个结点
    int len;                //指示线性链表中数据元素个数
}LinkList;

Status InitList(LinkList &L){//建立一个空表
    L.head = (Link)malloc(Listsize*sizeof(Link));
    L.head->next = NULL;
    if(!L.head) exit(OVERFLOW);
    L.len = 0;
    return OK;
}
Position Gethead(LinkList &L){//返回链表头结点位置
    return L.head;
}
Status setCurElem(Link &P, ElemType e){//设置头结点的数据元素
    P->data.coef = e.coef;
    P->data.expn = e.expn;
    return OK;
}
Position NextPos(LinkList &P, Link h){//返回当前结点
    return h->next;
}
Status LocateElem(LinkList &P, ElemType e, Link h, Link &found){
    Link cur = NextPos(P, h);
    if(cur != NULL){
    for (int i = 1; i <= P.len; i ++){
        if(cur->data.expn == e.expn){
            found = cur;
            return OK;
        }
        else
            cur = cur->next;
        }
    }
    return ERROR;
}
Status MakeNode(Link &s, ElemType e){
    s->data.coef = e.coef;
    s->data.expn = e.expn;
    return OK;
}
Status InsFirst(Link &h, Link &s, ElemType e){
    s->next = h->next;
    h->next = s;
    h = s;
    free(s);
    return OK;
}

void CreatPolyn(LinkList &L){//输入m项的系数和指数,建立表示一元多项式的有序链表P
    InitList(L);
    int m;
    Link h;
    h = Gethead(L);//获取P的是头结点
    ElemType e;
    e.coef = 0.0; e.expn = -1;
    setCurElem(h,e);//设置头结点数据元素
    cout << "Please input the term :  ";
    cin >> m;
    for(int i = 1; i <= m; i ++){//依次输入m个非零项
        cin >> e.coef >> e.expn;
        Link s;
        Link found;  //指向被找到的相同指数的项
        s = (Link)malloc(sizeof(Link));//为新结点分配内存
        if(!LocateElem(L, e, L.head, found)){//当前链表中不存在该指数项
                if(MakeNode(s, e)) {
                    if(InsFirst(h, s, e));
                        L.len ++;   //元素个数自加
                }
        }
    }
}
void PrintPQ(LinkList &P, LinkList &Q){
    Link curp = NextPos(P, P.head);Link curq = NextPos(Q, Q.head);
    cout << endl << "the first polynomial is : ";
    for(int i = 1; i <= P.len; i ++){
        cout << curp->data.coef << " " << curp->data.expn << " ";
        curp = curp->next;
    }
    cout << endl <<"the second polynomial is : ";

    for(int j = 1; j <= Q.len; j ++){
        cout << curq->data.coef << " " << curq->data.expn << " ";
        curq = curq->next;
    }
    cout << endl;
}
Status InsertR(LinkList &R, Link &temp, Link &hr){//把乘积项插入链表R中
    Link found;//指向被找到的相同指数的项
    if (LocateElem(R, temp->data, R.head, found)){
       found->data.coef += temp->data.coef;
       free(found);
    }
    else{
        temp->next = hr->next;
        hr->next = temp;
        hr = temp;
        free(temp);
        R.len ++;
    }
    return OK;
}
Status printR(LinkList &R){//打印出乘积
    Link curr = R.head->next;
    cout << endl << "The multiple is: ";
    for(int i = 1; i <= R.len; i ++){
        cout << curr->data.coef << " " << curr->data.expn << " ";
        curr = curr->next;
    }
    cout << endl;
    return OK;
}
void multiple(LinkList &P, LinkList &Q){
    int length;//存储乘积的链表的长度
    LinkList R;
    InitList(R);
    Link hp = Gethead(P);Link hq = Gethead(Q);Link hr = Gethead(R);//分别指向P,Q,R的头结点
    Link curp = NextPos(P,hp);Link curq = NextPos(Q,hq);//分别指向当前P,Q的结点位置
    for(int i = 1; i <= P.len; i ++){
        for(int j= 1; j <= Q.len; j ++){
                Link temp;//临时结点
                temp = (Link)malloc(sizeof(Link));//为临时结点分配内存
                temp->data.coef = curp->data.coef * curq->data.coef;
                temp->data.expn = curp->data.expn + curq->data.expn;
                InsertR(R, temp, hr);
                curq = curq->next;
        }
        curp = curp->next;
        curq = NextPos(Q,hq);
    }
    printR(R);
}

int main(){
    LinkList P;
    LinkList Q;
    CreatPolyn(P);//输入多项式P
    CreatPolyn(Q);//输入多项式Q
    PrintPQ(P,Q);//打印多项式P和Q
    multiple(P,Q);//输出多项式乘积

    //Status InitList(LinkList &L);//建立一个空表
    // void CreatPolyn(LinkList &P, int m);//输入m项的系数和指数,建立表示一元多项式的有序链表P
    //Position Gethead(LinkList &L);//返回链表头结点位置
    //Status setCurElem(Link &P, ElemType e);//设置头结点的数据元素
    //Status LocateElem(LinkList &P, ElemType e);//遍历P中是否存在与e的指数相同的项
    //Status MakeNode(Link &s, ElemType e);//生成结点
    //Status InsFirst(Link &h, Link &s);//将结点插入
    return 0;
}


展开阅读全文

没有更多推荐了,返回首页