码迷,mamicode.com
首页 > 其他好文 > 详细

to refactor for refactor

时间:2015-02-15 20:31:18      阅读:183      评论:0      收藏:0      [点我收藏+]

标签:

v1.1 if all algorithm are in one function, it will expand. so each operate type should be separated.

question: if add scientific 

operation.cpp:

#include "operation.h"

int OperationAdd(string number1, string number2){

    int result= atoi(number1.c_str())+atoi(number2.c_str());

    return result;

}

 

int OperationMinus(string number1, string number2){

    int result= atoi(number1.c_str())-atoi(number2.c_str());

    return result;

}

 

int OperationBy(string number1, string number2){

    int result= atoi(number1.c_str())*atoi(number2.c_str());

    return result;

}

 

int OperationDivide(string number1, string number2){

    int result= atoi(number1.c_str())/atoi(number2.c_str());

    return result;

}

 

int Operation(string number1, string number2, string operateType){

    int result=0;

    if(operateType=="+"){

        result = OperationAdd(number1, number2);

    }

    else if(operateType=="-"){

        result = OperationMinus(number1, number2);

    }

    else if(operateType=="*"){

        result = OperationBy(number1, number2);

    }

    else if(operateType=="/"){

        result = OperationDivide(number1, number2);

    }

    else{

        result =0;

    }

    return result;

}

 

to refactor for refactor

标签:

原文地址:http://www.cnblogs.com/dannykong/p/4293426.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!