calc-2.0/calculator/Form1.cs
2025-12-10 01:59:59 +04:00

329 lines
9.6 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Globalization;
using System.Text.RegularExpressions;
using System.Windows.Forms;
namespace calculator
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
SetupAppearance();
}
private void SetupAppearance()
{
this.BackColor = Color.FromArgb(240, 240, 240);
textBox1.BackColor = Color.White;
textBox2.BackColor = Color.White;
SetButtonColor(button1, Color.FromArgb(70, 130, 180)); // %
SetButtonColor(button8, Color.FromArgb(70, 130, 180)); // /
SetButtonColor(button12, Color.FromArgb(70, 130, 180)); // *
SetButtonColor(button16, Color.FromArgb(70, 130, 180)); // -
SetButtonColor(button20, Color.FromArgb(70, 130, 180)); // +
SetButtonColor(button21, Color.FromArgb(46, 204, 113)); // =
SetButtonColor(button22, Color.FromArgb(70, 130, 180)); // %
SetButtonColor(button27, Color.FromArgb(155, 89, 182)); // sin
SetButtonColor(button26, Color.FromArgb(155, 89, 182)); // cos
SetButtonColor(button25, Color.FromArgb(155, 89, 182)); // x²
SetButtonColor(button28, Color.FromArgb(155, 89, 182)); // x³
button5.BackColor = Color.FromArgb(231, 76, 60); // Сброс
button5.ForeColor = Color.White;
button7.BackColor = Color.FromArgb(52, 152, 219); // ←
button7.ForeColor = Color.White;
SetDigitButtonColor(button9); // 1
SetDigitButtonColor(button10); // 2
SetDigitButtonColor(button11); // 3
SetDigitButtonColor(button13); // 4
SetDigitButtonColor(button14); // 5
SetDigitButtonColor(button15); // 6
SetDigitButtonColor(button17); // 7
SetDigitButtonColor(button18); // 8
SetDigitButtonColor(button19); // 9
SetDigitButtonColor(button24); // 0
button3.BackColor = Color.FromArgb(149, 165, 166); // (
button3.ForeColor = Color.White;
button4.BackColor = Color.FromArgb(149, 165, 166); // )
button4.ForeColor = Color.White;
button23.BackColor = Color.FromArgb(230, 126, 34); // +/-
button23.ForeColor = Color.White;
button29.BackColor = Color.FromArgb(230, 126, 34); // .
button29.ForeColor = Color.White;
}
private void SetButtonColor(Button btn, Color bgColor)
{
if (btn != null)
{
btn.BackColor = bgColor;
btn.ForeColor = Color.White;
btn.FlatStyle = FlatStyle.Flat;
btn.FlatAppearance.BorderSize = 0;
}
}
private void SetDigitButtonColor(Button btn)
{
if (btn != null)
{
btn.BackColor = Color.White;
btn.ForeColor = Color.Black;
btn.FlatStyle = FlatStyle.Flat;
btn.FlatAppearance.BorderColor = Color.FromArgb(200, 200, 200);
btn.FlatAppearance.BorderSize = 1;
}
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
private void label1_Click(object sender, EventArgs e)
{
}
private void label2_Click(object sender, EventArgs e)
{
}
private void button9_Click(object sender, EventArgs e)
{
textBox1.Text += "1";
}
private void button10_Click(object sender, EventArgs e)
{
textBox1.Text += "2";
}
private void button11_Click(object sender, EventArgs e)
{
textBox1.Text += "3";
}
private void button13_Click(object sender, EventArgs e)
{
textBox1.Text += "4";
}
private void button14_Click(object sender, EventArgs e)
{
textBox1.Text += "5";
}
private void button15_Click(object sender, EventArgs e)
{
textBox1.Text += "6";
}
private void button17_Click(object sender, EventArgs e)
{
textBox1.Text += "7";
}
private void button18_Click(object sender, EventArgs e)
{
textBox1.Text += "8";
}
private void button19_Click_1(object sender, EventArgs e)
{
textBox1.Text += "9";
}
private void button24_Click(object sender, EventArgs e)
{
textBox1.Text += "0";
}
private void button29_Click(object sender, EventArgs e)
{
if (!textBox1.Text.EndsWith(".") && !string.IsNullOrEmpty(textBox1.Text))
textBox1.Text += ".";
}
private void button3_Click(object sender, EventArgs e)
{
textBox1.Text += "(";
}
private void button4_Click(object sender, EventArgs e)
{
textBox1.Text += ")";
}
private void button1_Click(object sender, EventArgs e)
{
textBox1.Text += "%";
}
private void button22_Click(object sender, EventArgs e)
{
textBox1.Text += "%";
}
private void button8_Click(object sender, EventArgs e)
{
textBox1.Text += "/";
}
private void button12_Click(object sender, EventArgs e)
{
textBox1.Text += "*";
}
private void button16_Click(object sender, EventArgs e)
{
textBox1.Text += "-";
}
private void button20_Click(object sender, EventArgs e)
{
textBox1.Text += "+";
}
private void button21_Click(object sender, EventArgs e)
{
string expression = textBox1.Text;
try
{
object result = CalculateExpression(expression);
textBox2.Text = Convert.ToString(result);
}
catch (Exception ex)
{
textBox2.Text = "Ошибка";
MessageBox.Show("Некорректное выражение: " + ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void button27_Click(object sender, EventArgs e)
{
textBox1.Text += "sin(";
}
private void button26_Click(object sender, EventArgs e)
{
textBox1.Text += "cos(";
}
private void button25_Click(object sender, EventArgs e)
{
textBox1.Text += "square(";
}
private void button28_Click(object sender, EventArgs e)
{
textBox1.Text += "cube(";
}
private void button23_Click(object sender, EventArgs e)
{
textBox1.Text = ToggleSign(textBox1.Text);
}
private void button5_Click(object sender, EventArgs e)
{
textBox1.Text = "";
textBox2.Text = "";
textBox1.Focus();
}
private void button7_Click(object sender, EventArgs e)
{
if (textBox1.Text.Length > 0)
{
string text = textBox1.Text;
textBox1.Text = text.Remove(text.Length - 1, 1);
}
}
private void button2_Click_1(object sender, EventArgs e)
{
textBox1.Text += "";
}
private void button6_Click(object sender, EventArgs e)
{
}
private void button19_Click(object sender, EventArgs e)
{
textBox1.Text += "9";
}
private object CalculateExpression(string expression)
{
expression = Regex.Replace(expression, @"(sin|cos|square|cube)\(([^)]+)\)", match =>
{
string functionName = match.Groups[1].Value;
string argumentStr = match.Groups[2].Value;
object argumentValObj = CalculateExpression(argumentStr);
double argumentVal = Convert.ToDouble(argumentValObj);
double result = 0;
switch (functionName)
{
case "sin":
result = Math.Sin(argumentVal);
break;
case "cos":
result = Math.Cos(argumentVal);
break;
case "square":
result = argumentVal * argumentVal;
break;
case "cube":
result = argumentVal * argumentVal * argumentVal;
break;
}
return result.ToString(CultureInfo.InvariantCulture);
});
DataTable dt = new DataTable();
dt.Locale = CultureInfo.InvariantCulture;
return dt.Compute(expression, null);
}
private string ToggleSign(string currentValue)
{
if (string.IsNullOrEmpty(currentValue))
{
return "0";
}
if (double.TryParse(currentValue, NumberStyles.Any, CultureInfo.InvariantCulture, out double number))
{
return (-number).ToString(CultureInfo.InvariantCulture);
}
return currentValue;
}
private long Mod(long a, long b)
{
return a % b;
}
}
}