您的位置:首页 > 编程语言 > ASP

计算器简单封装和ASP.net

2015-11-29 16:26 701 查看
封装:
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;

namespace计算器
{
classRichnone
{
publicstringfuhao;//计算符号
publicdoubleresult;//计算结果

privatedoublex;//第一个数
publicdoubleX
{
get{returnx;}
set{x=value;}
}
privatedoubley;//第二个数
publicdoubleY
{
get{returny;}
set{y=value;}
}
publicvoidAdd()//加法
{
if(fuhao=="+")
{
result=X+Y;
}
}
publicvoidSub()//减法
{
if(fuhao=="-")
{
result=X-Y;
}
}
publicvoidMul()//乘法
{
if(fuhao=="*")
{
result=X*Y;
}
}
publicvoidDiv()//除法
{
if(fuhao=="/")
{
result=X/Y;
}
}

}
}

Form1代码

privatevoidForm1_Load(objectsender,EventArgse)
{
if(File.Exists(path))
{
this.richTextBox1.LoadFile(path,RichTextBoxStreamType.RichText);
open.Enabled=true;
}
save.Enabled=true;
}

privatevoidopen_Click(objectsender,EventArgse)
{
OpenFileDialogTxTOPenDialog=newOpenFileDialog();
TxTOPenDialog.Filter="RTF文件(*.RTF)|*.RTF";
if(TxTOPenDialog.ShowDialog()==DialogResult.OK)
{
path=TxTOPenDialog.FileName;
this.richTextBox1.LoadFile(TxTOPenDialog.FileName,RichTextBoxStreamType.RichText);
open.Enabled=true;
save.Enabled=true;
MessageBox.Show("读取成功!","提示信息",MessageBoxButtons.OK,MessageBoxIcon.Asterisk);
this.richTextBox1.Show();
}
}

privatevoidsave_Click(objectsender,EventArgse)
{
SaveFileDialogTxTSaveDialog=newSaveFileDialog();
TxTSaveDialog.Filter="RTF文件(*.RTF)|*.RTF";
richTextBox1.Text=textBox2.Text;
if(File.Exists(path))
{
this.richTextBox1.SaveFile(path,RichTextBoxStreamType.RichText);
MessageBox.Show("保存成功!","提示信息",MessageBoxButtons.OK,MessageBoxIcon.Asterisk);
this.richTextBox1.Clear();
save.Enabled=false;
}
else
{
if(TxTSaveDialog.ShowDialog()==DialogResult.OK)
{
this.richTextBox1.SaveFile(TxTSaveDialog.FileName,RichTextBoxStreamType.RichText);
MessageBox.Show("保存成功!","提示信息",MessageBoxButtons.OK,MessageBoxIcon.Asterisk);
this.richTextBox1.Clear();
save.Enabled=false;
}
}
}

privatevoidadd_Click(objectsender,EventArgse)
{
SUAN.Text="+";
}

privatevoidsub_Click(objectsender,EventArgse)
{
SUAN.Text="-";
}

privatevoidmul_Click(objectsender,EventArgse)
{
SUAN.Text="*";
}
privatevoiddiv_Click_1(objectsender,EventArgse)
{
SUAN.Text="/";
}

//实例对象
Richnoneric=newRichnone();
ric.X=double.Parse(richTextBox1.Text);//第一个数
ric.Y=double.Parse(textBox2.Text);//第二个数
ric.fuhao=SUAN.Text;//运算符号
ric.result=result;//结果
ric.Add();//加法
ric.Sub();//减法
ric.Mul();//乘法
ric.Div();//除法
//intsum;
//sum=int.Parse(richTextBox1.Text)+int.Parse(textBox2.Text);

if(e.KeyCode==Keys.Enter)
{
if(textBox5.Text==ric.result.ToString())
{
right++;

MessageBox.Show("回答正确!");
}
else
{

MessageBox.Show("回答错误!");
}
Count++;
richTextBox1.Clear();
textBox2.Clear();
textBox5.Clear();

}

}截图





ASP.net

后台代码
protectedvoidButton1_Click(objectsender,EventArgse)
{
ric.X=double.Parse(TextBox1.Text);
ric.Y=double.Parse(TextBox3.Text);
ric.fuhao=DropDownList1.SelectedValue;
ric.result=result;
ric.Add();
ric.Sub();
ric.Mul();
ric.Div();
if(TextBox4.Text==ric.result.ToString())
{
Response.Write("<script>alert('回答正确')</script>");
}
else
{
Response.Write("<script>alert('回答错误')</script>");
}
}
protectedvoidDropDownList1_TextChanged(objectsender,EventArgse)
{
stringfuhao=DropDownList1.SelectedValue;
switch(fuhao)
{
case"+":
DropDownList1.SelectedValue="+";
break;
case"-":
DropDownList1.SelectedValue="-";
break;
case"*":
DropDownList1.SelectedValue="*";
break;
case"/":
DropDownList1.SelectedValue="/";
break;
default:
break;
}
}

封装
publicclassRichnone
{
publicstringfuhao;//计算符号
publicdoubleresult;

privatedoublex;//第一个数
publicdoubleX
{
get{returnx;}
set{x=value;}
}
privatedoubley;//第二个数
publicdoubleY
{
get{returny;}
set{y=value;}
}
publicvoidAdd()//加法
{
if(fuhao=="+")
{
result=X+Y;
}
}
publicvoidSub()
{
if(fuhao=="-")
{
result=X-Y;
}
}
publicvoidMul()
{
if(fuhao=="*")
{
result=X*Y;
}
}
publicvoidDiv()
{
if(fuhao=="/")
{
result=X/Y;
}
}

}

截图




                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: