OOP一元多项式类运算(类+对象)

【id:123】【10分】E. OOP一元多项式类运算(类+对象)
时间限制
1s
内存限制
128MB
题目描述

一元多项式按照升幂表示为:

Pn(x) = p0+ p1x + p2x2+ … +pnxn。(n>=0)

构建一元多项式类,数据成员用于保存多项式中每项的系数和指数,数据操作实现两个一元多项式的加、减、乘。


输入

测试数据数

对于每组测试数据

第一行,第一个多项式:多项式项数n  系数1 指数1 系数2 指数2  ...... 系数n 指数n

第二行,第二个多项式:多项式项数n  系数1 指数1 系数2 指数2 ......系数n 指数n

数据保证输入数据中多项式均为升幂且所有数据都为整数


输出

每组测试数据输出三行,分别是两个多项式的加、减、乘。

具体输出格式见样例(即人们习惯的多项式表示,需要额外注意系数或指数为1,-1的情况。)


样例查看模式 
正常显示
查看格式
输入样例1 <-复制
2
3 -9 0 4 1 -3 5
1 -7 0
2 1 0 -1 3
3 1 0 1 1 -1 2


输出样例1
-16+4x-3x^5
-2+4x-3x^5
63-28x+21x^5
2+x-x^2-x^3
-x+x^2-x^3
1+x-x^2-x^3-x^4+x^5

注意的点:

  • 使用一个数组来表示多项式,下标表示指数数值表示系数

  • 之前使用map的时候部分正确,不知道原因

  • 注意输出的格式

  • 分为下标0,下标1,下标其他进行输出

#include<iostream>
#include<iomanip>

using namespace std;

class Node {
public:
    int *xi;

    explicit Node(int *xi);

    Node();

    int *ADD(int *temp);

    int *SUB(int *temp);

    int *MULTI(int *temp);

    void print(int *temp);
};


Node::Node() {}

Node::Node(int *xi) {
    this->xi = new int[50];
    for (int i = 0; i < 50; ++i) {
        this->xi[i] = xi[i];
    }
}

void Node::print(int *temp) {
    bool first = true;
    int count = 0;
    for (int i = 0; i < 50; ++i) {
        if (temp[i] == 0) {
            count++;
        }
    }
    if (count == 50) {
        cout << 0 << endl;
        return;
    }
    if (temp[0] != 0) {
        first = false;
        if (temp[0] == 1) {
            cout << 1;
        } else if (temp[0] == -1) {
            cout << -1;
        } else {
            cout << temp[0];
        }
    }
    if (temp[1] != 0) {

        if (first) {
            first = false;
            if (temp[1] == 1) {
                cout << 'x';
            } else if (temp[1] == -1) {
                cout << "-x";
            } else {
                cout << temp[1] << "x";
            }
        } else {
            if (temp[1] == 1) {
                cout << "+x";
            } else if (temp[1] == -1) {
                cout << "-x";
            } else if (temp[1] > 0) {
                cout << "+" << temp[1] << "x";
            } else {
                cout << temp[1] << "x";
            }
        }

    }

    for (int i = 2; i < 50; ++i) {
        if (first) {
            if (temp[i] == 0) {
                continue;
            }
            first = false;
            if (temp[i] == 1) {
                cout << "x^" << i;
            } else if (temp[i] == -1) {
                cout << "-x^" << i;
            } else {
                cout << temp[i] << "x^" << i;
            }
        } else {
            if (temp[i] == 0) {
                continue;
            }
            if (temp[i] == 1) {
                cout << "+x^" << i;
            } else if (temp[i] == -1) {
                cout << "-x^" << i;
            } else if (temp[i] < 0) {
                cout << temp[i] << "x^" << i;
            } else {
                cout << "+" << temp[i] << "x^" << i;
            }

        }
    }
    cout << endl;
}

int *Node::ADD(int *temp) {
    int *res = new int[50];
    for (int i = 0; i < 50; ++i) {
        res[i] = xi[i] + temp[i];
    }
    print(res);
    return res;
}

int *Node::SUB(int *temp) {
    int *res = new int[50];
    for (int i = 0; i < 50; ++i) {
        res[i] = xi[i] - temp[i];
    }
    print(res);
    return res;
}

int *Node::MULTI(int *temp) {
    int *res = new int[50];
    for (int i = 0; i < 50; ++i) {
        res[i] = 0;
    }
    for (int i = 0; i < 50; ++i) {
        if (xi[i] == 0) {
            continue;
        }
        // //-----
        // int *multiTemp = new int[50];
        // for (int j = 0; j < 50; ++j) {
        //     multiTemp[i] = 0;
        // }
        // //-----
        // multiTemp[i] = xi[i];

        //xi*temp
        for (int j = 0; j < 50; ++j) {
            if (temp[j] == 0) {
                continue;
            }
            res[i + j] += xi[i] * temp[j];
        }

    }
    print(res);
    return res;
}

int main() {
    int tiems;
    cin >> tiems;
    while (tiems--) {
        int *xi = new int[50];
        for (int i = 0; i < 50; ++i) {
            xi[i] = 0;
        }
        int n;
        cin >> n;
        for (int i = 0; i < n; ++i) {
            int a, b;
            cin >> a >> b;
            xi[b] = a;
        }
        Node one(xi);
        //第二个
        for (int i = 0; i < 50; ++i) {
            xi[i] = 0;
        }
        cin >> n;
        for (int i = 0; i < n; ++i) {
            int a, b;
            cin >> a >> b;
            xi[b] = a;
        }
        Node two(xi);
        one.ADD(two.xi);
        one.SUB(two.xi);
        one.MULTI(two.xi);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
符号多项式的操作,已经成为表处理的典型用例。在数学上,一个一元多项式Pn(x)可按升幂写 成: Pn(x) = p0+ p1x+ p2x2+….+ pnxn 它由n+1个系数唯一确定,因此,在计算机里,它可用一个线 性表P来表示: P = (p0 ,p1 ,p2 ,… pn)每一项的指数i隐含在其系数pi的序号里。 假设Qm(x)是一元m次多项式,同样可用线性表Q来表示:Q = (q0 ,q1 ,q2 ,… qm)。 不失一般性,设m<n,则两个多项式相加的结果 Rn(x) = Pn(x)+Qm(x)可用线性表R表示:R = (p0+q0 , p1+q1 , p2 +q2 , … , pm +qm , pm+1 ,… pn)。显然,我们可以对P、Q和R采用顺序存储结构, 使得多项式相加的算法定义十分简洁。至此,一元多项式的表示及相加问题似乎已经解决了。 然而在通常的应用中,多项式的次数可能很高且变化很大,使得顺序存储结构的最大长度很难 决定。特别是在处理形如:S(x) = 1+3x10000+2x20000的多项式时,就要用一长度为20001的线性表来 表示,表中仅有三个非零元素,这种对内存空间的浪费是应当避免的,但是如果只存储非零系数项 则显然必须同时存储相应的指数。 一般情况下的一元n次多项式可写成: Pn(x) = p1x e1 + p2x e2 + … + pmx em 其中 pi,是指数为 ei 的项的非零系数,且满足 0 ≤ e1 < e2 < …< em = n,若用一个长度为 m 且 每个元素有两个数据项(系数项和指数项)的线性表便可唯一确定多项式 Pn(x)。 ((p1 ,e1) , (p2 ,e2) , … ,(pm,em)) 在最坏情况下,n+1(=m)个系数都不为零,则比只存储每项系数的方案要多存储一倍的数据。但 是,对于 S(x)的多项式,这种表示将大大节省空间。 本题要求选用线性表的一种合适的存储结构来表示一个一元多项式,并在此结构上实现一元多 项式的加法,减法和乘法操作

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值