尝试创建对象并将其打印到输出

时间:2011-09-11 23:27:27

标签: c++ visual-studio-2008

嗨我有一个多项式类,我正在尝试将多项式打印到屏幕上,但是我遇到了问题。我创建了一个方法,并且在方法中对象(多项式)打印,但是当我尝试从方法外部打印时,它并没有让我相信我没有正确地创建对象。当我在main中包含方法create()时,对象将自己打印到屏幕,但是当我使用printscreen()方法时,它不会打印。任何帮助将不胜感激。

我正在重新发布整个代码以避免混淆

    #include "stdafx.h"
    #include <vector>
    #include <iostream>




    using namespace std;

    class Polynomial
    {
    private:
int coef[100];

int deg;

    public:
Polynomial::Polynomial()
//~Polynomial(void);
{
    for ( int i = 0; i < 100; i++ )
    {
        coef[i] = 0;
    }
}
void set ( int a , int b )
{
    coef[b] = a;
    deg = degree();
}

int degree()
{
    int d = 0;
    for (int i = 0; i < 100; i++ )
        if ( coef[i] != 0 ) d = i;
    return d;
}

void print()
{
    for ( int i = 99; i >= 0; i-- ) {
        if ( coef[i] != 0 ) {
            cout << coef[i] << "x^" << i << " "; 
        }
    }
}
void reset()
{
    for ( int i = 99; i >= 0; i-- ) {
        if ( coef[i] != 0 ) {
            coef[i] = 0; 
        }

    }
}
int count()
{
    int ct = 0;
    for ( int i = 99; i >= 0; i-- ) {
        if (coef[i] != 0 ) {
            ct++;
            return ct;
        }
    }
}


Polynomial plus ( Polynomial b )
{
    Polynomial a = *this;
    Polynomial c;

    for ( int i = 0; i <= a.deg; i++ ) c.coef[i] += a.coef[i];
    for ( int i = 0; i <= b.deg; i++ ) c.coef[i] += b.coef[i];
    c.deg = c.degree();

    return c;


}
    Polynomial minus ( Polynomial b )
    {
    Polynomial a = *this; //a is the poly on the L.H.S
    Polynomial c;

     for ( int i = 0; i <= a.deg; i++ ) c.coef[i] += a.coef[i];
     for ( int i = 0; i <= b.deg; i++ ) c.coef[i] -= b.coef[i];
     c.deg = c.degree();

     return c;
     }
     void printscreen () const
     {
 //Polynomial a;
 std::cout << "Your polynomial is ";
 this->print();
     }

     Polynomial create ()
 {
  int temp = 0;
  int terms = 0;
  int exp = 0;
  int n = 0;
  Polynomial a = *this;
  cout << "How many terms would you like there to be in your polynomial?";
  cin >> terms;
  for ( int i = 1; i <= terms; i++ ){
    n++;
    cout << "\n";
    cout << "What would you like to be your coefficient #"; cout << n; cout << "?";
    cin >> temp;
    cout << "What would you like to be your exponent #"; cout << n; cout << "?";
    cin >> exp;
    a.set ( temp, exp );

    }
    cout << "Your polynomial is "; a.print();
    return a;
     }

    };

    void menu (void)
    { cout<<endl<<endl;
    cout<<"What would you like to do next?  Please select from the following menu:"   <<endl;
    cout<<"1.  Create another polynomial"<<endl;
    cout<<"2.  Reset the polynomial"<<endl;
    cout<<"3.  Display the number of terms in the polynomial"<<endl;
    cout<<"4.  Sum the polynomials"<<endl;
    cout<<"5.  Print the polynomial"<<endl;
    cout<<"6. Quit"<<endl<<endl;
    cout<<"Enter selection>";
    }

    int _tmain(int argc, _TCHAR* argv[])
    {



    Polynomial a, b, c;

int choice;


a.create();

do {
  menu();
  cin>>choice;

  switch (choice) {
    case 1 :a.create();
            cout<<endl;
            break;

    case 2 : a.reset();
            cout<<endl<<"The polynomial has been reset."<<endl;
            break;

    case 3: cout<<endl<<"Length is ";//<<a.length()<<endl;
            break;

    case 4: cout<<endl<<"Sum is "; a.plus(b);//<<s.sum()<<endl;
            break;

    case 5: printscreen(a);
            break;

    case 6: cout<<endl<<"Thanks and goodbye."<<endl;
            break;

    default :  cout<<endl<<"Invalid command.  Try again."<<endl;
  }

     } while (choice != 6);

2 个答案:

答案 0 :(得分:1)

使用

void printscreen (const Polynomial& a)
{
 cout<<"Your polynomial is "; a.print();
}

并致电

printscreen(a); 

答案 1 :(得分:1)

嗯,你在create()说:

{
  Polynomial a;
  //...
  a.set (temp, exp);
  //...
  a.print();
}

另一方面,在printscreen(),你说,

{
  Polynomial a;
  a.print();
}

显然,设置多项式的关键步骤完全没有了。

所有这些设置应该放在Polynomial类的成员函数中,而不是在自由函数中...

顺便说一下,C ++中没有术语“方法”;相反,我们更喜欢谈论“成员函数”,但您似乎没有在您的问题中定义任何成员函数。


如果我猜测上下文错误并且所有 实际上是成员函数,那么将printscreen()更改为:

void printscreen() const   // in global scope this is "Polynomial::printscreen()"
{
  std::cout << "Your polynomial is ";
  this->print();
}