2012-08-07 23 views
0

写排序算法在他们的应用中。修复我的(短)的Python到C#算法港口

public class Calculation 
{ 
    protected DateTime Epoch = new DateTime(1970,1,1); 

    protected int EpochSeconds(DateTime date) 
    { 
     var td = date - Epoch; 
     return td.Days*86400 + td.Seconds + ((td.Milliseconds)/1000000); 
    } 

    protected int Score(int upVotes,int downVotes) 
    { 
     return upVotes - downVotes; 
    } 

    public int HotScore(int upVotes,int downVotes,DateTime date) 
    { 
     var s = Score(upVotes, downVotes); 
     var order = Math.Log(Math.Max(Math.Abs(s), 1), 10); 
     var sign = Math.Sign(s); //Edit from Jonathon Reinhart 
     var seconds = EpochSeconds(date) - 1134028003; 
     return Math.Round(order + sign + *seconds/45000, 7); 
    } 
} 

编辑以获取更多信息

具体我就上线得到一个错误

return Math.Round(order + sign + *seconds/45000, 7); 
//error "The * or -> operator must be applied to a pointer" 

最接近的比赛,我可以在方法签名中找到是这样的: http://msdn.microsoft.com/en-us/library/f5898377

+2

究竟是什么问题? – 2012-08-07 20:53:55

+3

究竟是什么问题,或者你有什么问题? – goric 2012-08-07 20:54:11

+0

如果可能,我想检查一般错误,但我也添加了特定的构建错误。 – Wesley 2012-08-07 20:56:49

回答

2

我会提出的一个建议是使用Math.Sign(Int32)。所以:

var sign = new int(); 
if (s > 0) { sign = 1; } 
else if (s < 0) { sign = -1; } 
else { sign = 0; } 

变为:

var sign = Math.Sign(s); 

好了,你这条线有问题:

return Math.Round(order + sign + *seconds/45000, 7); 

好一个,你可能会失去因为它们都是int。首先投一个:((double)seconds/45000)

对于两个,您可能会有语法错误sign + *seconds。你的意思是+*?它认为你像指针一样使用seconds

最后,Math.Round返回double,但您的HotScore方法返回int。我猜你想要返回double

我的猜测是,这是你想要什么:

public double HotScore(int upVotes,int downVotes,DateTime date) 
{ 
    var score = Score(upVotes, downVotes); 
    var order = Math.Log(Math.Max(Math.Abs(score), 1), 10); 
    var sign = Math.Sign(score); 
    var seconds = EpochSeconds(date) - 1134028003; 
    return Math.Round(order + sign * ((double)seconds/45000), 7); 
} 

另外,我不认为你的EpochSeconds()是正确的。我适应这个从here

public long GetEpochTime(DateTime dt) 
{ 
    var ts = dt.Subtract(Convert.ToDateTime("1/1/1970 8:00:00 AM")); 

    return ((((((ts.Days * 24) + ts.Hours) * 60) + ts.Minutes) * 60) + ts.Seconds); 
} 
+0

谢谢。我可以交换代码? http://msdn.microsoft.com/en-us/library/ywb0xks3.aspx似乎表明是的。 – Wesley 2012-08-07 20:58:20

+0

是的,请注意,我链接到文档,但对于'Int32'版本,而不是'double'版本。 – 2012-08-07 21:00:28

+0

完美。减少3行代码并构建。 – Wesley 2012-08-07 21:08:26

1

你必须在最后一行语法错误 - 的+ *序列是无效的。我想最后一行是你想要的(使用this Math.Round overload):

return Math.Round(order + sign * ((double)seconds/45000), 7);