Pierwszy program (kalkulator w C#)

Dyskusje na temat języka C# oraz biblioteki .NET
piotrek0492
Początkujący
Początkujący
Posty: 6
Dołączył(a): poniedziałek, 29 października 2012, 15:54

Pierwszy program (kalkulator w C#)

Post przez piotrek0492 »

Witam.

Stworzyłem swój pierwszy program lecz mam pewne problemy:/
Otóż nie potrafię zastosować wyjątki dzielenia przez zero:/ gdy próbuje dzielić przez zero wyskakuje mi granica(nieskończoność). Zapewne nie to powinno wyjść:/ Jak to poprawić?
Drugi mój problem to jak zrobić abym mógł wpisywać cyfry dwucyfrowe? Bo w tym momencie mogę wpisywać tylko jedności:/

Oto kod:

Code: Zaznacz cały

using System;
using System.Windows.Forms;

namespace Kalkulator
{
    public partial class Form1 : Form
    {
        public double LB1;
        public double LB2;
        public string znak;
        public double liczba;
        public double ujemna;
                   
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button19_Click(object sender, EventArgs e)
        {            
            LB1 = 0;
            LB2 = 0;      
            textBox1.Text = " ";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            textBox1.Text = button1.Text;
        }
       
        private void button2_Click(object sender, EventArgs e)
        {
            textBox1.Text = button2.Text;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            textBox1.Text = button3.Text;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            textBox1.Text = button4.Text;
        }

        private void button5_Click(object sender, EventArgs e)
        {
            textBox1.Text = button5.Text;
        }

        private void button6_Click(object sender, EventArgs e)
        {
            textBox1.Text = button6.Text;
        }

        private void button7_Click(object sender, EventArgs e)
        {
            textBox1.Text = button7.Text;
        }

        private void button8_Click(object sender, EventArgs e)
        {
            textBox1.Text = button8.Text;
        }

        private void button9_Click(object sender, EventArgs e)
        {
            textBox1.Text = button9.Text;
        }

        private void button18_Click(object sender, EventArgs e)
        {
            textBox1.Text = button18.Text;
        }

        private void button12_Click(object sender, EventArgs e)
        {            
            LB1 = System.Double.Parse(textBox1.Text);
            textBox1.Text = button12.Text;
            znak = "+";
        }

        private void button15_Click(object sender, EventArgs e)
        {
            LB1 = System.Double.Parse(textBox1.Text);
            textBox1.Text = button15.Text;
            znak = "*";
        }

        private void button14_Click(object sender, EventArgs e)
        {
            LB1 = System.Double.Parse(textBox1.Text);
            textBox1.Text = button14.Text;
            znak = "/";
        }

        private void button13_Click(object sender, EventArgs e)
        {
            LB1 = System.Double.Parse(textBox1.Text);
            textBox1.Text = button13.Text;
            znak = "-";
        }

        private void button17_Click(object sender, EventArgs e)
        {
            wynik();
        }

        public void wynik()
        {
            LB2 = System.Double.Parse(textBox1.Text);
            switch (znak)
            {
                case "+": liczba = LB1 + LB2;
                    break;
                case "-": liczba = LB1 - LB2;
                    break;
                case "*": liczba = LB1 * LB2;
                    break;                
                case "/":
                    try
                    {
                        liczba = LB1 / LB2;
                    }
                    catch (DivideByZeroException e)
                    {
                        textBox1.Text = e.Message;
                    }
                    break;
                
            }
            textBox1.Text = LB1.ToString() + znak + LB2.ToString() + " = " + liczba.ToString();
        }

        private void button10_Click(object sender, EventArgs e)
        {
            LB1 = System.Double.Parse(textBox1.Text);
            liczba = LB1 * (-1);
            textBox1.Text = liczba.ToString();
        }
    }
}

Morfidon
Administrator
Administrator
Posty: 1332
Dołączył(a): wtorek, 5 sierpnia 2008, 21:48
Contact:

Re: Pierwszy program (kalkulator w C#)

Post przez Morfidon »

Witam,

W lekcji 40 ma Pan omówione obsługiwanie wyjątków. Jest tam przykład jak złapać wyjątek dzielenia przez zero... Wystarczy jednak przed obliczeniem sprawdzić czy druga wartość jest równa 0, a jeżeli tak jest to nie wykonywać działania, tylko wyświetlić error.

Nie ma czegoś takiego jak cyfry dwucyfrowe, nie do końca rozumiem pytanie.

Pozdrawiam
piotrek0492
Początkujący
Początkujący
Posty: 6
Dołączył(a): poniedziałek, 29 października 2012, 15:54

Re: Pierwszy program (kalkulator w C#)

Post przez piotrek0492 »

Teraz jak mam ten kalkulator to mogę operować tylko na liczba do 10. Przykładowo 1+2. A chciałbym np. 12+154. A takiego równania wykonać nie mogę...
Morfidon
Administrator
Administrator
Posty: 1332
Dołączył(a): wtorek, 5 sierpnia 2008, 21:48
Contact:

Re: Pierwszy program (kalkulator w C#)

Post przez Morfidon »

Przede wszystkim Twój program się nie kompiluje. Kompilator wyświetla, że nie wie co to jest textBox1. Wklejaj pełne poprawne kody, jeśli chcesz żeby ktoś je debugował. Czemu nie możesz?
piotrek0492
Początkujący
Początkujący
Posty: 6
Dołączył(a): poniedziałek, 29 października 2012, 15:54

Re: Pierwszy program (kalkulator w C#)

Post przez piotrek0492 »

Hmm...
Myślę że chodzi o to dodatkowo o form1.Designer.cs:

Code: Zaznacz cały

namespace Kalkulator
{
    partial class Form1
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.button1 = new System.Windows.Forms.Button();
            this.button2 = new System.Windows.Forms.Button();
            this.button3 = new System.Windows.Forms.Button();
            this.button4 = new System.Windows.Forms.Button();
            this.button5 = new System.Windows.Forms.Button();
            this.button6 = new System.Windows.Forms.Button();
            this.button7 = new System.Windows.Forms.Button();
            this.button8 = new System.Windows.Forms.Button();
            this.button9 = new System.Windows.Forms.Button();
            this.button12 = new System.Windows.Forms.Button();
            this.button13 = new System.Windows.Forms.Button();
            this.button14 = new System.Windows.Forms.Button();
            this.button15 = new System.Windows.Forms.Button();
            this.button16 = new System.Windows.Forms.Button();
            this.button18 = new System.Windows.Forms.Button();
            this.button17 = new System.Windows.Forms.Button();
            this.button19 = new System.Windows.Forms.Button();
            this.textBox1 = new System.Windows.Forms.TextBox();
            this.button10 = new System.Windows.Forms.Button();
            this.SuspendLayout();
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(11, 50);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(38, 32);
            this.button1.TabIndex = 0;
            this.button1.Text = "1";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // button2
            // 
            this.button2.Location = new System.Drawing.Point(55, 50);
            this.button2.Name = "button2";
            this.button2.Size = new System.Drawing.Size(37, 32);
            this.button2.TabIndex = 1;
            this.button2.Text = "2";
            this.button2.UseVisualStyleBackColor = true;
            this.button2.Click += new System.EventHandler(this.button2_Click);
            // 
            // button3
            // 
            this.button3.Location = new System.Drawing.Point(98, 50);
            this.button3.Name = "button3";
            this.button3.Size = new System.Drawing.Size(37, 32);
            this.button3.TabIndex = 2;
            this.button3.Text = "3";
            this.button3.UseVisualStyleBackColor = true;
            this.button3.Click += new System.EventHandler(this.button3_Click);
            // 
            // button4
            // 
            this.button4.Location = new System.Drawing.Point(11, 88);
            this.button4.Name = "button4";
            this.button4.Size = new System.Drawing.Size(38, 32);
            this.button4.TabIndex = 3;
            this.button4.Text = "4";
            this.button4.UseVisualStyleBackColor = true;
            this.button4.Click += new System.EventHandler(this.button4_Click);
            // 
            // button5
            // 
            this.button5.Location = new System.Drawing.Point(55, 88);
            this.button5.Name = "button5";
            this.button5.Size = new System.Drawing.Size(37, 32);
            this.button5.TabIndex = 4;
            this.button5.Text = "5";
            this.button5.UseVisualStyleBackColor = true;
            this.button5.Click += new System.EventHandler(this.button5_Click);
            // 
            // button6
            // 
            this.button6.Location = new System.Drawing.Point(98, 88);
            this.button6.Name = "button6";
            this.button6.Size = new System.Drawing.Size(37, 32);
            this.button6.TabIndex = 5;
            this.button6.Text = "6";
            this.button6.UseVisualStyleBackColor = true;
            this.button6.Click += new System.EventHandler(this.button6_Click);
            // 
            // button7
            // 
            this.button7.Location = new System.Drawing.Point(11, 126);
            this.button7.Name = "button7";
            this.button7.Size = new System.Drawing.Size(38, 32);
            this.button7.TabIndex = 6;
            this.button7.Text = "7";
            this.button7.UseVisualStyleBackColor = true;
            this.button7.Click += new System.EventHandler(this.button7_Click);
            // 
            // button8
            // 
            this.button8.Location = new System.Drawing.Point(55, 126);
            this.button8.Name = "button8";
            this.button8.Size = new System.Drawing.Size(37, 32);
            this.button8.TabIndex = 7;
            this.button8.Text = "8";
            this.button8.UseVisualStyleBackColor = true;
            this.button8.Click += new System.EventHandler(this.button8_Click);
            // 
            // button9
            // 
            this.button9.Location = new System.Drawing.Point(98, 126);
            this.button9.Name = "button9";
            this.button9.Size = new System.Drawing.Size(37, 32);
            this.button9.TabIndex = 8;
            this.button9.Text = "9";
            this.button9.UseVisualStyleBackColor = true;
            this.button9.Click += new System.EventHandler(this.button9_Click);
            // 
            // button12
            // 
            this.button12.Location = new System.Drawing.Point(206, 88);
            this.button12.Name = "button12";
            this.button12.Size = new System.Drawing.Size(37, 32);
            this.button12.TabIndex = 12;
            this.button12.Text = "+";
            this.button12.UseVisualStyleBackColor = true;
            this.button12.Click += new System.EventHandler(this.button12_Click);
            // 
            // button13
            // 
            this.button13.Location = new System.Drawing.Point(163, 126);
            this.button13.Name = "button13";
            this.button13.Size = new System.Drawing.Size(37, 32);
            this.button13.TabIndex = 11;
            this.button13.Text = "-";
            this.button13.UseVisualStyleBackColor = true;
            this.button13.Click += new System.EventHandler(this.button13_Click);
            // 
            // button14
            // 
            this.button14.Location = new System.Drawing.Point(206, 50);
            this.button14.Name = "button14";
            this.button14.Size = new System.Drawing.Size(37, 32);
            this.button14.TabIndex = 10;
            this.button14.Text = "/";
            this.button14.UseVisualStyleBackColor = true;
            this.button14.Click += new System.EventHandler(this.button14_Click);
            // 
            // button15
            // 
            this.button15.Location = new System.Drawing.Point(163, 88);
            this.button15.Name = "button15";
            this.button15.Size = new System.Drawing.Size(37, 32);
            this.button15.TabIndex = 9;
            this.button15.Text = "*";
            this.button15.UseVisualStyleBackColor = true;
            this.button15.Click += new System.EventHandler(this.button15_Click);
            // 
            // button16
            // 
            this.button16.Location = new System.Drawing.Point(98, 164);
            this.button16.Name = "button16";
            this.button16.Size = new System.Drawing.Size(37, 32);
            this.button16.TabIndex = 15;
            this.button16.Text = ",";
            this.button16.UseVisualStyleBackColor = true;
            // 
            // button18
            // 
            this.button18.Location = new System.Drawing.Point(11, 164);
            this.button18.Name = "button18";
            this.button18.Size = new System.Drawing.Size(81, 32);
            this.button18.TabIndex = 17;
            this.button18.Text = "0";
            this.button18.UseVisualStyleBackColor = true;
            this.button18.Click += new System.EventHandler(this.button18_Click);
            // 
            // button17
            // 
            this.button17.Location = new System.Drawing.Point(163, 164);
            this.button17.Name = "button17";
            this.button17.Size = new System.Drawing.Size(80, 32);
            this.button17.TabIndex = 19;
            this.button17.Text = "=";
            this.button17.UseVisualStyleBackColor = true;
            this.button17.Click += new System.EventHandler(this.button17_Click);
            // 
            // button19
            // 
            this.button19.Location = new System.Drawing.Point(163, 50);
            this.button19.Name = "button19";
            this.button19.Size = new System.Drawing.Size(37, 32);
            this.button19.TabIndex = 18;
            this.button19.Text = "CE";
            this.button19.UseVisualStyleBackColor = true;
            this.button19.Click += new System.EventHandler(this.button19_Click);
            // 
            // textBox1
            // 
            this.textBox1.Location = new System.Drawing.Point(11, 12);
            this.textBox1.Name = "textBox1";
            this.textBox1.Size = new System.Drawing.Size(232, 20);
            this.textBox1.TabIndex = 20;
            // 
            // button10
            // 
            this.button10.Location = new System.Drawing.Point(206, 126);
            this.button10.Name = "button10";
            this.button10.Size = new System.Drawing.Size(37, 32);
            this.button10.TabIndex = 22;
            this.button10.Text = "+/-";
            this.button10.UseVisualStyleBackColor = true;
            this.button10.Click += new System.EventHandler(this.button10_Click);
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(256, 208);
            this.Controls.Add(this.button10);
            this.Controls.Add(this.textBox1);
            this.Controls.Add(this.button17);
            this.Controls.Add(this.button19);
            this.Controls.Add(this.button18);
            this.Controls.Add(this.button16);
            this.Controls.Add(this.button12);
            this.Controls.Add(this.button13);
            this.Controls.Add(this.button14);
            this.Controls.Add(this.button15);
            this.Controls.Add(this.button9);
            this.Controls.Add(this.button8);
            this.Controls.Add(this.button7);
            this.Controls.Add(this.button6);
            this.Controls.Add(this.button5);
            this.Controls.Add(this.button4);
            this.Controls.Add(this.button3);
            this.Controls.Add(this.button2);
            this.Controls.Add(this.button1);
            this.Name = "Form1";
            this.Text = "Kalkulator";
            this.Load += new System.EventHandler(this.Form1_Load);
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.Button button1;
        private System.Windows.Forms.Button button2;
        private System.Windows.Forms.Button button3;
        private System.Windows.Forms.Button button4;
        private System.Windows.Forms.Button button5;
        private System.Windows.Forms.Button button6;
        private System.Windows.Forms.Button button7;
        private System.Windows.Forms.Button button8;
        private System.Windows.Forms.Button button9;
        private System.Windows.Forms.Button button12;
        private System.Windows.Forms.Button button13;
        private System.Windows.Forms.Button button14;
        private System.Windows.Forms.Button button15;
        private System.Windows.Forms.Button button16;
        private System.Windows.Forms.Button button18;
        private System.Windows.Forms.Button button17;
        private System.Windows.Forms.Button button19;
        private System.Windows.Forms.TextBox textBox1;
        private System.Windows.Forms.Button button10;
    }
}
U mnie program się kompiluje...

Czemu nie mogę? Bo jak klikam 3razy 2 to tylko ostanie klikniecie pamieta... czyli 2. I na tej liczbie wykonuje obliczenia...
Morfidon
Administrator
Administrator
Posty: 1332
Dołączył(a): wtorek, 5 sierpnia 2008, 21:48
Contact:

Re: Pierwszy program (kalkulator w C#)

Post przez Morfidon »

W takim wypadku musisz dopisywać do textBoxa coś nowego, a nie nadpisywać jego wartość.
piotrek0492
Początkujący
Początkujący
Posty: 6
Dołączył(a): poniedziałek, 29 października 2012, 15:54

Re: Pierwszy program (kalkulator w C#)

Post przez piotrek0492 »

Czyli składnia : textBox1.Text = button2.Text;
jest zła?

Ale jak mogę do textBox1 dopisać wartość jeżeli w textboxie1 zawarte jest wyświetlenie samego tekstu.
Wartość liczby określa mi ilość kliknięć w liczby i zakańcza je znak operacyjny "+" itp.
Czy tą wartość musiałbym jakoś zaznaczyć w samym textBox1?
Morfidon
Administrator
Administrator
Posty: 1332
Dołączył(a): wtorek, 5 sierpnia 2008, 21:48
Contact:

Re: Pierwszy program (kalkulator w C#)

Post przez Morfidon »

Dopisujesz do zawartości pola textBox1.Text to co już było czyli jego samego + zawartość z buttona. Łączysz 2 stringi. Sugeruję przed zabraniem się do programowania obejrzeć cały video kurs C#.
piotrek0492
Początkujący
Początkujący
Posty: 6
Dołączył(a): poniedziałek, 29 października 2012, 15:54

Re: Pierwszy program (kalkulator w C#)

Post przez piotrek0492 »

Postanowiłem obejrzeć jeszcze raz owy kurs. Teraz tak zagadnienia te które mnie interesowały lecz mam kolejny problem... otóż problem z wyjątkami...

Zapisałem tak:

Code: Zaznacz cały

public void wynik()
        {
            LB2 = System.Double.Parse(textBox1.Text);
            switch (znak)
            {
                case "+": liczba = LB1 + LB2;
                    break;
                case "-": liczba = LB1 - LB2;
                    break;
                case "*": liczba = LB1 * LB2;
                    break;                
                case "/":
                    try
                    {
                        liczba = LB1 / LB2;
                    }
                    catch (DivideByZeroException e)
                    {
                        textBox1.Text = e.Message;
                    }                   
                    break;
                
            }
            textBox1.Text = LB1.ToString() + znak + LB2.ToString() + " = " + liczba.ToString();
        }
Lecz... gdy wypiszę podzielenie przez zero wyskakuje mi błąd FormatException dla liczby LB2 a przecież użyłem wyjątku dla dzielenia przez zero:/
Morfidon
Administrator
Administrator
Posty: 1332
Dołączył(a): wtorek, 5 sierpnia 2008, 21:48
Contact:

Re: Pierwszy program (kalkulator w C#)

Post przez Morfidon »

No bo próbujesz przypisać wtedy do LB 2 nie to co trzeba, w takim razie obsłuż wyjątek domyślny przy pomocy catch(...)
Post Reply