123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Data;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using System.Windows.Navigation;
- using System.Windows.Shapes;
- namespace N3
- {
- /// <summary>
- /// Логика взаимодействия для MainWindow.xaml
- /// </summary>
- public partial class MainWindow : Window
- {
- private List<float> numbs = new List<float>();
- private float thisNum = 0;
- private float answer = 0;
- private bool operationsFlag = true;
- private string lastOperation = "";
- private string NullException = "ошибка деления на 0";
- private string stringShow = "";
- private bool placeFlag = true;//флаг записи дробного числа
- private int placeNum = 0;//разряд числа после запятой
- public MainWindow()
- {
- InitializeComponent();
- }
- /// <summary>
- /// Запись чисел
- /// </summary>
- private void NumClick(object sender, RoutedEventArgs e)
- {
- Button btn = (Button)sender;
- int num = Convert.ToInt32(btn.Content);
- if (placeNum == 0)
- {
- thisNum = thisNum * 10 + num;
- }
- else
- {
- float place = (float)Math.Pow(0.10, placeNum);
- placeNum++;
- thisNum = thisNum + num * place;
- }
- stringShow += num;
- TBshow.Text = stringShow;
- }
- /// <summary>
- /// Выполнение деятельности, по типу удаления и очищения элементов
- /// </summary>
- private void ActClick(object sender, RoutedEventArgs e)
- {
- Button btn = (Button)sender;
- string act = (string)btn.Content;
- switch (act)
- {
- case "C": //очищение всего
- {
- stringShow = "";
- thisNum = 0;
- answer = 0;
- numbs = new List<float>();
- operationsFlag = true;
- lastOperation = "";
- placeFlag = true;
- placeNum = 0;
- TBshow.Text = stringShow;
- TBanswer.Text = "";
- TBanswer.Visibility = Visibility.Collapsed;
- break;
- }
- case ","://число с запятой
- {
- if (placeFlag)
- {
- placeFlag = false;
- placeNum = 1;
- stringShow += ",";
- TBshow.Text = stringShow;
- }
- break;
- }
- case "="://решение уравнения
- {
- if (lastOperation != "")
- {
- otherOperation("=");
- }
- else if (operationsFlag)
- {
- answer += thisNum;
- }
- TBanswer.Text = "= " + answer;
- TBanswer.Visibility = Visibility.Visible;
- break;
- }
- }
- }
- private void newNum()
- {
- numbs.Add(thisNum);
- thisNum = 0;
- placeFlag = true;
- placeNum = 0;
- }
- /// <summary>
- /// Операции над элементами
- /// </summary>
- private void OperationClick(object sender, RoutedEventArgs e)
- {
- Button btn = (Button)sender;
- string operation = (string)btn.Content;
- if (operationsFlag)
- {
- answer += thisNum;
- operationsFlag = false;
- lastOperation = "";
- }
- switch (operation)
- {
- case "√x":
- {
- if (!operationsFlag)
- {
- answer = OperationClass.extract(answer);
- stringShow = "√(" + stringShow + ")";
- TBshow.Text = stringShow;
- }
- operationsFlag = true;
- newNum();
- break;
- }
- case "x^2":
- {
- if (!operationsFlag)
- {
- answer = OperationClass.power(answer);
- stringShow = "(" + stringShow + ")^2";
- TBshow.Text = stringShow;
- }
- operationsFlag = true;
- newNum();
- break;
- }
- case "log10(x)":
- {
- if (!operationsFlag)
- {
- if (answer < 0)
- {
- TBanswer.Text = "Число не может быть отрицательным";
- }
- answer = OperationClass.logorifm(answer);
- stringShow = "log10(" + stringShow + ")";
- TBshow.Text = stringShow;
- operationsFlag = true;
- }
- newNum();
- break;
- }
- case "sin":
- {
- if (!operationsFlag)
- {
- answer = OperationClass.sinus(answer);
- stringShow = "sin(" + stringShow + ")";
- TBshow.Text = stringShow;
- }
- operationsFlag = true;
- newNum();
- break;
- }
- case "ctg":
- {
- if (!operationsFlag)
- {
- answer = OperationClass.cotangent(answer);
- stringShow = "ctg(" + stringShow + ")";
- TBshow.Text = stringShow;
- }
- operationsFlag = true;
- newNum();
- break;
- }
- default:
- {
- stringShow += operation;
- TBshow.Text = stringShow;
- newNum();
- break;
- }
- }
- if (lastOperation == "") lastOperation = operation;
- else otherOperation(operation);
- }
- private void otherOperation(string operation)
- {
- switch (lastOperation)
- {
- case "+":
- {
- if (!operationsFlag)
- {
- answer = OperationClass.sum(answer, thisNum);
- }
- newNum();
- break;
- }
- case "-":
- {
- if (!operationsFlag)
- {
- answer = OperationClass.difference(answer, thisNum);
- }
- newNum();
- break;
- }
- case "/":
- {
- if (!operationsFlag)
- {
- answer = OperationClass.quotient(answer, thisNum);
- }
- newNum();
- break;
- }
- case "*":
- {
- if (!operationsFlag)
- {
- answer = OperationClass.product(answer, thisNum);
- }
- newNum();
- break;
- }
- case "%":
- {
- if (!operationsFlag)
- {
- answer = OperationClass.procent(answer, thisNum);
- }
- newNum();
- break;
- }
- }
- if (operation != "=") lastOperation = operation;
- }
- }
- }
|