一、计算器
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace _4计算器
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button8_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + button8.Text;
}
private void button11_Click(object sender, EventArgs e)
{
double r;//用于保存结果
string t = textBox1.Text;//用以保存文本框中的算术表达式
int space = t.IndexOf(' ');//用以搜索文本框中的空格位置,注意是位置
string s1 = t.Substring(0, space);//s1用于保存第一个运算数
char op = Convert.ToChar(t.Substring(space + 1, 1));//op用以保存运算符
string s2 = t.Substring(space+3);//s2用于保存第二个运算数
double arg1 = Convert.ToDouble(s1);//将运算数从string转换成double
double arg2 = Convert.ToDouble(s2);
switch (op)
{
case '+':
r = arg1 + arg2;
break;
case '-':
r = arg1 - arg2;
break;
case '*':
r = arg1 * arg2;
break;
case '/':
if (arg2 == 0)
{
// MessageBox.Show("0不能做分母","错误",MessageBoxButtons.OK,MessageBoxIcon.Error);
throw new ApplicationException();
}
else
{
r = arg1 / arg2;
break; //跳出if语句
}
break; //这是case'/'的break,跳出switch
default:
throw new ApplicationException();
}
textBox1.Text = r.ToString();
}
private void button1_Click(object sender, EventArgs e)
{
// Button btn = (Button)sender;
// textBox1.Text = textBox1.Text + btn.Text;
textBox1.Text = textBox1.Text + button1.Text;
}
private void button2_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + button2.Text;
}
private void button3_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + button3.Text;
}
private void button6_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + button6.Text;
}
private void button5_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + button5.Text;
}
private void button4_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + button4.Text;
}
private void button9_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + button9.Text;
}
private void button7_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + button7.Text;
}
private void button12_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + button12.Text;
}
private void button14_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + " "+button14.Text+" ";
}
private void button13_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + " " + button13.Text + " ";
}
private void button10_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + " " + button10.Text + " ";
}
private void button15_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + " " + button15.Text + " ";
}
private void button16_Click(object sender, EventArgs e)
{
textBox1.Text = "";
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
DialogResult dr=MessageBox.Show("确定退出吗","退出",MessageBoxButtons.OKCancel,MessageBoxIcon.Question);
if (dr==DialogResult.OK)
{
e.Cancel=false;
}
else
{
e.Cancel=true;
}
}
}
}