数据结构-多项式的求和(相减)顺序表实现

#include <stdio.h>
#include <stdlib.h>
#include<iostream>
using namespace std;
#define MAXSIZE 100
#define OK 1
#define OVERFLOW -1
#define ERROR -2
#define False 0
typedef int status;
typedef int ElemType;

typedef struct
{
    int coef;
    int exp;
}polynomial;
typedef struct
{
    polynomial *elem;
    int length;
    int listsize;
}Sqlist;
status Initlist_Sq(Sqlist &L)
{
    L.elem=new polynomial[MAXSIZE];
    if(!L.elem) exit(OVERFLOW);
    L.length=0;
    return OK;
}
int ListInput(Sqlist *L,polynomial e)
{
    int i=L->length;
    L->elem[i]=e;
    L->length++;
    return OK;
}
void Input1(Sqlist &L)
{
    int n,i;
    printf("输入多项式的项数:\n");
    scanf("%d",&n);
    printf("多项式:\n");
    for(i=0;i<n;i++)
    {
        scanf("%d %d",&L.elem[i].coef,&L.elem[i].exp);
        L.length++;
    }
}
status ListInsert_Sq(Sqlist &L,int i,int e1,int e2)
{
    if(i<1||i>L.length) return ERROR;
    if(L.length==MAXSIZE) return ERROR;
    for(int j=L.length-1;j>=i-1;j--)
    {
        L.elem[j+1]=L.elem[j];
    }
    L.elem[i-1].coef=e1;
    L.elem[i-1].exp=e2;
    ++L.length;
    return OK;
}
status Listadd(Sqlist &p,Sqlist &q)
{
    int k;
    Sqlist c;
    c.elem =new polynomial;
    //c数组的最大存储容量一定为p,q容量之和
    int i, j;
    //i-p j-q k-c
    i = j = k = 0;
    while (i<p.length&&j<q.length)
    {
        if (p.elem[i].exp< q.elem[j].exp) {
            c.elem[k++] = p.elem[i];
            i++;
        }
        else if (p.elem[i].exp > q.elem[j].exp) {
            c.elem[k++] = q.elem[j];
            j++;
        }
        else {
            if (p.elem[i].coef!=0&&q.elem[j].coef!=0) {
                c.elem[k].coef = p.elem[i].coef + q.elem[j].coef;
                c.elem[k].exp = p.elem[i].exp;
                k++;
                i++;
                j++;}
            if(p.elem[i].coef==0){  
               c.elem[k].coef =q.elem[j].coef;
               j++;
                k++;}
             }
        if(q.elem[j].coef==0)
        {
            c.elem[k].coef =p.elem[i].coef;
               k++;
              i++;
          }
    }
    while(i<p.length){
        c.elem[k++]=p.elem[i];
        i++;
    }
    while(j<q.length){
        c.elem[k]=q.elem[j];
        k++;
        j++;
    }
    p.elem=c.elem;
    return OK;
}
status Listsub_(Sqlist &p,Sqlist &q)
{
    Sqlist c;
    c.elem = (polynomial *)malloc((p.length + q.length) * sizeof(polynomial));
    //c数组的最大存储容量一定为p,q容量之和
    int i, j, k;
    //i-p j-q k-c
    i = j = k = 0;
    while (i<p.length&&j<q.length)
    {
        if (p.elem[i].exp < q.elem[j].exp) {
            c.elem[k++] = p.elem[i];
            i++;
        }
        else if (p.elem[i].exp > q.elem[j].exp) {
            c.elem[k++] = q.elem[j];
            j++;
        }
        else {
            if (p.elem[i].coef - q.elem[j].coef) { //系数相减不为0时
                c.elem[k].coef = p.elem[i].coef - q.elem[j].coef;
                c.elem[k].exp = p.elem[i].exp;
                k++;
                i++;
                j++;
            }
            else{  //为0时
            i++;
            j++;
        }
        }
    }
    while(i<p.length){
        c.elem[k++]=p.elem[i];
        i++;
    }
    while(j<q.length){
        c.elem[k]=q.elem[j];
        c.elem[k].coef=-c.elem[k].coef;
        k++;
        j++;
    }
    p.elem=c.elem;
    return OK;
}//顺序表实现多项式减法
 //顺序表查找值为e的数据元素
int LocateELem(Sqlist &L,polynomial e1,polynomial e2)
{
    for (int i=0;i< L.length;i++)    
         if (L.elem[i].coef==e1.coef&&L.elem[i].exp==e2.exp)     
             return i+1;    
        else return 0;
}
//顺序表中删除第i组数据元素
status ListDelete_Sq(Sqlist &L,int i)
{
    int j;  
    if((i<1)||(i>L.length)) return ERROR;//i值不合法   
    for(j=i;j<=L.length-1;j++)
        {    
            L.elem [j-1].coef=L.elem [j].coef;    
            L.elem [j-1].exp=L.elem [j].exp;
        }              //被删除元素之后的元素前移     
        --L.length;     //表长减1  
        return OK;
}


void Printlist(Sqlist &L)
{
    printf("输出顺序表:");
    printf("F(x)=");
    for(int i=0;i<L.length;i++)
    {
        printf("%dX^%d",L.elem[i].coef,L.elem[i].exp);
        if(i<L.length-1)
            printf("+");
    }
}
int main()
{
    Sqlist a,b;
    Initlist_Sq(a);
    Initlist_Sq(b);
    Input1(a);
    Input1(b);
    Listadd(a,b);
    Printlist(a);
    return 0;
}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

i染

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值