Untitled

 avatar
unknown
plain_text
6 months ago
18 kB
5
Indexable
namespace CalculatorApp
{
    partial class CalculatorForm
    {
        private System.ComponentModel.IContainer components = null;

        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void InitializeComponent()
        {
            this.textBox_Result = new System.Windows.Forms.TextBox();
            this.labelCurrentOperation = new System.Windows.Forms.Label();

            // Przyciski numeryczne
            this.button0 = new System.Windows.Forms.Button();
            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();

            // Przyciski operacyjne
            this.buttonAdd = new System.Windows.Forms.Button();
            this.buttonSubtract = new System.Windows.Forms.Button();
            this.buttonMultiply = new System.Windows.Forms.Button();
            this.buttonDivide = new System.Windows.Forms.Button();
            this.buttonEquals = new System.Windows.Forms.Button();
            this.buttonClear = new System.Windows.Forms.Button();
            this.buttonCE = new System.Windows.Forms.Button();
            this.buttonBackspace = new System.Windows.Forms.Button();
            this.buttonSqrt = new System.Windows.Forms.Button();
            this.buttonReciprocal = new System.Windows.Forms.Button();
            this.buttonSquare = new System.Windows.Forms.Button();

            // Ustawienia textBoxa
            this.textBox_Result.Font = new System.Drawing.Font("Microsoft Sans Serif", 18F);
            this.textBox_Result.Location = new System.Drawing.Point(10, 10);
            this.textBox_Result.Size = new System.Drawing.Size(270, 35);
            this.textBox_Result.Text = "0";
            this.textBox_Result.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;

            // Ustawienia labela
            this.labelCurrentOperation.AutoSize = true;
            this.labelCurrentOperation.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F);
            this.labelCurrentOperation.Location = new System.Drawing.Point(10, 45);
            this.labelCurrentOperation.Size = new System.Drawing.Size(0, 20);

            // Dodawanie przycisków numerycznych (z ustawieniami)
            int startX = 10, startY = 100, buttonWidth = 60, buttonHeight = 50, spacing = 10;

            // Przyciski numeryczne
            this.button7.Text = "7";
            this.button7.Font = new System.Drawing.Font("Microsoft Sans Serif", 14F);
            this.button7.Location = new System.Drawing.Point(startX, startY);
            this.button7.Size = new System.Drawing.Size(buttonWidth, buttonHeight);
            this.button7.Click += new System.EventHandler(this.NumberButton_Click);

            this.button8.Text = "8";
            this.button8.Font = new System.Drawing.Font("Microsoft Sans Serif", 14F);
            this.button8.Location = new System.Drawing.Point(startX + (buttonWidth + spacing), startY);
            this.button8.Size = new System.Drawing.Size(buttonWidth, buttonHeight);
            this.button8.Click += new System.EventHandler(this.NumberButton_Click);

            this.button9.Text = "9";
            this.button9.Font = new System.Drawing.Font("Microsoft Sans Serif", 14F);
            this.button9.Location = new System.Drawing.Point(startX + 2 * (buttonWidth + spacing), startY);
            this.button9.Size = new System.Drawing.Size(buttonWidth, buttonHeight);
            this.button9.Click += new System.EventHandler(this.NumberButton_Click);

            this.button4.Text = "4";
            this.button4.Font = new System.Drawing.Font("Microsoft Sans Serif", 14F);
            this.button4.Location = new System.Drawing.Point(startX, startY + (buttonHeight + spacing));
            this.button4.Size = new System.Drawing.Size(buttonWidth, buttonHeight);
            this.button4.Click += new System.EventHandler(this.NumberButton_Click);

            this.button5.Text = "5";
            this.button5.Font = new System.Drawing.Font("Microsoft Sans Serif", 14F);
            this.button5.Location = new System.Drawing.Point(startX + (buttonWidth + spacing), startY + (buttonHeight + spacing));
            this.button5.Size = new System.Drawing.Size(buttonWidth, buttonHeight);
            this.button5.Click += new System.EventHandler(this.NumberButton_Click);

