Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- TType.cs
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace Сalculator_RGZ
- {
- //интерфейс, определяющий свойства кастомных типов данных
- public interface TType<T>
- {
- T Sqr();
- T Rev();
- T Add(T b);
- T Dvd(T b);
- T Mul(T b);
- T Sub(T b);
- }
- }
- TFrac.cs
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace Сalculator_RGZ
- {
- public class TFrac: TType<TFrac>
- {
- public TFrac()
- {
- Num = 0;
- Den = 1;
- }
- public int Num { get; private set; }
- public int Den { get; private set; }
- public TFrac(int Numerator, int Deninator)
- {
- if (Deninator == 0)
- {
- throw new ArgumentException("Deninator cannot be zero.", nameof(Deninator));
- }
- Num = Numerator;
- Den = Deninator;
- reduceTFrac();
- }
- public TFrac(string value)
- {
- string temp = value;
- int _Num = Int32.Parse(temp.Split('/')[0]);
- int _Den = Int32.Parse(temp.Split('/')[1]);
- if (_Den == 0)
- {
- throw new ArgumentException("Deninator cannot be zero.", nameof(_Den));
- }
- Num = _Num;
- Den = _Den;
- reduceTFrac();
- }
- //общий делитель для чеслителя и знаменателя
- private int commonDiv()
- {
- if (Math.Abs(Num) == Math.Abs(Den))
- {
- if (Num != 0 && Math.Abs(Den) != 1)
- {
- return Math.Abs(Num);
- }
- else
- {
- return 0;
- }
- }
- int n = Math.Abs(Num) > Math.Abs(Den) ? Math.Abs(Num) : Math.Abs(Den);
- while (n > 1)
- {
- if (Math.Abs(Num) % n == 0 && Math.Abs(Den) % n == 0)
- {
- return n;
- }
- n--;
- }
- return 0;
- }
- //операция сокращения дроби
- private void reduceTFrac()
- {
- int cD = commonDiv();
- cD *= Den < 0 ? -1 : 1;
- while (cD != 0)
- {
- Num /= cD;
- Den /= cD;
- cD = commonDiv();
- }
- }
- public TFrac Sqr()
- {
- return new TFrac(Num * Num, Den * Den);
- }
- public TFrac Rev()
- {
- if (Num == 0)
- throw new Exception("Num cannot be zero in Rev.");
- return new TFrac(Den, Num);
- }
- public TFrac Negative(TFrac a) => new TFrac(-a.Num, a.Den);
- public TFrac Add(TFrac b)
- {
- TFrac res = new TFrac(this.Num * b.Den + b.Num * this.Den, this.Den * b.Den);
- res.reduceTFrac();
- return res;
- }
- public TFrac Sub(TFrac b)
- => this.Add(this.Negative(b));
- public TFrac Mul(TFrac b)
- {
- TFrac res = new TFrac(this.Num * b.Num, this.Den * b.Den);
- res.reduceTFrac();
- return res;
- }
- public TFrac Dvd(TFrac b)
- {
- if (b.Num == 0)
- {
- throw new DivideByZeroException();
- }
- TFrac res = new TFrac(this.Num * b.Den, this.Den * b.Num); ;
- res.reduceTFrac();
- return res;
- }
- public TFrac Copy()
- {
- TFrac temp = new TFrac(Num, Den);
- return temp;
- }
- public override string ToString() => $"{Num}/{Den}";
- public static bool operator ==(TFrac arg1, TFrac arg2)
- {
- if (arg1.Num == 0 && arg2.Num == 0)
- {
- return true;
- }
- else if (arg1.Den == arg2.Den && arg1.Num == arg2.Num)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- public static bool operator !=(TFrac arg1, TFrac arg2)
- {
- if (arg1.Num == 0 && arg2.Num == 0)
- {
- return false;
- }
- else if (arg1.Den == arg2.Den && arg1.Num == arg2.Num)
- {
- return false;
- }
- else
- {
- return true;
- }
- }
- public static bool operator >(TFrac arg1, TFrac arg2)
- {
- int Num1 = arg1.Num * arg2.Den;
- int Num2 = arg2.Num * arg1.Den;
- if (Num1 > Num2)
- {
- return true;
- }
- return false;
- }
- public static bool operator <(TFrac arg1, TFrac arg2)
- {
- int Num1 = arg1.Num * arg2.Den;
- int Num2 = arg2.Num * arg1.Den;
- if (Num1 < Num2)
- {
- return true;
- }
- return false;
- }
- }
- }
- Proc.cs
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace Сalculator_RGZ
- {
- public enum TOprtn { None, Add, Sub, Mul, Dvd };
- public enum TFunc { Rev, Sqr };
- public class Proc<T> where T:TType<T>, new()
- {
- public T Lop_Res { get; set; }
- public T Rop { get; set; }
- public TOprtn Operation { get; set; }
- public Proc(T Lop_Res,T Rop)
- {
- this.Lop_Res = Lop_Res;
- this.Rop = Rop;
- Operation = TOprtn.None;
- }
- public Proc()
- {
- ProcClear();
- }
- public void ProcClear()
- {
- Lop_Res = default(T);
- Rop = default(T);
- Operation = TOprtn.None;
- }
- public void OprtnClear()
- {
- Operation = TOprtn.None;
- }
- //запуск операций
- public void OprtnRun()
- {
- switch (Operation)
- {
- case TOprtn.None:
- break;
- case TOprtn.Add:
- Lop_Res = Lop_Res.Add(Rop);
- break;
- case TOprtn.Sub:
- Lop_Res = Lop_Res.Sub(Rop);
- break;
- case TOprtn.Mul:
- Lop_Res = Lop_Res.Mul(Rop);
- break;
- case TOprtn.Dvd:
- Lop_Res = Lop_Res.Dvd(Rop);
- break;
- default:
- break;
- }
- }
- //запуск функция
- public void funcRun(TFunc Func)
- {
- switch (Func)
- {
- case TFunc.Rev:
- Rop = Rop.Rev();
- break;
- case TFunc.Sqr:
- Rop = Rop.Sqr();
- break;
- default:
- break;
- }
- }
- }
- }
- Memory.cs
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace Сalculator_RGZ
- {
- public class Memory<T> where T : TType<T>, new()
- {
- private const int _OffT = 0;
- private const int _OnT = 1;
- private T ValueT;
- private int StateT;
- public Memory(T E)
- {
- ValueT = E;
- StateT = _OffT;
- }
- public Memory()
- {
- ValueT = new T();
- StateT = _OffT;
- }
- //установить значение
- public void Write(T E)
- {
- ValueT = E;
- StateT = _OnT;
- }
- //получить значение
- public T Get()
- {
- StateT = _OnT;
- return ValueT;
- }
- //добавить значение к памяти
- public void Add(T E)
- {
- ValueT = ValueT.Add(E);
- StateT = _OnT;
- }
- //очистить память
- public void Clear()
- {
- ValueT = new T();
- StateT = _OffT;
- }
- public string ReadStateMemory()
- {
- return StateT.ToString();
- }
- public T ReadNumber()
- {
- return ValueT;
- }
- }
- }
- FracControl.cs
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace Сalculator_RGZ
- {
- public enum State { Rewrite, Result, Work };
- public class FracControl
- {
- FracEditor fracEditor = new FracEditor();
- Proc<TFrac> proc = new Proc<TFrac>();
- Memory<TFrac> memory = new Memory<TFrac>();
- public string insertVal(string val)
- {
- fracEditor.WriteString(val);
- return fracEditor.ToString();
- }
- //вспомогательная функция
- private string getOper(int i)
- {
- switch (i)
- {
- case 1: return "+";
- case 2: return "-";
- case 3: return "*";
- case 4: return "/";
- }
- return "non";
- }
- public override string ToString()
- {
- return fracEditor.ToString();
- }
- public State St { get; set; }
- //предает управление редактору/процессору/памяти
- public string DoCmnd(int j)
- {
- if (j >= 0 && j <= 20)
- {
- if(St == State.Work)
- {
- St = State.Rewrite;
- fracEditor.Clear();
- }
- if (St == State.Result)
- {
- St = State.Rewrite;
- fracEditor.Clear();
- proc.ProcClear();
- }
- return fracEditor.Edit(j);
- }
- if (j >= 22 && j <= 25)
- {
- if (St == State.Work)
- {
- return fracEditor.ToString();
- }
- if(St == State.Rewrite)
- {
- if(proc.Operation == TOprtn.None)
- {
- proc.Lop_Res = new TFrac(fracEditor.ToString());
- }
- else
- {
- proc.Rop = new TFrac(fracEditor.ToString());
- string his = proc.Lop_Res.ToString() + getOper((int)proc.Operation) + proc.Rop.ToString() + "=";
- proc.OprtnRun();
- his += proc.Lop_Res.ToString();
- History.L.Add(his);
- fracEditor.WriteString(proc.Lop_Res.ToString());
- }
- }
- switch (j)
- {
- case 22: proc.Operation = TOprtn.Add; break;
- case 23: proc.Operation = TOprtn.Sub; break;
- case 24: proc.Operation = TOprtn.Mul; break;
- case 25: proc.Operation = TOprtn.Dvd; break;
- }
- St = State.Work;
- return fracEditor.ToString();
- }
- if(j == 26 || j == 27)
- {
- string his = fracEditor.ToString();
- proc.Rop = new TFrac(fracEditor.ToString());
- if(j == 27)
- {
- proc.funcRun(TFunc.Rev);
- his += " Rev = " + proc.Rop.ToString();
- History.L.Add(his);
- }
- else
- {
- proc.funcRun(TFunc.Sqr);
- his += " Sqr = " + proc.Rop.ToString();
- History.L.Add(his);
- }
- if (EqualityComparer<TFrac>.Default.Equals(proc.Lop_Res, default(TFrac)))
- {
- proc.Lop_Res = proc.Rop;
- }
- fracEditor.WriteString(proc.Rop.ToString());
- return fracEditor.ToString();
- }
- if (j == 28)
- {
- if(proc.Operation == TOprtn.None)
- {
- St = State.Result;
- return fracEditor.ToString();
- }
- if(St != State.Result)
- {
- proc.Rop = new TFrac(fracEditor.ToString());
- }
- St = State.Result;
- string his = proc.Lop_Res.ToString() + getOper((int)proc.Operation) + proc.Rop.ToString() + "=";
- proc.OprtnRun();
- his += proc.Lop_Res.ToString();
- History.L.Add(his);
- fracEditor.WriteString(proc.Lop_Res.ToString());
- return fracEditor.ToString();
- }
- if (j == 29)
- {
- proc.ProcClear();
- return fracEditor.Edit(18);
- }
- if(j == 30)
- {
- memory.Clear();
- return fracEditor.ToString();
- }
- if (j == 31)
- {
- memory.Write(new TFrac(fracEditor.ToString()));
- return fracEditor.ToString();
- }
- if (j == 32)
- {
- fracEditor.WriteString(memory.Get().ToString());
- return fracEditor.ToString();
- }
- if (j == 33)
- {
- memory.Add(new TFrac(fracEditor.ToString()));
- return fracEditor.ToString();
- }
- return fracEditor.ToString();
- }
- }
- }
- FracEditor.cs
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Text.RegularExpressions;
- namespace Сalculator_RGZ
- {
- public enum PartToEdit { TOP, BOTTOM };
- public class FracEditor
- {
- private const string strSeparator = "/";
- private const string strZero = "0/1";
- public PartToEdit Mode { private get; set; }
- private string strFraction;
- public FracEditor() {
- Mode = PartToEdit.TOP;
- strFraction = strZero;
- }
- public bool IsZero()
- {
- return strFraction == strZero || strFraction == ("-" + strZero);
- }
- public string AddSign()
- {
- if (strFraction[0] == '-')
- strFraction = strFraction.Substring(1);
- else strFraction = '-' + strFraction;
- return strFraction;
- }
- public string Clear()
- {
- Mode = PartToEdit.TOP;
- strFraction = strZero;
- return strFraction;
- }
- public override string ToString()
- {
- return strFraction;
- }
- //определяет соответствие формата
- public void WriteString(string a)
- {
- Regex rg = new Regex("-?([0-9]+)/[1-9][0-9]*");
- if (rg.IsMatch(a))
- strFraction = a;
- else
- throw new Exception("Wrong value");
- }
- //добавляет числовой символ
- public string AddNumber(int a)
- {
- if (a < 0 || a > 10)
- throw new Exception("Wrong value");
- int indexSep = strFraction.IndexOf(strSeparator);
- if (Mode == PartToEdit.TOP)
- {
- if (strFraction[0] == '0')
- strFraction = strFraction.Remove(0, 1).Insert(0, a.ToString());
- else if (strFraction[0] == '-' && strFraction[1] == '0')
- strFraction = strFraction.Remove(1, 1).Insert(1, a.ToString());
- else
- {
- if (indexSep > 0)
- strFraction = strFraction.Insert(indexSep,a.ToString());
- else strFraction += a.ToString();
- }
- }
- else
- {
- if(strFraction.Length < indexSep + 1)
- if (strFraction[indexSep + 1] == '0')
- strFraction = strFraction.Remove(indexSep + 1, 1);
- strFraction += a.ToString();
- }
- return strFraction;
- }
- public string AddZero()
- {
- return AddNumber(0);
- }
- public string DeleteCharacter()
- {
- if (Mode == PartToEdit.TOP)
- {
- if (strFraction[0] == '0' || (strFraction[0] == '-' && strFraction[1] == '0'))
- return strFraction;
- else strFraction = strFraction.Remove(strFraction.IndexOf(strSeparator) - 1, 1);
- if (strFraction[0] == '/' || (strFraction[0] == '-' && strFraction[1] == '/'))
- strFraction = strFraction.Insert(strFraction.IndexOf(strSeparator),"0");
- }
- else
- {
- if (strFraction.IndexOf(strSeparator) + 1 != strFraction.Length)
- strFraction = strFraction.Remove(strFraction.Length - 1);
- }
- return strFraction;
- }
- //основная функция, определяющая операцию изменения содержимым
- public string Edit(int a, string f = "")
- {
- if (a >= 0 && a <= 10)
- {
- return AddNumber(a);
- }
- if(a == 16)
- {
- return AddSign();
- }
- if (a == 17)
- {
- return DeleteCharacter();
- }
- if (a == 18)
- {
- return Clear();
- }
- if(a == 19)
- {
- Mode = Mode == PartToEdit.TOP ? PartToEdit.BOTTOM : PartToEdit.TOP;
- }
- if (a == 21)
- {
- WriteString(f);
- }
- return ToString();
- }
- }
- }
- History.cs
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace Сalculator_RGZ
- {
- public static class History
- {
- public static List<string> L;
- static History()
- {
- L = new List<string>();
- }
- }
- }
- Form1.cs
- 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 System.Windows;
- namespace Сalculator_RGZ
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- myControl = new FracControl();
- textBox1.Text = myControl.ToString();
- button24.Text = "/";
- button24.Show();
- }
- FracControl myControl;
- //обраотчик нажатия кнопок
- private void button1_Click(object sender, EventArgs e)
- {
- Button but = (Button)sender;
- int j = Convert.ToInt16(but.Tag.ToString());
- DoCmnd(j);
- }
- //Выполнить команду.
- private void DoCmnd(int j)
- {
- if(j < 35)
- textBox1.Text = myControl.DoCmnd(j);
- }
- private void fullToolStripMenuItem_Click(object sender, EventArgs e)
- {
- button24.Show();
- }
- private void restrictionToolStripMenuItem_Click(object sender, EventArgs e)
- {
- button24.Hide();
- }
- private void AboutToolStripMenuItem_Click(object sender, EventArgs e)
- {
- AboutBox1 a = new AboutBox1();
- a.Show();
- }
- private void HistoryToolStripMenuItem1_Click(object sender, EventArgs e)
- {
- Form2 history = new Form2();
- history.Show();
- if (History.L.Count() == 0)
- {
- history.textBox1.AppendText("История пуста");
- return;
- }
- //Ообразить историю.
- for (int i = 0; i < History.L.Count(); i++)
- {
- history.textBox1.AppendText(History.L[i] + Environment.NewLine);
- }
- }
- //обработчик нажатия клавиш
- private void Form1_KeyPress(object sender, KeyPressEventArgs e)
- {
- int i = -1;
- if (e.KeyChar >= 'A' && e.KeyChar <= 'F')
- i = (int)e.KeyChar - 'A' + 10;
- if (e.KeyChar >= 'a' && e.KeyChar <= 'f')
- i = (int)e.KeyChar - 'a' + 10;
- if (e.KeyChar >= '0' && e.KeyChar <= '9')
- i = (int)e.KeyChar - '0';
- if (e.KeyChar == '-')
- i = 23;
- if (e.KeyChar == '+')
- i = 22;
- if (e.KeyChar == '*')
- i = 24;
- if (e.KeyChar == '/')
- i = 25;
- if (e.KeyChar == '=')
- i = 28;
- if ((i < 10) || (i >= 16))
- DoCmnd(i);
- }
- //Обработка клавиш управления.
- private void Form1_KeyDown(object sender, KeyEventArgs e)
- {
- if (e.KeyCode == Keys.Delete)
- //Клавиша Delete.
- DoCmnd(17);
- }
- //работа с буфером обмена
- private void copyToolStripMenuItem_Click(object sender, EventArgs e)
- {
- Clipboard.SetText(myControl.ToString());
- }
- //работа с буфером обмена
- private void pasteToolStripMenuItem_Click(object sender, EventArgs e)
- {
- textBox1.Text = myControl.insertVal(Clipboard.GetText());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement