You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

388 lines
11 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Mesnac.Maths;
using System.Collections;
namespace Mesnac.Math.Default
{
#region 位计算函数
/// <summary>
/// 位计算函数类
/// </summary>
public class Bit : IMath
{
private object NullResult = null;
public string Name
{
get { return "位计算"; }
}
public string Caption
{
get
{
StringBuilder sb = new StringBuilder();
sb.AppendLine("按位进行数据计算。返回int类型");
sb.AppendLine("例:");
sb.Append(" ").Append(Name).AppendLine("(1)");
sb.Append(" ").Append(Name).AppendLine("(1,2)");
return sb.ToString();
}
}
private int Execute(int index, int len, int[] buff)
{
if (buff.Length == 0)
{
return 0;
}
int source = (int)buff[0];
int addInt = 1;
addInt = (addInt << (index + 1)) - 1;
int result = source & addInt;
result = result >> (index + 1 - len);
return result;
}
public object Execute(string[] parameters, object[] buff)
{
if (buff.Length == 0)
{
return this.NullResult;
}
int[] _buff = new int[buff.Length];
for (int i = 0; i < buff.Length; i++)
{
object obj = buff[i];
if (obj != null)
{
int d = 0;
if (int.TryParse(obj.ToString(), out d))
{
_buff[i] = d;
}
}
}
if (parameters.Length == 1)
{
return Execute(Convert.ToInt16(parameters[0]), 1, _buff);
}
if (parameters.Length == 2)
{
return Execute(Convert.ToInt16(parameters[0]), Convert.ToInt16(parameters[1]), _buff);
}
return buff[0];
}
}
#endregion
#region 除法函数
/// <summary>
/// 除法函数类
/// </summary>
public class Div : IMath
{
private object NullResult = null;
public string Name
{
get { return "除法"; }
}
public string Caption
{
get
{
StringBuilder sb = new StringBuilder();
sb.AppendLine("计算第一个值除以固定值");
sb.AppendLine("例:");
sb.Append(" ").Append(Name).AppendLine("(100)");
return sb.ToString();
}
}
public object Execute(string[] parameters, object[] buff)
{
if (buff.Length == 0)
{
return this.NullResult;
}
if (buff[0] == null)
{
return this.NullResult;
}
if (parameters.Length > 0)
{
double d1 = 0;
double d2 = 0;
if ((double.TryParse(parameters[0], out d1))
&& (double.TryParse(buff[0].ToString(), out d2)))
{
return d2 / d1;
}
}
return buff[0];
}
}
#endregion
#region Short除法函数
/// <summary>
/// 进行Short类型转换后进行除法运算的函数类
/// </summary>
public class ShortDiv : IMath
{
private object NullResult = null;
public string Name
{
get { return "Short除法"; }
}
public string Caption
{
get
{
StringBuilder sb = new StringBuilder();
sb.AppendLine("进行Short类型转换后计算第一个值除以固定值");
sb.AppendLine("例:");
sb.Append(" ").Append(Name).AppendLine("(100)");
return sb.ToString();
}
}
public object Execute(string[] parameters, object[] buff)
{
if (buff.Length == 0)
{
return this.NullResult;
}
if (buff[0] == null)
{
return this.NullResult;
}
if (parameters.Length > 0)
{
double d1 = 0;
double d2 = 0;
if ((double.TryParse(parameters[0], out d1))
&& (double.TryParse(buff[0].ToString(), out d2)))
{
return (short)d2 / d1;
}
}
return buff[0];
}
}
#endregion
#region 时间函数(时分秒)
public class FormatTimes : IMath
{
private object NullResult = null;
#region IMath 成员
public string Name
{
get { return "时间格式化"; }
}
public string Caption
{
get
{
StringBuilder sb = new StringBuilder();
sb.AppendLine("时:分:秒 格式化");
sb.AppendLine("例:");
sb.Append(" ").Append(Name).AppendLine("(100)");
return sb.ToString();
}
}
public object Execute(string[] parameters, object[] buff)
{
if (buff.Length == 0)
{
return this.NullResult;
}
if (buff[0] == null)
{
return this.NullResult;
}
string time = string.Empty;
int h = 0;
int m = 0;
int s = 0;
int times = (int)buff[0];
if (times / 3600 != 0)
{
h = times / 3600;
time = (times / 3600).ToString().PadLeft(2, '0') + ":";
}
else
time = "00:";
if (times > 60)
{
m = (times - h * 3600) / 60;
time += ((times - h * 3600) / 60).ToString().PadLeft(2, '0') + ":";
}
else
time = "00:";
time += ((times - h * 3600) - m * 60).ToString().PadLeft(2, '0');
return time;
}
#endregion
}
#endregion
public class FormatHX : IMath
{
private object NullResult = null;
#region IMath 成员
public string Name
{
get { return "十进制除"; }
}
public string Caption
{
get
{
StringBuilder sb = new StringBuilder();
sb.AppendLine("十六进制转化十进制除法");
sb.AppendLine("例:");
sb.Append(" ").Append(Name).AppendLine("(100)");
return sb.ToString();
}
}
public object Execute(string[] parameters, object[] buff)
{
if (buff.Length == 0)
{
return this.NullResult;
}
if (buff[0] == null)
{
return this.NullResult;
}
string ox = buff[0].ToString();
uint test;
test = System.Convert.ToUInt32(ox, 16); //转换为10禁止
if (parameters.Length == 1)
{
double d1 = 0;
double d2 = 0;
if ((double.TryParse(parameters[0], out d1))
&& (double.TryParse(test.ToString(), out d2)))
{
return d2 / d1;
}
}
return test.ToString();
}
#endregion
public class BCDbit : IMath
{
#region IMath 成员
private object NullResult = null;
public string Name
{
get { return "BCD位"; }
}
public string Caption
{
get
{
StringBuilder sb = new StringBuilder();
sb.AppendLine("转化为BCD位");
sb.AppendLine("例:");
sb.Append(" ").Append(Name).AppendLine("(0)");
return sb.ToString();
}
}
public object Execute(string[] parameters, object[] buff)
{
if (buff.Length == 0)
{
return this.NullResult;
}
int[] _buff = new int[buff.Length];
for (int i = 0; i < buff.Length; i++)
{
object obj = buff[i];
if (obj != null)
{
int d = 0;
if (int.TryParse(obj.ToString(), out d))
{
_buff[i] = d;
}
}
}
if (parameters.Length == 1)
{
return Execute(Convert.ToInt16(parameters[0]), 1, _buff);
}
return buff[0];
}
object Execute(int index, int len, int[] buff)
{
string retValue = string.Empty;
if (buff[0] == 0)
return "0";
string Dec = buff[0].ToString();
string bv = string.Empty;
for (int j = 0; j < Dec.Length; j++)
{
int[] bitValue = ParseBinaryValue(Convert.ToInt32(Dec.Substring(Dec.Length - 1 - j, 1)), 4);
for (int i = 0; i < 4; i++)
{
bv += bitValue[i];
}
retValue = bv + retValue;
bv = string.Empty;
}
string temp = retValue.PadLeft(16, '0');
if (temp.Length == 16)
retValue = temp.Substring(15 - index, 1);
return retValue;
}
/// <summary>
/// 解析二进制位
/// </summary>
/// <param name="x">要解析的变量</param>
/// <param name="len">解析的长度</param>
/// <returns>返回解析的二进制值数组</returns>
int[] ParseBinaryValue(int x, int len)
{
int[] result = new int[len];
int b = 1;
for (int i = len - 1; i >= 0; i--)
{
result[i] = (x & b) == 0 ? 0 : 1;
b = b << 1;
}
return result;
}
#endregion
}
}
}