            this.button6.Text = "6";
            this.button6.Font = new System.Drawing.Font("Microsoft Sans Serif", 14F);
            this.button6.Location = new System.Drawing.Point(startX + 2 * (buttonWidth + spacing), startY + (buttonHeight + spacing));
            this.button6.Size = new System.Drawing.Size(buttonWidth, buttonHeight);
            this.button6.Click += new System.EventHandler(this.NumberButton_Click);

            this.button1.Text = "1";
            this.button1.Font = new System.Drawing.Font("Microsoft Sans Serif", 14F);
            this.button1.Location = new System.Drawing.Point(startX, startY + 2 * (buttonHeight + spacing));
            this.button1.Size = new System.Drawing.Size(buttonWidth, buttonHeight);
            this.button1.Click += new System.EventHandler(this.NumberButton_Click);

            this.button2.Text = "2";
            this.button2.Font = new System.Drawing.Font("Microsoft Sans Serif", 14F);
            this.button2.Location = new System.Drawing.Point(startX + (buttonWidth + spacing), startY + 2 * (buttonHeight + spacing));
            this.button2.Size = new System.Drawing.Size(buttonWidth, buttonHeight);
            this.button2.Click += new System.EventHandler(this.NumberButton_Click);

            this.button3.Text = "3";
            this.button3.Font = new System.Drawing.Font("Microsoft Sans Serif", 14F);
            this.button3.Location = new System.Drawing.Point(startX + 2 * (buttonWidth + spacing), startY + 2 * (buttonHeight + spacing));
            this.button3.Size = new System.Drawing.Size(buttonWidth, buttonHeight);
            this.button3.Click += new System.EventHandler(this.NumberButton_Click);

            this.button0.Text = "0";
            this.button0.Font = new System.Drawing.Font("Microsoft Sans Serif", 14F);
            this.button0.Location = new System.Drawing.Point(startX, startY + 3 * (buttonHeight + spacing));
            this.button0.Size = new System.Drawing.Size(buttonWidth, buttonHeight);
            this.button0.Click += new System.EventHandler(this.NumberButton_Click);

            // Dodawanie przycisków operacyjnych (z ustawieniami)
            this.buttonAdd.Text = "+";
            this.buttonAdd.Font = new System.Drawing.Font("Microsoft Sans Serif", 14F);
            this.buttonAdd.Location = new System.Drawing.Point(startX + 3 * (buttonWidth + spacing), startY);
            this.buttonAdd.Size = new System.Drawing.Size(buttonWidth, buttonHeight);
            this.buttonAdd.Click += new System.EventHandler(this.OperatorButton_Click);

            this.buttonSubtract.Text = "-";
            this.buttonSubtract.Font = new System.Drawing.Font("Microsoft Sans Serif", 14F);
            this.buttonSubtract.Location = new System.Drawing.Point(startX + 3 * (buttonWidth + spacing), startY + (buttonHeight + spacing));
            this.buttonSubtract.Size = new System.Drawing.Size(buttonWidth, buttonHeight);
            this.buttonSubtract.Click += new System.EventHandler(this.OperatorButton_Click);

            this.buttonMultiply.Text = "×";
            this.buttonMultiply.Font = new System.Drawing.Font("Microsoft Sans Serif", 14F);
            this.buttonMultiply.Location = new System.Drawing.Point(startX + 3 * (buttonWidth + spacing), startY + 2 * (buttonHeight + spacing));
            this.buttonMultiply.Size = new System.Drawing.Size(buttonWidth, buttonHeight);
            this.buttonMultiply.Click += new System.EventHandler(this.OperatorButton_Click);

            this.buttonDivide.Text = "÷";
            this.buttonDivide.Font = new System.Drawing.Font("Microsoft Sans Serif", 14F);
            this.buttonDivide.Location = new System.Drawing.Point(startX + 3 * (buttonWidth + spacing), startY + 3 * (buttonHeight + spacing));
            this.buttonDivide.Size = new System.Drawing.Size(buttonWidth, buttonHeight);
            this.buttonDivide.Click += new System.EventHandler(this.OperatorButton_Click);

            this.buttonEquals.Text = "=";
            this.buttonEquals.Font = new System.Drawing.Font("Microsoft Sans Serif", 14F);
            this.buttonEquals.Location = new System.Drawing.Point(startX + 2 * (buttonWidth + spacing), startY + 3 * (buttonHeight + spacing));
            this.buttonEquals.Size = new System.Drawing.Size(buttonWidth, buttonHeight);
            this.buttonEquals.Click += new System.EventHandler(this.EqualsButton_Click);

            this.buttonClear.Text = "C";
            this.buttonClear.Font = new System.Drawing.Font("Microsoft Sans Serif", 14F);
            this.buttonClear.Location = new System.Drawing.Point(startX + (buttonWidth + spacing), startY + 3 * (buttonHeight + spacing));
            this.buttonClear.Size = new System.Drawing.Size(buttonWidth, buttonHeight);
            this.buttonClear.Click += new System.EventHandler(this.ClearButton_Click);

            this.buttonCE.Text = "CE";
            this.buttonCE.Font = new System.Drawing.Font("Microsoft Sans Serif", 14F);
            this.buttonCE.Location = new System.Drawing.Point(startX, startY + 3 * (buttonHeight + spacing));
            this.buttonCE.Size = new System.Drawing.Size(buttonWidth, buttonHeight);
            this.buttonCE.Click += new System.EventHandler(this.CEButton_Click);

            this.buttonBackspace.Text = "←";
            this.buttonBackspace.Font = new System.Drawing.Font("Microsoft Sans Serif", 14F);
            this.buttonBackspace.Location = new System.Drawing.Point(startX + 3 * (buttonWidth + spacing), startY + 3 * (buttonHeight + spacing));
            this.buttonBackspace.Size = new System.Drawing.Size(buttonWidth, buttonHeight);
            this.buttonBackspace.Click += new System.EventHandler(this.BackspaceButton_Click);

            this.buttonSqrt.Text = "√";
            this.buttonSqrt.Font = new System.Drawing.Font("Microsoft Sans Serif", 14F);
            this.buttonSqrt.Location = new System.Drawing.Point(startX + (buttonWidth + spacing), startY - (buttonHeight + spacing));
            this.buttonSqrt.Size = new System.Drawing.Size(buttonWidth, buttonHeight);
            this.buttonSqrt.Click += new System.EventHandler(this.SqrtButton_Click);

            this.buttonSquare.Text = "x²";
            this.buttonSquare.Font = new System.Drawing.Font("Microsoft Sans Serif", 14F);
            this.buttonSquare.Location = new System.Drawing.Point(startX + 2 * (buttonWidth + spacing), startY - (buttonHeight + spacing));
            this.buttonSquare.Size = new System.Drawing.Size(buttonWidth, buttonHeight);
            this.buttonSquare.Click += new System.EventHandler(this.SquareButton_Click);

            this.buttonReciprocal.Text = "1/x";
            this.buttonReciprocal.Font = new System.Drawing.Font("Microsoft Sans Serif", 14F);
            this.buttonReciprocal.Location = new System.Drawing.Point(startX, startY - (buttonHeight + spacing));
            this.buttonReciprocal.Size = new System.Drawing.Size(buttonWidth, buttonHeight);
            this.buttonReciprocal.Click += new System.EventHandler(this.ReciprocalButton_Click);

            // Dodanie komponentów do formularza
            this.Controls.Add(this.textBox_Result);
            this.Controls.Add(this.labelCurrentOperation);
            this.Controls.Add(this.button0);
            this.Controls.Add(this.button1);
            this.Controls.Add(this.button2);
            this.Controls.Add(this.button3);
            this.Controls.Add(this.button4);
            this.Controls.Add(this.button5);
            this.Controls.Add(this.button6);
            this.Controls.Add(this.button7);
            this.Controls.Add(this.button8);
            this.Controls.Add(this.button9);
            this.Controls.Add(this.buttonAdd);
            this.Controls.Add(this.buttonSubtract);
            this.Controls.Add(this.buttonMultiply);
            this.Controls.Add(this.buttonDivide);
            this.Controls.Add(this.buttonEquals);
            this.Controls.Add(this.buttonClear);
            this.Controls.Add(this.buttonCE);
            this.Controls.Add(this.buttonBackspace);
            this.Controls.Add(this.buttonSqrt);
            this.Controls.Add(this.buttonReciprocal);
            this.Controls.Add(this.buttonSquare);

            // Ustawienia formularza
            this.ClientSize = new System.Drawing.Size(300, 450);
            this.Name = "CalculatorForm";
            this.Text = "Kalkulator";
        }

        private System.Windows.Forms.TextBox textBox_Result;
        private System.Windows.Forms.Label labelCurrentOperation;
        private System.Windows.Forms.Button button0;
        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 buttonAdd;
        private System.Windows.Forms.Button buttonSubtract;
        private System.Windows.Forms.Button buttonMultiply;
        private System.Windows.Forms.Button buttonDivide;
        private System.Windows.Forms.Button buttonEquals;
        private System.Windows.Forms.Button buttonClear;
        private System.Windows.Forms.Button buttonCE;
        private System.Windows.Forms.Button buttonBackspace;
        private System.Windows.Forms.Button buttonSqrt;
        private System.Windows.Forms.Button buttonSquare;
        private System.Windows.Forms.Button buttonReciprocal;
    }
}


using System;
using System.Windows.Forms;

namespace CalculatorApp
{
    public partial class CalculatorForm : Form
    {
        double result = 0;
        string operation = "";
        bool isOperationPerformed = false;

        public CalculatorForm()
        {
            InitializeComponent();
        }

        private void NumberButton_Click(object sender, EventArgs e)
        {
            if ((textBox_Result.Text == "0") || isOperationPerformed)
                textBox_Result.Clear();

            isOperationPerformed = false;
            Button button = (Button)sender;
            textBox_Result.Text += button.Text;
        }

        private void OperatorButton_Click(object sender, EventArgs e)
        {
            Button button = (Button)sender;
            if (result != 0)
            {
                buttonEquals.PerformClick();
                operation = button.Text;
                labelCurrentOperation.Text = $"{result} {operation}";
                isOperationPerformed = true;
            }
            else
            {
                operation = button.Text;
                result = double.Parse(textBox_Result.Text);
                labelCurrentOperation.Text = $"{result} {operation}";
                isOperationPerformed = true;
            }
        }

        private void ClearButton_Click(object sender, EventArgs e)
        {
            textBox_Result.Text = "0";
            result = 0;
            labelCurrentOperation.Text = "";
        }

        private void CEButton_Click(object sender, EventArgs e)
        {
            textBox_Result.Text = "0";
        }

        private void BackspaceButton_Click(object sender, EventArgs e)
        {
            if (textBox_Result.Text.Length > 0)
                textBox_Result.Text = textBox_Result.Text.Remove(textBox_Result.Text.Length - 1);

            if (textBox_Result.Text == "")
                textBox_Result.Text = "0";
        }

        private void EqualsButton_Click(object sender, EventArgs e)
        {
            switch (operation)
            {
                case "+":
                    textBox_Result.Text = (result + double.Parse(textBox_Result.Text)).ToString();
                    break;
                case "-":
                    textBox_Result.Text = (result - double.Parse(textBox_Result.Text)).ToString();
                    break;
                case "×":
                    textBox_Result.Text = (result * double.Parse(textBox_Result.Text)).ToString();
                    break;
                case "÷":
                    if (double.Parse(textBox_Result.Text) != 0)
                        textBox_Result.Text = (result / double.Parse(textBox_Result.Text)).ToString();
                    else
                        MessageBox.Show("Nie można dzielić przez zero!");
                    break;
                default:
                    break;
            }
            result = double.Parse(textBox_Result.Text);
            labelCurrentOperation.Text = "";
            operation = "";
        }

        private void SqrtButton_Click(object sender, EventArgs e)
        {
            double number = double.Parse(textBox_Result.Text);
            textBox_Result.Text = Math.Sqrt(number).ToString();
        }

        private void SquareButton_Click(object sender, EventArgs e)
        {
            double number = double.Parse(textBox_Result.Text);
            textBox_Result.Text = (number * number).ToString();
        }

        private void ReciprocalButton_Click(object sender, EventArgs e)
        {
            double number = double.Parse(textBox_Result.Text);
            if (number != 0)
                textBox_Result.Text = (1 / number).ToString();
            else
                MessageBox.Show("Nie można obliczyć odwrotności zera!");
        }
    }
}

Editor is loading...
Leave a Comment