C++ 新手问下有没有办法链式定义一个类 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
BRS5672023
V2EX    C++

C++ 新手问下有没有办法链式定义一个类

  •  
  •   BRS5672023 2024-10-21 14:38:15 +08:00 2656 次点击
    这是一个创建于 420 天前的主题,其中的信息可能已经有所发展或是发生改变。

    比如说我先定义一个单项式类为这种形式:

    a * pow(p, n) * pow(q, m) 

    其中 a, m, n 都是整数,p 和 q 是两个字符,然后现在我想定义操作一个更复杂的类,就是一般所说的多项式,但是它要满足我添加一个单项式进来之后仍然还是一个多项式的要求。。

    第 1 条附言    2024-10-21 16:50:26 +08:00

    用了下 chatgpt 对照着写了下大概完成了(手法很原始,抱歉我实在没啥基础)

    #include <iostream> using namespace std; #include <string> #include <vector> // We define first the monomial class to be of the form a * pow(p, n) * power(q, m). We also fix the variables p, q here. class Mono { public: int coefficient; int power1; int power2; Mono(int a, int n = 0, int m = 0) : coefficient(a), power1(n), power2(m) {} }; class Poly { friend ostream & operator<<(ostream & os, const Poly & poly); private: vector<Mono> terms; public: Poly & addTerm(const Mono & term) { terms.push_back(term); return * this; } }; ostream & operator<<(ostream & os, const Poly & poly) { for (size_t i = 0; i < poly.terms.size(); i++) { const auto & term = poly.terms[i]; if (term.coefficient != 0) { if (term.power1 == 0 && term.power2 == 0) { os << term.coefficient; } else if (term.power1 == 0) { os << term.coefficient << " * pow(q, " << term.power2 << ")"; } else if (term.power2 == 0) { os << term.coefficient << " * pow(p, " << term.power1 << ")"; } else { os << term.coefficient << " * pow(p, " << term.power1 << ") * " << "pow(q, " << term.power2 << ")"; } if (i < poly.terms.size() - 1) { os << " + "; } } } return os; } int main() { Poly poly; poly.addTerm(Mono(3)).addTerm(Mono(0,1)).addTerm(Mono(1,1)).addTerm(Mono(1,0,1)).addTerm(Mono(2,2,2)); cout << "Polynomial:\n" << poly << endl; return 0; } 

    输出结果是

    Polynomial: 3 * pow(p, 2) * pow(q, 1) + 5 * pow(p, 2) * pow(q, 1) 
    第 2 条附言    2024-10-21 16:51:56 +08:00

    上面 append 里的输出结果复制错了,应该是

    Polynomial: 3 + 1 * pow(p, 1) + 1 * pow(q, 1) + 2 * pow(p, 2) * pow(q, 2) 
    9 条回复    2024-10-22 15:54:02 +08:00
    jones2000
        1
    jones2000  
       2024-10-21 14:52:17 +08:00
    不就是一个四则运算, 直接字符解析下, 不完是了, 计算机本科“编译原理”就有。 压栈出栈。
    c0xt30a
        2
    c0xt30a  
       2024-10-21 14:53:17 +08:00
    你可能需要模版表达式跟操作符重载这两个特性(手法)。前者不如后者常见,你搜索下 C++ expression templates
    molika
        3
    molika  
       2024-10-21 15:42:56 +08:00
    写个逆波兰表达式就完事了.
    ipwx
        4
    ipwx  
       2024-10-21 17:15:03 +08:00   1
    楼主要不要试试怎么能让你的 C++ 程序支持:

    auto p = Symbol("p");
    auto q = Symbol("q");

    auto expression = 3 * pow(p, 2) * pow(q, 1) + 5 * pow(p, 2) * pow(q, 1);
    std::cout << expression << std::endl;
    BRS5672023
        5
    BRS5672023  
    OP
       2024-10-21 21:38:01 +08:00
    @ipwx 我试了一下,大概是下面的形式
    ```
    #include <iostream>
    using namespace std;

    #include <string>
    #include <vector>

    class Mono {
    public:
    int coefficient;
    char variable1;
    int power1;
    char variable2;
    int power2;

    Mono(int a, char p, int n = 0, char q = '1', int m = 0) : coefficient(a), variable1(p), power1(n), variable2(q), power2(m) {}

    bool isSameTerm(const Mono & other) {
    return(variable1 == other.variable1 && power1 == other.power1 && variable2 == other.variable2 && power2 == other.power2);
    }

    };

    Mono operator * (int coef, const Mono & mono) {
    return Mono(coef * mono.coefficient, mono.variable1, mono.power1, mono.variable2, mono.power2);
    }

    Mono operator * (const Mono & lhs, const Mono & rhs) {
    if (lhs.variable2 == '1' && rhs.variable2 == '1') {
    return Mono(lhs.coefficient * rhs.coefficient, lhs.variable1, lhs.power1, rhs.variable1, rhs.power1);
    }
    else if (lhs.variable1 == rhs.variable1 && lhs.variable2 == '1') {
    if (rhs.variable2 == '1') {
    return Mono(lhs.coefficient * rhs.coefficient, lhs.variable1, lhs.power1 + rhs.power1);
    }
    return Mono(lhs.coefficient * rhs.coefficient, lhs.variable1, lhs.power1 + rhs.power1, lhs.variable2, lhs.power2);
    }
    else if (lhs.variable1 == rhs.variable1 && rhs.variable2 == '1') { // We do not need consider the case when the second variable of lhs is '1', which was already contained in the last case.
    return Mono(lhs.coefficient * rhs.coefficient, lhs.variable1, lhs.power1 + rhs.power1, rhs.variable2, rhs.power2);
    }
    else if (lhs.variable1 == rhs.variable1 && lhs.variable2 == rhs.variable2) {
    return Mono(lhs.coefficient * rhs.coefficient, lhs.variable1, lhs.power1 + rhs.power1, lhs.variable2, lhs.power2 + rhs.power2);
    }
    else if (lhs.variable1 == rhs.variable2 && lhs.variable2 == rhs.variable1) {
    return Mono(lhs.coefficient * rhs.coefficient, lhs.variable1, lhs.power1 + rhs.power2, lhs.variable2, lhs.power2 + rhs.power1);
    }
    throw -1;
    }

    Mono pow(char variable, int power) {
    return Mono(1, variable, power);
    }

    class Poly {
    friend ostream & operator << (ostream & os, const Poly & poly);
    private:
    vector<Mono> terms;

    public:
    Poly & addTerm(const Mono & term) {
    terms.push_back(term);
    return * this;
    }

    Poly & operator + (const Mono & term) {
    addTerm(term);
    return * this;
    }

    void simplify() {
    for (size_t i = 0; i < terms.size(); i++) {
    for (size_t j = i + 1; j < terms.size(); j++) {
    if (terms[i].isSameTerm(terms[j])) {
    terms[i].coefficient = terms[i].coefficient + terms[j].coefficient;
    terms.erase(terms.begin() + j);
    j--;
    }
    }
    }
    }
    };

    ostream & operator << (ostream & os, const Poly & poly) {
    for (size_t i = 0; i < poly.terms.size(); i++) {
    const auto & term = poly.terms[i];
    if (term.coefficient != 0) {
    if (term.power1 == 0 && term.power2 == 0) {
    os << term.coefficient;
    }
    else if (term.power1 == 0) {
    os << term.coefficient << " * pow(" << term.variable2 << ", " << term.power2 << ")";
    }
    else if (term.power2 == 0) {
    os << term.coefficient << " * pow(" << term.variable1 << ", " << term.power1 << ")";
    }
    else {
    os << term.coefficient << " * pow(" << term.variable1 << ", " << term.power1 << ") * " << "pow(" << term.variable2 << ", " << term.power2 << ")";
    }
    if (i < poly.terms.size() - 1) {
    os << " + ";
    }
    }
    }
    return os;
    }

    int main() {
    try {
    Poly poly;
    poly = poly + (3 * pow('p', 2) * pow('q', 1)) + (5 * pow('p', 2) * pow('q', 1)) + (2 * pow('p', 1) * pow('q', 1));
    cout << "Polynomial before simplification:\n" << poly << endl;

    poly.simplify();
    cout << "Polynomial after simplification:\n" << poly << endl;
    }

    catch(int err0) {
    cout << "You must input polynomials of two variables with aligned variables for multiplication!" << endl;
    cout << "Error number: " << err0 << endl;
    }
    return 0;
    }
    ```
    问题是我这样写就必须这样定义一个多项式
    ```
    Poly poly;
    poly = poly + (3 * pow('p', 2) * pow('q', 1)) + (5 * pow('p', 2) * pow('q', 1)) + (2 * pow('p', 1) * pow('q', 1));
    ```
    还有就是无法区分 pow(p, n) * pow(q, m) 和 pow(q, m) * pow(p, n) 这两种形式(除非我指定两个 char 的顺序);不过第二个问题就是我一开始想要的对于非交换的多项式的计算。。(然后我期待可以计算两个多项式的对易子,最后计算得到关于 p q 的对易子的一个多项式)
    BRS5672023
        6
    BRS5672023  
    OP
       2024-10-21 21:39:02 +08:00
    上面的程序输出的结果是
    ```
    Polynomial before simplification:
    3 * pow(p, 2) * pow(q, 1) + 5 * pow(p, 2) * pow(q, 1) + 2 * pow(p, 1) * pow(q, 1)
    Polynomial after simplification:
    8 * pow(p, 2) * pow(q, 1) + 2 * pow(p, 1) * pow(q, 1)
    ```
    blacktail
        7
    blacktail  
       2024-10-22 13:34:58 +08:00
    看最后的回复,这是要多项式化简?
    BRS5672023
        8
    BRS5672023  
    OP
       2024-10-22 14:58:32 +08:00
    @blacktail 只是随便写了下化简的部分;重点也不是在化简的部分,但是当然希望我的计算结果能够输出一个简化过的形式。。

    另外程序写的还是有挺多问题的,包括甚至不能计算展开多项式的乘积。。
    ipwx
        9
    ipwx  
       2024-10-22 15:54:02 +08:00
    @BRS5672023 把 Mono 再拆开成为最基本的形式比较好。那样的话自然可以区分 p * q 或者 q * p 了。
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     3150 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 26ms UTC 11:32 PVG 19:32 LAX 03:32 JFK 06:32
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86