microsoft visual c# 2015 krok po kroku - john sharp

0

rozdial drugi cw. 2
MathsOperators

dlaczego w każdym z kawałków kodu występuje int outcome = 0 skoro potem przypisujemy do outcome działanie arytmetyczne np. outcome = lhs + rhs

    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
        }

        private void calculateClick(object sender, RoutedEventArgs e)
        {
            try
            {
                if ((bool)addition.IsChecked)
                {
                    addValues();
                }
                else if ((bool)subtraction.IsChecked)
                {
                    subtractValues();
                }
                else if ((bool)multiplication.IsChecked)
                {
                    multiplyValues();
                }
                else if ((bool)division.IsChecked)
                {
                    divideValues();
                }
                else if ((bool)remainder.IsChecked)
                {
                    remainderValues();
                }
            }
            catch (Exception caught)
            {
                expression.Text = "";
                result.Text = caught.Message;
            }
        }

        private void addValues()
        {
            int lhs = int.Parse(lhsOperand.Text);
            int rhs = int.Parse(rhsOperand.Text);
            int outcome = 0;
            // TODO: Add rhs to lhs and store the result in outcome
            outcome = lhs + rhs;
            expression.Text = $"{lhsOperand.Text} + {rhsOperand.Text}";
            result.Text = outcome.ToString();
        }

        private void subtractValues()
        {
            int lhs = int.Parse(lhsOperand.Text);
            int rhs = int.Parse(rhsOperand.Text);
            int outcome = 0;
            // TODO: Subtract rhs from lhs and store the result in outcome
            outcome = lhs - rhs
            expression.Text = $"{lhsOperand.Text} - {rhsOperand.Text}";
            result.Text = outcome.ToString();
        }

        private void multiplyValues()
        {
            int lhs = int.Parse(lhsOperand.Text);
            int rhs = int.Parse(rhsOperand.Text);
            int outcome = 0;
            // TODO: Multiply lhs by rhs and store the result in outcome
            outcome = lhs * rhs;
            expression.Text = $"{lhsOperand.Text} * {rhsOperand.Text}";
            result.Text = outcome.ToString();
        }

        private void divideValues()
        {
            int lhs = int.Parse(lhsOperand.Text);
            int rhs = int.Parse(rhsOperand.Text);
            int outcome =0;
            // TODO: Divide lhs by rhs and store the result in outcome
            outcome = lhs / rhs;
            expression.Text = $"{lhsOperand.Text} / {rhsOperand.Text}";
            result.Text = outcome.ToString();
        }

        private void remainderValues()
        {
            int lhs = int.Parse(lhsOperand.Text);
            int rhs = int.Parse(rhsOperand.Text);
            int outcome = 0;
            // TODO: Work out the remainder after dividing lhs by rhs and store the result in outcome
            outcome = lhs % rhs
            expression.Text = $"{lhsOperand.Text} % {rhsOperand.Text}";
            result.Text = outcome.ToString(); 

dlaczego w każdym z kawałków kodu występuje int outcome = 0 skoro potem przypisujemy do outcome działanie arytmetyczne np. outcome = lhs + rhs

1

pomijajac, ze jest to jeden wielki copy paste (co jest zle). To warto przy definicji zainicializowac zmienna jakas domyslna wartoscia.

Akuratnie 0 w tym wypadku nie ma sensu (bo w c# zmienne tego typu beda automatycznie posiadaly 0), ale np jakby robil wartosc Double.NaN to mialoby to jakis sens.
ALE rowniez, w tym konkretnym przypadku powinno byc

int outcome = lhs % rhs;

A dlaczego to sie robi? Kod sie rozrasta, zmienia sie, ewoluuje. Warto starac sie przewidywac jakies tam drobne zmiany w przyszlosci. Inicjalizacja nic Cie nie kosztuje

0

To jest jakieś kompletne spaghetti. Wyrzuć tę książkę, zanim nauczy Cię złych wzorców.

Zarejestruj się i dołącz do największej społeczności programistów w Polsce.

Otrzymaj wsparcie, dziel się wiedzą i rozwijaj swoje umiejętności z najlepszymi.