3. 數值轉換程式
namespace Q3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{
Close();
}
private void button1_Click(object sender, EventArgs e)
{
var n = double.Parse(textBox1.Text);
if (n > Math.Pow(2, 15) ||
n < -Math.Pow(2, 15) - 1)
{
textBox2.Text = "overflow";
return;
}
string k = n < 0 ? "1" : "0";
k += Convert.ToString((int)Math.Abs(n), 2).PadLeft(15, '0');
k += ".";
double digit = Math.Abs(n) - (int)Math.Abs(n);
while (k.Length < 25)
{
digit *= 2;
k += digit >=1 ? "1" : "0";
if (digit >= 1) digit--;
}
textBox2.Text = k;
}
}
}
Last updated