w********p 发帖数: 948 | 1 evaluator (String expression)
1。将expression parse 成三块 expr1 operator expr2
2 。如果expr1, expr2 都是数字,return 计算结果。 比如6*6
3。 不然,如果operator 是乘除的话,parse 来string2里的第一个数字,得到结果
4. 再不然,recursively call for rest expression.
有两个links很好和大家分析。
http://www.strchr.com/expression_evaluator
http://compsci.ca/v3/viewtopic.php?t=21703
把网上的code帖出来,给爱偷懒的同伙。我还没仔细看。
无意中运行了下面的code,并不能handle所有的cases 。个人还是喜欢stack的版本。
不会没关系,学学就会了吗。呵呵, 会了不用还是会忘嘛。
本科compiler课是要用java写一个compiler出来的。还有微积分,还给老师的知识还少
嘛?。。。
/*
* The "ExpressionEv... 阅读全帖 |
|
s******n 发帖数: 3946 | 2 优化一下,避免反复做乘法:
class PowerImpl implements Power {
List values;
public PowerImpl() {
//初始长度为3, 0和1闲置不用为了方便, values[2]对应到2^2
values = new ArrayList(3);
values[0]=dum; values[1]=dum;
values[2]=4;
}
public void reset() {
values.setSize(3);
values[2] = 4;
}
public int next() {
int nextValue=Integer.Max;
int nextIndex=-1;
for (int i=2; i
int t = values[i];
if (t < nextValue) {
nextValue = t;
nextIndex = i;
... 阅读全帖 |
|
s******s 发帖数: 508 | 3 select
CusipCol
, case when ValueCol is null and nextValue is not null then nextID else
IDCol end IDCol
, case when ValueCol is null and nextValue is not null then nextValue else
ValueCol end ValueCol
from
(
select CusipCol, IDCol, ValueCol
,lead(ValueCol) over (partition by CusipCol order by IDCol) NextValue
,lead(ValueCol) over (partition by CusipCol order by IDCol) NextID
,row_number() over (partition by CusipCol order by IDCol) rownum
from mytable
) x
where rownum = |
|
g**u 发帖数: 504 | 4 这个是不是更好理解一点
void myrotate(int* a, int N, int k)
{
if (N<=1) return;
if (k>N) k=k%N;
if (k==0) return;
int currentIndex,nextIndex;
int currentValue,nextValue;
currentIndex = 0;
currentValue=a[currentIndex];
for (int i=0; i
nextIndex=(currentIndex+N-k)%N;
nextValue=a[nextIndex];
a[nextIndex]=currentValue;
currentIndex=nextIndex;
currentValue=nextValue;
}
} |
|
g**u 发帖数: 504 | 5 Yes, got it.
Here is the revised one, it works. Now it's almost same as swan's.
void myrotate(int* a, int N, int k)
{
if (N<=1) return;
if (k>N) k=k%N;
if (k==0) return;
int currentIndex,nextIndex;
int currentValue,nextValue;
int numMove=0;
int startIndex;
for (int i=0; numMove
startIndex=i;
currentIndex = i;
currentValue=a[currentIndex];
while(1){
numMove++;
nextIndex=(currentIndex+N-k)%N;
... 阅读全帖 |
|
c**t 发帖数: 2744 | 6 a typo: the 2nd lead should be lead(IDCol)
NextValue |
|
b******y 发帖数: 139 | 7 Yes, this works well.
Thumb up!
NextValue |
|