c++ - 使用链表读取基本多项式

标签 c++ printing polynomial-math

好的,在读取多项式失败后,我首先尝试一种基本方法。

所以我有带有读取和打印功能的类 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   

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

那么知道哪里出了问题吗?

谢谢!

最终更新

好的,我通过修改 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;
        }
    }
} 

}

最佳答案

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++ )

请注意,比较 ipoly.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
} 

关于c++ - 使用链表读取基本多项式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2418244/

相关文章:

c++ - 如何过滤 vector 并返回指向 vector 元素的指针 vector

c++ - 带有截断下溢的字符减法

css - 如何用CSS改变打印页面的页眉和页脚的字体?

python - "a bytes-like object is required"但我使用的是字节

html - 在 chrome 中打印纯 HTML 时不同的字体大小

java - Java 中的复系数多项式求根

C++ 求解四次根(四阶多项式)

c++ - 内联成员缺少返回时没有错误(VS2013)

c++ - VS-2019 程序在某些服务器上的第一个浮点指令处崩溃

lisp - 根据符号和指数对多项式排序