Как написать калькулятор, подобный калькулятору в Windows - C#

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

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

нужно сделать так как в майкрософте виндоуз калькулятор чтобы все функции как и там чтобы все было ваот код отредактируйте пож его что там неверно если че и скиньте
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
 
namespace CalculatorPPPI
{
    public partial class Form1 : Form
    {
 
        public Form1()
        {
            InitializeComponent();
            CurrentState = state.nonstate;
           
        }
        #region variables
        string buff;//some operanions +-*/
        state CurrentState;//state
        string memory = "";//operations with memory
        string memory2 = "";//tool strip memory operations
        double buff2;
        bool changetxt = true;
        enum state
        {
            substraction,
            addiction,
            multiplication,
            divide,
            nonstate
        }
        #endregion
        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!(Char.IsDigit(e.KeyChar)))
            {
                if (e.KeyChar == '.' || e.KeyChar == ',' || e.KeyChar == (char)Keys.Back)
                {
                    if (e.KeyChar == (char)Keys.Back)
                    {
 
                    }
                    else
                        if (textBox1.Text.Contains(",") || textBox1.Text.Contains("."))
                        {
                            e.Handled = true;
                        }
                }
                else
                {
                    e.Handled = true;
                }
            }
        }//запрет на ввод букв(только цифры и знаки)
        private void button16_Click(object sender, EventArgs e)
        {
            vypolnenie();
           /* try
            { 
                if (changetxt ==true)
                    buff2 = double.Parse(textBox1.Text);
                if (CurrentState == state.divide)
                {
                    //CurrentState = state.nonstate;
 
                    //textBox1.Text = double.Parse(buff).ToString() + "/" + double.Parse(textBox1.Text) + "=";
                    textBox1.Text = (double.Parse(buff) / buff2).ToString();
                }
                if (CurrentState == state.multiplication)
                {
                   // CurrentState = state.nonstate;
 
                    //textBox1.Text = double.Parse(buff).ToString() + "*" + double.Parse(textBox1.Text) + "=";
                    textBox1.Text = (double.Parse(buff) * buff2).ToString();
                }
                if (CurrentState == state.substraction)
                {
                    //CurrentState = state.nonstate;
 
                    //textBox1.Text = double.Parse(buff).ToString() + "-" + double.Parse(textBox1.Text) + "=";
                    textBox1.Text = (double.Parse(buff) - buff2).ToString();
                }
                if (CurrentState == state.addiction)
                {
                   // CurrentState = state.nonstate;
 
                    //textBox1.Text = double.Parse(buff).ToString() + "+" + double.Parse(textBox1.Text) + "=";
                    textBox1.Text = (double.Parse(buff) + buff2).ToString();
                }
                buff = textBox1.Text;
            }
            catch (Exception)
            {
                MessageBox.Show("Try input incorrect data");
            }
            changetxt = false;*/
        }//=
        #region арифметические действия(нажатия не кнопки)
        private void multiplicationButton_Click(object sender, EventArgs e)
        {
            if (CurrentState == state.nonstate)
            {
                CurrentState = state.multiplication;
                buff = textBox1.Text;
                textBox1.Text = "";
                textBox1.Focus();//focus on textbox1 and clear it
            }
        }//умножение
        private void subtractionButton_Click(object sender, EventArgs e)
        {
            if (CurrentState == state.nonstate)
            {
                CurrentState = state.substraction;
                buff = textBox1.Text;
                textBox1.Text = "";
                textBox1.Focus();//focus on textbox1 and clear it
            }
        }//вычитание
        private void divideButton_Click(object sender, EventArgs e)
        {
            if (CurrentState == state.nonstate)
            {
                CurrentState = state.divide;
                buff = textBox1.Text;
                textBox1.Text = "";
                textBox1.Focus();//focus on textbox1 and clear it
            }
        }//деление
        private void additionButton_Click(object sender, EventArgs e)
        {
            if (CurrentState == state.nonstate)
            {
                CurrentState = state.addiction;
                buff = textBox1.Text;
                textBox1.Text = "";
                textBox1.Focus();//focus on textbox1 and clear it
            }
        }//сложение
        private void sqrtButton_Click(object sender, EventArgs e)//извлечение квадратного корня
        {
            try
            {
                if (textBox1.Text.Length > 0)
                {
                    textBox1.Text = Math.Sqrt(double.Parse(textBox1.Text)).ToString();
                }
 
            }
            catch (Exception) { }
        }
        private void percentButton_Click(object sender, EventArgs e)
        {
            try
            {
                textBox1.Text = (1 / double.Parse(textBox1.Text)).ToString();//);
            }
            catch (Exception) { }
        }//1/x
        private void dividedByOneButton_Click(object sender, EventArgs e)
        {
            try
            {
                textBox1.Text = (Math.Pow(double.Parse(textBox1.Text.Replace('.',',')), 2)).ToString();//x^2
            }
            catch (Exception)
            {
                MessageBox.Show("Попытайтесь ввести корректные для\nданной операции данные и повторите", "Калькулятор", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }//x^2
        private void ClearButton_Click(object sender, EventArgs e)
        {
            textBox1.Clear();
        }//clear
        private void ClearAllButton_Click(object sender, EventArgs e)//reset all
        {
            textBox1.Clear();
            CurrentState = state.nonstate;
            buff = "";
        }
        private void BackspaceButton_Click(object sender, EventArgs e)
        {
            try
            {
                textBox1.Text = textBox1.Text.Substring(0, textBox1.Text.Length - 1);
            }
            catch (Exception) { }
        }//-1 symbol
        
        private void button22_Click(object sender, EventArgs e)
        {
            textBox1.Text = memory;
        }//get from memory
        private void копироватьToolStripMenuItem_Click(object sender, EventArgs e)//ctrl+C
        {
            memory2 = textBox1.Text;
        }
        private void вставитьToolStripMenuItem_Click(object sender, EventArgs e)
        {
            textBox1.Text = memory2;
        }//ctrl+V
        private void buttonPlusMinus_Click(object sender, EventArgs e)
        {
            try
            {
                textBox1.Text = (double.Parse(textBox1.Text) * (-1)).ToString();
            }
            catch (Exception) { }
        }
        #region numbers
        private void button1_Click(object sender, EventArgs e)
        {
            textBox1.Text += "1";
            textBox1.Focus();
            textBox1.SelectionStart = textBox1.Text.Length;
        }
        private void button2_Click(object sender, EventArgs e)
        {
            textBox1.Text += "2";
            textBox1.Focus();
            textBox1.SelectionStart = textBox1.Text.Length;
        }
        private void button3_Click(object sender, EventArgs e)
        {
            textBox1.Text += "3";
            textBox1.Focus();
            textBox1.SelectionStart = textBox1.Text.Length;
        }
        private void button4_Click(object sender, EventArgs e)
        {
            textBox1.Text += "4";
            textBox1.Focus();
            textBox1.SelectionStart = textBox1.Text.Length;
        }
        private void button5_Click(object sender, EventArgs e)
        {
            textBox1.Text += "5";
            textBox1.Focus();
            textBox1.SelectionStart = textBox1.Text.Length;
        }
        private void button6_Click(object sender, EventArgs e)
        {
            textBox1.Text += "6";
            textBox1.Focus();
            textBox1.SelectionStart = textBox1.Text.Length;
        }
        private void button7_Click(object sender, EventArgs e)
        {
            textBox1.Text += "7";
            textBox1.Focus();
            textBox1.SelectionStart = textBox1.Text.Length;
        }
        private void button8_Click(object sender, EventArgs e)
        {
            textBox1.Text += "8";
            textBox1.Focus();
            textBox1.SelectionStart = textBox1.Text.Length;
        }
        private void button9_Click(object sender, EventArgs e)
        {
            textBox1.Text += "9";
            textBox1.Focus();
            textBox1.SelectionStart = textBox1.Text.Length;
        }
        private void button0_Click(object sender, EventArgs e)
        {
            textBox1.Text += "0";
            textBox1.Focus();
            textBox1.SelectionStart = textBox1.Text.Length;
        }
        private void buttonOEMComma_Click(object sender, EventArgs e)
        {
            if (textBox1.Text.IndexOf(',') == -1)
            {
                textBox1.Text += ",";
                textBox1.Focus();
            }
            
        }
 
        #endregion
 
        private void Form1_Load(object sender, EventArgs e)
        {
 
        }

        #endregion
 
        private void menuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
 
        }
 
        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            changetxt = true;
        }
private void vypolnenie()
{
            try
            { 
                if (changetxt ==true)
                    buff2 = double.Parse(textBox1.Text);
                if (CurrentState == state.divide)
                {
                    //CurrentState = state.nonstate;
 
                    //textBox1.Text = double.Parse(buff).ToString() + "/" + double.Parse(textBox1.Text) + "=";
                    textBox1.Text = (double.Parse(buff) / buff2).ToString();
                }
                if (CurrentState == state.multiplication)
                {
                   // CurrentState = state.nonstate;
 
                    //textBox1.Text = double.Parse(buff).ToString() + "*" + double.Parse(textBox1.Text) + "=";
                    textBox1.Text = (double.Parse(buff) * buff2).ToString();
                }
                if (CurrentState == state.substraction)
                {
                    //CurrentState = state.nonstate;
 
                    //textBox1.Text = double.Parse(buff).ToString() + "-" + double.Parse(textBox1.Text) + "=";
                    textBox1.Text = (double.Parse(buff) - buff2).ToString();
                }
                if (CurrentState == state.addiction)
                {
                   // CurrentState = state.nonstate;
 
                    //textBox1.Text = double.Parse(buff).ToString() + "+" + double.Parse(textBox1.Text) + "=";
                    textBox1.Text = (double.Parse(buff) + buff2).ToString();
                }
                buff = textBox1.Text;
            }
            catch (Exception)
            {
                MessageBox.Show("Try input incorrect data");
            }
            changetxt = false;
}
    }
}

Решение задачи: «Как написать калькулятор, подобный калькулятору в Windows»

textual
Листинг программы
 
private void button1_Click
private void button2_Click
private void button3_Click
private void button4_Click
private void button5_Click
private void button6_Click
private void button7_Click
private void button8_Click
private void button9_Click
private void button0_Click

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


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

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

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