calculator22/Form1.cs
Your Name 3d9d18bb4a file
2025-12-08 15:13:52 +04:00

340 lines
10 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
using BaseCalculator;
using System.Data.SqlTypes;
using System.Net.NetworkInformation;
namespace UISP
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
textBox1.Text += "+/-";
}
private void button2_Click(object sender, EventArgs e)
{
textBox1.Text += "0";
}
private void button3_Click(object sender, EventArgs e)
{
textBox1.Text += "mod";
}
private void button8_Click(object sender, EventArgs e)
{
textBox1.Text += "-";
}
private void button29_Click(object sender, EventArgs e)
{
textBox1.Text += "²";
}
private void button24_Click(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button20_Click(object sender, EventArgs e)
{
try
{
string expression = textBox1.Text;
AnalaizerClass.expression = expression;
if (!AnalaizerClass.CheckCurrency())
{
textBox2.Text = $"ошибка: незаконченное выражение";
return;
}
expression = expression.Replace(" ", "").Replace("(", "").Replace(")", "");
if (expression.StartsWith("sin(", StringComparison.OrdinalIgnoreCase) && expression.EndsWith(")"))
{
string innerExpression = expression.Substring(4, expression.Length - 5);
if (double.TryParse(innerExpression, out double angle))
{
double result = CalcClass.Sinus(angle);
textBox2.Text = result.ToString("F6");
return;
}
else
{
textBox2.Text = "Ошибка: некорректный аргумент для sin";
return;
}
}
if (expression.StartsWith("cos(", StringComparison.OrdinalIgnoreCase) && expression.EndsWith(")"))
{
string innerExpression = expression.Substring(4, expression.Length - 5);
if (double.TryParse(innerExpression, out double angle))
{
double result = CalcClass.Cosinus(angle);
textBox2.Text = result.ToString("F6");
return;
}
else
{
textBox2.Text = "ошибка: некоррентный аргумент для cos";
return;
}
}
if (expression.Contains("+"))
{
string[] parts = expression.Split('+');
if (parts.Length == 2)
{
long a = long.Parse(parts[0]);
long b = long.Parse(parts[1]);
int result = CalcClass.Add(a, b);
textBox2.Text = result.ToString();
}
}
else if (expression.Contains("√"))
{
string[] parts = expression.Split('√');
if (parts.Length == 2)
{
long a = long.Parse(parts[0]);
long b = long.Parse(parts[1]);
double result = CalcClass.Power(a, b);
textBox2.Text = result.ToString();
}
}
else if (expression.Contains("-"))
{
string[] parts = expression.Split('-');
if (parts.Length == 2)
{
long a = long.Parse(parts[0]);
long b = long.Parse(parts[1]);
int result = CalcClass.Sub(a, b);
textBox2.Text = result.ToString();
}
}
else if (expression.Contains("*"))
{
string[] parts = expression.Split('*');
if (parts.Length == 2)
{
long a = long.Parse(parts[0]);
long b = long.Parse(parts[1]);
int result = CalcClass.Mult(a, b);
textBox2.Text = result.ToString();
}
}
else if (expression.Contains("/"))
{
string[] parts = expression.Split('/');
if (parts.Length == 2)
{
long a = long.Parse(parts[0]);
long b = long.Parse(parts[1]);
int result = CalcClass.Div(a, b);
textBox2.Text = result.ToString();
}
}
else if (expression.Contains("²"))
{
string[] parts = expression.Split('²');
if (parts.Length == 2)
{
double a = double.Parse(parts[0]);
double result = CalcClass.Square(a);
textBox2.Text = result.ToString();
}
}
else if (expression.Contains("³"))
{
string[] parts = expression.Split('³');
if (parts.Length == 2)
{
double a = double.Parse(parts[0]);
double result = CalcClass.Cube(a);
textBox2.Text = result.ToString();
}
}
else if (expression.Contains("√"))
{
string[] parts = expression.Split('√');
if (parts.Length == 2)
{
long a = long.Parse(parts[0]);
long b = long.Parse(parts[1]);
double result = CalcClass.Power(a, b);
textBox2.Text = result.ToString();
}
}
else if (expression.Contains("mod"))
{
string[] parts = expression.Split(new string[] { "mod" }, StringSplitOptions.None);
if (parts.Length == 2)
{
long a = long.Parse(parts[0].Trim());
long b = long.Parse(parts[1].Trim());
if (b == 0)
{
textBox2.Text = "ошибка: деления на ноль в операции mod";
return;
}
int result = CalcClass.Mod(a, b);
textBox2.Text = result.ToString();
}
else
{
textBox2.Text = "ошибка: неверный формат выражения для mod";
}
}
else
{
textBox2.Text = $"ошибка: неверный формат выражения";
}
}
catch (Exception ex)
{
textBox2.Text = $"ошибка: {ex.Message}";
}
}
private void button26_Click(object sender, EventArgs e)
{
}
private void button4_Click(object sender, EventArgs e)
{
textBox1.Text += "+";
}
private void button5_Click(object sender, EventArgs e)
{
textBox1.Text += "7";
}
private void button6_Click(object sender, EventArgs e)
{
textBox1.Text += "8";
}
private void button7_Click(object sender, EventArgs e)
{
textBox1.Text += "9";
}
private void button9_Click(object sender, EventArgs e)
{
textBox1.Text += "4";
}
private void button10_Click(object sender, EventArgs e)
{
textBox1.Text += "5";
}
private void button11_Click(object sender, EventArgs e)
{
textBox1.Text += "6";
}
private void button12_Click(object sender, EventArgs e)
{
textBox1.Text += "*";
}
private void button13_Click(object sender, EventArgs e)
{
textBox1.Text += "1";
}
private void button14_Click(object sender, EventArgs e)
{
textBox1.Text += "2";
}
private void button15_Click(object sender, EventArgs e)
{
textBox1.Text += "3";
}
private void button16_Click(object sender, EventArgs e)
{
textBox1.Text += "/";
}
private void button21_Click(object sender, EventArgs e)
{
textBox1.Text += "(";
}
private void button22_Click(object sender, EventArgs e)
{
textBox1.Text += ")";
}
private void button27_Click(object sender, EventArgs e)
{
textBox1.Text += "cos";
}
private void button28_Click(object sender, EventArgs e)
{
textBox1.Text += "sin";
}
private void button30_Click(object sender, EventArgs e)
{
textBox1.Text += "³";
}
private void button23_Click(object sender, EventArgs e)
{
if (!String.IsNullOrEmpty(textBox1.Text))
{
textBox1.Text = textBox1.Text.Substring(0, textBox1.Text.Length - 1);
}
}
private void button26_Click_1(object sender, EventArgs e)
{
textBox1.Text += "√";
}
}
}