多项式Polynomial

本文介绍了一个使用C语言实现的多项式运算程序,该程序能够进行多项式的创建、加法、减法、乘以单项式等操作,并通过栈来管理多项式。用户可以通过简单的命令界面输入指令来完成多项式的基本运算。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

/*将多项式存放在栈中,实现逆波兰运算
  默认多项式的项按倒序排列*/

typedef struct term {
// 存放多项式的结构体
    int coef;
    int exp;
    struct term *link;
}Term, *sTerm;

typedef struct termStack {
// 存放多项式结构体的结构体
    sTerm childTerm;
    struct termStack *link;
}TermStack, *sTermStack;

sTermStack myStack = NULL;

void createTerm(sTerm head)
{
// 创建多项式
    int coefInput, expInput;
    int count = 0;
    sTerm tmp, p;
    p = (sTerm)malloc(sizeof(Term));
    p = head;
    p->link = NULL;
    printf("Coef: ");
    scanf(" %d", &coefInput);
    while (0 != coefInput)
    {
        if (0 == count)// 多项式的第一项
        {
            p->coef = coefInput;
            printf("Exp: ");
            scanf(" %d", &expInput);
            p->exp = expInput;
            if (0 == expInput)
                break;
        }
        else
        {
            tmp = (sTerm)malloc(sizeof(Term));
            tmp->coef = coefInput;
            printf("Exp: ");
            scanf(" %d", &expInput);
            tmp->exp = expInput;
            tmp->link = p->link;
            p->link = tmp;
            p = tmp;
            if (0 == expInput)
                break;
        }
        printf("Coef: ");
        scanf(" %d", &coefInput);
        count++;
    }
}

/*void createStack(sTermStack thead, sTerm head)
{
    sTermStack p;
    p = (sTermStack)malloc(sizeof(TermStack));
    p = thead;
    p->link = NULL;
    p->childTerm = head;
}*/

void push(sTerm head)
{
// 多项式入栈
    sTermStack p;
    p = (sTermStack)malloc(sizeof(TermStack));
    p->childTerm = head;
    p->link = myStack;
    myStack = p;
}

void pop()
{
// 多项式出栈
    sTermStack tmp;
    tmp = (sTermStack)malloc(sizeof(TermStack));
    tmp = myStack;
    if (!myStack)
    {
        printf("The stack is empty!\n");
        return;
    }
    myStack = tmp->link;
    free(tmp);
}

void addTerm()
{
/*实现多项式加法
  总体思想是将第二个多项式遍历,与第一个多项式做比较,指数相同的项相加,不同的项插入*/
    sTerm p1, p2, tmp, t;
    p2 = myStack->childTerm;
    pop();
    p1 = myStack->childTerm;
    pop();
    tmp = p1;
    while (p2)
    {
        while(tmp->link)
        {
            if (tmp->exp == p2->exp)    // 指数相同
            {
                tmp->coef += p2->coef;
                break;
            }
            else if (p2->exp < tmp->exp && p2->exp > tmp->link->exp)    // 指数不同,且可以插入
            {
                t = (sTerm)malloc(sizeof(Term));
                t->coef = p2->coef;
                t->exp = p2->exp;
                t->link = tmp->link;
                tmp->link = t;
                break;
            }
            else if (p2->exp <= tmp->link->exp)
                tmp = tmp->link;
        }
        if (tmp->link == NULL)    // 插入到最后一项
        {
            if (tmp->exp == p2->exp)
                tmp->coef += p2->coef;
            else if(tmp->exp > p2->exp)
            {
                t = (sTerm)malloc(sizeof(Term));
                t->coef = p2->coef;
                t->exp = p2->exp;
                t->link = tmp->link;
                tmp->link = t;
            }
        }
        p2 = p2->link;
    }
    push(p1);
}

void minusTerm()
{
// 减法,具体思想同加法
    sTerm p1, p2, tmp, t;
    p2 = myStack->childTerm;
    pop();
    p1 = myStack->childTerm;
    pop();
    tmp = p1;
    while (p2)
    {
        while(tmp->link)
        {
            if (tmp->exp == p2->exp)
            {
                tmp->coef -= p2->coef;
                break;
            }
            else if (p2->exp < tmp->exp && p2->exp > tmp->link->exp)
            {
                t = (sTerm)malloc(sizeof(Term));
                t->coef = (-p2->coef);
                t->exp = p2->exp;
                t->link = tmp->link;
                tmp->link = t;
                break;
            }
            else if (p2->exp <= tmp->link->exp)
                tmp = tmp->link;
        }
        if (tmp->link == NULL)
        {
            if (tmp->exp == p2->exp)
                tmp->coef -= p2->coef;
            else if(tmp->exp > p2->exp)
            {
                t = (sTerm)malloc(sizeof(Term));
                t->coef = (-p2->coef);
                t->exp = p2->exp;
                t->link = tmp->link;
                tmp->link = t;
            }
        }
        p2 = p2->link;
    }
    push(p1);
}

// (不要在意函数名,瞎28起的2333
sTerm mutiNumTerm(sTermStack thead, int coef, int exp)
{
// 用一项乘多项式
    sTerm p, tmp;
    p = tmp = (sTerm)malloc(sizeof(Term));
    p = thead->childTerm;
    pop();
    tmp = p;
    while(tmp)
    {
        tmp->coef *= coef;
        tmp->exp += exp;
        tmp = tmp->link;
    }
    return p;
}

// 这个函数想实现多项式乘多项式,不过没做完,就不要看了
/*void mutiTermTerm(sTermStack thead)
{
    sTerm p1, p2, tmp, t;
    p1 = p2 = tmp = t = (sTerm)malloc(sizeof(Term));
    p2 = thead->childTerm;
    p1 = thead->link->childTerm;
    pop();
    pop();
    t = p1;
    while (t)
    {
        tmp = p2;
        while (tmp)
        {
            t->coef *= tmp->coef;
            t->exp += tmp->exp;
            tmp = tmp->link;
        }
        t = t->link;
    }
    push(p1);
}*/

void printTerm(sTermStack thead)
{
// 打印多项式
    int count = 0;
    sTerm p;
    p = (sTerm)malloc(sizeof(Term));
    p = thead->childTerm;
    while(p)
    {
        if (0 == count)    // 第一项
        {
            if (p->coef < 0)
                printf("- ");
            if (0 == p->coef)
                continue;
            if (0 == p->exp)
                printf("%d ", abs(p->coef));
            else
            {
                if (1 == p->coef)
                    printf("x^%d ", p->exp);
                else
                    printf("%d x^%d ", abs(p->coef), p->exp);
            }
        }
        else
        {
            if (p->coef > 0)
                printf("+ ");
            else if (p->coef < 0)
                printf("- ");
            if (0 == p->coef)
                continue;
            if (0 == p->exp)
                printf("%d ", abs(p->coef));
            else
            {
                if (1 == p->coef)
                    printf("x^%d ", p->exp);
                else
                    printf("%d x^%d ", abs(p->coef), p->exp);
            }
        }
        p = p->link;
        count++;
    }
    printf("\n");
}

int main(void)
{
    int count = 0;
    int coef, exp;
    char choice;
    sTerm head;
    printf("This is a polynomials program. \nPlease enter a valid command: \n[?] Read a polynomial. \n[=] Return top polynomial. \n[-] Difference two polynomials. \n[+] Add two polynomials. \n[*] Multiply a polynomial with a term. \n[X] Multiply two polynomials. \n[Q] Quit. \n");
    scanf(" %c", &choice);
    while ('Q' != choice)
    {
        switch (choice)
        {
        case '?':
            head = (sTerm)malloc(sizeof(Term));
            createTerm(head);
            push(head);
            break;
        case '=':
            printTerm(myStack);
            break;
        case '+':
            addTerm();
            break;
        case '-':
            minusTerm();
            break;
        case '*':
            printf("Coef: ");
            scanf(" %d", &coef);
            printf("Exp: ");
            scanf(" %d", &exp);
            mutiNumTerm(myStack, coef, exp);
            break;
        case 'X':
            mutiTermTerm(myStack);
            break;
        default:
            break;
        }
        count++;
        scanf(" %c", &choice);
    }
    return 0;
}

### 多项式转换为 Polynomial 形式的代码实现 在 Python 中,可以利用 `sklearn.preprocessing.PolynomialFeatures` 来将输入数据转换为多项式形式。以下是具体的代码示例: ```python from sklearn.preprocessing import PolynomialFeatures import numpy as np # 创建一个简单的二维数组作为输入数据 X = np.array([[2], [3]]) # 初始化 PolynomialFeatures 对象,指定 degree=2 表示二次多项式 poly = PolynomialFeatures(degree=2) # 转换原始数据 X 到多项式形式 X_poly = poly.fit_transform(X) print("原始数据:\n", X) print("多项式变换后的数据:\n", X_poly) ``` 上述代码展示了如何使用 `PolynomialFeatures` 将输入矩阵 \( X \) 转换成多项式形式的数据集[^2]。 --- ### 数学中的多项式Polynomial 的表示方法 假设有一个单变量的多项式函数 \( f(x) = a_0 + a_1x + a_2x^2 + ... + a_nx^n \),其中 \( n \) 是多项式的阶数 (degree)[^3]。 对于多变量的情况,比如两个变量 \( x_1 \) 和 \( x_2 \),其二阶多项式展开的形式如下所示: \[ f(x_1, x_2) = c_{0} + c_{1}x_1 + c_{2}x_2 + c_{3}x_1^2 + c_{4}x_1x_2 + c_{5}x_2^2 \] 这种扩展可以通过组合不同幂次的方式得到更高维度的空间映射[^1]。 当应用到实际场景时,例如支持向量机(SVM)中的核技巧,多项式核函数定义为: \[ K(\mathbf{x}, \mathbf{y}) = (\mathbf{x}^\top \mathbf{y} + r)^d \] 这里 \( d \) 代表多项式的次数,\( r \) 是常数偏移量[^1]。 --- #### 注意事项 如果选择过高次方程可能导致模型过度拟合训练数据而失去泛化能力[^3]。因此,在实践中需谨慎调整参数并验证效果。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值