2011-09-11 18 views
0

こんにちは多項式クラスがあり、多項式を画面に印刷しようとしていますが、問題があります。私はメソッドを作成し、そのメソッド内でオブジェクト(多項式)を印刷しますが、メソッドの外側から印刷しようとすると、オブジェクトを正しく作成していないと思われます。 main()メソッドにcreate()メソッドをインクルードすると、オブジェクトは画面に表示されますが、printscreen()メソッドを使用すると印刷されません。どんな助けもありがとう。オブジェクトを作成して出力に出力しよう

私はcreate()にあなたが言っている、任意の混乱

#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); 
+4

? –

+0

あなたのコードを少しきちんとして読みやすくすることができますか?また、あなたは 'printscreen'機能を持っていますが、あなたの印刷機能は表示されていません。 –

答えて

1

使用

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

を避け、まあ

printscreen(a); 
+0

ええと、私はこれらすべてがクラス内のメンバ関数であるという疑いを持っています。だから多分 'this-> print()'かそうでなければならないかもしれません。 –

+0

はいメンバー関数です。あなたはphaedrusをsuggesstedしていましたが、2つのエラーがあります: – NewAtCPlusPlus

+0

エラーC2662:Polynomial :: print: 'this'ポインタを 'Polynomial'から 'Polynomial'に変換できず、2番目のエラーはC3861 'printscreen:'見つかりました。 – NewAtCPlusPlus

1

を呼び出すためにコード全体を再投稿しています:

、他の一方で、あなたは

{ 
    Polynomial a; 
    a.print(); 
} 

が明らかに多項式を設定する重要なステップが完全に欠けている、と言っています。

すべてこの設定は、おそらくところで... Polynomialクラスのメンバ関数ではなく、自由な機能に

に行く必要があり、Cには用語「方法」++ありません。代わりに、私たちは "メンバ関数"について言いますが、あなたの質問にはメンバ関数を定義していないようです。


私は間違ったコンテキストを推測しているし、すべてのそれらのが実際のメンバ関数であり、その後にprintscreen()を変更する場合:同様にあなたの多項式:: printメソッドを追加する方法について

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

申し訳ありませんが、ここに私の印刷機能があるを発見!= 0){ \t \t \t \tcout << coef [i] << "x <<" << i << ""; \t \t \t \t \t \t} – NewAtCPlusPlus

+0

無効印刷() \t { \t \tため(私は99を= int型; I> = 0; i--){ \t \t \t(COEF [i]があれば: – NewAtCPlusPlus

関連する問題