# 请问怎样把下面的代码精简一下

0

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;

Neo_Lc | 初学一级 | 园豆：10

1

string str=s_iCode.substr(0, 2).replacs("*","11").replace("#","10");

int value=0;

if(int.tryparse(str,out value))

m_iCode=value;

Neo_Lc | 园豆：10 (初学一级) | 2015-05-19 11:10

0

string str = s_iCode.substr(0, 2);
int m_iCode = 0;
switch(str)
{
case "0":
m_iCode = 0;
break;
.
.
.
case "#":
m_iCode = 10;
break;

}

wangYiYi | 园豆：13 (初学一级) | 2015-05-13 11:17
1

1，代码有重复：substr这个多次使用可以提取，使用一次即可

2，多次使用了if，圈复杂度过高：N多个条件的情况建议使用key-value存储，然后取值的时候直接通过key渠道对应的value，可使用数百上千甚至更多的条件判断问题的优化，否则你得要多少if

0

switch  case

您需要登录以后才能回答，未注册用户请先注册