求高手帮个忙,关于计算器小数点的问题,还有联加。附代码,请帮忙修改

梦断代码_0512 2009-11-05 07:21:23
求高手帮个忙,关于计算器小数点的问题,还有联加。附代码,请帮忙修改

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 crta
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private double nPrevValue = 0;//数据1
private bool bAppend = true;//数字按钮判断标识符
private string strPrevOpar = "";//运算符标识符
public double M,m;//记忆标识符
public string a;//小数点标识符
private void NumButton_Click(object sender, EventArgs e)//数字按钮输入
{
if (bAppend)
{
Button btn = sender as Button;
if (btn.Text == "0")
{
if (textBoxResult.Text == "")
{
textBoxResult.Text += "0";
}
else if (textBoxResult.Text == "0")
{
textBoxResult.Text +=btn.Text ;
}
else
{
textBoxResult.Text += btn.Text;
}
}
else
{
if (textBoxResult.Text == "0")
{
textBoxResult.Text = "";
}
textBoxResult.Text += btn.Text;
}
}
else
{
textBoxResult.Text = "";
Button btn = sender as Button;
textBoxResult.Text += btn.Text;
bAppend = true;
}
}


private void OperButton_Click(object sender, EventArgs e)
{
Button btn = sender as Button;
double result;//结果
if (btn.Text!="="&&btn.Text!="CE"&&btn.Text!="MS"&&btn.Text!="MR"&&btn.Text!="MC"&&btn.Text!="M+"&&btn.Text!="BackSpace"&&btn.Text!="."&&btn.Text!="sqrt"&&btn.Text!="1/x"&&btn.Text!="x^2"&&btn.Text!="+/-")//判断不为如下字符的情况
{
strPrevOpar = btn.Text;
try
{
nPrevValue = Convert.ToDouble(textBoxResult.Text);
}
catch (System.Exception )
{
nPrevValue = 0;
}
bAppend = false;
}
else if (btn.Text=="CE")//运算符为“CE”的情况
{
nPrevValue = 0;
bAppend = true;
strPrevOpar = "";
textBoxResult.Text = "0";
}
else if(btn.Text=="MS")//运算符为“MS”的情况
{
bAppend = true;
if (textBoxResult.Text == "0.")
{
M = 0;
lab_m.Text = null;
}
else
{
if (textBoxResult.Text == "0")
{
M = 0;
lab_m.Text = null;
}
else
{
if (M != 0)
{
M = 0;
M += Convert.ToDouble(textBoxResult.Text);
}
else if (M == 0)
{
lab_m.Text= "M";
M += Convert.ToDouble(textBoxResult.Text);
}
}
}
}
else if(btn.Text=="MR")//运算符为“MR”的情况
{
textBoxResult.Text = null;
textBoxResult.Text = Convert.ToString(M);

}
else if (btn.Text == "MC")//运算符为“MC”的情况
{
lab_m.Text = null;
M = 0;
m = 0;
}
else if (btn.Text == "M+")//运算符为“M+”的情况
{
m += Convert.ToDouble(textBoxResult.Text);
M += m;
m = 0;
}
else if (btn.Text == "BackSpace")//运算符为“backspace”的情况
{
if (textBoxResult.Text == null)
{
MessageBox.Show("it`s Empty!");
return;
}
else
{
if (textBoxResult.Text.Length != 0)
{
textBoxResult.Text = textBoxResult.Text.Remove(textBoxResult.Text.Length - 1, 1).ToString();
}
else
{
MessageBox.Show("left a little!");
textBoxResult.Text = "0";
}
}
}
else if(btn.Text==".")//运算符为"."的情况
{
if (a != null)
{
if (a != ".")
{
a += btn_piont.Text;
textBoxResult.Text += btn_piont.Text;
}
else
{ } //防止开始点多输入了的错误
}
else
{
textBoxResult.Text += btn_piont.Text;
a += btn_piont.Text;
}


}
else if (btn.Text == "sqrt")//运算符为“sqrt”的情况
{
textBoxResult.Text = Math.Sqrt(Convert.ToDouble(textBoxResult.Text)).ToString();

}
else if (btn.Text == "1/x")//运算符为“1/x”的情况
{

if (textBoxResult.Text != "0")
{
textBoxResult.Text =(1/ Convert.ToDouble(textBoxResult.Text)).ToString();

}
else
{
MessageBox.Show("除数不能为零");

}
}
else if (btn.Text == "x^2")//运算符为“x^2”的情况
{
textBoxResult.Text = Math.Pow(Convert.ToDouble(textBoxResult.Text), 2).ToString();
}
else if (btn.Text == "+/-")//运算符为“+/-”的情况
{
textBoxResult.Text = (Convert.ToDouble(textBoxResult.Text) * (-1)).ToString();
}
else
{
double current = 0;//数据2
try
{
current = Convert.ToDouble(textBoxResult.Text);
}
catch (System.Exception)
{
current = 0;
}

switch (strPrevOpar)
{
case "+"://运算符为“+”的情况
result = current + nPrevValue;
break;
case "-"://运算符为“-”的情况
result = current + nPrevValue;
break;
case "*"://运算符为“*”的情况
result = current * nPrevValue;
break;
case "/"://运算符为“/”的情况
result = nPrevValue / current;
break;
default:
result = current;
break;
}
textBoxResult.Text = result.ToString();
nPrevValue = 0;
bAppend = false;
strPrevOpar = "";
}
}
}
}




我这个程序,就是小数点儿,还有联加有点问题。
小数点只能输入一次,当第二次输入时就无法使用了。
联加不能运算
修改的有点累了,贴出来,请大家帮忙,并附上100分。有耐心的朋友帮忙改下。谢谢!锻炼自己的同时,帮助别人嘛
...全文
321 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
梦断代码_0512 2009-11-05
  • 打赏
  • 举报
回复
谢了,我自个儿琢磨吧!
qldsrx 2009-11-05
  • 打赏
  • 举报
回复
如果是为了达到目的,可以选择第三方控件,有这方面的开源控件的;如果是为了锻炼自己的能力,那就应该多想想,其实也不是那么难做到的,但是要很好得实现那个功能,还是必须重写TextBox控件才行,因为TextBox控件无法监视剪贴板事件,想想Windows自带的计算器,复制粘贴可是完全重写的过程,是一个模仿键盘输入的过程。
梦断代码_0512 2009-11-05
  • 打赏
  • 举报
回复
不好意思,这个不能贴出来,其实这只是个简单的计算器程序,我的写法是用两个事件完成所有的计算。
数字在numbutton_click中,运算符在operbutton_click中,减少长长的代码。
yyhlove 2009-11-05
  • 打赏
  • 举报
回复
帮顶~
ajaxtop 2009-11-05
  • 打赏
  • 举报
回复
项目保密性高不?,可以把测试用例上传上来,这样大家可以直接放到项目里帮你改,看有点头晕,放到编辑器里有助于调试。
梦断代码_0512 2009-11-05
  • 打赏
  • 举报
回复
兄弟,你出现了。对不住,刚学C#不久,还不是很明白是怎么回事,就开始写了。这也是中国教育的结果。
能不能帮我改下,不胜感激
wuyq11 2009-11-05
  • 打赏
  • 举报
回复
上一边帖子的工厂类模式很简单
小数点只能输入一次

110,545

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