ASP.NET 二次计算错误地返回 NaN

发布于 2024-09-30 12:43:18 字数 3902 浏览 2 评论 0原文

我的 ASP.NET MVC Web 应用程序应该计算二次多项式(二次)的根,但我错误地得到了 Nan 响应。我相信这是由于我的设置不当造成的,所以让我在这里发布一些代码:

视图:

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<RootFinder.Models.QuadCalc>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Polynomial Root Finder - Quadratic
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <h2>Quadratic</h2>

    <% using(Html.BeginForm("Quadratic", "Calculate")) %>
    <% { %>
    <div>
        a: <%= Html.TextBox("quadAValue", Model.quadraticAValue) %>
        <br />
        b: <%= Html.TextBox("quadBValue", Model.quadraticBValue) %>
        <br />
        c: <%= Html.TextBox("quadCValue", Model.quadraticCValue) %>
        <br />
        <input type="submit" id="quadraticSubmitButton" value="Calculate!" />
        <br />
        <br />
        <strong>Root 1:</strong>
        <p><%= Model.x1 %></p>
        <br />
        <strong>Root 2:</strong>
        <p><%= Model.x2 %></p>
    </div>
    <% } %>
</asp:Content>

控制器(处理二次计算等计算请求的所有控制):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using RootFinder.Models;

namespace RootFinder.Controllers
{
    public class CalculateController : Controller
    {
        //
        // GET: /Calculate/

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

        [HttpGet]
        public ViewResult Quadratic()
        {
            return View(new QuadCalc());
        }

        [HttpPost]
        public ViewResult Quadratic(QuadCalc newQuadCalc)
        {
            newQuadCalc.QuadCalculate();
            return View(newQuadCalc);
        }

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

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

模型(执行计算):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace RootFinder.Models
{
    public class QuadCalc
    {
        public double quadraticAValue { get; set; }
        public double quadraticBValue { get; set; }
        public double quadraticCValue { get; set; }
        public double x1 { get; set; }
        public double x2 { get; set; }

        public void QuadCalculate()
        {
            //Quadratic Formula: x = (-b +- sqrt(b^2 - 4ac)) / 2a

            //Calculate discriminant
            double insideSquareRoot = (quadraticBValue * quadraticBValue) - 4 * quadraticAValue * quadraticCValue;

            if (insideSquareRoot < 0)
            {
                //No real solution
                x1 = double.NaN;
                x2 = double.NaN;
            }
            else if (insideSquareRoot == 0)
            {
                //One Solution
                double sqrtOneSolution = Math.Sqrt(insideSquareRoot);
                x1 = (-quadraticBValue + sqrtOneSolution) / (2 * quadraticAValue);
                x2 = double.NaN;
            }
            else if (insideSquareRoot > 0)
            {
                //Two Solutions
                double sqrtTwoSolutions = Math.Sqrt(insideSquareRoot);
                x1 = (-quadraticBValue + sqrtTwoSolutions) / (2 * quadraticAValue);
                x2 = (-quadraticBValue - sqrtTwoSolutions) / (2 * quadraticAValue);
            }
        }
    }
}

所以,让我们在视图中说,最终用户输入 a=1b=0c=0,我的应用程序将输出 Nan对于 Root1 和 Root2。我认为这可能是由于我没有将最终用户输入正确连接到 newQuadCalc 对象的实例变量中......

My ASP.NET MVC web application is supposed to calculate the roots of a second-degree polynomial (quadratic) but I'm erroneously getting Nan responses. I believe this is due to my improper setup, so let me post some of my code here:

View:

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<RootFinder.Models.QuadCalc>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Polynomial Root Finder - Quadratic
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <h2>Quadratic</h2>

    <% using(Html.BeginForm("Quadratic", "Calculate")) %>
    <% { %>
    <div>
        a: <%= Html.TextBox("quadAValue", Model.quadraticAValue) %>
        <br />
        b: <%= Html.TextBox("quadBValue", Model.quadraticBValue) %>
        <br />
        c: <%= Html.TextBox("quadCValue", Model.quadraticCValue) %>
        <br />
        <input type="submit" id="quadraticSubmitButton" value="Calculate!" />
        <br />
        <br />
        <strong>Root 1:</strong>
        <p><%= Model.x1 %></p>
        <br />
        <strong>Root 2:</strong>
        <p><%= Model.x2 %></p>
    </div>
    <% } %>
</asp:Content>

Controller (handles all controlling of requests for calculations like the quadratic):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using RootFinder.Models;

namespace RootFinder.Controllers
{
    public class CalculateController : Controller
    {
        //
        // GET: /Calculate/

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

        [HttpGet]
        public ViewResult Quadratic()
        {
            return View(new QuadCalc());
        }

        [HttpPost]
        public ViewResult Quadratic(QuadCalc newQuadCalc)
        {
            newQuadCalc.QuadCalculate();
            return View(newQuadCalc);
        }

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

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

Model (performing calculation):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace RootFinder.Models
{
    public class QuadCalc
    {
        public double quadraticAValue { get; set; }
        public double quadraticBValue { get; set; }
        public double quadraticCValue { get; set; }
        public double x1 { get; set; }
        public double x2 { get; set; }

        public void QuadCalculate()
        {
            //Quadratic Formula: x = (-b +- sqrt(b^2 - 4ac)) / 2a

            //Calculate discriminant
            double insideSquareRoot = (quadraticBValue * quadraticBValue) - 4 * quadraticAValue * quadraticCValue;

            if (insideSquareRoot < 0)
            {
                //No real solution
                x1 = double.NaN;
                x2 = double.NaN;
            }
            else if (insideSquareRoot == 0)
            {
                //One Solution
                double sqrtOneSolution = Math.Sqrt(insideSquareRoot);
                x1 = (-quadraticBValue + sqrtOneSolution) / (2 * quadraticAValue);
                x2 = double.NaN;
            }
            else if (insideSquareRoot > 0)
            {
                //Two Solutions
                double sqrtTwoSolutions = Math.Sqrt(insideSquareRoot);
                x1 = (-quadraticBValue + sqrtTwoSolutions) / (2 * quadraticAValue);
                x2 = (-quadraticBValue - sqrtTwoSolutions) / (2 * quadraticAValue);
            }
        }
    }
}

So, let's say in the View, the end-user enters a=1, b=0 and c=0, my application will output Nan for both Root1 and Root2. I think this may be due to the fact that I have not properly connected my end-user input into the instance variables of my newQuadCalc object...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

话少情深 2024-10-07 12:43:18

您的代码失败,因为在网站上输入的值不会传播到您的计算逻辑。发生这种情况是因为文本框名称与模型属性名称不匹配导致模型绑定失败。你的观点应该是这样的:

<% using (Html.BeginForm("Quadratic", "Calculate")) %>
<% { %>
<div>
    a: <%= Html.TextBoxFor(m=> m.quadraticAValue) %>
    <br />
    b: <%= Html.TextBoxFor(m => m.quadraticBValue) %>
    <br />
    c: <%= Html.TextBoxFor(m => m.quadraticCValue)%>
    <br />
    <input type="submit" id="quadraticSubmitButton" value="Calculate!" />
    <br />
    <strong>Root 1:</strong>
    <p><%= Html.DisplayFor(m => m.x1) %></p>
    <br />
    <strong>Root 2:</strong>
    <p><%= Html.DisplayFor(m => m.x2)%></p>
</div>
<% } %>

Your code is failing because the values that get entered on the website are not propagated to your calculation logic. This is happening because model binding is failing due to a mismatch between the textbox names and the names of your model's properties. Your view should instead be something like this:

<% using (Html.BeginForm("Quadratic", "Calculate")) %>
<% { %>
<div>
    a: <%= Html.TextBoxFor(m=> m.quadraticAValue) %>
    <br />
    b: <%= Html.TextBoxFor(m => m.quadraticBValue) %>
    <br />
    c: <%= Html.TextBoxFor(m => m.quadraticCValue)%>
    <br />
    <input type="submit" id="quadraticSubmitButton" value="Calculate!" />
    <br />
    <strong>Root 1:</strong>
    <p><%= Html.DisplayFor(m => m.x1) %></p>
    <br />
    <strong>Root 2:</strong>
    <p><%= Html.DisplayFor(m => m.x2)%></p>
</div>
<% } %>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文