if(s_iCode.substr(0, 2) == "0" ) m_iCode = 0;
if(s_iCode.substr(0, 2) == "1" ) m_iCode = 1;
if(s_iCode.substr(0, 2) == "2" ) m_iCode = 2;
if(s_iCode.substr(0, 2) == "3" ) m_iCode = 3;
if(s_iCode.substr(0, 2) == "4" ) m_iCode = 4;
if(s_iCode.substr(0, 2) == "5" ) m_iCode = 5;
if(s_iCode.substr(0, 2) == "6" ) m_iCode = 6;
if(s_iCode.substr(0, 2) == "7" ) m_iCode = 7;
if(s_iCode.substr(0, 2) == "8" ) m_iCode = 8;
if(s_iCode.substr(0, 2) == "9" ) m_iCode = 9;
if(s_iCode.substr(0, 2) == "10") m_iCode = 10;
if(s_iCode.substr(0, 2) == "11") m_iCode = 11;
if(s_iCode.substr(0, 2) == "*" ) m_iCode = 11;
if(s_iCode.substr(0, 2) == "#" ) m_iCode = 10;
根据代码分析得出:
string str=s_iCode.substr(0, 2).replacs("*","11").replace("#","10");
int value=0;
if(int.tryparse(str,out value))
m_iCode=value;
谢谢
你可以把它换成switch。。。case语句,那样看起来代码比较简洁点。只是个建议
代码:
string str = s_iCode.substr(0, 2);
int m_iCode = 0;
switch(str)
{
case "0":
m_iCode = 0;
break;
.
.
.
case "#":
m_iCode = 10;
break;
}
代码优化考虑:
1,代码有重复:substr这个多次使用可以提取,使用一次即可
2,多次使用了if,圈复杂度过高:N多个条件的情况建议使用key-value存储,然后取值的时候直接通过key渠道对应的value,可使用数百上千甚至更多的条件判断问题的优化,否则你得要多少if
另外建议你看下《重构_改善既有代码的设计》这本书
switch case