zoukankan      html  css  js  c++  java
  • 与公式相关的代码片段

    前段时间,一同事写得代码惨不忍睹,核心是自定义公式,他的实现方式是“纯手工”打造...

    以下代码仅作参考:

    View Code
    class Express
    {
    DataTable TmpDt;
    public Express()
    {
    TmpDt
    =new DataTable();
    TmpDt.Columns.Add(
    "f1", typeof(double));
    TmpDt.Columns.Add(
    "f2", typeof(double));
    TmpDt.Columns.Add(
    "f3", typeof(double));

    DataRow dr
    =TmpDt.NewRow();
    dr[
    "f1"]=5;
    dr[
    "f2"]=3;
    dr[
    "f3"]=1;
    TmpDt.Rows.Add(dr);

    DataRow dr1
    =TmpDt.NewRow();
    dr1[
    "f1"] =15;
    dr1[
    "f2"] =13;
    dr1[
    "f3"] = DBNull.Value;
    TmpDt.Rows.Add(dr1);
    }

    publicstring test()
    {
    return TmpDt.Columns["f1"].Expression ="f2*f3";//将第二列与第三列 相乘
    }

    publicobject test(int i)
    {
    object objTmp=null;
    switch(i)
    {
    case1:
    objTmp
    = TmpDt.Compute("Count(f1)", "f1=5");
    break;
    case2:
    objTmp
    = TmpDt.Compute("1=2", "");
    break;
    case3:
    objTmp
    = TmpDt.Compute("5+30", "");
    break;
    case4:
    objTmp
    = TmpDt.Compute("IIF(2000>1000, null, 1)","");
    break;
    case5:
    objTmp
    = TmpDt.Compute("IsNull(null,0)", "");
    break;
    case6:
    objTmp
    = TmpDt.Compute("Trim('1235 ')", "");
    break;
    case7:
    objTmp
    = TmpDt.Compute("SubString('1235',1,2)", "");//从1开始
    break;
    case8:
    objTmp
    = TmpDt.Compute("IsNull(IIF(2000>1000, null, 1),5)", "");//允许嵌套
    break;
    default:
    break;

    }
    return objTmp;
    }

    publicdouble? test12(int i)
    {
    double? _value=null;
    var employees
    =new List<Employess>
    {
    new Employess{ Salary=123},
    new Employess{Salary=234},
    new Employess{Salary=null}
    };
    switch (i)
    {
    case1:
    _value
    = employees.Sum(c => c.Salary);
    break;
    case2:
    _value
    = employees.Average(c => c.Salary);
    break;
    case3:
    _value
    = employees.Min(c => c.Salary);
    break;
    case4:
    _value
    = employees.Max(c => c.Salary);
    break;
    case5:
    _value
    = employees.Select(c => c.Salary).Aggregate((x, y) => x * y);
    break;
    default:
    break;
    }
    return _value;
    }

    }

    publicsealedclass Employess
    {
    privatedouble? salary;

    publicdouble? Salary
    {
    get { return salary; }
    set { salary = value; }
    }


    }
    View Code
    class Program
    {
    staticvoid Main(string[] args)
    {
    DataTable dt
    =new DataTable();
    //A:
    try
    {
    Console.WriteLine(dt.Compute(Console.ReadLine(),
    ""));
    }
    catch(Exception en)
    {
    Console.WriteLine(en.Message);
    }
    //goto A;

    Express ex
    =new Express();
    //Console.WriteLine(ex.test(3));
    //ex.test();
    Console.WriteLine(ex.test12(5));

    int[] nums =newint[] { 10, 20, 30, 40, 50 };
    int sum1 = nums.Where((n, i) => i %2==0).Sum();//Where用法 10 + 30 + 50
    Console.WriteLine(sum1);

    //取款问题
    double startBalance =100.0;
    int[] test3 = { 20, 10, 40, 50, 10, 70, 30 };
    double endBalance = test3.Aggregate(startBalance,
    (balance, nextWithdrawal)
    =>
    ((nextWithdrawal
    <= balance) ? (balance - nextWithdrawal) : balance));
    Console.WriteLine(endBalance);


    //1~n放在含有n+1个元素的数组中,只有唯一的一个元素值重复,最简算法找出重复的数 o(╯□╰)o暂时不懂神马意思
    int[] array =newint[] { 8,1,1,1,5 };
    int repeatedNum1 = array.Select((i, j) => i - j).Sum();
    Console.WriteLine(repeatedNum1);
    //Select用法


    List
    <double> lst =new List<double>();
    lst.AddRange(
    newdouble[] {1.1,1.123123
    });
    Console.WriteLine(lst.Aggregate((total, next)
    => total * next));


    string[] words =newstring[] { "Able", "was", "I", "ere", "I", "saw", "Elba" };
    string normal = words.Aggregate((a, n) => a +""+ n);
    Console.WriteLine(
    "正常:"+ normal);

    //sql 中 in
    StringBuilder sb =new StringBuilder() ;
    words.Aggregate
    <string, StringBuilder>(sb, (sbobj, str) => sbobj.Append("'"+str).Append("',"));
    Console.WriteLine(sb.ToString());
    }
    }
  • 相关阅读:
    头插法链表的基本操作:创建空链表,插入结点,遍历链表,求链表长度,查找结点,删除结点
    尾插法链表拆分
    头插法链表拆分
    尾插法创建链表
    头插法创建链表
    二维数组45度反斜线扫描分析。
    [LeetCode] Binary Search Tree Iterator | 二叉查找树迭代器
    只用递归翻转栈
    [LeetCode] Wiggle Sort II | 摆动排序
    今天回归刷题的日子
  • 原文地址:https://www.cnblogs.com/yanzhiyuan928/p/2164648.html
Copyright © 2011-2022 走看看