我目前正在构建一个半复杂的计算器,它基本上是我提供的 Excel 电子表格的转换。
我已经完成了大部分工作,但是 Excel 电子表格中有一部分在 6 行和 7 列之间发生了多次计算,但问题是这些计算没有任何特定的顺序发生。
例如,Row0[Column1]
计算使用(Row2[Column4] * Row2[Column5])
和Row1[Column4]
计算使用(Row4[Column2] / Row5[Column1])
等等......你明白了。
我曾考虑过使用二维数组,但担心这些值会按特定顺序计算,因此在达到它们时没有任何价值。据我所知,将首先计算 Row1 ,然后是Row2、Row3等。
因此,如果不为我的 excel 电子表格中的每个单元格创建一个变量(并对其进行适当排序),有没有一种方法可以使用 C# 计算它?
我真的很感激任何帮助、建议、指点,无论你认为什么是可能的——我很想听听!
编辑在实现@dtb提供的 Lazy 类之后,我得到了以下代码。这是我提供的 Excel 电子表格中内容的直接副本,包括指针和计算。
var sr = new Lazy<decimal>[6, 6];
sr[0, 0] = new Lazy<decimal>(() => sr[1, 0].Value - eNumber);
sr[0, 3] = new Lazy<decimal>(() => sr[0, 4].Value - sr[1, 0].Value - sr[1, 4].Value);
sr[0, 4] = new Lazy<decimal>(() => sr[0, 0].Value * edD);
sr[0, 5] = new Lazy<decimal>(() => sr[0, 0].Value);
sr[1, 0] = new Lazy<decimal>(() => sr[1, 5].Value);
sr[1, 4] = new Lazy<decimal>(() => sr[1, 0].Value * edD);
sr[1, 5] = new Lazy<decimal>(() => sr[2, 0].Value + sr[2, 5].Value);
sr[2, 0] = new Lazy<decimal>(() => eNumber * rRate);
sr[2, 4] = new Lazy<decimal>(() => sr[2, 0].Value * hdD);
sr[2, 5] = new Lazy<decimal>(() => sr[1, 5].Value);
sr[3, 1] = new Lazy<decimal>(() => sr[2, 5].Value);
sr[4, 2] = new Lazy<decimal>(() => eNumber * (ePc / 100) + sr[2, 0].Value * (hlPc / 100) - sr[3, 1].Value);
sr[5, 0] = new Lazy<decimal>(() => (sr[0, 0].Value + sr[1, 0].Value + sr[2, 0].Value) / ePerR);
sr[5, 2] = new Lazy<decimal>(() => sr[5, 0].Value / rLifecycle);
sr[5, 4] = new Lazy<decimal>(() => sr[5, 2].Value);
sr[5, 5] = new Lazy<decimal>(() => sr[5, 0].Value + sr[5, 2].Value - sr[5, 4].Value);
但是我收到以下错误
ValueFactory attempted to access the Value property of this instance.
谷歌搜索错误返回了一堆垃圾搜索类型的网站。
马尔科