標題:
Visual studio c#簡易計算器例程
[打印本頁]
作者:
540750247
時間:
2017-10-15 11:26
標題:
Visual studio c#簡易計算器例程
如題。計算器的運行界面如下:
0.png
(70.6 KB, 下載次數(shù): 65)
下載附件
2017-10-15 23:54 上傳
所有資料51hei提供下載:
Calculators.rar
(63.24 KB, 下載次數(shù): 29)
2017-10-15 11:26 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
Visual studio c#計算器源程序如下:
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;
using System.Text.RegularExpressions;
using System.Collections;
namespace Calculators
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void ShowAsStandard()//“標準型”模式
{
groupT.Location = new Point(6, 78);//groupT中存放標準型模式下的按鈕控件
txtEnterBox.Size = new Size(250, 23);
txtExpression.Size = new Size(250, 14);
groupShowWindow.Size = new Size(260, 49);
groupS.Location = new Point(-333, 77);//groupT中存放科學型模式下特有的按鈕控件
this.Size = new Size(290, 349);
}
private void ShowAsScience()//“科學型”模式
{
groupT.Location = new Point(268, 78);
txtEnterBox.Size = new Size(511, 23);
txtExpression.Size = new Size(511, 14);
groupShowWindow.Size = new Size(517, 49);
groupS.Location = new Point(3, 77);
this.Size = new Size(551, 349);
}
private void Form1_Load(object sender, EventArgs e)
{
//界面初始化為“標準型”的樣子
ShowAsStandard();
}
//================標志================================================
private Expression leftValue = null;//左操作數(shù)
bool txtEnterBoxClearFlag = true;//是否清除txtEnterBox的標志,是否可啟用backspace的標志
bool txtExpressionClearFlag = true;//是否清除txtExpression的標志
bool txtExpressionInsertFlag = true;//是否在txtExpression中插入新數(shù)據(jù)的標志
bool btnBackSpaceEnableFlag = true;//是否啟用BackSpace鍵
//====================================================================
string op;//操作符
Expression expression = null;//表達式對象
private void btn0_Click(object sender, EventArgs e)//數(shù)字及點號按鈕被按下時觸發(fā)該事件
{
Button btn = sender as Button;
if (txtEnterBoxClearFlag)
{
txtEnterBox.Clear();
}
if (txtExpressionClearFlag)
{
txtExpression.Clear();
}
txtEnterBox.Text += btn.Text;
txtEnterBoxClearFlag = false;
txtExpressionClearFlag = false;
btnBackSpaceEnableFlag = true;
}
private void buttonClear_Click(object sender, EventArgs e)
{
#region 清除數(shù)字顯示框、表達式顯示框及左操作數(shù)
txtEnterBox.Text = "0";
txtExpression.Text = "";
leftValue = null;//左操作數(shù)清空
#endregion
}
private void txtEnterBox_TextChanged(object sender, EventArgs e)
{
#region 檢查輸入數(shù)字是否規(guī)范
if (Regex.IsMatch(txtEnterBox.Text, "^[0]+[1-9]+")) //1.非小數(shù)數(shù)字不能以0開頭。
{
txtEnterBox.Text = Regex.Replace(txtEnterBox.Text, "^0+", "");
}
if (Regex.IsMatch(txtEnterBox.Text, "^0+"))//2.數(shù)字開頭不能連續(xù)輸入多個0,最多只能輸入一個
{
txtEnterBox.Text = Regex.Replace(txtEnterBox.Text, "^0+", "0");
}
if (Regex.IsMatch(txtEnterBox.Text, "[.].*[.]"))//3.數(shù)字中小數(shù)點個數(shù)不能超過1
{
txtEnterBox.Text = Regex.Replace(txtEnterBox.Text, "[.]{1}$", "");
}
#endregion
}
private void btnNegative_Click(object sender, EventArgs e)
{
#region 將輸入框中的數(shù)字乘以-1
double dTemp = Convert.ToDouble(txtEnterBox.Text);
dTemp = dTemp * (-1);
txtEnterBox.Text = dTemp.ToString();
#endregion
}
private void btnAdd_Click(object sender, EventArgs e)//雙目運算符:加減乘除、取余按鈕按下時觸發(fā)該事件
{
Button btn = sender as Button;
if (txtExpressionInsertFlag)
{
txtExpression.Text += txtEnterBox.Text + btn.Text;//表達式顯示控件添加數(shù)值輸入控件中的值和操作符的值
}
else
{
txtExpression.Text += btn.Text;//表達式顯示控件僅添加操作符的值
}
if (leftValue == null)
{
leftValue = new Constant(Convert.ToDouble(txtEnterBox.Text));//創(chuàng)建左操作數(shù)
}
else
{
Constant RightVaue = new Constant(Convert.ToDouble(txtEnterBox.Text));//創(chuàng)建右操作數(shù)
expression = new Operation(leftValue, op, RightVaue);//創(chuàng)建表達式
leftValue = expression;//將表達式賦值給左操作數(shù)
txtEnterBox.Text = expression.Evaluate().ToString();//數(shù)值輸入控件顯示當前計算值
}
op = btn.Text;//更新操作符
txtEnterBoxClearFlag = true;
txtExpressionClearFlag = false;
txtExpressionInsertFlag = true;
btnBackSpaceEnableFlag = false;
}
private void btnEvaluate_Click(object sender, EventArgs e)//按下等于號按鈕
{
if (leftValue != null)
{
Constant RightVaue = new Constant(Convert.ToDouble(txtEnterBox.Text));//創(chuàng)建右操作數(shù)
expression = new Operation(leftValue, op, RightVaue);//創(chuàng)建表達式
leftValue = null;//清空左操作數(shù)
txtEnterBox.Text = expression.Evaluate().ToString();
}
else
{
leftValue = new Constant(Convert.ToDouble(txtEnterBox.Text));//創(chuàng)建左操作數(shù)
}
txtExpression.Clear();
txtEnterBoxClearFlag = true;
btnBackSpaceEnableFlag = false;
}
private void btnSqrt_Click(object sender, EventArgs e)//單目運算符:計算平方根,求倒數(shù)按鈕按下時觸發(fā)該事件
{
Button btn = sender as Button;
txtExpression.Text +=(string)btn.Tag+ "(" + txtEnterBox.Text + ")";//表達式顯示控件顯示當前表達式
leftValue = new Constant(Convert.ToDouble(txtEnterBox.Text));//創(chuàng)建左操作數(shù)
expression = new Operation(leftValue, btn.Text, null);//創(chuàng)建表達式
leftValue = null;
txtEnterBox.Text = expression.Evaluate().ToString();
txtEnterBoxClearFlag = true;
txtExpressionClearFlag = true;
txtExpressionInsertFlag = false;
btnBackSpaceEnableFlag = false;
}
private void btnBackSpace_Click(object sender, EventArgs e)
{
if (btnBackSpaceEnableFlag)
{
int strLength = txtEnterBox.Text.Length;
if (strLength==1)
{
txtEnterBox.Text = "0";//只剩一個字符時,將該字符修改為0
……………………
…………限于本文篇幅 余下代碼請從51黑下載附件…………
復制代碼
歡迎光臨 (http://www.torrancerestoration.com/bbs/)
Powered by Discuz! X3.1