Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace WindowsFormsApplication2
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- private void btnZetOm_Click(object sender, EventArgs e)
- {
- if(rdbNaarFloat.Checked)
- {
- string []getallen = new string[2];
- if (txtInvoer.Text.Contains(','))
- {
- getallen = txtInvoer.Text.Split(',');
- }
- else
- {
- getallen[0] = txtInvoer.Text;
- getallen[1] = "0";
- }
- int voorKomma = Math.Abs(int.Parse(getallen[0]));
- int remainder;
- string result = string.Empty;
- while (voorKomma > 0){
- remainder = voorKomma % 2;
- voorKomma /= 2;
- result = remainder.ToString() + result;
- }
- if (result == "")
- txtGeheel.Text = "0";
- else
- txtGeheel.Text = result;
- int lengte = 0;
- int tussengetal = 0;
- if (getallen[1] != "")
- { tussengetal = int.Parse(getallen[1]); }
- bool Eind5 = false;
- string achterKomma = null;
- int testa = 0;
- while (tussengetal > 0) {
- Eind5 = false;
- lengte = Convert.ToString(tussengetal).Length;
- testa = Convert.ToString(tussengetal).Length;
- if (Convert.ToString(tussengetal).Substring(0, 1) == Convert.ToString("5"))
- {
- Eind5 = true;
- }
- tussengetal = tussengetal * 2;
- if (Eind5 == true)
- {
- int testb = Convert.ToString(tussengetal).Length;
- if ((testb + 1) > lengte)
- {
- achterKomma = achterKomma + "1";
- int testc = Convert.ToString(tussengetal).Length;
- tussengetal = Convert.ToInt32(Convert.ToString(tussengetal).Substring(1, testc - 1));
- }
- else
- achterKomma = achterKomma + "0";
- }
- else
- {
- if ((Convert.ToString(tussengetal).Length) > lengte)
- {
- achterKomma = achterKomma + "1";
- int testc = Convert.ToString(tussengetal).Length;
- tussengetal = Convert.ToInt32(Convert.ToString(tussengetal).Substring(1, testc - 1));
- }
- else
- achterKomma = achterKomma + "0";
- }
- if (achterKomma.Length == 8)
- {
- tussengetal = 0;
- }
- }
- txtAchterKomma.Text = achterKomma;
- int exponentDecimaal = 0;
- string volledigGetal = result + achterKomma;
- for (int i = 0; i < volledigGetal.Length;i++ )
- {
- if (volledigGetal.Substring(i, 1) == "1")
- {
- volledigGetal = volledigGetal.Substring(i + 1 , volledigGetal.Length - i - 1);
- if (result == "")
- {
- exponentDecimaal = -i - 1 + 127;
- }
- else
- {
- exponentDecimaal = 127 + result.Length - 1;
- }
- i = 100;
- }
- }
- string exponent = null;
- remainder = 0;
- result = string.Empty;
- while (exponentDecimaal > 0)
- {
- remainder = exponentDecimaal % 2;
- exponentDecimaal /= 2;
- exponent = remainder.ToString() + exponent;
- }
- if(exponent.Length < 8)
- {
- for(int i = 0; i < 8 - exponent.Length; i++ )
- {
- exponent = "0" + exponent;
- }
- }
- txtExponent.Text = exponent;
- int posNeg = 0;
- if (Convert.ToInt32(getallen[0]) < 0)
- posNeg = 1;
- txtTekenbit.Text = Convert.ToString(posNeg);
- for(int i = volledigGetal.Length; i < 23; i++)
- {
- volledigGetal = volledigGetal + "0";
- }
- txtMantisse.Text = volledigGetal;
- txtFloatingPoint.Text = Convert.ToString(posNeg + exponent + volledigGetal);
- }
- if (rdbNaarDec.Checked)
- {
- string floatInvoer = txtInvoer.Text;
- string posNeg = floatInvoer.Substring(0, 1);
- int exponenentUit = Convert.ToInt32(floatInvoer.Substring(1, 8), 2) - 127;
- string MantisseUitg = "1" + floatInvoer.Substring(9, 23);
- txtMantisse.Text = floatInvoer.Substring(9, 23);
- txtExponent.Text = floatInvoer.Substring(1, 8);
- txtTekenbit.Text = posNeg;
- string geheel;
- if (exponenentUit < 0)
- {
- geheel = "0";
- }
- else
- {
- geheel = MantisseUitg.Substring(0, exponenentUit + 1);
- }
- string achterKomma = null; ;
- if(exponenentUit > 0)
- achterKomma = MantisseUitg.Substring(exponenentUit + 1, MantisseUitg.Length - exponenentUit - 1);
- else
- {
- achterKomma = MantisseUitg;
- for (int i = 0; i < Math.Abs(exponenentUit) - 1; i++)
- {
- achterKomma = "0" + achterKomma;
- }
- // achterKomma = MantisseUitg.Substring(Math.Abs(exponenentUit) + 1, MantisseUitg.Length - exponenentUit - 1);
- }
- txtGeheel.Text = geheel;
- int achterKommaInt = 0;
- int multiplier = 1;
- int HoudLengte = achterKomma.Length;
- for (int i = 0; i < HoudLengte; i++ )
- {
- if (achterKomma.Substring(achterKomma.Length - 1, 1) == "0")
- achterKomma = achterKomma.Substring(0, achterKomma.Length - 1);
- else
- i = 100;
- }
- txtAchterKomma.Text = achterKomma;
- int Delergetal = 1;
- for (int i = 0; i < achterKomma.Length; i++) {
- Delergetal = Convert.ToInt32(Convert.ToString(Delergetal) + "0");
- }
- for (int i = 0; i < achterKomma.Length; i++)
- {
- multiplier = multiplier * 2;
- if (achterKomma.Substring(i, 1) == "1")
- {
- achterKommaInt += Delergetal / multiplier;
- }
- }
- int voorKomma = Convert.ToInt32(geheel, 2);
- string signed = null;
- if (posNeg == "0")
- signed = "";
- else
- signed = "-";
- txtFloatingPoint.Text = signed + Convert.ToString(voorKomma) + ',' + Convert.ToString(achterKommaInt);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement