Пример #1
0
        public void ProcessRequest(HttpContext context)
        {
            var req  = context.Request;
            var resp = context.Response;

            var    template = Template.GetTemplate(path);
            string query    = req.Url.Query.ToString();

            if (query.Length == 0)
            {
                resp.Write("no parametrs");
                return;
            }

            string[] parameters = query.Split('/');
            if (parameters.Length < 4)
            {
                resp.Write("not enough parameters");
                return;
            }

            double a = Convert.ToInt32(parameters[1]);
            double b = Convert.ToInt32(parameters[2]);
            double c = Convert.ToInt32(parameters[3]);

            try
            {
                var    result   = QuadraticEquationSolver.FindEquationRoots(a, b, c);
                string solution = "";
                switch (result.type)
                {
                case QuadraticEquationSolver.SolutionType.ComplexRoots:
                    solution = string.Format("x<sub>1</sub> = {0}, x<sub>2</sub> = {1}", result.x1.ToString(), result.x2.ToString());
                    break;

                case QuadraticEquationSolver.SolutionType.OneRoot:
                    solution = string.Format("x<sub>1</sub> = {0}", result.x1.ToString());
                    break;

                case QuadraticEquationSolver.SolutionType.TwoRoots:
                    solution = string.Format("x<sub>1</sub> = {0}, x<sub>2</sub> = {1}", result.x1.ToString(), result.x2.ToString());
                    break;

                default:
                    solution = "no solution";
                    break;
                }
                StringBuilder sb = new StringBuilder();
                sb.AppendFormat(template, a, b, c, solution);
                resp.Write(sb);
            }
            catch (DivideByZeroException)
            {
                resp.Write("The number can't be divided by zero");
            }
        }
Пример #2
0
        public void ProcessRequest(HttpContext context)
        {
            var req  = context.Request;
            var resp = context.Response;

            string templateWithForms = Template.GetTemplate(pathToFileWithForms);

            resp.Write(templateWithForms);

            NameValueCollection parameters = req.QueryString;
            int count = parameters.AllKeys.Length;

            string templateWithResult = Template.GetTemplate(pathToFileWithResult);

            if (count == 0)
            {
                resp.Write("no parameters");
                return;
            }

            if (count < 3)
            {
                resp.Write("not enough parametrs");
                return;
            }

            double a = Convert.ToInt32(parameters[parameters.AllKeys[0]]);
            double b = Convert.ToInt32(parameters[parameters.AllKeys[1]]);
            double c = Convert.ToInt32(parameters[parameters.AllKeys[2]]);

            try
            {
                var    result   = QuadraticEquationSolver.FindEquationRoots(a, b, c);
                string solution = "";
                switch (result.type)
                {
                case QuadraticEquationSolver.SolutionType.ComplexRoots:
                    solution = string.Format("x<sub>1</sub> = {0}, x<sub>2</sub> = {1}", result.x1.ToString(), result.x2.ToString());
                    break;

                case QuadraticEquationSolver.SolutionType.OneRoot:
                    solution = string.Format("x<sub>1</sub> = {0}", result.x1.ToString());
                    break;

                case QuadraticEquationSolver.SolutionType.TwoRoots:
                    solution = string.Format("x<sub>1</sub> = {0}, x<sub>2</sub> = {1}", result.x1.ToString(), result.x2.ToString());
                    break;

                default:
                    solution = "no solution";
                    break;
                }

                StringBuilder sb = new StringBuilder();
                sb.AppendFormat(templateWithResult, a, b, c, solution);
                resp.Write(sb);
            }
            catch (DivideByZeroException)
            {
                resp.Write("The number can't be divided by zero");
            }
        }