24h購物| | PChome| 登入
2012-06-22 15:37:56| 人氣610| 回應0 | 上一篇 | 下一篇

[C++][OOP] Lab6 仿作練習

推薦 0 收藏 0 轉貼0 訂閱站台

C++ 沒有 trim(), Exception class, string.toLowerCase(), 因此必須自己來 !
下面代碼讓我學習到了 例外處理 在 C++ 上的使用 !




#include <iostream>

#include "Calculator.h"
using namespace std;

int main()
{
    Calculator M;
    M.run();
    return 0;
}

#ifndef CALCULATOR_H
#define CALCULATOR_H

#include <iostream>
#include "Exception.h"
using namespace std;

class Calculator
{
    public:
        Calculator();
        void run();
        char getOperator();
        double getNumber()throw(Exception);
        bool checkAgain();
        virtual ~Calculator();
    protected:
    private:
        string cmd;
};

#endif // CALCULATOR_H

#include "Calculator.h"
#include "UNknownException.h"
#include <iostream>
#include <math.h>
#include <sstream>
using namespace std;
Calculator::Calculator()
{
    //ctor
}

Calculator::~Calculator()
{
    //dtor
}
void Calculator::run() {
    cout << "Calculator is on" << endl;
    do {
        char op;
        int flag = 0;
        double result, number;
        result = 0.0;
        cout << "result = " << result << endl;
        while(true) {
            getline(cin, cmd);
            if(cmd == "r")
                break;
            try {
                op = getOperator();
                number = getNumber();
                switch(op) {
                    case '+':result += number;break;
                    case '-':result -= number;break;
                    case '*':result *= number;break;
                    case '/':
                        if(fabs(result) <= 1E-5)
                            throw 0;
                        result /= number;
                        break;
                    default:
                        throw UnKnownException(op);
                }
                if(flag == 0)
                    cout << "new result = " << result << endl;
                else
                    cout << "update result = " << result << endl;
                flag = 1;
            } catch(UnKnownException e) {
                cout << e._op << " is an unknown operation.\n";
                cout << "Reenter, your last line:\n";
            } catch(Exception e) {
                cout << "Format Error: " + e.getMessage() + "\n";
                cout << "Reenter, your last line:\n";
            }
        }
        cout << "Final result = " << result << endl;
    } while(checkAgain());
    cout << "End of Program" << endl;
}
char Calculator::getOperator() {
    char op = cmd[0];
    cmd = cmd.substr(1, cmd.length()-1);
    return op;
}
double parseDouble(string str) throw(Exception) {
    stringstream sin(str);
    double var;
    if(sin >> var)
        return var;
    else
        throw Exception("parseDouble Error");
}
double Calculator::getNumber() throw(Exception) {
    return parseDouble(cmd);
}
bool Calculator::checkAgain() {
    while(true) {
        cout << "Again? (y/n)" << endl;
        getline(cin, cmd);
        if(cmd == "y" || cmd == "yes")
            return true;
        if(cmd == "n" || cmd == "no")
            return false;
        cout << "You should make a decision" << endl;
    }
}

#ifndef UNKNOWNEXCEPTION_H
#define UNKNOWNEXCEPTION_H
#include "Exception.h"

class UnKnownException : public Exception
{
    public:
        UnKnownException();
        UnKnownException(char op);
        virtual ~UnKnownException();
        char _op;
    protected:
    private:

};

#endif // UNKNOWNEXCEPTION_H

#include "UnKnownException.h"

UnKnownException::UnKnownException()
{
    //ctor
}

UnKnownException::~UnKnownException()
{
    //dtor
}

UnKnownException::UnKnownException(char op) {
    _op = op;
}

#ifndef EXCEPTION_H
#define EXCEPTION_H
#include <iostream>
using namespace std;
class Exception
{
    public:
        Exception();
        Exception(string msg);
        string getMessage();
        virtual ~Exception();
    protected:
        string _msg;
};

#endif // EXCEPTION_H

#include "Exception.h"

Exception::Exception()
{
    //ctor
    _msg = "";
}

Exception::~Exception()
{
    //dtor
}
Exception::Exception(string msg) {
    _msg = msg;
}
string Exception::getMessage() {
    return _msg;
}

台長: Morris
人氣(610) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: [學習]Java |
此分類下一篇:[JAVA][Eclipse] Tetris 俄羅斯方塊
此分類上一篇:[C++][OOP] Lab4 仿作練習

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文