- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using System.Text.RegularExpressions;//正则表达式需要用到的
- namespace Calculator
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- private void button1_Click(object sender, EventArgs e)
- {
- Regex reg1;
- reg1 = new Regex("^[+-]?(([0-9]+[.][0-9]+)|([0-9]+))$");
- if (reg1.IsMatch(textBox1.Text) && reg1.IsMatch(textBox2.Text))
- {
- double num1 = Convert.ToDouble(textBox1.Text);
- double num2 = Convert.ToDouble(textBox2.Text);
- int ch = comboBox1.SelectedIndex;
- if (ch == 3 && num2 == 0)
- {//错误提示:
- textBox3.Text = "被除数不能为零哦!!Please try again!!";
- }
- else
- {
- double x = 0;
- switch(ch) {
- case 0: x = num1 + num2; break;
- case 1: x = num1 - num2; break;
- case 2: x = num1 * num2; break;
- case 3: x = num1 / num2; break;
- default: break;
- }
- textBox3.Text = x.ToString();
- }
- }
- else
- {//错误提示:
- if (!reg1.IsMatch(textBox1.Text) && !reg1.IsMatch(textBox2.Text))
- {
- textBox3.Text = "上面两个框中都没有输入合法的数哦~~!";
- }
- else if (!reg1.IsMatch(textBox1.Text))
- {
- textBox3.Text = "上面左边框中输入的不是合法的数哦~~!";
- }
- else if (!reg1.IsMatch(textBox2.Text))
- {
- textBox3.Text = "上面右边框中输入的不是合法的数哦~~!";
- }
- }
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- comboBox1.SelectedIndex = 0;
- textBox1.Text = "这里输入第一个数";
- textBox2.Text = "这里输入第二个数";
- textBox3.Text = "这里显示计算结果";
- }
- private void textBox1_TextChanged(object sender, EventArgs e)
- {
- textBox1.Text = "";
- }
- private void textBox2_TextChanged(object sender, EventArgs e)
- {
- textBox2.Text = "";
- }
- private void textBox3_TextChanged(object sender, EventArgs e)
- {
- Regex ansreg;
- ansreg = new Regex("^[+-]?(([0-9]+[.][0-9]+)|([0-9]+))$");
- if (ansreg.IsMatch(textBox3.Text))
- {
- Clipboard.SetDataObject(textBox3.Text);
- MessageBox.Show("计算结果已经复制到剪切板了哟~", "帮助", MessageBoxButtons.OK);
- }
- }
- private void button2_Click(object sender, EventArgs e)
- {
- string ss_help = "";
- ss_help += "使用方法:\n\n";
- ss_help += " ①在第一个框中输入第一个操作数\n";
- ss_help += " ②在第二个框中输入第二个操作数\n";
- ss_help += " ③在下拉列表中选择想要的计算方式\n";
- ss_help += " ④点击“=”按钮使结果显示在第三个框中\n";
- ss_help += " ⑤输入的数据要求是小数或者整数\n";
- ss_help += " ⑥数据计算范围为double类型范围\n";
- ss_help += " ⑦得到计算结果后点击第三个框会自动\n 将结果复制到剪切板中哦(~ ̄▽ ̄)~\n\n";
- ss_help += " @author:软开142都颜汗";
- MessageBox.Show(ss_help, "帮助", MessageBoxButtons.OK);
- }
- }
- }
- /*复制粘贴参考代码
- //复制:
- private void button1_Click(object sender, System.EventArgs e) {
- if(textBox1.SelectedText != "")
- Clipboard.SetDataObject(textBox1.SelectedText);
- }
- //粘贴:
- private void button2_Click(object sender, System.EventArgs e) {
- IDataObject iData = Clipboard.GetDataObject();
- if(iData.GetDataPresent(DataFormats.Text)) {
- textBox2.Text = (String)iData.GetData(DataFormats.Text);
- }
- }
- */
感言:我计算器写过c/c++控制台的,写过java界面的, 写过JSP页面动态的, 这次是C#桌面版本的, 感觉这个是我写过的所有简单计算器中最简约美观便捷的一个,虽然简单,是我最喜欢的一个
可执行程序链接:链接: http://pan.baidu.com/s/1cJnmm6 密码: fp3k
开发环境:VS2012
解决方案链接:链接: http://pan.baidu.com/s/1hsllbyk 密码: 7t71