gpt4 book ai didi

c# - ASP.NET MVC 中的简单图形计算器,如 MS Windows 计算器

转载 作者:太空宇宙 更新时间:2023-11-03 16:05:58 25 4
gpt4 key购买 nike

当我单击 btnOne 时,我在 ASP.NET MVC 中无法获取表达式-> btn0 -> btnPlus -> btn2 -> btn1 .我想要这个表达式:10+21然后点击 btnEqual我想得到这个表达式的总和并且在 textBox31 中只得到一个值就像在简单的 Windows 计算器中一样。如何做到这一点?

我有一个名为 Calculate 的 View :

<body>
<div id="CalculatorSpace">
<form name="Calc">
<input type="text" name="textBox" id="textBox" value=""/>
<table>
<tr>
<td><input type="button" name="btnSeven" id="btnSeven" value="7" /></td>
<td><input type="button" id="btnEight" value="8" /></td>
<td><input type="button" id="btnNine" value="9" /></td>
<td><input type="button" id="btnDelete" value="DEL" /></td>
<td><input type="button" id="btnClear" value="CLR" /></td>
</tr>
<tr>
<td><input type="button" id="btnFour" value="4" /></td>
<td><input type="button" id="btnFive" value="5" /></td>
<td><input type="button" id="btnSix" value="6" /></td>
<td><input type="button" id="btnMultiply" value="*" /></td>
<td><input type="button" id="btnDivide" value="/" /></td>
</tr>
<tr>
<td><input type="button" id="btnOne" value="1" /></td>
<td><input type="button" id="btnTwo" value="2" /></td>
<td><input type="button" id="btnThree" value="3" /></td>
<td><input type="button" id="btnPlus" value="+" /></td>
<td><input type="button" id="btnMinus" value="-" /></td>
</tr>
<tr>
<td><input type="button" id="btnZero" value="0" /></td>
<td><input type="button" id="btnDot" value="." /></td>
<td><input type="button" id="btnEqual" value="=" /></td>
</tr>
</table>
<script>
window.onload = function () {
document.getElementById('btnSeven').addEventListener('click', function () {
document.getElementById('textBox').value = document.getElementById('textBox').value + document.getElementById('btnSeven').value;
});
document.getElementById('btnEight').addEventListener('click', function () {
document.getElementById('textBox').value = document.getElementById('textBox').value + document.getElementById('btnEight').value;
});
document.getElementById('btnNine').addEventListener('click', function () {
document.getElementById('textBox').value = document.getElementById('textBox').value + document.getElementById('btnNine').value;
});
document.getElementById('btnDelete').addEventListener('click', function () {
document.getElementById('textBox').value = document.getElementById('textBox').value + document.getElementById('btnDelete').value;
});
document.getElementById('btnClear').addEventListener('click', function () {
document.getElementById('textBox').value = document.getElementById('textBox').value + document.getElementById('btnClear').value;
});
document.getElementById('btnFour').addEventListener('click', function () {
document.getElementById('textBox').value = document.getElementById('textBox').value + document.getElementById('btnFour').value;
});
document.getElementById('btnFive').addEventListener('click', function () {
document.getElementById('textBox').value = document.getElementById('textBox').value + document.getElementById('btnFive').value;
});
document.getElementById('btnSix').addEventListener('click', function () {
document.getElementById('textBox').value = document.getElementById('textBox').value + document.getElementById('btnSix').value;
});
document.getElementById('btnMultiply').addEventListener('click', function () {
document.getElementById('textBox').value = document.getElementById('textBox').value + document.getElementById('btnMultiply').value;
});
document.getElementById('btnDivide').addEventListener('click', function () {
document.getElementById('textBox').value = document.getElementById('textBox').value + document.getElementById('btnDivide').value;
});
document.getElementById('btnOne').addEventListener('click', function () {
document.getElementById('textBox').value = document.getElementById('textBox').value + document.getElementById('btnOne').value;
});
document.getElementById('btnTwo').addEventListener('click', function () {
document.getElementById('textBox').value = document.getElementById('textBox').value + document.getElementById('btnTwo').value;
});
document.getElementById('btnThree').addEventListener('click', function () {
document.getElementById('textBox').value = document.getElementById('textBox').value + document.getElementById('btnThree').value;
});
document.getElementById('btnPlus').addEventListener('click', function () {
document.getElementById('textBox').value = document.getElementById('textBox').value + document.getElementById('btnPlus').value;
});
document.getElementById('btnMinus').addEventListener('click', function () {
document.getElementById('textBox').value = document.getElementById('textBox').value + document.getElementById('btnMinus').value;
});
document.getElementById('btnZero').addEventListener('click', function () {
document.getElementById('textBox').value = document.getElementById('textBox').value + document.getElementById('btnZero').value;
});
document.getElementById('btnDot').addEventListener('click', function () {
document.getElementById('textBox').value = document.getElementById('textBox').value + document.getElementById('btnDot').value;
});
document.getElementById('btnEqual').addEventListener('click', function () {
document.getElementById('textBox').value = document.getElementById('textBox').value + document.getElementById('btnEqual').value;
});
}
</script>
</form>
</div>

我的 CalculatorController 看起来像这样:

    public class CalculatorController : Controller
{
//
// GET: /Calculator/

public ActionResult Index()
{
return View();
}

//GET: /Calculator/Calculate
public ActionResult Calculate(string value)
{

return View();
}

}

现在如何在表达式中输入x + x当你点击 btnEqual进行数据的计算(相比按+、-、*、/)?

最佳答案

进口NCalc在您的项目中,然后将 textBox 传递给 Controller ​​并返回 Expression 的结果。

public ActionResult Calculate(String textBox)
{
Expression e = new Expression(textBox);
MyViewModel model = new MyViewModel {
Answer = e.Evaluate()
};
return View(model);
}

或者类似的东西......

关于c# - ASP.NET MVC 中的简单图形计算器,如 MS Windows 计算器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19428407/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com