1 / 4
文档名称:

计算器编程代码.doc

格式:doc   大小:116KB   页数:4页
下载后只包含 1 个 DOC 格式的文档,没有任何的图纸或源代码,查看文件列表

如果您已付费下载过本站文档,您可以点这里二次下载

分享

预览

计算器编程代码.doc

上传人:amilus 2021/9/28 文件大小:116 KB

下载得到文件列表

计算器编程代码.doc

文档介绍

文档介绍:using System;
using ;
using ;
using ;
using ;
using ;
using ;
using ;
namespace calculator
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load( object sender, EventArgs e)
{
}
private void btn_Add_Click( object sender, EventArgs e)
{
Button btn = ( Button )sender;
空格用来分隔数值和运算符
= + " " + btn .Text + " " ;
}
private void btn_Result_Click( object sender, EventArgs e)
{
保存计算结果
double d_result;
保存文本框中的算术表达式
string s_txt = ;
查找空格位置
int space = (

' '

);
//s1 保存第一个运算数
string s1 = ( 0, space );
//operation 用来保存运算数
char opoperation = Convert .ToChar(((space + 1), 1)); //s2 保存第二个运算数
string
s2 = (space + 3);
// 将运算数从 string
转成 Double 型
double
arg1 =
Convert
.ToDouble(s1);
double
arg2 =
Convert
.ToDouble(s2);
根据运算符进行计算
switch (opoperation )
{
case '+' :
d_result = arg1 + arg2;
break ;
case '-' :
d_result = arg1 - arg2;
精选文库
break ;
case '*' :
d_result = arg1 * arg2;
break ;
case '/' :
if (arg2 == 0)
{
抛出异常