使用链表读取基本多项式

发布于 2024-08-25 08:13:24 字数 4195 浏览 10 评论 0原文

好吧,在未能阅读多项式之后,我首先尝试一种基本方法。

所以我有带有 read 和 print 函数的 polinom 类:

#ifndef _polinom_h
#define _polinom_h

#include <iostream>
#include <list>
#include <cstdlib>
#include <conio.h>

using namespace std;

class polinom 
{
    class term
    {
    public:
        double coef;
        int pow;

    term(){
        coef = 0;
        pow = 0;
    }  
    };

list<term> poly;
list<term>::iterator i;

public:

void read(int id) 
{ 
    term t;
    double coef = 1;
    int pow = 0;
    int nr_term = 1;

    cout << "P" << id << ":\n";
    while (coef != 0) {
        cout << "Term" << nr_term << ": ";
        cout << "coef = "; 
        cin >> coef;
        if (coef == 0) break;
        cout << " grade = ";
        cin >> pow;

        t.coef = coef;
        t.pow = pow;
        if (t.coef != 0) poly.push_back(t);
        nr_term++;
    } 
}



    void print(char var) 
    { 
        for (i=poly.begin() ; i != poly.end(); i++ ) { //going through the entire list to retrieve the terms and print them  

            if (poly.size() < 2) {
                  if (i->pow == 0) //if the last term's power is 0 we print only it's coefficient 
                     cout << i->coef;

                  else if (i->pow == 1) {
                      if (i->coef == 1)
                          cout << var;
                      else if (i->coef == -1)
                          cout << "-" << var;
                      else 
                          cout << i->coef << var;
                  }

                  else
                     cout << i->coef << var << "^" << i->pow; //otherwise we print both 
            }

            else {
                if (i == poly.end()) { // if we reached the last term  
                    if (i->pow == 0) //if the last term's power is 0 we print only it's coefficient 
                        cout << i->coef;
                    else if (i->pow == 1)
                        cout << i->coef << var;
                    else
                        cout << i->coef << var << "^" << i->pow; //otherwise we print both 
                } 

                else { 
                    if (i->coef > 0) {
                        if (i->pow == 1)//if the coef value is positive  
                            cout << i->coef << var << " + "; //we also add the '+' sign 
                        else 
                            cout << cout << i->coef << var << "^" << i->pow << " + ";
                    }

                    else {
                        if (i->pow == 1)//if the coef value is positive  
                            cout << i->coef << var << " + "; //we also add the '+' sign 
                        else 
                            cout << cout << i->coef << var << "^" << i->pow << " + ";
                    }
            }
        }
    }
}

};


#endif   

嗯,它在只读取一项时有效,但是当读取更多时,打印的系数是一些随机值,并且在最后一项之后它应该打印“+”或“-” t。

那么知道出了什么问题吗?

谢谢!

最终更新

好的,我通过修改 Bill 的代码使其完美运行,非常感谢 Bill 和其他评论或回答的人!

这是最终的打印函数:

   void print(char var)  
{  
 list<term>::iterator endCheckIter;  
 for (i=poly.begin() ; i != poly.end(); i++ )
{ 
     //going through the entire list to retrieve the terms and print them  
     endCheckIter = i; 
     ++endCheckIter;  

     if (i->pow == 0)
         cout << i->coef;
     else if (i->pow == 1)
         cout << i->coef << var;
     else         
         cout << i->coef << var << "^" << i->pow;

     if (endCheckIter != poly.end()) { 
         if (endCheckIter->coef > 0) 
             cout << " + "; 
         else {  
             cout << " - "; 
             endCheckIter->coef *= -1;
        }
    }
} 

}

Ok, after failing to read a polynomial, I'm trying first a basic approach to this.

So i have class polinom with function read and print:

#ifndef _polinom_h
#define _polinom_h

#include <iostream>
#include <list>
#include <cstdlib>
#include <conio.h>

using namespace std;

class polinom 
{
    class term
    {
    public:
        double coef;
        int pow;

    term(){
        coef = 0;
        pow = 0;
    }  
    };

list<term> poly;
list<term>::iterator i;

public:

void read(int id) 
{ 
    term t;
    double coef = 1;
    int pow = 0;
    int nr_term = 1;

    cout << "P" << id << ":\n";
    while (coef != 0) {
        cout << "Term" << nr_term << ": ";
        cout << "coef = "; 
        cin >> coef;
        if (coef == 0) break;
        cout << " grade = ";
        cin >> pow;

        t.coef = coef;
        t.pow = pow;
        if (t.coef != 0) poly.push_back(t);
        nr_term++;
    } 
}



    void print(char var) 
    { 
        for (i=poly.begin() ; i != poly.end(); i++ ) { //going through the entire list to retrieve the terms and print them  

            if (poly.size() < 2) {
                  if (i->pow == 0) //if the last term's power is 0 we print only it's coefficient 
                     cout << i->coef;

                  else if (i->pow == 1) {
                      if (i->coef == 1)
                          cout << var;
                      else if (i->coef == -1)
                          cout << "-" << var;
                      else 
                          cout << i->coef << var;
                  }

                  else
                     cout << i->coef << var << "^" << i->pow; //otherwise we print both 
            }

            else {
                if (i == poly.end()) { // if we reached the last term  
                    if (i->pow == 0) //if the last term's power is 0 we print only it's coefficient 
                        cout << i->coef;
                    else if (i->pow == 1)
                        cout << i->coef << var;
                    else
                        cout << i->coef << var << "^" << i->pow; //otherwise we print both 
                } 

                else { 
                    if (i->coef > 0) {
                        if (i->pow == 1)//if the coef value is positive  
                            cout << i->coef << var << " + "; //we also add the '+' sign 
                        else 
                            cout << cout << i->coef << var << "^" << i->pow << " + ";
                    }

                    else {
                        if (i->pow == 1)//if the coef value is positive  
                            cout << i->coef << var << " + "; //we also add the '+' sign 
                        else 
                            cout << cout << i->coef << var << "^" << i->pow << " + ";
                    }
            }
        }
    }
}

};


#endif   

Well, it works when reading only one term but when reading more the printed coefficients are some random values and also after the last term it print '+' or '-' when it shouldn't.

So any idea what's wrong?

Thanks!

FINAL UPDATE

Ok, i made it work perfectly by modifying Bill's code so thanks a lot Bill and everyone else who commented or answered!

