C++写出计算器之后,我又用C#重写了计算器。算法较V1.20变化不大,关键这回我用的是WPF,UI升级。看图大家就知道了。
这是我做的第一个WPF应用程序,谨以此纪念。
结尾同样附源码下载。
///MainWindow.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace Calculator
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
bool bEquation, bNumber, bDot;
double a, b, c, answer;
string m, n;
int cOperator;
public MainWindow()
{
InitializeComponent();
}
private void Startup()
{//初始化所有变量
txtNumber.Text = "0";
txtFormula.Text = "";
a = b = c = answer = 0;
m = n = "\0";
bEquation = false;
bNumber = true;
bDot = false;
cOperator = 0;
}
private void Input(string s)
{//输入函数
switch (s)
{
case ".":
if (bDot == false && bNumber)
{
txtNumber.Text += ".";
}
bDot = true;
//bEquation=false;
bNumber = true;
break;
case "0":
case "1":
case "2":
case "3":
case "4":
case "5":
case "6":
case "7":
case "8":
case "9":
if (bEquation)
{
txtFormula.Text = "";
txtNumber.Text = s.ToString();
}
else if (cOperator != 0 && !bNumber)
{
txtNumber.Text = s;
}
else
{
if (txtNumber.Text == "0") txtNumber.Text = s;
else txtNumber.Text += s;
}
bEquation = false;
bNumber = true;
break;
case "+":
case "-":
case "*":
case "/":
if (bEquation)
{//如果刚刚按了等号按钮
txtFormula.Text = txtNumber.Text;
txtFormula.Text += s;
a = Convert.ToDouble(txtNumber.Text);
m = s;
bEquation = false;
cOperator = 1;
break;
}
if (cOperator == 0)
{//第一回按运算符按钮
a = Convert.ToDouble(txtNumber.Text);
m = s;
}
else if (cOperator == 1)
{//第二回按按钮
b = Convert.ToDouble(txtNumber.Text);
n = s;
}
else
{//第三回按按钮
c = Convert.ToDouble(txtNumber.Text);
Count();
c = 0;
n = s;
}
txtFormula.Text += txtNumber.Text;//将txtNumber的数据添加到txtFormula
txtFormula.Text += s;//运算符添加到txtFormula
bEquation = false;
bNumber = false;
cOperator++;
bDot = false;
break;
case "=":
if (bEquation)
{
break;
}
bEquation = true;
bNumber = false;
txtFormula.Text += txtNumber.Text;
txtFormula.Text += "=";
if (cOperator == 0) { break; }
else if (cOperator == 1) { b = Convert.ToDouble(txtNumber.Text); }
else { c = Convert.ToDouble(txtNumber.Text); }
Count();
answer = a;
txtNumber.Text = answer.ToString();
cOperator = 0;
a = b = c = 0;
m = n = "0";
break;
case "CE":
Startup();
break;
}
}
private void Count()
{//计算函数
switch (n)
{
case "+":
case "-":
switch (m)
{
case "+": a = a + b; b = c; m = n; break;
case "-": a = a - b; b = c; m = n; break;
case "*": a = a * b; b = c; m = n; break;
case "/": a = a / b; b = c; m = n; break;
}
break;
case "*":
if (m == "/") { a = a / b; b = c; m = n; break; }
else { b = b * c; break; }
case "/":
if (m == "/") { a = a / b; b = c; m = n; break; }
else { b = b / c; break; }
case "\0": break;
}
if (bEquation)
{//如果bEquation为真,则执行如下运算
switch (m)
{
case "+": a = a + b; break;
case "-": a = a - b; break;
case "*": a = a * b; break;
case "/": a = a / b; break;
case "\0": txtFormula.Text += "Error!"; break;
}
}
}
private void Calculator_Loaded(object sender, RoutedEventArgs e)
{
Startup();
}
private void btn0_Click(object sender, RoutedEventArgs e)
{
Input("0");
}
private void btn1_Click(object sender, RoutedEventArgs e)
{
Input("1");
}
private void btn2_Click(object sender, RoutedEventArgs e)
{
Input("2");
}
private void btn3_Click(object sender, RoutedEventArgs e)
{
Input("3");
}
private void btn4_Click(object sender, RoutedEventArgs e)
{
Input("4");
}
private void btn5_Click(object sender, RoutedEventArgs e)
{
Input("5");
}
private void btn6_Click(object sender, RoutedEventArgs e)
{
Input("6");
}
private void btn7_Click(object sender, RoutedEventArgs e)
{
Input("7");
}
private void btn8_Click(object sender, RoutedEventArgs e)
{
Input("8");
}
private void btn9_Click(object sender, RoutedEventArgs e)
{
Input("9");
}
private void btnDot_Click(object sender, RoutedEventArgs e)
{
Input(".");
}
private void btnPlus_Click(object sender, RoutedEventArgs e)
{
Input("+");
}
private void btnSubtration_Click(object sender, RoutedEventArgs e)
{
Input("-");
}
private void btnMultiplication_Click(object sender, RoutedEventArgs e)
{
Input("*");
}
private void btnDevide_Click(object sender, RoutedEventArgs e)
{
Input("/");
}
private void btnEqual_Click(object sender, RoutedEventArgs e)
{
Input("=");
}
private void btnInitialize_Click(object sender, RoutedEventArgs e)
{
Input("CE");
}
}
}
xaml不再列出,先看的朋友直接下源码吧。
Calculator0417CSWPF http://cid-c96c6ec8ad8dcfea.office.live.com/browse.aspx/Program/Calculator