2016-07-02 113 views
1

我正在为产品写一个小型投票,每个用户可以为其每个属性(如:清洁和整洁,服务,位置,工作人员)提供5分(可能会增加)的轮询。每个分数都有一个形容词(1:最差,2:差,3:好,4:非常好,5:非凡)。小轮询机制的最佳算法是什么?

例如用户可以轮询到这样的产品中的一种: 清洁度和整洁:4(非常好) 服务:3(良好) 位置:1(最差) 人员:5(特别)

这个分数的平均值将是产品的分数,它将是十进制的,在这个例子中它是3.25。

现在,我想通过此结果(3.25)为产品提供一个形容词,如果它的点是在3.25的一半以下,则向下滚动(对于此3),并且如果它的点等于并高于一半像3.7,它轮到(4)

我想知道什么是最好的算法呢?

我班组长的设计是象下面这样:

public class Product 
{} 

public Class Poll 
{ 
    public int Id {get; set;} 
    public int ProductId {get; set;} 
    public Product Product {get; set;} 
    public decimal Score {get; set} 
    public string Adjective {get; set;} 
    public ICollection<PollAttributes> Attributes {get; set;} 

} 

public class Attribute // for the attribute like Services 
{ 
    public int Id {get; set;} 
    public string Title {get; set;} 
    public ICollection<PollAttributes> Attributes {get; set;} 
} 

public Class PollAttributes 
{ 
    public decimal score {get; set;} 

    public int AttributeId {get; set;} 
    public Attribute{get; set;} 

    public int PollId {get; set;} 
    public Poll Poll {get; set;} 
} 

回答

1

你可以使用Convert.ToInt32(Math.Round(得分)),以获得四舍五入到一个整数值的数值,并有一个Dictionary()持有属性值,例如,你可以这样做:

poll.attribute = lookup[Convert.toInt32(Math.Round(score))];

+1

还要记住.NET的“奇怪”默认舍入行为:银行家四舍五入。它可能并不总是给你你期望的答案。所以我强烈推荐使用: 'Math.round(score,MidpointRounding.AwayFromZero)' ,因为它会给你四舍五入你所期望的。 – daf

0

平均是简单:你只是不断的谁投赞成票的人的数量,和分数的总和为每个参数(洁净度,服务,... )。 投票完成后,您可以通过将该参数的总和除以计数来获得每个参数的平均值。然后,将5个平均得分相加,并将总和除以5,得到产品的总体平均值。

现在,你犯了一个字符串数组是这样的:

String[] adj = {"Worst", "Acceptable", "Good", "Very Good", "Excellent"}; 
//let "score" be the product average score 
double epsilon = 1e-8; 
double score = 3.51; 

int adj_index = (int)(score + epsilon); 
if(score - adj_index >= 0.5){//the floating part was bigger than half 
    adj_index++; 
} 
printf("Product is %s", adj[adj_index]); 

注意,小量的存在是非常必要的,因为3.99999999999999999和4.0被认为是相同的,所以我们需要一个精确的参数。事实上,双4.0不能一直表示为4。

相关问题