Here's the final print function:

   void print(char var)  
{  
 list<term>::iterator endCheckIter;  
 for (i=poly.begin() ; i != poly.end(); i++ )
{ 
     //going through the entire list to retrieve the terms and print them  
     endCheckIter = i; 
     ++endCheckIter;  

     if (i->pow == 0)
         cout << i->coef;
     else if (i->pow == 1)
         cout << i->coef << var;
     else         
         cout << i->coef << var << "^" << i->pow;

     if (endCheckIter != poly.end()) { 
         if (endCheckIter->coef > 0) 
             cout << " + "; 
         else {  
             cout << " - "; 
             endCheckIter->coef *= -1;
        }
    }
} 

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

回忆那么伤 2024-09-01 08:13:24
if (i == poly.end()) { // if we reached the last term   

此评论显示您的错误。对于任何给定的项目集合,items.end() 返回最后一项之后的条目。

例如,假设我有一个 5 项 std::vector:

[0] [1] [2] [3] [4]

然后 begin() 指向:

[0] [1] [2] [3] [4]
/\

而 end() 指向:

[0] [1] [2] [3] [4] []
                    /\

你的 for 循环,看起来像:

for (i=poly.begin() ; i != poly.end(); i++ )

请注意,将 i 与 < code>poly.end() 发生在使用 iter 之前。一旦i == poly.end(),你就完成了。

if (i == poly.end()) { 中的代码永远不会被执行,因为这永远不可能是真的。

您可以使用以下方法测试结束:

// get access to the advance function
#include <iterator>

....

std::list<term>::iterator endCheckIter = i;
std::advance(endCheckIter, 1);

if (endCheckIter == poly.end())
{
  ...
}

但更简单的方法可能是:

std::list<term>::iterator endCheckIter = i;
++endCheckIter;
if (endCheckIter == poly.end())
{
  ...
}

编辑:
我不知道你为什么会收到垃圾。添加缺少的大括号并处理非结束情况,一切正常:

void print(char var)  
{  
    list<term>::iterator endCheckIter;  
    for (i=poly.begin() ; i != poly.end(); i++ )
    { // <- MISSING BRACE
         //going through the entire list to retrieve the terms and print them  
         endCheckIter = i; 
         ++endCheckIter;  

         cout << i->coef << var << "^" << i->pow; // <- MISSING OUTPUT
         if (endCheckIter != poly.end()) { 
             if (i->coef > 0) 
                 cout << " + "; 
             else   
                 cout << " - "; 
        }
    } // <- MISSING BRACE
} 
if (i == poly.end()) { // if we reached the last term   

This comment shows your error. For any given collection of items, items.end() returns the entry after the last item.

For instance, say I have a 5-item std::vector:

[0] [1] [2] [3] [4]

Then begin() points to:

[0] [1] [2] [3] [4]
/\

And end() points to:

[0] [1] [2] [3] [4] []
                    /\

Your for loop, it looks like:

for (i=poly.begin() ; i != poly.end(); i++ )

Note that comparing i to poly.end() happens before iter is used. As soon as i == poly.end(), you're done.

Your code inside of if (i == poly.end()) { will never be executed because this can never be true.

You can test for the end using the following:

// get access to the advance function
#include <iterator>

....

std::list<term>::iterator endCheckIter = i;
std::advance(endCheckIter, 1);

if (endCheckIter == poly.end())
{
  ...
}

But a simpler way might be:

std::list<term>::iterator endCheckIter = i;
++endCheckIter;
if (endCheckIter == poly.end())
{
  ...
}

Edit:
I'm not sure why you're getting garbage. Add in your missing braces and handle the non-end case, and everything works here:

void print(char var)  
{  
    list<term>::iterator endCheckIter;  
    for (i=poly.begin() ; i != poly.end(); i++ )
    { // <- MISSING BRACE
         //going through the entire list to retrieve the terms and print them  
         endCheckIter = i; 
         ++endCheckIter;  

         cout << i->coef << var << "^" << i->pow; // <- MISSING OUTPUT
         if (endCheckIter != poly.end()) { 
             if (i->coef > 0) 
                 cout << " + "; 
             else   
                 cout << " - "; 
        }
    } // <- MISSING BRACE
} 
葬﹪忆之殇 2024-09-01 08:13:24

好吧,既然 Vlad 已经决定了他将如何去做,那么我会这样做:

#ifndef _polinom_h
#define _polinom_h

#include <iostream>
#include <list>
#include <cstdlib>
#include <cmath>

#include "infix_iterator.h"

using namespace std;

char var;

class polinom {
    class term {
        double coef;
        int power;
        ostream &write(ostream &os) const { 
            // At least to me, the logic is easier to follow if we 
            // handle one piece at a time. 
            // It may be longer, but I think it's easier to understand.

            // First, if the coefficient is negative, subtract the term instead of adding it.
            if (coef < 0) 
                // backspace over the "+ " and print '- ' in its place.
                os << "\b\b- ";

            // Then print the absolute value of the coefficient (if needed).
            if (fabs(coef) != 1)
                os << fabs(coef);

            // Then print the var (if needed)
            if (power != 0)
                os << var;

            // then print the power (if needed)
            if (abs(power) > 1)
                os << "^" << power;

            // And we're done.
            return os;
        }

        // support inserting a term into a stream.
        friend std::ostream &operator<<(std::ostream &os, term const &t) {  
            return t.write(os); 
        }

    public:
        term(double c=0.0, int p=0) : coef(c), power(p) {}
        bool read(std::ostream &os, std::istream &is, int num) {
            // This is only slightly modified from the originally posted question
            os << "\nTerm " << num << ": coef = ";
            is >> coef;
            if (coef == 0.0)
                return false;
            if (coef != 0.0) {
                os << " grade = ";
                is >> power;
            }
            return true;
        }

        bool operator<(term const &other) const { 
            // order by descending powers.
            return other.power < power;
        }
    };

    list<term> poly;

public:
    void read(int id) {
        term t;
        int nr_term = 1;
        std::cout << "P: " << id;

        // Read and save individual terms:
        while (t.read(std::cout, std::cin, nr_term++))
            poly.push_back(t);
    }

    void write(char var) { 
        // sort the polynomial so the highest powers come first.
        poly.sort();

        // save the variable name for later use.
        ::var = var;

        // Print out all the terms:
        std::copy(poly.begin(), poly.end(), infix_ostream_iterator<term>(std::cout, " + "));
    }
};

#endif   

使用它非常简单:

#include "polynom.h"

int main() { 
    polinom p;

    p.read(1);

    p.write('x');
    return 0;
}

Okay, now that Vlad has decided how he's going to do it, here's how I'd do it:

#ifndef _polinom_h
#define _polinom_h

#include <iostream>
#include <list>
#include <cstdlib>
#include <cmath>

#include "infix_iterator.h"

using namespace std;

char var;

class polinom {
    class term {
        double coef;
        int power;
        ostream &write(ostream &os) const { 
            // At least to me, the logic is easier to follow if we 
            // handle one piece at a time. 
            // It may be longer, but I think it's easier to understand.

            // First, if the coefficient is negative, subtract the term instead of adding it.
            if (coef < 0) 
                // backspace over the "+ " and print '- ' in its place.
                os << "\b\b- ";

            // Then print the absolute value of the coefficient (if needed).
            if (fabs(coef) != 1)
                os << fabs(coef);

            // Then print the var (if needed)
            if (power != 0)
                os << var;

            // then print the power (if needed)
            if (abs(power) > 1)
                os << "^" << power;

            // And we're done.
            return os;
        }

        // support inserting a term into a stream.
        friend std::ostream &operator<<(std::ostream &os, term const &t) {  
            return t.write(os); 
        }

    public:
        term(double c=0.0, int p=0) : coef(c), power(p) {}
        bool read(std::ostream &os, std::istream &is, int num) {
            // This is only slightly modified from the originally posted question
            os << "\nTerm " << num << ": coef = ";
            is >> coef;
            if (coef == 0.0)
                return false;
            if (coef != 0.0) {
                os << " grade = ";
                is >> power;
            }
            return true;
        }

        bool operator<(term const &other) const { 
            // order by descending powers.
            return other.power < power;
        }
    };

    list<term> poly;

public:
    void read(int id) {
        term t;
        int nr_term = 1;
        std::cout << "P: " << id;

        // Read and save individual terms:
        while (t.read(std::cout, std::cin, nr_term++))
            poly.push_back(t);
    }

    void write(char var) { 
        // sort the polynomial so the highest powers come first.
        poly.sort();

        // save the variable name for later use.
        ::var = var;

        // Print out all the terms:
        std::copy(poly.begin(), poly.end(), infix_ostream_iterator<term>(std::cout, " + "));
    }
};

#endif   

Using this is pretty trivial:

#include "polynom.h"

int main() { 
    polinom p;

    p.read(1);

    p.write('x');
    return 0;
}
失眠症患者 2024-09-01 08:13:24
void print(char var)
{
    for (list<term>::const_iterator i = poly.begin(), e = poly.end(); i != e; ++i) {
        if (i != poly.begin() || i->coef < 0) {
            cout << (i->coef > 0 ? '+' : '-');
        }
        if (abs(i->coef) != 1) {
            cout << abs(i->coef);
        }
        if (i->pow == 0) {
            if (abs(i->coef) == 1) {
                cout << 1;
            }
        } else {
            cout << var;
            if (i->pow != 1) {
                cout << '^' << i->pow;
            }
        }
    }
}
void print(char var)
{
    for (list<term>::const_iterator i = poly.begin(), e = poly.end(); i != e; ++i) {
        if (i != poly.begin() || i->coef < 0) {
            cout << (i->coef > 0 ? '+' : '-');
        }
        if (abs(i->coef) != 1) {
            cout << abs(i->coef);
        }
        if (i->pow == 0) {
            if (abs(i->coef) == 1) {
                cout << 1;
            }
        } else {
            cout << var;
            if (i->pow != 1) {
                cout << '^' << i->pow;
            }
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文