2009-11-22 57 views
1

我無法弄清楚如何顯示一個月,而不必像顯示「開關」一樣顯示「一月」 - 如果我嘗試只是oputput它,我得到一個數字1顯示一個月不是數字1

我是否需要爲日期priceDate創建內存;私人會員? 比複製我的拷貝構造函數?

抱歉關於格式化。我必須移動的東西,所以它會顯示在這個頁面上

這就是我所擁有的。我是否需要爲私人會員日期創建記憶?

class date 
    { 
    public: 
typedef enum {INVALID, JANUARY, FEBRUARY, MARCH, APRIL, MAY, JUNE, 
       JULY, AUGUST, SEPTEMBER, OCTOBER, NOVEMBER, DECEMBER} 
Month; 

date(Month month, int day, int year); 
date(const date& date);     // copy constructor 
date(void);        // default constructor 
~date(void); 

friend ostream& operator<<(ostream& out, const date& d); 

    private: 
Month month; 
int  day; 
int  year; 
    }; 

也。

class stock 
{ 
public: 
stock(char const * const symbol, char const * const name, int sharePrice, date priceDate); 
// sharePrice is given as a number of CENTS 
stock(const stock& s);      // copy constructor 
stock(void);        // default constructor 
char const * const getSymbol(void) const; 
stock& operator=(const stock& s); 
stock& operator=(stock const * const s); 
~stock(void); 

// display column headers 
static void displayHeaders(ostream& out); // display the headers when this instance is printed 

// prints share price as DOLLARS 
// (e.g. 2483 would print out as 24.83 and 200 would print out as 2.00) 
friend ostream& operator<<(ostream& out, const stock& s); 

friend class hashmap; 


    private: 

int sharePrice; 
char* name; 
char* symbol; 
date priceDate; 
int removed; 


    }; 

現在的功能。

date::date(Month month, int day, int year) 
{ 

this->day = day; 
this->month = month; 
this->year = year; 



    } 

/** 
* date: copy constructor 
* in: date 
* out: out 
* return: none 
**/ 

date::date(const date& date):day(date.day),month(date.month),year(date.year) 
{ 

    } 

    date::date() 
    { 

day = 0; 
year = 0; 
month = INVALID; 


    } 

    date::~date(void) 
{ 



    } 

    ostream& operator<<(ostream& out, const date& d) 

{

switch(d.month) 
{ 

case 1: out << "January " << d.day <<", " << d.year; 
    break; 
case 2: out << "Febuary "<< d.day <<", " << d.year; 
    break; 
case 3: out << "March "<< d.day <<", " << d.year; 
    break; 
case 4: out << "April "<< d.day <<", " << d.year; 
    break; 
case 5: out << "May "<< d.day <<", " << d.year; 
    break; 
case 6: out << "June "<< d.day <<", " << d.year; 
    break; 
case 7: out << "July " << d.day <<", " << d.year; 
    break; 
case 8: out << "august "<< d.day <<", " << d.year; 
    break; 
case 9: out << "September "<< d.day <<", " << d.year; 
    break; 
case 10: out << "October "<< d.day <<", " << d.year; 
    break; 
case 11: out << "November "<< d.day <<", " << d.year; 
    break; 
case 12: out << "december "<< d.day <<", " << d.year; 
    break; 
} 

return out; 
    } 
+0

不知道你的問題是什麼。你是否想說打印d.month直接打印一個數字?如果是這樣,那麼這就是應該發生的事情,因爲月是枚舉類型。 – 2009-11-22 00:56:57

回答

0

枚舉只是數字。如果你想要一個枚舉值與字符串對應,那麼你需要創建的某些種類的映射,例如:

const char * asString(Month month) 
{ 
    static const char * cMonths[] = { "January", "February", "March", 
             "April", "May", "June", 
             "July", "August", "September", 
             "October", "November", "December" }; 
    return cMonths[static_cast<int>(month) - 1]; 
} 


我是否需要爲 私人創建的內存會員日期?

date priceDate; 

這段代碼就可以了。運行時將爲您分配堆棧中所需的內存。

4

像這樣:

const char* const MonthNames[] = { "", "January", "February", "March", "April", "May", "June" ... }; 

out << MonthNames[d.month] << ' ' << d.day << ", " << d.year; 
+0

+1。但是你可以使用「d.month - 1」,並在數組開始處刪除空字符串。 – paxdiablo 2009-11-22 01:18:37

+0

..或將第一個字符串命名爲「無效」。 – StackedCrooked 2009-11-22 02:20:14

1

使用一個陣列和一個枚舉類型。

class date { 
public: 
    enum Month { 
    JAN, FEB, MAR, APR, MAY, JUN, 
    JUL, AUG, SEP, OCT, NOV, DEC 
    }; 

    date(int y, Month m, int d) : _y(y), _m(m), _d(d) {} 

    int year() const { return _y; } 
    Month month() const { return _m; } 
    int day()  const { return _d; } 

    const std::string& month_name(Month m) const { 
    return months[m]; 
    } 

private: 
    int _y; 
    Month _m; 
    int _d; 
    static std::string months[]; 

    friend std::ostream& operator <<(std::ostream &o, const date &d); 
} 

不要忘記定義儲存date::months和流插入運算符:

std::string date::months[] = { 
    "January", "February", "March", "April", "May", "June", 
    "July", "August", "September", "October", "November", "December" 
}; 

std::ostream& operator <<(std::ostream &o, const date &d) 
{ 
    o << d.day()     << ' ' 
    << d.month_name(d.month()) << ' ' 
    << d.year(); 

    return o; 
} 

使用範例:

int main() 
{ 
    date d(2009, date::NOV, 21); 

    std::cout << d << std::endl; 

    return 0; 
}