MainWindow.xaml.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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 numbersystem
  16. {
  17. /// <summary>
  18. /// Логика взаимодействия для MainWindow.xaml
  19. /// </summary>
  20. public partial class MainWindow : Window
  21. {
  22. private numbersystemContext _context;
  23. public MainWindow()
  24. {
  25. InitializeComponent();
  26. _context = new numbersystemContext();
  27. str.Focus();
  28. Load();
  29. }
  30. private void Load()
  31. {
  32. historyGrid.ItemsSource = _context.history.ToList();
  33. }
  34. // выбор системы счисления
  35. int ss = 0;
  36. private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  37. {
  38. ComboBox comboBox = (ComboBox)sender;
  39. ComboBoxItem selectedItem = (ComboBoxItem)comboBox.SelectedItem;
  40. if (Convert.ToString(selectedItem.Content) == "Двоичная")
  41. ss = 2;
  42. else if (Convert.ToString(selectedItem.Content) == "Восьмеричная")
  43. ss = 8;
  44. else if (Convert.ToString(selectedItem.Content) == "Шестнадцатиричная")
  45. ss = 16;
  46. str.Focus();
  47. }
  48. string expression; // переменная для выражения
  49. int res; // переменная для результата
  50. // ввод цифр, букв и знаков операций
  51. private void str_PreviewTextInput(object sender, TextCompositionEventArgs e)
  52. {
  53. // при нажатии на цифры или буквы
  54. if(Int32.TryParse(e.Text, out int n)
  55. || e.Text == "a" || e.Text == "b" || e.Text == "c" || e.Text == "d" || e.Text == "e" || e.Text == "f")
  56. {
  57. str.Text += e.Text;
  58. }
  59. // при нажатии на знаки операций
  60. if(e.Text == "+" || e.Text == "-" || e.Text == "/" || e.Text == "*" || e.Text == "%")
  61. {
  62. str.Text += " ";
  63. str.Text += e.Text;
  64. str.Text += " ";
  65. }
  66. // при нажатии на "равно"
  67. if(e.Text == "=")
  68. {
  69. if(ss == 0)
  70. {
  71. MessageBox.Show("Не выбрана система счисления");
  72. return;
  73. }
  74. // записываем выражение в переменную
  75. expression = str.Text;
  76. // разбиваем строку на массив
  77. string[] arr = expression.Split(' ');
  78. try
  79. {
  80. // перебираем массив и считаем высокоприоритетные операции
  81. for (int i = 0; i < arr.Length; i++)
  82. {
  83. // умножение
  84. if (arr[i] == "*")
  85. {
  86. if (arr[i - 1] == "_")
  87. {
  88. arr[i] = Convert.ToString(Convert.ToInt32(arr[i - 2], ss) * Convert.ToInt32(arr[i + 1], ss), ss);
  89. arr[i - 2] = "_";
  90. arr[i + 1] = "_";
  91. }
  92. else
  93. {
  94. arr[i] = Convert.ToString(Convert.ToInt32(arr[i - 1], ss) * Convert.ToInt32(arr[i + 1], ss), ss);
  95. arr[i - 1] = "_";
  96. arr[i + 1] = "_";
  97. }
  98. }
  99. // деление
  100. else if (arr[i] == "/")
  101. {
  102. if (arr[i - 1] == "_")
  103. {
  104. arr[i] = Convert.ToString(Convert.ToInt32(arr[i - 2], ss) / Convert.ToInt32(arr[i + 1], ss), ss);
  105. arr[i - 2] = "_";
  106. arr[i + 1] = "_";
  107. }
  108. else
  109. {
  110. arr[i] = Convert.ToString(Convert.ToInt32(arr[i - 1], ss) / Convert.ToInt32(arr[i + 1], ss), ss);
  111. arr[i - 1] = "_";
  112. arr[i + 1] = "_";
  113. }
  114. }
  115. // остаток от деления
  116. else if (arr[i] == "%")
  117. {
  118. if (arr[i - 1] == "_")
  119. {
  120. arr[i] = Convert.ToString(Convert.ToInt32(arr[i - 2], ss) % Convert.ToInt32(arr[i + 1], ss), ss);
  121. arr[i - 2] = "_";
  122. arr[i + 1] = "_";
  123. }
  124. else
  125. {
  126. arr[i] = Convert.ToString(Convert.ToInt32(arr[i - 1], ss) % Convert.ToInt32(arr[i + 1], ss), ss);
  127. arr[i - 1] = "_";
  128. arr[i + 1] = "_";
  129. }
  130. }
  131. }
  132. // узнаем размер будущего массива
  133. int l = 0;
  134. for (int i = 0; i < arr.Length; i++)
  135. {
  136. if (arr[i] != "_")
  137. {
  138. l++;
  139. }
  140. }
  141. // записать в новый массив результат предыдущего шага
  142. string[] arr2 = new string[l];
  143. int j = 0;
  144. for (int i = 0; i < arr.Length; i++)
  145. {
  146. if (arr[i] != "_")
  147. {
  148. arr2[j] = arr[i];
  149. j++;
  150. }
  151. }
  152. // первое число массива присваиваем переменной
  153. res = Convert.ToInt32(arr2[0], ss);
  154. // перебираем массив и считаем остальные операции
  155. for (int i = 1; i < arr2.Length; i++)
  156. {
  157. // сложение
  158. if (arr2[i] == "+")
  159. {
  160. res += Convert.ToInt32(arr2[i + 1], ss);
  161. }
  162. // вычитание
  163. else if (arr2[i] == "-")
  164. {
  165. res -= Convert.ToInt32(arr2[i + 1], ss);
  166. }
  167. }
  168. // выводим результат
  169. str.Text = Convert.ToString(res, ss);
  170. // добавляем вычисление в бд
  171. history record = new history()
  172. {
  173. inquiry = expression,
  174. result = Convert.ToString(res, ss),
  175. notation = Convert.ToString(ss)
  176. };
  177. _context.history.Add(record);
  178. _context.SaveChanges();
  179. Load();
  180. }
  181. catch (Exception ex)
  182. {
  183. MessageBox.Show(ex.ToString());
  184. }
  185. }
  186. }
  187. // кнопки стереть
  188. private void str_KeyDown(object sender, KeyEventArgs e)
  189. {
  190. if(e.Key == Key.Delete)
  191. {
  192. str.Text = "";
  193. }
  194. if(e.Key == Key.Back)
  195. {
  196. if (str.Text.Length > 0)
  197. {
  198. if (str.Text[str.Text.Length - 1] == ' ') //если последний символ это знак операции
  199. {
  200. for(int i = 0; i < 3; i++)
  201. str.Text = str.Text.Substring(0, str.Text.Length - 1);
  202. }
  203. else
  204. {
  205. str.Text = str.Text.Substring(0, str.Text.Length - 1);
  206. }
  207. }
  208. }
  209. }
  210. }
  211. }