我正在创建一个程序,允许用户使用 4 个基本操作定义公式:使用 XML 的加法、减法、除法、乘法。让我们举个例子:用户想要定义公式,如(a + b) x (c + d)
. xml的格式如下:
编辑我已经实现了这个
编辑解决。非常感谢Yaniv的建议。我的解决方案如下:
<xPlugins>
<xPlugin>
<Multiple>
<Add>
<Operator>
<value>1</value>
</Operator>
<Operator>
<value>2</value>
</Operator>
</Add>
<Add>
<Operator>
<value>3</value>
</Operator>
<Operator>
<value>4</value>
</Operator>
</Add>
</Multiple>
</xPlugin>
</xPlugins>
班级
//root element
public class xPlugins
{
[XmlElement("xPlugin", typeof(xPlugin))]
public xPlugin[] Plugin { get; set; }
}
public class xPlugin
{
[XmlElement("Multiple", typeof(Multiple))]
[XmlElement("Add", typeof(Add))]
[XmlElement("Subtract", typeof(Divide))]
[XmlElement("Divide", typeof(Divide))]
[XmlElement("Operator", typeof(Operand))]
public Calculator calculator { get; set; }
}
//Deseirialize ultility
static class readXML
{
public static void getObject(ref xPlugins plugins)
{
try
{
List<Type> type = new List<Type>();
type.Add(typeof(Add));
type.Add(typeof(Minus));
type.Add(typeof(Multiple));
type.Add(typeof(Subtract));
type.Add(typeof(Operator));
XmlSerializer xml = new XmlSerializer(typeof(xPlugin), type.ToArray());
FileStream fs = new FileStream("test.xml", FileMode.Open);
plugins = (xPlugins)xml.Deserialize(fs);
}
catch (Exception ex)
{
throw;
}
}
}
public abstract class Calculator
{
[XmlElement("Multiple", typeof(Multiple))]
[XmlElement("Add", typeof(Add))]
[XmlElement("Subtract", typeof(Subtract))]
[XmlElement("Divide", typeof(Divide))]
[XmlElement("Operator", typeof(Operand))]
public List<Calculator> calculators{ get; set; }
public virtual int Calculate()
{
return 0;
}
}
public class Operator : Calculator
{
public int value { get; set; }
public Operator() { }
public override int Calculate()
{
return value;
}
}
public class Add : Calculator
{
public Add() { }
public override int Calculate()
{
List<int> value = new List<int>();
foreach (Calculator calculator in calculators)
{
value.Add(calculator.Calculate());
}
return value.Sum();
}
}
public class Minus : Calculator
{
public Minus() { }
public override int Calculate()
{
int value = calculators[0].Calculate();
for (int i = 1; i < calculators.Count; i++)
{
value -= calculators[i].Calculate();
}
return value;
}
}
public class Divide: Calculator
{
public Divide() { }
public override int Calculate()
{
int value = calculators[0].Calculate();
for (int i = 1; i < calculators.Count; i++)
{
value /= calculators[i].Calculate();
}
return value;
}
}
public class Multiple : Calculator
{
public Multiple() { }
public override int Calculate()
{
int value = calculators[0].Calculate();
for (int i = 1; i < calculators.Count; i++)
{
value *= calculators[i].Calculate();
}
return value;
}
}
//running test
private void button1_Click(object sender, EventArgs e)
{
readXML.getObject(ref this.plugins);
foreach (Calculator plugin in plugins.calculators)
{
plugin.Calculate();
}
}
我只需Calculator
要用以下方式装饰财产:
[XmlElement("Multiple", typeof(Multiple))]
[XmlElement("Add", typeof(Add))]
[XmlElement("Subtract", typeof(Divide))]
[XmlElement("Divide", typeof(Divide))]
[XmlElement("Operator", typeof(Operand))]