}
重构后的代码
/*
* Created bySharpDevelop.
* User: dylan ren
* Date: 2007-8-7
* Time: 13:58
*/
using System;
using System.Collections.Generic;
namespace countNoExpress
{
class express
{
private string operatorSet= ""; //运算符:+ - * /
private string[]numberSet; //参与运算的数字
privatestring expressString;
publicexpress(string e)
{
expressString=e;
}
//将exprssSting拆分为运算符和参与运算的数字
privatevoid DivideIntoOperatorAndNumber()
{
numberSet= expressString.Split(new char[] {'+', '-', '*', '/' });
for (inti = 0; i < expressString.Length; i++)
{
if (isOperator(expressString) )
operatorSet += expressString;
}
}
//判断某字符是否是+-*/符号
privatebool isOperator(char c)
{
if ((c == '+') || (c == '-') || (c == '*') ||(c == '/'))
return true;
else
return false;
}
//当执行了一次运算后,将参与运算的数字替换为结果,删除运算符
private voidrebulid(int i,string oldString,string newString)
{
expressString =expressString.Replace(oldString,newString);
numberSet= expressString.Split(new char[] { '+', '-', '*', '/' });
operatorSet=operatorSet.Remove(i,1);
}
public stringCountExpress(){
int tempResult;
string replacedStr = "";
DivideIntoOperatorAndNumber();
//先对所有的*、/进行运算
do
{
for(int i = 0; i < operatorSet.Length; i++)
{
if (operatorSet=='*')
{
tempResult = int.Parse(numberSet) *int.Parse(numberSet[i + 1]);
replacedStr =numberSet + "*" + numberSet[i + 1];
rebulid(i,replacedStr,tempResult.ToString());
break;
}
if (operatorSet=='/')
{
tempResult =int.Parse(numberSet) /int.Parse(numberSet[i+ 1]);
replacedStr =numberSet + "/" + numberSet[i + 1];
rebulid(i,replacedStr,tempResult.ToString());
break;
}
}
} while(expressString.Contains("*") || expressString.Contains("/"));
//计算所有的加减运算
do
{
for(int i = 0; i < operatorSet.Length; i++)
{
if (operatorSet=='+')
{
tempResult =int.Parse(numberSet) + int.Parse(numberSet[i + 1]);
replacedStr =numberSet + "+" + numberSet[i + 1];
rebulid(i,replacedStr,tempResult.ToString());
break;
}
if (operatorSet=='-')
{
tempResult =int.Parse(numberSet) - int.Parse(numberSet[i+ 1]);
replacedStr =numberSet + "-" + numberSet[i + 1];
rebulid(i,replacedStr,tempResult.ToString());
break;
}
}
} while(expressString.Contains("+") || expressString.Contains("-"));
returnexpressString;
}
}
class MainClass
{
public staticvoid Main(string[] args)
{
string a="3+100*2*3/2/5/2*2*3/5/4";
Console.WriteLine(a);
express exp=new express(a);
Console.WriteLine(exp.CountExpress());
Console.ReadKey();
}
}
}
一次典型的重构
背景描述:
近期要去讲一次重构,想收集一个案例,恰好从网上看到了一个朋友写的一个计算算术表达式的C#程序,原程序是计算含括号的正整数表达式的四则运算值。读后,发现问题比较多,而且逻辑有错误,因此对其进行了重构。
为简化起间,将程序的功能进行了简化:
1 计算的算术表达式只含有+、-、*、/运算
2 不含有括号
3 表达式的第一个符号是数字
4 假定表达式是合法的算术表达式且满足上边的约束条件
重构后,代码的规模与重构前差不多,但是逻辑的正确性、清晰性、易维护性提高了,内在的质量提高了。
使用的重构手法:
1 变量的重新命名
旧程序采用拼音的简写做变量名,难以猜测含义,重构后采取有意义的英文名字,而不是汉语拼音的简拼,增加了易读性。
2 抽取方法
旧程序中只采用了一个大方法,重构后拆分成了四个方法,易于理解和复用。
3 抽取类
重构后增加了一个类,将计算的方法封装在了类中。
4 优化了算法
旧程序逻辑复杂,计算*/部分和计算+-部分的思路还不一致
5 纠正了逻辑错误
旧程序隐藏了逻辑错误,当输入的表达式为3*3/3/3是错误。
6 增加了简单注释
旧程序中基本没有注释
重构前的代码:
/*
*Created by SharpDevelop.
*User: dylan ren
*Date: 2007-8-7
*/
using System;
using System.Collections.Generic;
namespace OldExpress
{
classMainClass
{
publicstatic void Main(string[] args)
{
stringa="3+100*3/2";
Console.WriteLine(a);
Console.WriteLine(countExpress(a));
Console.ReadKey();
}
publicstatic string countExpress(string fc){
boolkongzhi=true;
intxls=0;
while (kongzhi)
{
kongzhi = false;
string zfh = "";
string[] zsz = fc.Split(newchar[] { '+', '-', '*', '/' });
for (int i = 0; i< fc.Length; i++)
{
if ((fc == '+')|| (fc == '-') || (fc == '*') || (fc == '/')) zfh += fc;
}
string th = "";
for (int i = 0; i< zfh.Length; i++)
{
if (zfh == '*')// 2*3*4
{
xls = int.Parse(zsz)* int.Parse(zsz[i + 1]);
th = zsz +"*" + zsz[i + 1];
fc = fc.Replace(th,xls.ToString());
}
if (zfh == '/')
{
xls = int.Parse(zsz)/ int.Parse(zsz[i + 1]);
th = zsz +"/" + zsz[i + 1];
fc = fc.Replace(th,xls.ToString());
}
}
for (int i = 0; i< fc.Length; i++)
{
if ((fc == '*')|| (fc == '/')) kongzhi = true;
}
}
string jzfh = "";
int jxls = 0;
string[] jzsz = fc.Split(newchar[] { '+', '-', '*', '/' });
for (int i = 0; i< fc.Length; i++)
{
if ((fc == '+')|| (fc == '-')) jzfh += fc;
}
if (jzfh.Length != 0)
{
for (int i =0; i < jzfh.Length; i++)
{
if (jzfh =='+')
{
if (i == 0)
{
jxls = int.Parse(jzsz)+ int.Parse(jzsz[i + 1]);
}
else
{
jxls += int.Parse(jzsz[i+ 1]);
}
}
if (jzfh =='-')
{
if (i == 0)
{
jxls = int.Parse(jzsz)- int.Parse(jzsz[i + 1]);
}
else
{
jxls -= int.Parse(jzsz[i+ 1]);
}
}
}
return jxls.ToString();
}
else
{
return fc;
}
}
}