c#简单计算器
本文关键字:计算器 简单 | 更新日期: 2023-09-27 18:15:36
我正在练习编程,所以我可以擅长它,但我不能完成这个计算器代码。我不会加、减、乘、除三个或更多的数字。请帮助。我的代码有什么问题?
提前感谢!
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 Calculator
{
public partial class frmMain : Form
{
int oprtn;
String firstNumber;
String nextNumber;
public frmMain()
{
InitializeComponent();
}
private void btn0_Click(object sender, EventArgs e)
{
txtShow.Text = txtShow.Text + btn0.Text;
}
private void btn1_Click(object sender, EventArgs e)
{
txtShow.Text = txtShow.Text + btn1.Text;
}
private void btn2_Click(object sender, EventArgs e)
{
txtShow.Text = txtShow.Text + btn2.Text;
}
private void btn3_Click(object sender, EventArgs e)
{
txtShow.Text = txtShow.Text + btn3.Text;
}
private void btn4_Click(object sender, EventArgs e)
{
txtShow.Text = txtShow.Text + btn4.Text;
}
private void btn5_Click(object sender, EventArgs e)
{
txtShow.Text = txtShow.Text + btn5.Text;
}
private void btn6_Click(object sender, EventArgs e)
{
txtShow.Text = txtShow.Text + btn6.Text;
}
private void btn7_Click(object sender, EventArgs e)
{
txtShow.Text = txtShow.Text + btn7.Text;
}
private void btn8_Click(object sender, EventArgs e)
{
txtShow.Text = txtShow.Text + btn8.Text;
}
private void btn9_Click(object sender, EventArgs e)
{
txtShow.Text = txtShow.Text + btn9.Text;
}
private void btnClear_Click(object sender, EventArgs e)
{
txtShow.Clear();
}
private void btnSubtract_Click(object sender, EventArgs e)
{
firstNumber = txtShow.Text;
oprtn = 2;
txtShow.Text = "";
}
private void btnAdd_Click(object sender, EventArgs e)
{
firstNumber = txtShow.Text;
oprtn = 1;
txtShow.Text = "";
}
private void btnDivide_Click(object sender, EventArgs e)
{
firstNumber = txtShow.Text;
oprtn = 4;
txtShow.Text = "";
}
private void btnMultiply_Click(object sender, EventArgs e)
{
firstNumber = txtShow.Text;
oprtn = 3;
txtShow.Text = "";
}
private void btnEquals_Click(object sender, EventArgs e)
{
nextNumber = txtShow.Text;
int num1 = Convert.ToInt32(firstNumber);
int num2 = Convert.ToInt32(nextNumber);
switch (oprtn)
{
case 1:
int sum = num1 + num2;
String equals = Convert.ToString(sum);
sum = sum + num2;
txtShow.Text = equals;
break;
case 2:
int difference = num1 - num2;
equals = Convert.ToString(difference);
difference = difference - num2;
txtShow.Text = equals;
break;
case 3:
int product = num1 * num2;
equals = Convert.ToString(product);
product = product * num2;
txtShow.Text = equals;
break;
case 4:
int quotient = num1 / num2;
equals = Convert.ToString(quotient);
quotient = quotient / num2;
txtShow.Text = equals;
break;
}
}
private void btnBackspace_Click(object sender, EventArgs e)
{
}
private void txtShow_TextChanged(object sender, EventArgs e)
{
}
private void btnPoint_Click(object sender, EventArgs e)
{
txtShow.Text = txtShow.Text + ".";
}
}
}
根据你目前的解决方案,如果不先按=键,你不能加两个以上的数字
结果是这样的:
>3
//Number '3' is stored in txtShow.Text
>3+
//'3' is stored in nextNumber
>3+3
//Number '3' is stored in txtShow.Text again
>3+3+
//'3' is stored in nextNumber, overriding the first number
>3+3+3
//Number '3' is stored in txtShow.Text again
>3+3+3=
//Calculation is done, but nextNumber contains '3', and then 3 is added, yielding 6
最简单的解决方案是每次按操作符时执行数学运算。所以你需要先计算3+3=6
,然后再加上3。