269 lines
6.9 KiB
C#
269 lines
6.9 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel;
|
|
using System.Data;
|
|
using System.Drawing;
|
|
using System.Globalization;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Text.RegularExpressions;
|
|
using System.Threading.Tasks;
|
|
using System.Windows.Forms;
|
|
|
|
namespace calculator
|
|
{
|
|
public partial class Form1 : Form
|
|
{
|
|
public Form1()
|
|
{
|
|
InitializeComponent();
|
|
}
|
|
|
|
private void label1_Click(object sender, EventArgs e)
|
|
{
|
|
|
|
}
|
|
|
|
private void textBox1_TextChanged(object sender, EventArgs e)
|
|
{
|
|
|
|
}
|
|
|
|
private void button19_Click(object sender, EventArgs e)
|
|
{
|
|
textBox1.Text += "9";
|
|
}
|
|
|
|
private void button1_Click(object sender, EventArgs e)
|
|
{
|
|
textBox1.Text += "%";
|
|
}
|
|
|
|
private void button2_Click_1(object sender, EventArgs e)
|
|
{
|
|
textBox1.Text += "";
|
|
}
|
|
|
|
private void button3_Click(object sender, EventArgs e)
|
|
{
|
|
textBox1.Text += "(";
|
|
}
|
|
|
|
private void button4_Click(object sender, EventArgs e)
|
|
{
|
|
textBox1.Text += ")";
|
|
}
|
|
|
|
private void button5_Click(object sender, EventArgs e)
|
|
{
|
|
textBox1.Text = "";
|
|
textBox2.Text = "";
|
|
textBox1.Focus();
|
|
}
|
|
|
|
private void button6_Click(object sender, EventArgs e)
|
|
{
|
|
|
|
}
|
|
|
|
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 textBox2_TextChanged(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 button23_Click(object sender, EventArgs e)
|
|
{
|
|
textBox1.Text = CalcClass.ToggleSign(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 = CalcClass.EvaluateExpression(expression);
|
|
textBox2.Text = Convert.ToString(result);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
textBox2.Text = "Error";
|
|
MessageBox.Show("Invalid expression: " + ex.Message, "будет реализовано позже", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
}
|
|
}
|
|
|
|
private void label2_Click(object sender, EventArgs e)
|
|
{
|
|
|
|
}
|
|
|
|
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 button29_Click(object sender, EventArgs e)
|
|
{
|
|
textBox1.Text += ".";
|
|
}
|
|
}
|
|
|
|
public class CalcClass
|
|
{
|
|
public static object EvaluateExpression(string expression)
|
|
{
|
|
//square(), cube(), sin(), cos()
|
|
expression = Regex.Replace(expression, @"(sin|cos|square|cube)\(([^)]+)\)", match => {
|
|
string functionName = match.Groups[1].Value;
|
|
string argumentStr = match.Groups[2].Value;
|
|
|
|
object argumentValObj = EvaluateExpression(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);
|
|
}
|
|
|
|
public static long Mod(long a, long b)
|
|
{
|
|
return a % b;
|
|
}
|
|
|
|
public static 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;
|
|
}
|
|
}
|
|
} |