C 一元多项式求和

利用 线性表 数据结构 求一元多项式的和

附代码(插入操作 当表长超过初始长度时 元素赋值会有问题  记下来 以后研究)

#include <stdio.h>
#include <stdlib.h>

#define ERROR 0
#define OK 1

#define INIT_SIZE 5
#define INCERMENT 5

typedef int ElemType;

typedef struct
{
    ElemType *elem;
    int length;
    int size;
}List;

int InitList(List *L)
{
    //apply memory for list
    //integer poniert
    L->elem = (ElemType *) malloc(INIT_SIZE * sizeof(ElemType));
    if (!L->elem)
    {
        return ERROR;
    }
    L->length = 0;
    L->size = INIT_SIZE;
    return OK; 
}

//插入元素需要做哪些操作(越界的那一次操作没有给元素附上值)
int InsertElem(List *L, int i, ElemType e)
{
    //边界判断
    if ( i<1 || i > L->length+1 )
    {
        return ERROR;
    }

    ElemType *new;
    //whether it needs to apply memory for new element
    if ( L->length > L->size )
    {
        new = (ElemType*) realloc(L->elem, (L->length + INCERMENT) * sizeof(ElemType));
        if ( !new )
        {
            return ERROR;
        }

        L->elem = new;
        L->length += INCERMENT;
    }

    //把i以后的元素向后移动 高位向低位移动
    ElemType *p = &L->elem[i - 1];            //第i个的位置
    ElemType *q = &L->elem[L->length - 1];    //最后一个元素的位置

    //逐个往后退一
    for (; q >= p; q--)
    {
        *(q + 1) = *q;
    }
    *p = e;

    ++L->length;
    return OK;
}

int findElemByIndex(List L, int i, ElemType *e)
{
    if ( i<0 || i>L.length )
    {
        return ERROR;
    }
    *e = L.elem[i-1];
    return OK;
}


int main()
{
    //initilize a linear_table
    List L;
    if ( InitList(&L) )
    {
        printf("InitList successful\n");
        ElemType e;
        //tips
        printf("Please enter the length of arr\n");
        int num;
        scanf("%d",&num);

        int arr[num],i=0;
        printf("Please each element:\n");
        for (;i < num; i++)
        {
            scanf("%d", &arr[i]);
            InsertElem(&L, i+1, arr[i]);
        }

        int sum = 0;
        i = 0;
        for (;i < num; i++)
        {
            printf("i:%d\n", i);
            printf("e:%d\n", L.elem[i]);
            sum += L.elem[i];
        }
        printf("\nsum:%d\n", sum);
        //计算一元多项式的和 
        //思路 把每一项的值存入一个节点 遍历节点相加 求和

    }


}



转载于:https://my.oschina.net/u/2241804/blog/656725

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值