您的位置:首页 > 编程语言 > C#

利用C#实现inch和cm的互换(含源码)

2009-12-02 11:21 441 查看
原理:1inch = 1/12foot = 2.54cm = 25.4mm
说明:inchcm时可精确转换,cminch时约等。
不当之处望指教。
点击下载:inchEXcm--liong.rar
---------
直接贴出源码:


//利用:1inch = 1/12foot = 2.54cm = 25.4mm 实现。


namespace inchEXcm






{


public partial class frmMain : Form






{


public frmMain()






{


InitializeComponent();


}


private double f = 0;


private double i = 0;


private double c = 0;


private double r = 0;




//字符串strIn若为空返回0;若为实数返回此实数的双精度值;若含字符串返回-1.


private double GetNum(string strIn)






{


if (strIn == "")






{


return 0;


}


else






{


try






{


double re = Convert.ToDouble(strIn);


return re;


}


catch






{


return -1;


}


}


}




private void btnConvert_Click(object sender, EventArgs e)






{


if (radinch.Checked)






{


//inch to cm


f = GetNum(txtFoot.Text);


if (f == -1)






{


MessageBox.Show("Number only!", "Error",


MessageBoxButtons.OK, MessageBoxIcon.Information);


return;


}


i = GetNum(txtInch.Text);


if (i == -1)






{


MessageBox.Show("Number only!", "Error",


MessageBoxButtons.OK, MessageBoxIcon.Information);


return;


}


r = f * 12 * 2.54 + i * 2.54;


txtCm.Text = r.ToString();


}


if (radcm.Checked)






{


//cm to inch


c = GetNum(txtCm.Text);


if (c == -1)






{


MessageBox.Show("Number only!", "Error",


MessageBoxButtons.OK, MessageBoxIcon.Information);


return;


}


r = c / 2.54;


txtFoot.Text = Convert.ToInt32(r / 12-r%12/12).ToString();//实现强制取整


txtInch.Text = Convert.ToInt32(r % 12).ToString();


}


}




private void btnCopy_Click(object sender, EventArgs e)






{


string strCB = "";


if (radinch.Checked)






{


strCB = txtCm.Text + "cm";


}


if (radcm.Checked)






{


strCB = txtFoot.Text + "foot " + txtInch.Text + "inch";


}


Clipboard.SetText(strCB);//复制到剪切板


MessageBox.Show("Message '"+strCB+"' has been copied!", ";)",


MessageBoxButtons.OK, MessageBoxIcon.Information);


}




private void btnExit_Click(object sender, EventArgs e)






{


this.Close();


}




private void radinch_CheckedChanged(object sender, EventArgs e)






{


txtInch.Text = "0";


txtFoot.Text = "0";


txtCm.Text = "0";


txtCm.Enabled = false;


txtFoot.Enabled = true;


txtInch.Enabled = true;


}




private void radcm_CheckedChanged(object sender, EventArgs e)






{


txtInch.Text = "0";


txtFoot.Text = "0";


txtCm.Text = "0";


txtCm.Enabled = true;


txtFoot.Enabled = false;


txtInch.Enabled = false;


}




private void aboutToolStripMenuItem_Click(object sender, EventArgs e)






{


MessageBox.Show("-= inchEXcm v.01 =-/n/nliong/n20080402", "inchEXcm v0.1",


MessageBoxButtons.OK, MessageBoxIcon.Information);


}


}


}

转贴地址:
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: