Sorting polynomial terms based on degrees

I have this code which is about polynomials multiplication. I meet no error, just what I want is to sort the terms based on degrees (from lower degree to higher). If I add condition in operator* in //commented part, it will be printed too many of times. I would like to know if I can use m_Polynomial.sort()? If yes how? If not, what other methods can I use?

As it is both for printing polynomials and their multiplication result it would be nice if it could be added in print function.

Also if it is possible to change the printing style of Polynomials to desired format (to add coefficients of same degree terms)

I need to say this assignment was too harder than my knowledge in C++, with your help I could progress this much and learned a lot. If I ask too many questions is not becasue I don't want to think and work, I am doing my best, just here is not only forum for me, but it is the C++ class that I don't have. Thanks


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#include <iostream>
#include <fstream>
#include <string>
#include <list>
#include <vector>
using namespace std;


typedef struct Node
{
    double  cof;      // coefficient 
    int     deg;      // degree
} Node;

class CPolynomial
{
public:
    CPolynomial();
    CPolynomial(const string& file);
    ~CPolynomial();
    CPolynomial operator*(const CPolynomial &right);
    CPolynomial& operator=(const CPolynomial &right);
    void Print() const;

private:
    void ReadFromFile(string file);

private:
    list<Node> m_Polynomial;
};



int main()
{
    CPolynomial p1("P3.txt");
    CPolynomial p2("P4.txt");
    CPolynomial p3;
    p1.Print();
    p2.Print();

    p3 = p1*p2;
    p3.Print();

    system("pause");
    return 0;
}

CPolynomial::CPolynomial()
{
    Node term;
    term.cof = 0;
    term.deg = 0;
    m_Polynomial.push_back(term);
}

CPolynomial::~CPolynomial()
{
    m_Polynomial.clear();
}

CPolynomial::CPolynomial(const string& file)
{
    ReadFromFile(file);
}

CPolynomial CPolynomial:: operator*(const CPolynomial &right)
{
    CPolynomial result;
    result.m_Polynomial = m_Polynomial;

    for (list<Node>::iterator itr = result.m_Polynomial.begin(); itr != result.m_Polynomial.end(); ++itr)
    {
        itr->cof = 0;
        itr->deg = 0;
    }

    Node term;
    Node termR;
    Node temp;

    for (list<Node>::const_iterator it = m_Polynomial.begin(); it != m_Polynomial.end(); ++it)
    {
        for (list<Node>::const_iterator itR = right.m_Polynomial.begin(); itR != right.m_Polynomial.end(); ++itR)
        {
            term = *it;
            termR = *itR;

            temp.cof = termR.cof* term.cof;
            temp.deg = termR.deg + term.deg;

            for (list<Node>::iterator itr = result.m_Polynomial.begin(); itr != result.m_Polynomial.end(); ++itr)
            {
                if (temp.deg == itr->deg)
                {
                    temp.cof += itr->cof;
                    itr->cof = 0;
                }
            // if(temp.deg < it->deg)
            //result.m_Polynomial.insert(it, temp);
            }
            result.m_Polynomial.push_back(temp);
        }
    }
    return result;
}

CPolynomial& CPolynomial:: operator=(const CPolynomial &right)
{

    this->m_Polynomial = right.m_Polynomial;
    return *this;
}

void CPolynomial::Print() const
{
    list<Node>::const_iterator it;

    for (it = m_Polynomial.begin(); it != m_Polynomial.end(); it++)
    {
        if (it->cof == 0)
        {
            ;
        }
        else
        {
            if (it->cof > 0)
            {
                if (it != m_Polynomial.begin()) // if 'it' is not the first term, '+' is not necessary
                    cout << "+";
            }
            cout << it->cof;
            if (it->deg != 0)
                cout << "x^" << it->deg;
        }
    }
    cout << endl;
}

void CPolynomial::ReadFromFile(string file)
{
    Node term;
    fstream MyFile;
    string p;
    int num;

    MyFile.open(file);

    if (!MyFile.is_open())
    {
        cerr << "Unable to open input file" << endl;
        exit(EXIT_FAILURE);
    }
    else
    {
        MyFile >> p >> num;

        std::list<Node>::iterator it = m_Polynomial.begin();

        for (int i = 0; i < num; i++)
        {
            MyFile >> term.deg >> term.cof;

            m_Polynomial.push_back(term);
        }
        MyFile.close();
    }

}


Output:
1
2
3
4
5
6
7
2x^1-3x^4     (P1)
4x^1-3x^4     (P2)
8x^2
-6x^5
-12x^5
9x^8
2x^1-1x^2-3x^4    (P1*P2)


P1.txt
1
2
3
4
5
6
7
8
9
P 8
0 2
5 -3
12 5
2 6
5 7
3 -4
2 9
2 2


P4.txt
1
2
3
P 2
1 4
4 -3


Output
1
2
3
2-3^5+5x^12+6x^2+7x^5-4x^3+9x^2+2x^2         (P1)
4x^1-3x^4                                    (P2)
8x^1+20x^13-15x^16_12x^9-22x^4+12x^7+68x^3    (P1*P2)


desired output:
1
2
3
2+17x^2-4x^3+4x^5+5x^12
4x^1-3x^4 
8x^1+68x^3-22x^4+12x^7_12x^9+20x^13-15x^16


Last edited on
http://www.cplusplus.com/forum/beginner/194257/
TheIdeasMan wrote:
Just a note for the future :+)

There also shouldn't be 2 topics about essentially the same subject. There is a risk that the same things will be said in both, possibly making it a time waster for those who reply.
@closed account 5a8Ym39o6
Thank you for reminding but my problem is really different, if someone wants to understand my problem and help should read all comments and it doesn't happen.
How can I ask people to check a different problem for same topic
> How can I ask people to check a different problem for same topic
Just go to the first page of old topic and press "Edit topic". Then change the topic name to "Why my operator* acts illogical?".

That's the way.
Ok thank you

@closed account 5a8Ym39o6
Line 89: Don't you want to be calling AddOneTerm() with temp, not term?

You're building temp, but not doing anything with it (other than displaying it). Passing term is simply going to build result like the left hand side.
@AbstractionAnon

Oh that's a stupid mistake!
thanks
(question edited)
Last edited on
Did you get it working correctly?

I believe an earlier version of your last post said you were still not getting the correct results.
Topic archived. No new replies allowed.