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 Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnNumbersClicked(object sender,EventArgs evArgs)
{
Button btn = (Button) sender;
//在文本框中现实按钮代表的数字
txtFormula.Text+= btn.Text;
}
private void btnOpClicked(object sender, EventArgs evArgs)
{
Button btn = (Button) sender;
//加入运算符
txtFormula.Text=txtFormula.Text+" "+btn.txt+" ";
}
//计算表达式的结果
private void btnEqualsClicked(object sender, EventArgs evArgs)
{
//分析公式并获得参数
Parse p=new Parse();
Arguments a=p.Parse(txtFormula.Text);
//显示计算结果
try
{
MathUtil.Math m=new MathUtil.Math();
txtFormula.Text=m.GetResult(Single.FromString(a.Arg1),a.Op, Single.FromString(a.Arg2));
}
//例外处理
catch (MathUtil.InvalidFormulaException)
{
MessageBox.Show("计算错误", "请重试");
}
}
public class Arguments
{
//参数类
private String m_arg1;
//操作符
private Char m_op;
//运算数
private String m_arg2;
//在私有成员的基础上定义的属性
public String Arg1
{
get
{
return m_arg1;
}
set
{
m_arg1 = value;
return;
}
}
public Char Op
{
get
{
return m_op;
}
set
{
m_op=value;
return;
}
}
public String Arg2
{
get
{
return m_arg2;
}
set
{
m_arg2=value;
return;
}
}
}
public string Substring(
int startIndex,
int length
);
public string Substring(
int startIndex
);
//Parse类
public class Parse
{
public Arguments Parse(String formula)
{
Arguments args = new Arguments();
//获得第一个参数
int spacePos = formula.IndexOf(' ');
args.Arg1 = formula.Substring(0, spacePos);
//获得运算符
args.Op = formula.Substring(spacePos + 1, 1).ToChar();
//获得第二个参数
args.Arg2 = formula.Substring(spacePos + 3);
return args;
}
}
}
}
namespace MathUtil
{
//自定义的例外类
public class InvalidFormulaException:ApplicationException{ }
//Math类
public class Math
{
//计算运行结果
public String GetResult(String arg1, Char op, Single arg2)
{
Single r;
//判断操作符
switch (op)
{
case '+':
r=arg1+arg2;
return r.ToString();
case '-':
r = arg1-arg2;
return r.ToString();
case '*':
r = arg1*arg2;
return r.ToString();
case '/':
if (arg2==0)
{
throw new InvalidFormulaException();
}
else
{
r = arg1 / arg2;
return r.ToString();
}
//非法操作符
default:
throw new InvalidFormulaException();
}
}
}
}
错误 1 “Parse”: 成员名称不能与它们的封闭类型相同 C:\Documents and Settings\Administrator\桌面\Form1.cs 112 28 WindowsApplication1
哪位大哥好心帮忙看看哪里错了啊???:7::7::7: