内容发布更新时间 : 2025/1/22 15:56: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 简易计算器 {
public partial class Form1 : Form {
public Form1() {
InitializeComponent(); }
double num1, num2, result; bool decimalFlag = false;
#region 调用NumClick方法
public void NumClick(int myNumber) {
if (decimalFlag) {
txtReadout.Text = txtReadout.Text + myNumber; } else {
if (txtReadout.Text == \) // 输入整数 {
// 输入的是整数的第一个数字 txtReadout.Text = myNumber + \; } else {
// 输入的不是整数的第一个数字
// 输入小数
// 操作数及运算结果
// 判断输入的是否为小数 // 操作类型
string myOperator;
txtReadout.Text = txtReadout.Text.Substring
(0, txtReadout.Text.Length - 1) + myNumber + \; } } }
#endregion
#region 窗体载入事件代码
private void Form1_Load(object sender, EventArgs e) {
txtReadout.Text = \;
// 指定文本框的对齐方式为右对齐
txtReadout.TextAlign = HorizontalAlignment.Right; }
#endregion
#region 数字按钮【0】~【9】的单击事件代码
private void btnNum0_Click(object sender, EventArgs e) {
NumClick(0);
}
private void btnNum1_Click(object sender, EventArgs e) {
NumClick(1); }
private void btnNum2_Click(object sender, EventArgs e) {
NumClick(2); }
private void btnNum3_Click(object sender, EventArgs e) {
NumClick(3); }
private void btnNum4_Click(object sender, EventArgs e) {
NumClick(4); }
private void btnNum5_Click(object sender, EventArgs e) {
NumClick(5); }
private void btnNum6_Click(object sender, EventArgs e) {
NumClick(6); }
private void btnNum7_Click(object sender, EventArgs e) {
NumClick(7); }
private void btnNum8_Click(object sender, EventArgs e) {
NumClick(8); }
private void btnNum9_Click(object sender, EventArgs e) {
NumClick(9); }
#endregion
#region 【.】按钮的单击事件代码
private void btnDot_Click(object sender, EventArgs e) {
decimalFlag = true; }
#endregion
#region【C】按钮的单击事件代码
private void btnC_Click(object sender, EventArgs e) {
num1 = 0; num2 = 0;
txtReadout.Text = \; decimalFlag = false; }
// 输入小数点