好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

C#实现简单加减乘除计算器

第一次学习c#,做了个简单的加减乘除计算器,只能实现两个因数的运算。

主要是练习下c#编程,和以前用过的vb差不多。与vb6不同的是,c#代码区分大小写。

windows窗口程序主要也是由一些控件组成,响应响应的事件(event),实现具体的功能。

1.效果图如下所示

2.代码如下所示

?

using system;

using system.collections.generic;

using system测试数据ponentmodel;

using system.data;

using system.drawing;

using system.text;

using system.windows.forms;

 

namespace windowsapplication1

{

   public partial class main : form

   {

     public main()

     {

       initializecomponent();

     }

 

     private void main_load( object sender, eventargs e)

     {

 

     }

 

     private void txtinshu1_textchanged( object sender, eventargs e)

     {

 

     }

 

     private void txtinshu1_keypress( object sender, keypresseventargs e)

     {

       onlyenternumber(sender, e);

     }

 

     //// <summary>

     /// 只能输入数字(含负号小数点)

     /// </summary>

     /// <param name="sender"></param>

     /// <param name="e"></param>

     public static void onlyenternumber( object sender, keypresseventargs e)

     {

       if ((e.keychar < 48 || e.keychar > 57) && e.keychar != 8 && e.keychar != 13 && e.keychar != 45 && e.keychar != 46)

       {

         e.handled = true ;

       }

 

       // 输入为负号时,只能输入一次且只能输入一次

       if (e.keychar == 45 && (((textbox)sender).selectionstart != 0 || ((textbox)sender).text.indexof( "-" ) >= 0)) e.handled = true ;

       if (e.keychar == 46 && ((textbox)sender).text.indexof( "." ) >= 0) e.handled = true ;

     }

 

 

     /*

      * 参数:d表示要四舍五入的数;i表示要保留的小数点后位数。

      * 正负数都四舍五入,适合数据统计的显示

      */

     double round( double d, int i)

     {

       if (d >= 0)

       {

         d += 5 * math.pow(10, -(i + 1));

       }

       else

       {

         d += -5 * math.pow(10, -(i + 1));

       }

       string str = d.tostring();

       string [] strs = str.split( '.' );

       int idot = str.indexof( '.' );

       string prestr = strs[0];

       string poststr = strs[1];

       if (poststr.length > i)

       {

         poststr = str.substring(idot + 1, i);

       }

       string strd = prestr + "." + poststr;

       d = double .parse(strd);

       return d;

     }

 

     private void txtinshu2_textchanged( object sender, eventargs e)

     {

 

     }

 

     private void txtinshu2_keypress_1( object sender, keypresseventargs e)

     {

       onlyenternumber(sender, e);

     }

 

     private void btnjisuan_click( object sender, eventargs e)

     {

       if (txtinshu1.text == "" ) {

         messagebox.show( "因数1不能为空!" , "警告" , messageboxbuttons.ok, messageboxicon.warning);

         return ;

       }

       if (txtinshu2.text == "" )

       {

         messagebox.show( "因数2不能为空!" , "警告" , messageboxbuttons.ok, messageboxicon.warning);

         return ;

       }

 

       double inshu1 = convert.todouble(txtinshu1.text);

       double inshu2 = convert.todouble(txtinshu2.text);

       double result = 0.0;

 

       if (radiobtnjia. checked ) {

         result = inshu1 + inshu2;

       }

 

       if (radiobtnjian. checked )

       {

         result = inshu1 - inshu2;

       }

 

       if (radiobtncheng. checked )

       {

         result = inshu1 * inshu2;

       }

 

       if (radiobtnchu. checked )

       {

         if (0 == inshu2)

         {

           messagebox.show( "因数2做除数不能为0!" , "警告" , messageboxbuttons.ok, messageboxicon.warning);

           return ;

         }

         result = inshu1 / inshu2;

         result = round(result, 6);

       }

 

       txtresult.text = convert.tostring(result);

     }

   }

}

因数输入框只允许输入数字和小数点负号的代码是从网络上引用的。
除法运算时四舍五入的处理也是引用自网络上的 文章 。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:http://blog.csdn.net/ameyume/article/details/8194680

dy("nrwz");

查看更多关于C#实现简单加减乘除计算器的详细内容...

  阅读:45次