中綴表示式解析器的特點:
- 可自定義函式、並且函式可任意巢狀的中綴表示式轉化成宣告函式引數個數的字尾表示式
- 支援算術運算和邏輯運算
- {}用來表示優先順序,()用來標識自定義函式的引數列表
示例:
中綴表示式max(abs(random(-9,-1)),random(1,9))-3>0轉化成宣告函式引數個數的字尾表示式:-9,-1,2,random,(),1,abs,(),1,9,2,random,(),2,max,(),3,-,0,> (其中粗體數字為函式引數個數)
主要思路:
下面直接給出中綴表示式解析器的完整**,包含有完整注釋:
using system.collections;
using system.collections.generic;
public
class intoposttransfer
; private
private
static dictionary funcparamrtercountmap = new dictionary();
private
static
int customfuncindex;
public
static
string
intopost(string infixexpression)
int index = 0;
stack stack = new stack();
int lastindexofoperator = 0;
string operatorcurrent;
string operatorinstacktop = string.empty;
int lastindexofoperand = 0;
string operandcurrent = string.empty;
int lastindexofcustomfunction = 0;
string customfunction = string.empty;
int parametercount = 0;
int lastindexofstring = 0;
string stringinexpression = string.empty;
while (index < infixexpression.length || stack.count > 0)
else
if (infixexpression[index] == '
else
if (infixexpression[index] == '}')
index++;
continue;
}else
if (infixexpression[index] == '(' || infixexpression[index] == ',')
else
if (infixexpression[index] == ' ')
else
if (infixexpression[index] == ')')
else
if (infixexpression[index] == '\'')
else
ispreoperator = true;
if (stack.count == 0)
operatorinstacktop = stack.peek().tostring();
if (getoperatorpriority(operatorcurrent) > getoperatorpriority(operatorinstacktop))
else
}else
//operand
else
//custom function}}
}postfixexpression = postfixexpression.substring(0, postfixexpression.length - 1);
return postfixexpression;
}private
static
intmatchoperator(string infixexpression, int beginindex)
str = infixexpression.substring(beginindex, lastindex - beginindex + 1);
}if (lastindex == beginindex)
return lastindex;
}private
static
intgetoperand(string infixexpression, int beginindex)
break;
case
'f':
if (infixexpression.substring(beginindex, 5) == "false")
break;
default:
while ( (char.isdigit(ch) || ch == '.') && lastindex < infixexpression.length )
ch = infixexpression[lastindex];
}break;
}if (lastindex == beginindex)
return lastindex;
}private
static
intgetcustomfunction(string infixexpression, int beginindex)
ch = infixexpression[lastindex];
}return lastindex;
}private
static stack stackparam = new stack();
private
static
intgetfuncparametercount(string infixexpression, int beginindex)
while (ch != ')' )
else
if ((ch == ',' || ch.equals(','))
&& stackparam.count == 0)
ch = infixexpression[++index];
while (ch == ')' && stackparam.count != 0)
}return parametrcount;
}private
static
intgetstring(string infixexpression, int beginindex)
return lastindex;
}private
static
intgetoperatorpriority(string ope)
}}
可任意自定義的UITableViewCell
uitableview的強大更多程度上來自於可以任意自定義uitableviewcell單元格。通常,uitableview中的cell是動態的,在使用過程中,會建立乙個cell池,根據每個cell的高度 即tableview heightforrowatindexpath 返回值 以及螢幕高度計算...
可任意自定義的UITableViewCell
uitableview的強大更多程度上來自於可以任意自定義uitableviewcell單元格。通常,uitableview中的cell是動態的,在使用過程中,會建立乙個cell池,根據每個cell的高度 即tableview heightforrowatindexpath 返回值 以及螢幕高度計算...
自定義函式 巢狀
目錄 函式巢狀的優點 保證內部函式的隱私。內部函式只能被其外部函式呼叫和訪問,不會暴露到全域性變數。coding utf 8 連線資料庫優化 from sqlalchemy import create engine import configparser,os def connectdb def g...