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

时间:2022-03-23 08:08:13

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

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

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

1.效果图如下所示

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

2.代码如下所示

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
using system;
using system.collections.generic;
using system.componentmodel;
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