Принцип объектно-ориентированного программирования при написании калькулятора - C#

Узнай цену своей работы

Формулировка задачи:

Помогите пожалуйста сделать такой же калькулятор, только в 2 или 3 классах, так чтобы не нарушать принцип объектно-ориентированного программирования
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace ConsoleApplication8
{
    class Program
    {
 
        static double a, b, c, D;
        static double x1, x2;
 
        static void Main(string[] args)
        {

            while (true)
            {
                string command;
        
                Console.Write("Введите параметр или нажмите x, чтобы выйти:");
                command = Console.ReadLine();
                if (command == "x") break;
                if (command == "a")
                {
                    Console.Write("Введите a: ");
                    a = Double.Parse(Console.ReadLine());
                    Reshenie();
                    Console.WriteLine("D=" + D.ToString());
                    Console.WriteLine("x1=" + x1.ToString());
                    Console.WriteLine("x2=" + x2.ToString());
 
                    continue;
                   
                }
                if (command == "b")
                {
                    Console.WriteLine("Введите b: ");
                    b = Double.Parse(Console.ReadLine());
                    Reshenie();
                    Console.WriteLine("D=" + D.ToString());
                    Console.WriteLine("x1=" + x1.ToString());
                    Console.WriteLine("x2=" + x2.ToString());
 
                    continue;
                    
                }
                if (command == "c")
                {
                    Console.WriteLine("Введите c: ");
                   c = Double.Parse(Console.ReadLine());
                   Reshenie();
                   Console.WriteLine("D=" + D.ToString());
                   Console.WriteLine("x1=" + x1.ToString());
                   Console.WriteLine("x2=" + x2.ToString());
 
                    continue;
                   
                }
                
            }
        }
 
        static void Reshenie()
        {
            D = b*b - 4 * a * c;
            if (D > 0 || D == 0)
            {
                x1 = (-b + Math.Sqrt(D)) / (2 * a);
                x2 = (-b - Math.Sqrt(D)) / (2 * a);

            }

            else
            {
                Console.WriteLine("Дискриминант d < 0 ---> Решение квадратного уравнения невозможно.");
                x1 = 0;
                x2 = 0;
 
            }
        }
    }
}

Решение задачи: «Принцип объектно-ориентированного программирования при написании калькулятора»

textual
Листинг программы
public enum CommandTypes
    {
        Quad = 1
    }
 
    public interface IInputService
    {
        CommandTypes ReadCommand(string input);
        Arguments ReadArguments();
    }
 
    public class Arguments
    {
        public int A { get; set; }
        public int B { get; set; }
        public int C { get; set; }
    }
 
    public class ConsoleService : IInputService
    {
        public CommandTypes ReadCommand(string command)
        {
            return (CommandTypes) Enum.Parse(typeof (CommandTypes), command);
        }
 
        public Arguments ReadArguments()
        {
            Console.Write("Enter arguments A ");
            int a = int.Parse(Console.ReadLine());
            Console.Write("Enter arguments B ");
            int b = int.Parse(Console.ReadLine());
            Console.Write("Enter arguments C ");
            int c = int.Parse(Console.ReadLine());
            return (new Arguments() {A = a, B = b, C = c});
        }
    }
 
    public class Result
    {
        public Result(string name, double value)
        {
            Name = name;
            Value = value;
        }
        public string Name { get; set; }
        public double Value { get; set; }
    }
 
    public interface ICalculator
    {
        List<Result> Execute(CommandTypes command, Arguments args);
    }
 
    public class Calculator : ICalculator
    {
        public List<Result> Execute(CommandTypes command, Arguments args)
        {
            switch (command)
            {
                case CommandTypes.Quad:
                    return Quad(args);
                default:
                    throw new InvalidOperationException();
            }
        }
 
        private List<Result> Quad(Arguments a)
        {
            List<Result> r = new List<Result>();
            double def = a.B*a.B - 4*a.A*a.C;
            if (def >= 0)
            {
                r.Add(new Result("D", def));
                double x1 = (-a.B + Math.Sqrt(def))/(2*a.A);
                r.Add(new Result("x1", x1));
                double x2 = (-a.B - Math.Sqrt(def))/(2*a.A);
                r.Add(new Result("x2", x2));
            }
            else
            {
                Console.WriteLine("Arguments are invalid");
            }
            return r;
        }
    }
 
    internal class Program
    {
        private static void Main()
        {
            while (true)
            {
                try
                {
                    Console.Write("Enter command: ");
                    IInputService cs = new ConsoleService();
                    CommandTypes command = cs.ReadCommand(Console.ReadLine());
                    Arguments arguments = cs.ReadArguments();
                    ICalculator c = new Calculator();
                    var result = c.Execute(command, arguments);
                    foreach (var r in result)
                    {
                        Console.WriteLine("{0}: {1}", r.Name, r.Value);
                    }
                }
                catch
                {
                    Console.WriteLine("Argument input error");
                }
            }
        }
    }

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

8   голосов , оценка 4.125 из 5
Похожие ответы