A
A
Alexander2015-12-08 14:26:20
C++ / C#
Alexander, 2015-12-08 14:26:20

What changes can be made for the better, and why?

Hi all.
I don't know any programmers. So there is no one to ask. I'm learning C# from "G. Schildt C# 4.0 Complete Guide" - 241 pages so far.
Wrote a small console calculator. Everything seems to work and does not fall. The question is actually in the title.
I will accept any constructive criticism.

using System;

class Program
{
    static void Main(string[] args)
    {
        Calculator calculator = new Calculator();
        calculator.Header();
        calculator.Instruction();
        calculator.Start();
    }
}

public class Calculator
{
    string number;
    char znak;
    double[] Numbers = {0, 0};

    public void Start()
    {
        InputNumbers();
        InputAction();
        DataProcessing();
    }

    public void Header()
    {
        Console.WriteLine("Программа \"Простой калькулятор\"");
    }

    public void Instruction()
    {
        Console.WriteLine("\nДля дроби используй запятую - ','");
        Console.WriteLine("+ - для сложения чисел");
        Console.WriteLine("- - для вычитания чисел");
        Console.WriteLine("* - для умножения чисел");
        Console.WriteLine("/ - для деления чисел");
        Console.WriteLine("q - для выхода из программы\n");
    }

    void InputNumbers()
    {
        for (int count = 0; count < Numbers.Length; count++)
        {
            Console.Write("Введите {0} число: ", count + 1);
            number = Console.ReadLine();

            if (number == "q")
            {
                Environment.Exit(0);
            }
            else
            {
                try
                {
                    Numbers[count] = Convert.ToDouble(number);
                }
                catch
                {
                    Console.WriteLine("{0}-число не может принять значение \"{1}\". Внимательно читайте интрукцию.", count + 1, number);
                    Instruction();
                    Start();
                }
            }
        }        
    }

    void InputAction()
    {
        Console.Write("Введите действие: ");
        number = Console.ReadLine();

        if (number == "q")
        {
            Environment.Exit(0);
        }
        else
        {
            try
            {
                znak = Convert.ToChar(number);
            }
            catch
            {
                Instruction();
                InputAction();
            }
        }

        if(Numbers[1] == 0 & znak == '/')
        {
            Console.WriteLine("\n<<<<<<      Деление на нуль невозможно      >>>>>>\n");
            Start();
        }
    }

    void DataProcessing()
    {
        switch (znak)
        {
            case '+': Console.WriteLine("\n<<<<<<      {0} + {1} = {2}      >>>>>>\n", Numbers[0], Numbers[1], Numbers[0] + Numbers[1]); break;
            case '-': Console.WriteLine("\n<<<<<<      {0} - {1} = {2}      >>>>>>\n", Numbers[0], Numbers[1], Numbers[0] - Numbers[1]); break;
            case '*': Console.WriteLine("\n<<<<<<      {0} * {1} = {2}      >>>>>>\n", Numbers[0], Numbers[1], Numbers[0] * Numbers[1]); break;
            case '/': Console.WriteLine("\n<<<<<<      {0} / {1} = {2}      >>>>>>\n", Numbers[0], Numbers[1], Numbers[0] / Numbers[1]); break;
        }
        Start();
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Kovalsky, 2015-12-08
@dmitryKovalskiy

I read the code superficially and I will not give advice on optimization, but I will say that your code smells strongly of procedural programming. I'm talking about something else here. Have you ever used a calculator? real or screw or some other. No one asks for a number, no one asks for actions. A person presses a sequence of buttons based on which the action is decided. Read not a line, but each pressed button. If it's a digit - update the digit, if it's a sign - put down the action, if enter - display the result. Think not about how easy it is for you to write calculations, but about how a real calculator works.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question