MainWindow.xaml.cs 10 KB

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