Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2014
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.32 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10.  
  11. namespace WindowsFormsApplication2
  12. {
  13. public partial class Form1 : Form
  14. {
  15. public Form1()
  16. {
  17. InitializeComponent();
  18. }
  19.  
  20. private void btnZetOm_Click(object sender, EventArgs e)
  21. {
  22. if(rdbNaarFloat.Checked)
  23. {
  24. string []getallen = new string[2];
  25. if (txtInvoer.Text.Contains(','))
  26. {
  27. getallen = txtInvoer.Text.Split(',');
  28. }
  29. else
  30. {
  31. getallen[0] = txtInvoer.Text;
  32. getallen[1] = "0";
  33. }
  34.  
  35.  
  36.  
  37.  
  38. int voorKomma = Math.Abs(int.Parse(getallen[0]));
  39.  
  40. int remainder;
  41. string result = string.Empty;
  42. while (voorKomma > 0){
  43. remainder = voorKomma % 2;
  44. voorKomma /= 2;
  45. result = remainder.ToString() + result;
  46. }
  47.  
  48. if (result == "")
  49. txtGeheel.Text = "0";
  50. else
  51. txtGeheel.Text = result;
  52.  
  53.  
  54. int lengte = 0;
  55. int tussengetal = 0;
  56. if (getallen[1] != "")
  57. { tussengetal = int.Parse(getallen[1]); }
  58.  
  59. bool Eind5 = false;
  60. string achterKomma = null;
  61. int testa = 0;
  62.  
  63. while (tussengetal > 0) {
  64.  
  65. Eind5 = false;
  66. lengte = Convert.ToString(tussengetal).Length;
  67. testa = Convert.ToString(tussengetal).Length;
  68.  
  69. if (Convert.ToString(tussengetal).Substring(0, 1) == Convert.ToString("5"))
  70. {
  71. Eind5 = true;
  72. }
  73.  
  74.  
  75.  
  76. tussengetal = tussengetal * 2;
  77. if (Eind5 == true)
  78. {
  79. int testb = Convert.ToString(tussengetal).Length;
  80. if ((testb + 1) > lengte)
  81. {
  82.  
  83. achterKomma = achterKomma + "1";
  84. int testc = Convert.ToString(tussengetal).Length;
  85.  
  86. tussengetal = Convert.ToInt32(Convert.ToString(tussengetal).Substring(1, testc - 1));
  87. }
  88. else
  89. achterKomma = achterKomma + "0";
  90. }
  91. else
  92. {
  93. if ((Convert.ToString(tussengetal).Length) > lengte)
  94. {
  95. achterKomma = achterKomma + "1";
  96. int testc = Convert.ToString(tussengetal).Length;
  97. tussengetal = Convert.ToInt32(Convert.ToString(tussengetal).Substring(1, testc - 1));
  98. }
  99. else
  100. achterKomma = achterKomma + "0";
  101. }
  102.  
  103.  
  104.  
  105. if (achterKomma.Length == 8)
  106. {
  107. tussengetal = 0;
  108. }
  109.  
  110.  
  111.  
  112. }
  113.  
  114.  
  115. txtAchterKomma.Text = achterKomma;
  116. int exponentDecimaal = 0;
  117.  
  118. string volledigGetal = result + achterKomma;
  119. for (int i = 0; i < volledigGetal.Length;i++ )
  120. {
  121. if (volledigGetal.Substring(i, 1) == "1")
  122. {
  123. volledigGetal = volledigGetal.Substring(i + 1 , volledigGetal.Length - i - 1);
  124.  
  125. if (result == "")
  126. {
  127. exponentDecimaal = -i - 1 + 127;
  128. }
  129. else
  130. {
  131. exponentDecimaal = 127 + result.Length - 1;
  132. }
  133. i = 100;
  134. }
  135. }
  136.  
  137.  
  138.  
  139.  
  140.  
  141.  
  142.  
  143. string exponent = null;
  144. remainder = 0;
  145. result = string.Empty;
  146. while (exponentDecimaal > 0)
  147. {
  148. remainder = exponentDecimaal % 2;
  149. exponentDecimaal /= 2;
  150. exponent = remainder.ToString() + exponent;
  151. }
  152. if(exponent.Length < 8)
  153. {
  154. for(int i = 0; i < 8 - exponent.Length; i++ )
  155. {
  156. exponent = "0" + exponent;
  157. }
  158. }
  159. txtExponent.Text = exponent;
  160.  
  161.  
  162. int posNeg = 0;
  163. if (Convert.ToInt32(getallen[0]) < 0)
  164. posNeg = 1;
  165. txtTekenbit.Text = Convert.ToString(posNeg);
  166. for(int i = volledigGetal.Length; i < 23; i++)
  167. {
  168. volledigGetal = volledigGetal + "0";
  169. }
  170. txtMantisse.Text = volledigGetal;
  171.  
  172.  
  173.  
  174. txtFloatingPoint.Text = Convert.ToString(posNeg + exponent + volledigGetal);
  175.  
  176.  
  177.  
  178. }
  179. if (rdbNaarDec.Checked)
  180. {
  181. string floatInvoer = txtInvoer.Text;
  182. string posNeg = floatInvoer.Substring(0, 1);
  183. int exponenentUit = Convert.ToInt32(floatInvoer.Substring(1, 8), 2) - 127;
  184. string MantisseUitg = "1" + floatInvoer.Substring(9, 23);
  185.  
  186. txtMantisse.Text = floatInvoer.Substring(9, 23);
  187. txtExponent.Text = floatInvoer.Substring(1, 8);
  188. txtTekenbit.Text = posNeg;
  189. string geheel;
  190.  
  191. if (exponenentUit < 0)
  192. {
  193. geheel = "0";
  194. }
  195. else
  196. {
  197. geheel = MantisseUitg.Substring(0, exponenentUit + 1);
  198. }
  199. string achterKomma = null; ;
  200. if(exponenentUit > 0)
  201. achterKomma = MantisseUitg.Substring(exponenentUit + 1, MantisseUitg.Length - exponenentUit - 1);
  202. else
  203. {
  204.  
  205. achterKomma = MantisseUitg;
  206.  
  207. for (int i = 0; i < Math.Abs(exponenentUit) - 1; i++)
  208. {
  209. achterKomma = "0" + achterKomma;
  210. }
  211. // achterKomma = MantisseUitg.Substring(Math.Abs(exponenentUit) + 1, MantisseUitg.Length - exponenentUit - 1);
  212.  
  213. }
  214.  
  215.  
  216.  
  217.  
  218. txtGeheel.Text = geheel;
  219.  
  220.  
  221. int achterKommaInt = 0;
  222. int multiplier = 1;
  223. int HoudLengte = achterKomma.Length;
  224. for (int i = 0; i < HoudLengte; i++ )
  225. {
  226.  
  227. if (achterKomma.Substring(achterKomma.Length - 1, 1) == "0")
  228. achterKomma = achterKomma.Substring(0, achterKomma.Length - 1);
  229. else
  230. i = 100;
  231.  
  232. }
  233. txtAchterKomma.Text = achterKomma;
  234.  
  235. int Delergetal = 1;
  236. for (int i = 0; i < achterKomma.Length; i++) {
  237. Delergetal = Convert.ToInt32(Convert.ToString(Delergetal) + "0");
  238. }
  239.  
  240.  
  241. for (int i = 0; i < achterKomma.Length; i++)
  242. {
  243. multiplier = multiplier * 2;
  244. if (achterKomma.Substring(i, 1) == "1")
  245. {
  246.  
  247.  
  248.  
  249.  
  250. achterKommaInt += Delergetal / multiplier;
  251.  
  252.  
  253. }
  254. }
  255.  
  256. int voorKomma = Convert.ToInt32(geheel, 2);
  257. string signed = null;
  258.  
  259. if (posNeg == "0")
  260. signed = "";
  261. else
  262. signed = "-";
  263.  
  264. txtFloatingPoint.Text = signed + Convert.ToString(voorKomma) + ',' + Convert.ToString(achterKommaInt);
  265.  
  266.  
  267.  
  268. }
  269. }
  270. }
  271. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement