Untitled

 avatar
user_2031329
plain_text
2 months ago
6.7 kB
3
Indexable
Never
---------controller--

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


namespace sample_project.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult About()
        {
            ViewBag.Message = "Your application description page.";

            return View();
        }

        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }
        public ActionResult Sample()
        {
            return View();
        }
        public ActionResult Angle()
        {
            return View();
        }
        public ActionResult AjaxSample()
        {
            return View();
        }
        public ActionResult granville()
        {
            var data = new List<object>();
            var num = Request["gran_number"];

            if (string.IsNullOrEmpty(num))
            {
                return Json(new { error = "Missing number parameter" }, JsonRequestBehavior.AllowGet);
            }

            int number;
            if (!int.TryParse(num, out number) || number <= 1)
            {
                return Json(new { error = "Invalid number format or not a positive integer greater than 1" }, JsonRequestBehavior.AllowGet);
            }

            int sumOfDivisors = 1;
            for (int i = 2; i <= number / 2; i++)
            {
                if (number % i == 0)
                {
                    sumOfDivisors += i;
                }
            }

            double factorial = 1;
            for (int i = 2; i <= number; i++)
            {
                factorial *= i;
            }

            string formattedFactorial = FormatLargeNumber(factorial);


            if (sumOfDivisors == number)
            {
                data.Add(new
                {
                    number = number,
                    isGranville = true,
                    factorial = formattedFactorial
                });
            }
            else
            {
                data.Add(new
                {
                    number = number,
                    isGranville = false,
                    factorial = "1"
                });
            }

            return Json(data, JsonRequestBehavior.AllowGet);
        }

        private string FormatLargeNumber(decimal number)
        {
            string formattedNumber = number.ToString("0", System.Globalization.CultureInfo.InvariantCulture);
            return formattedNumber;
        }


        public ActionResult property()
        {
            var data = new List<object>();
            var num = Request["pos_number"];

            if (string.IsNullOrEmpty(num))
            {
                return Json(new { error = "Missing number parameter" }, JsonRequestBehavior.AllowGet);
            }

            if (!int.TryParse(num, out int number) || number <= 0)
            {
                return Json(new { error = "Invalid number format or not a positive integer" }, JsonRequestBehavior.AllowGet);
            }

            int length = detLength(number);

            if (isSatisfyProperty(number))
            {
                data.Add(new
                {
                    number = number,
                    length = length,
                    satisfiesProperty = true
                });
            }
            else
            {
                data.Add(new
                {
                    number = number,
                    length = length,
                    satisfiesProperty = false
                });
            }

            return Json(data, JsonRequestBehavior.AllowGet);
        }

        private int detLength(int num)
        {
            int count = 0;
            while (num != 0)
            {
                num /= 10;
                count++;
            }
            return count;
        }

        private bool isSatisfyProperty(int num)
        {
            int originalNum = num;
            int sumOfCubes = 0;

            while (num > 0)
            {
                int digit = num % 10;
                sumOfCubes += digit * digit * digit;
                num /= 10;
            }

            return sumOfCubes == originalNum;
        }
    }
}

-------------html

@{
    ViewBag.Title = "AjaxSample";
}

<h4>Granville Numbers</h4>
<br />
Enter a Positive Number: <input type="number" id="gran_number"/>
<button id="subButton1">Submit</button>
<p id="fact">Factorial: </p>
<p id="ans_num">The Number is: </p>


<h2>Property Numbers</h2>
<br />
Enter a Positive Number: <input type="number" id="pos_number"/>
<button id="subButton2">Submit</button>
<p id="pro_num"></p>

<script src="~/Scripts/Jquery File/jquery-3.7.1.min.js"></script>
<script src="~/Scripts/myJquery/AjaxSamply.js"></script>

--------------js
$().ready(function () {

    $('#subButton1').click(function () {
        var gran_number = $('#gran_number').val();
        if (!gran_number || isNaN(gran_number)) {
            alert("Please enter a valid positive number.");
            return;
        }
        $.post('../Home/granville',
            {
                gran_number: gran_number,
            },
            function (data) {
                if (data.error) {
                    alert(data.error);
                } else {
                    var result = data[0].isGranville ? 'Granville' : 'Not Granville';
                    $('#ans_num').text('The Number is: ' + result);
                    $('#fact').text('Factorial: ' + data[0].factorial);
                }
            }
        );
    });
    $('#subButton2').click(function () {
        var pos_number = $('#pos_number').val();
        if (!pos_number || isNaN(pos_number) || parseInt(pos_number) <= 0) {
            alert("Please enter a valid positive number.");
            return;
        }
        $.post('../Home/property',
            {
                pos_number: pos_number,
            },
            function (data) {
                if (data.error) {
                    alert(data.error);
                } else {
                    var result = data[0].satisfiesProperty ? 'Satisfy the property' : 'Does not satisfy the property';
                    $('#pro_num').text(result);
                }
            }
        );
    });
});
Leave a Comment