Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.58 KB | None | 0 0
  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.  
  16. namespace WpfApplication5
  17. {
  18. /// <summary>
  19. /// Interaction logic for MainWindow.xaml
  20. /// </summary>
  21. public partial class MainWindow : Window
  22. {
  23. Number a = new Number();
  24. Number b = new Number();
  25. Number d = new Number();
  26. public class Number
  27. {
  28. public string num;
  29. public int cc;
  30.  
  31. char[] symbols = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
  32.  
  33.  
  34. }
  35. public MainWindow()
  36. {
  37. InitializeComponent();
  38. }
  39.  
  40. public int Tonum(string num, int cc, int el)
  41. {
  42. switch (num[el])
  43. {
  44. case '0':
  45. return 0;
  46. break;
  47. case '1':
  48. return 1;
  49. break;
  50. case '2':
  51. return 2;
  52. break;
  53. case '3':
  54. return 3;
  55. break;
  56. case '4':
  57. return 4;
  58. break;
  59. case '5':
  60. return 5;
  61. break;
  62. case '6':
  63. return 6;
  64. break;
  65. case '7':
  66. return 7;
  67. break;
  68. case '8':
  69. return 8;
  70. break;
  71. case '9':
  72. return 9;
  73. break;
  74. case 'A':
  75. return 10;
  76. break;
  77. case 'B':
  78. return 11;
  79. break;
  80. case 'C':
  81. return 12;
  82. break;
  83. case 'D':
  84. return 13;
  85. break;
  86. case 'E':
  87. return 14;
  88. break;
  89. case 'F':
  90. return 15;
  91. break;
  92. default:
  93. return 10001;
  94. break;
  95. }
  96.  
  97. }
  98. public char Tochar(int s)
  99. {
  100. switch (s)
  101. {
  102. case 0:
  103. return '0';
  104. break;
  105. case 1:
  106. return '1';
  107. break;
  108. case 2:
  109. return '2';
  110. break;
  111. case 3:
  112. return '3';
  113. break;
  114. case 4:
  115. return '4';
  116. break;
  117. case 5:
  118. return '5';
  119. break;
  120. case 6:
  121. return '6';
  122. break;
  123. case 7:
  124. return '7';
  125. break;
  126. case 8:
  127. return '8';
  128. break;
  129. case 9:
  130. return '9';
  131. break;
  132. case 10:
  133. return 'A';
  134. break;
  135. case 11:
  136. return 'B';
  137. break;
  138. case 12:
  139. return 'C';
  140. break;
  141. case 13:
  142. return 'D';
  143. break;
  144. case 14:
  145. return 'E';
  146. break;
  147. case 15:
  148. return 'F';
  149. break;
  150. default:
  151. return 'X';
  152. break;
  153. }
  154. }
  155. public void Contoten(Number f)
  156. {
  157. int ccin = f.cc;
  158. string numin = f.num;
  159. int ccout = 10;
  160. string numout;
  161. int len = numin.Length;
  162. int sum = 0;
  163. for (int i = 0; i < len; i++)
  164. {
  165. sum = sum + Tonum(numin, ccin, i)*(int)Math.Pow((double)ccin, (double)(len - i - 1));
  166. }
  167.  
  168. d.cc = 10;
  169. d.num = sum.ToString();
  170. sum = 0;
  171.  
  172. label2.Content = d.cc.ToString();
  173. label3.Content = d.num;
  174. }
  175.  
  176. public void Confromten(Number f)
  177. {
  178. int ccin = f.cc;
  179. string numin = f.num;
  180. int ccout = int.Parse(ns2.Text);
  181. string numout;
  182. int len = numin.Length;
  183.  
  184. int[] mas = new int[100];
  185.  
  186. int num = int.Parse(numin);
  187. int s = 0;
  188. if (int.Parse(f.num) > ccout)
  189. {
  190. do
  191. {
  192. mas[s] = num % ccout;
  193. num = num / ccout;
  194. s++;
  195. } while (num >= ccout);
  196. }
  197. mas[s] = num;
  198.  
  199.  
  200. string ans = " ";
  201.  
  202. StringBuilder sb = new StringBuilder(ans);
  203.  
  204. for (int i = 0; i <= s; i++)
  205. {
  206. sb[i] = Tochar(mas[s - i]);
  207. }
  208.  
  209. ans = sb.ToString();
  210.  
  211. b.cc = ccout;
  212. b.num = ans;
  213. }
  214.  
  215. private void button_Click(object sender, RoutedEventArgs e)
  216. {
  217. try
  218. {
  219. Number number = new Number();
  220. number.cc = int.Parse(ns1.Text);
  221. if (number.cc < 2 || number.cc > 16) throw new Exception();
  222. number.num = n1.Text;
  223. if (int.Parse(number.num) < 0) throw new Exception();
  224. for (int i = 0; i < number.num.Length; i++)
  225. {
  226. if (Tonum(number.num, 0, i) >= number.cc) throw new Exception();
  227. }
  228.  
  229. Contoten(number);
  230. Confromten(d);
  231. n2.Text = b.num;
  232. ns2.Text = b.cc.ToString();
  233. }
  234. catch (Exception)
  235. {
  236. MessageBox.Show("Wrong data!");
  237. }
  238. }
  239. }
  240. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement