MainWindow.xaml.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using System.Windows.Data;
  9. using System.Windows.Documents;
  10. using System.Windows.Input;
  11. using System.Windows.Media;
  12. using System.Windows.Media.Imaging;
  13. using System.Windows.Navigation;
  14. using System.Windows.Shapes;
  15. namespace N3
  16. {
  17. /// <summary>
  18. /// Логика взаимодействия для MainWindow.xaml
  19. /// </summary>
  20. public partial class MainWindow : Window
  21. {
  22. private List<float> numbs = new List<float>();
  23. private float thisNum = 0;
  24. private float answer = 0;
  25. private bool operationsFlag = true;
  26. private string lastOperation = "";
  27. private string NullException = "ошибка деления на 0";
  28. private string stringShow = "";
  29. private bool placeFlag = true;//флаг записи дробного числа
  30. private int placeNum = 0;//разряд числа после запятой
  31. public MainWindow()
  32. {
  33. InitializeComponent();
  34. }
  35. /// <summary>
  36. /// Запись чисел
  37. /// </summary>
  38. private void NumClick(object sender, RoutedEventArgs e)
  39. {
  40. Button btn = (Button)sender;
  41. int num = Convert.ToInt32(btn.Content);
  42. if (placeNum == 0)
  43. {
  44. thisNum = thisNum * 10 + num;
  45. }
  46. else
  47. {
  48. float place = (float)Math.Pow(0.10, placeNum);
  49. placeNum++;
  50. thisNum = thisNum + num * place;
  51. }
  52. stringShow += num;
  53. TBshow.Text = stringShow;
  54. }
  55. /// <summary>
  56. /// Выполнение деятельности, по типу удаления и очищения элементов
  57. /// </summary>
  58. private void ActClick(object sender, RoutedEventArgs e)
  59. {
  60. Button btn = (Button)sender;
  61. string act = (string)btn.Content;
  62. switch (act)
  63. {
  64. case "C": //очищение всего
  65. {
  66. stringShow = "";
  67. thisNum = 0;
  68. answer = 0;
  69. numbs = new List<float>();
  70. operationsFlag = true;
  71. lastOperation = "";
  72. placeFlag = true;
  73. placeNum = 0;
  74. TBshow.Text = stringShow;
  75. TBanswer.Text = "";
  76. TBanswer.Visibility = Visibility.Collapsed;
  77. break;
  78. }
  79. case ","://число с запятой
  80. {
  81. if (placeFlag)
  82. {
  83. placeFlag = false;
  84. placeNum = 1;
  85. stringShow += ",";
  86. TBshow.Text = stringShow;
  87. }
  88. break;
  89. }
  90. case "="://решение уравнения
  91. {
  92. if (lastOperation != "")
  93. {
  94. otherOperation("=");
  95. }
  96. else if (operationsFlag)
  97. {
  98. answer += thisNum;
  99. }
  100. TBanswer.Text = "= " + answer;
  101. TBanswer.Visibility = Visibility.Visible;
  102. break;
  103. }
  104. }
  105. }
  106. private void newNum()
  107. {
  108. numbs.Add(thisNum);
  109. thisNum = 0;
  110. placeFlag = true;
  111. placeNum = 0;
  112. }
  113. /// <summary>
  114. /// Операции над элементами
  115. /// </summary>
  116. private void OperationClick(object sender, RoutedEventArgs e)
  117. {
  118. Button btn = (Button)sender;
  119. string operation = (string)btn.Content;
  120. if (operationsFlag)
  121. {
  122. answer += thisNum;
  123. operationsFlag = false;
  124. lastOperation = "";
  125. }
  126. switch (operation)
  127. {
  128. case "√x":
  129. {
  130. if (!operationsFlag)
  131. {
  132. answer = OperationClass.extract(answer);
  133. stringShow = "√(" + stringShow + ")";
  134. TBshow.Text = stringShow;
  135. }
  136. operationsFlag = true;
  137. newNum();
  138. break;
  139. }
  140. case "x^2":
  141. {
  142. if (!operationsFlag)
  143. {
  144. answer = OperationClass.power(answer);
  145. stringShow = "(" + stringShow + ")^2";
  146. TBshow.Text = stringShow;
  147. }
  148. operationsFlag = true;
  149. newNum();
  150. break;
  151. }
  152. case "log10(x)":
  153. {
  154. if (!operationsFlag)
  155. {
  156. if (answer < 0)
  157. {
  158. TBanswer.Text = "Число не может быть отрицательным";
  159. }
  160. answer = OperationClass.logorifm(answer);
  161. stringShow = "log10(" + stringShow + ")";
  162. TBshow.Text = stringShow;
  163. operationsFlag = true;
  164. }
  165. newNum();
  166. break;
  167. }
  168. case "sin":
  169. {
  170. if (!operationsFlag)
  171. {
  172. answer = OperationClass.sinus(answer);
  173. stringShow = "sin(" + stringShow + ")";
  174. TBshow.Text = stringShow;
  175. }
  176. operationsFlag = true;
  177. newNum();
  178. break;
  179. }
  180. case "ctg":
  181. {
  182. if (!operationsFlag)
  183. {
  184. answer = OperationClass.cotangent(answer);
  185. stringShow = "ctg(" + stringShow + ")";
  186. TBshow.Text = stringShow;
  187. }
  188. operationsFlag = true;
  189. newNum();
  190. break;
  191. }
  192. default:
  193. {
  194. stringShow += operation;
  195. TBshow.Text = stringShow;
  196. newNum();
  197. break;
  198. }
  199. }
  200. if (lastOperation == "") lastOperation = operation;
  201. else otherOperation(operation);
  202. }
  203. private void otherOperation(string operation)
  204. {
  205. switch (lastOperation)
  206. {
  207. case "+":
  208. {
  209. if (!operationsFlag)
  210. {
  211. answer = OperationClass.sum(answer, thisNum);
  212. }
  213. newNum();
  214. break;
  215. }
  216. case "-":
  217. {
  218. if (!operationsFlag)
  219. {
  220. answer = OperationClass.difference(answer, thisNum);
  221. }
  222. newNum();
  223. break;
  224. }
  225. case "/":
  226. {
  227. if (!operationsFlag)
  228. {
  229. answer = OperationClass.quotient(answer, thisNum);
  230. }
  231. newNum();
  232. break;
  233. }
  234. case "*":
  235. {
  236. if (!operationsFlag)
  237. {
  238. answer = OperationClass.product(answer, thisNum);
  239. }
  240. newNum();
  241. break;
  242. }
  243. case "%":
  244. {
  245. if (!operationsFlag)
  246. {
  247. answer = OperationClass.procent(answer, thisNum);
  248. }
  249. newNum();
  250. break;
  251. }
  252. }
  253. if (operation != "=") lastOperation = operation;
  254. }
  255. }
  256. }