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

来源:互联网 发布:孔浩java视频 编辑:程序博客网 时间:2024/06/06 09:53

 

 

原理: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);
        }

    }

}

 

转贴地址: