|
楼主 |
发表于 2013-12-18 21:55:37
|
显示全部楼层
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 calculator
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void but1_Click(object sender, EventArgs e)
{
Button btn = (Button)sender;
textBox1.Text += btn.Text;
}
private void but2_Click(object sender, EventArgs e)
{
Button btn = (Button)sender;
textBox1.Text += btn.Text;
}
private void but3_Click(object sender, EventArgs e)
{
Button btn = (Button)sender;
textBox1.Text += btn.Text;
}
private void but4_Click(object sender, EventArgs e)
{
Button btn = (Button)sender;
textBox1.Text += btn.Text;
}
private void but5_Click(object sender, EventArgs e)
{
Button btn = (Button)sender;
textBox1.Text += btn.Text;
}
private void but6_Click(object sender, EventArgs e)
{
Button btn = (Button)sender;
textBox1.Text += btn.Text;
}
private void but7_Click(object sender, EventArgs e)
{
Button btn = (Button)sender;
textBox1.Text += btn.Text;
}
private void but8_Click(object sender, EventArgs e)
{
Button btn = (Button)sender;
textBox1.Text += btn.Text;
}
private void but9_Click(object sender, EventArgs e)
{
Button btn = (Button)sender;
textBox1.Text += btn.Text;
}
private void but10_Click(object sender, EventArgs e)
{
Button btn = (Button)sender;
textBox1.Text += btn.Text;
}
private void butAdd_Click(object sender, EventArgs e)
{
Button btn = (Button)sender;
textBox1.Text = textBox1.Text + " " + btn.Text + " ";
}
private void butSub_Click(object sender, EventArgs e)
{
Button btn = (Button)sender;
textBox1.Text = textBox1.Text + " " + btn.Text + " ";
}
private void butMul_Click(object sender, EventArgs e)
{
Button btn = (Button)sender;
textBox1.Text = textBox1.Text + " " + btn.Text + " ";
}
private void butDiv_Click(object sender, EventArgs e)
{
Button btn = (Button)sender;
textBox1.Text = textBox1.Text + " " + btn.Text + " ";
}
private void butClear_Click(object sender, EventArgs e)
{
textBox1.Text = ""; }
private void butCalculator_Click(object sender, EventArgs e)
{
Single r; //用于保存计算结果
string t = textBox1.Text; //t用于保存文本框中的算术表达式
int space = t.IndexOf(' ');//用于搜索空格位置
string s1 = t.Substring(space - 1, 1);//s1用于保存第一个运算数
char op = Convert.ToChar(t.Substring(space + 1, 1));//op用于保存运算符
string s2 = t.Substring(space + 3);//s2用于保存第二个运算数
Single arg1 = Convert.ToSingle(s1);//将运算数从string转换为Single
Single arg2 = Convert.ToSingle(s2);
switch (op)
{
case '+':
r = arg1 + arg2;
break;
case '-':
r = arg1 - arg2;
break;
case '*':
r = arg1 * arg2;
break;
case '/':
if (arg2 == 0)
{
throw new ApplicationException();
}
else
{
r = arg1 / arg2;
break;
}
break;
default:
throw new ApplicationException();
textBox1.Text = r.ToString();
}
}
}
}
|
|