C# 小型轮询机制的最佳算法是什么?
我正在写一个针对产品的小调查,每个用户可以对每个产品的属性(如:清洁和整洁、服务、位置、员工)进行5分(以后可能会增加)的调查。每个分数都有一个形容词(1:最差,2:差,3:好,4:非常好,5:非凡) 例如,用户可以对以下产品之一进行轮询: 干净整洁:4(很好) 服务:3(良好) 地点:1(最差) 工作人员:5人(非常) 这个分数的平均值将是乘积的分数,它将是十进制的,例如,它是3.25 现在我想通过这个结果(3.25)给乘积加一个形容词,如果它的点在半个下,比如3.25,它向下舍入(对于这个3),如果它的点等于半个上,比如3.7,它向上舍入(4) 我想知道这方面最好的算法是什么 我的班级设计如下:C# 小型轮询机制的最佳算法是什么?,c#,algorithm,C#,Algorithm,我正在写一个针对产品的小调查,每个用户可以对每个产品的属性(如:清洁和整洁、服务、位置、员工)进行5分(以后可能会增加)的调查。每个分数都有一个形容词(1:最差,2:差,3:好,4:非常好,5:非凡) 例如,用户可以对以下产品之一进行轮询: 干净整洁:4(很好) 服务:3(良好) 地点:1(最差) 工作人员:5人(非常) 这个分数的平均值将是乘积的分数,它将是十进制的,例如,它是3.25 现在我想通过这个结果(3.25)给乘积加一个形容词,如果它的点在半个下,比如3.25,它向下舍入(对于这个3
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;}
}
公共类产品
{}
公众阶级投票
{
公共int Id{get;set;}
public int ProductId{get;set;}
公共产品产品{get;set;}
公共十进制分数{get;set}
公共字符串形容词{get;set;}
公共ICollection属性{get;set;}
}
公共类属性//用于类似服务的属性
{
公共int Id{get;set;}
公共字符串标题{get;set;}
公共ICollection属性{get;set;}
}
公共类属性
{
公共十进制分数{get;set;}
公共int AttributeId{get;set;}
公共属性{get;set;}
公共int PollId{get;set;}
公共投票{get;set;}
}
您可以使用Convert.ToInt32(Math.Round(score))将值四舍五入为整数值,并使用一个字典()保存属性值,这样您可以执行以下操作:
poll.attribute=lookup[Convert.toInt32(Math.Round(score))代码>求平均值很容易:只需保留投票人数以及每个参数(清洁度、服务等)的得分总和。
投票完成后,通过将每个参数的总和除以计数得到每个参数的平均值。然后将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.9999999999999999和4.0被认为是相同的,所以我们需要一个精度参数。事实上,double 4.0不能一直精确地表示为4。还要记住.NET的“奇怪”默认舍入行为:银行家舍入。它可能并不总是给你你期望的答案。因此,我强烈建议使用:Math.round(score,middpointrounding.AwayFromZero)
,因为它将为您提供预期的舍入。