且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何根据数量获得价格?

更新时间:2023-11-29 21:44:28

int qty=5;//YOUR VALUE
stirng[] data=str.Split(' ');
if(data.length>0)
{
decimal price=data[0];
for(int i=1;i<data.length;i+2)
{
if(qty<=data[i])
{
price=convert.toDecimal(data[i-1])*qty;
}
else
{
break;
}
}
}







i dint测试,希望它会工作




i dint tested,hope it will work


(对不起,我的 C#有点生锈)

(Sorry, my C# is a bit rusty)
public class Pricer
{
  Dictionary<double, double> price;

  public Pricer(string s)
  {
    string[] a = s.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
    price = new Dictionary<double, double>();
    CultureInfo INVC = CultureInfo.InvariantCulture;

    for (int n = 0; n < a.Length - 1; n += 2)
      price.Add(double.Parse(a[n+1], INVC), double.Parse(a[n],INVC));
    price.Add(double.PositiveInfinity, double.Parse(a[a.Length - 1], INVC));
  }

  public double getPrice(double quantity)
  {
    double result = 0.0;
    foreach (double d in price.Keys)
      if (quantity < d)
      {
        result = price[d]*quantity;
        break;
      }
    return result;
  }
}





然后



then

static void Main(string[] args)
{
  string str = " 28.00 3.00 71.40 6.00 134.40 ";

  Pricer pr = new Pricer(str);
  Console.WriteLine("Quantity {0} price {1}", 0.5, pr.getPrice(0.5));
  Console.WriteLine("Quantity {0} price {1}", 5.5, pr.getPrice(5.5));
  Console.WriteLine("Quantity {0} price {1}", 1000, pr.getPrice(1000));
}


试试这个
string str=" 28.00 3.00 71.40 6.00 134.40 ";
string[] s=str.split(' '); double price;double unitprice;
if(Quantity==1 || Quatity==2)
{
if(Quantity==2)
price=convert.todouble(s[0]*2);
else
price=s[0];
}
elseif(Quantity==3 || Quatity==4 || Quantity==5)
{
 unitprice=convert.todouble(s[2]/3);
if(Quantity==4)
price=unitprice*4;
elseif(Quantity==5)
price=unitprice*5;
else
price=convert.todouble(s[2]);
}
else
{
 unitprice=convert.todouble(s[4]/6);
if(Quantity>6)
price=unitprice*Quantity;

else

{
price=convert.todouble(s[4]);
}