using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace 计算器试做
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private int opMain = 0;
private double mainNum1 = 0;
private double mainNum2 = 0;
private bool isSecond = false;
private bool isDone = false;
private bool isDecimal = false;
private bool isNokeydown = false;
private bool isKeyupclear = true;
public void setText(string textlast) {
if (textlast.Equals(“clear”))
{
textBox1.Text = “0.”;
isSecond = false;
isDone = false;
isDecimal = false;
isKeyupclear = true;
}
else {
if (isSecond)
{
textBox1.Text = textlast;
isSecond = false;
isDecimal = false;
}
else
{
if(isDone){
textBox1.Text = textlast;
isDone = false;
}
else{
if (isKeyupclear){
textBox1.Text = textlast;
isKeyupclear = false;
}
else
textBox1.Text += textlast;
}
}
}
equal.Select();
}
public void calculator(double num1, double num2, int op)
{
double answer = 0;
switch (op)
{
case 1:
answer = num1 + num2;
break;
case 2:
answer = num1 – num2;
break;
case 3:
answer = num1 * num2;
break;
case 4:
answer = num1 / num2;
break;
}
setText(answer.ToString());
}
private void doEquals(){
if(isNokeydown){
mainNum2 = double.Parse(textBox1.Text);
setText(“clear.”);
calculator(mainNum1,mainNum2,opMain);
isDone = false;
isNokeydown = false;
}
}
private void setOperator(int operation){
if(textBox1.Text.Length > 0) {
opMain = operation;
mainNum1 = double.Parse(textBox1.Text);
isSecond = true;
isDone = false;
isNokeydown = true;
equal.Select();
}
}
private void setDecimal(){
if(!isDecimal){
setText(“.”);
isDecimal = true;
}
equal.Select();
}
private void setclear() {
textBox1.Text = “0.”;
}
private void doSquart(){
double storNum;
storNum = double.Parse(textBox1.Text);
if(storNum > 0){
storNum = Math.Sqrt(storNum);
textBox1.Text = storNum.ToString();
}
else
textBox1.Text = “Hello world!”;
equal.Select();
}
private void button1_Click(object sender, EventArgs e)
{
setText(“1”);
}
private void button2_Click(object sender, EventArgs e)
{
setText(“2”);
}
private void button3_Click(object sender, EventArgs e)
{
setText(“3”);
}
private void button4_Click(object sender, EventArgs e)
{
setText(“4”);
}
private void button5_Click(object sender, EventArgs e)
{
setText(“5”);
}
private void button6_Click(object sender, EventArgs e)
{
setText(“6”);
}
private void button7_Click(object sender, EventArgs e)
{
setText(“7”);
}
private void button8_Click(object sender, EventArgs e)
{
setText(“8”);
}
private void button9_Click(object sender, EventArgs e)
{
setText(“9”);
}
private void button0_Click(object sender, EventArgs e)
{
setText(“0”);
}
private void point_Click(object sender, EventArgs e)
{
setDecimal();
}
private void equal_Click(object sender, EventArgs e)
{
doEquals();
}
private void add_Click(object sender, EventArgs e)
{
setOperator(1);
}
private void minus_Click(object sender, EventArgs e)
{
setOperator(2);
}
private void multipy_Click(object sender, EventArgs e)
{
setOperator(3);
}
private void divide_Click(object sender, EventArgs e)
{
setOperator(4);
}
private void clearbutton_Click(object sender, EventArgs e)
{
textBox1.Text = “”;
}
private void square_Click(object sender, EventArgs e)
{
doSquart();
}
private void backspace_Click(object sender, EventArgs e)
{
string old = textBox1.Text;
if (old.Length > 1 && old != “0.”)
textBox1.Text = old.Substring(0, old.Length – 1);
else
textBox1.Text = “0.”;
}
}
}
上次那个大佬把计算器的整个代码直接贴给了本人,然后本人试了一下,发现有一个不太对的地方,但是又不知道该怎么改,所以发个帖问一下。
第一张图片上的问题是,按下一个数字键后,等号键便始终处于按下的状态,虽然并没有什么实际影响,但是看起来怪怪的。
第二章图片上,本人输入了66+33后按下了等于键,然后它就出现了clear.99的字样,怎样改才能只显示99这个结果啊?
谢谢各位了~~
本人还处于学习阶段,真心感谢诸位的教导。
20
60
比你判断的多了个点号,去掉点号就行了