Re: C++的程式..這樣應該不對 但是該如何改呢?

看板Programming作者 (艾爾墨)時間17年前 (2008/09/05 19:19), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串2/2 (看更多)
※ 引述《quota@kkcity.com.tw (大改名片檔)》之銘言: : 我知道這個程式觀念上有問題 : 雖然可以執行...但這完全不符合物件導向的精神 : 請問..可否給我一個方向 : 該如何改呢? 改看看: #include <iostream> using namespace std; namespace ACU { const int Continue = 1; const int Stop = 0; static class CalculationCore { private: int m_ReturnValue; public: int Count(const int StartValue, const int EndValue) { for (int i = StartValue; i <= EndValue; i++) { m_ReturnValue += i; } return m_ReturnValue; } void Clear(void) { m_ReturnValue = 0; } }; class Acumulator { private: CalculationCore m_cc; int m_StartValue; int m_EndValue; private: bool PromptNext(void) { static int nUserInput = Stop; cout << "輸入 0 離開, 1 開始計算." << endl; cin >> nUserInput; if (nUserInput == Stop) return false; else if (nUserInput == Continue) return true; else PromptNext(); } void PromptCheck(void) { cout << "輸入起始值:" << endl; cin >> m_StartValue; cout << "輸入結束值:" << endl; cin >> m_EndValue; if (m_EndValue < m_StartValue) { cout << "結束值小過起始值, 你玩我阿? 重來!" << endl; PromptCheck(); } } public: void Start(void) { bool bContinue = PromptNext(); while (bContinue) { PromptCheck(); m_cc.Clear(); int nRetVal = m_cc.Count(m_StartValue, m_EndValue); cout << "從" << m_StartValue << "到" << m_EndValue << "的累加值為" << nRetVal << endl; bContinue = PromptNext(); } } }; }; int _tmain(int argc, _TCHAR* argv[]) { ACU::Acumulator MyAcu; MyAcu.Start(); return 0; } : 附上我的語法如下: : #include <iostream> : using namespace std; : class fraction : { : private: : int numerator; : int denominator; : int i,j,end; : public: : void set_value() : { : cout<<"輸入起始值:"; : cin>>numerator; : cout<<"輸入結束值:"; : cin>>denominator; : print_value(); : } : void print_value() : { : if (denominator<numerator) : { : cout<<"結束值小於起始值,請重新輸入!"<<endl; : set_value(); : } : else : { : j=0; : for (i=numerator ; i<=denominator;) : { : j+=i; : i++; : } : cout<<"從"<<numerator<<"累加至"<<denominator; : cout<<"的總合為:"<<j<<endl; : cout<<"請輸入0來結束程式或輸入1來進行下筆計算:"; : cin>>end; : do_again(); : } : } : void do_again() : { : if (end == 0) : cout<<"感謝您使用本程式!"<<endl; : else : if (end == 1) : set_value(); : else : { : cout<<"您所輸入的數字是錯誤的,請重新輸入:"; : cin>>end; : do_again(); : } : } : }; : int main() : { : fraction x; : x.set_value(); : //有多少數量 應該是在程式流程裡面規劃 : //x.print_value(); : //x.do_again(); : return 0; : } -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 61.218.64.132 ※ 編輯: Elmerts 來自: 61.218.64.132 (09/05 19:20) ※ 編輯: Elmerts 來自: 61.218.64.132 (09/05 19:29)
文章代碼(AID): #18mHN86q (Programming)
文章代碼(AID): #18mHN86q (Programming)