2011-09-11 38 views
0

嗨,我有一個多項式類,我試圖打印多項式到屏幕,但我有問題。我創建了一個方法,在對象(多項式)打印的方法中,但是當我嘗試從方法外部打印時,它不會導致我相信我沒有正確創建對象。當我在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); 
+4

有關將多項式::打印方法,以及如何? –

+0

你可以讓你的代碼更整潔一些嗎?此外,你有一個'printscreen'功能,但你沒有顯示你的打印功能。 –

回答

1

使用

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

,並呼籲

printscreen(a); 
+0

嗯,我懷疑所有這些都是類內的成員函數......所以也許它應該是'this-> print()'或類似的東西。 –

+0

是的,他們是成員函數。我做了,因爲你suggessted phaedrus,我得到了2個錯誤: – NewAtCPlusPlus

+0

他們是錯誤C2662:Polynomial ::打印:不能將'this'指針從'const Polynomial'轉換爲'Polynomial&',第二個錯誤C3861'printscreen:'identifier not找到。 – NewAtCPlusPlus

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(); 
} 
+0

對不起,這是我的打印功能: – NewAtCPlusPlus

+0

無效打印() \t { \t \t對(INT I = 99; I> = 0;我 - ){ \t \t \t如果(COEF [I]!= 0){ \t \t \t \t cout << coef [i] <<「x ^」<< i <<「」; \t \t \t} \t \t} \t} – NewAtCPlusPlus

相關問題