闲着无聊研究了下C#的wpf,主要是学习界面,第一次学习有可能有bug或写的不好的地方各位大佬们不要吐槽哈,只做了加减乘除,其他的算法没做不过原理很简单的用下C#的Math函数就行了,快下班了不想做了谁想做代码copy继续去实现。
Calculator.xaml文件
Calculator.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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.Shapes;
namespace Calcu
{
///
/// jisuanqi.xaml 的交互逻辑
///
public partial class Calculator : Window
{
private string currentStatus = "";
private double firstNum = -1;
private double secondNum = -1;
private string tempContent = "";
private int firstNumLen = 0;
private const string PLUS = "+";
private const string SUBTRACTION = "-";
private const string MULTIPLICATION = "*";
private const string DIVISION = "/";
private const string EQUAL = "=";
public Calculator()
{
InitializeComponent();
}
private void Keydown(object sender, KeyEventArgs e)
{
e.Handled = true;
}
private void onClick(object sender, RoutedEventArgs e)
{
Button btn = sender as Button;
if (btn != null)
{
string value = (string)btn.Content;
switch (value)
{
case DIVISION:
if (tempContent.Length == 0)
{
return;
}
firstNum = double.Parse(tempContent);
currentStatus = DIVISION;
tempContent += currentStatus;
inputContent.Text = tempContent;
firstNumLen = tempContent.Length;
break;
case MULTIPLICATION:
if (tempContent.Length == 0)
{
return;
}
firstNum = double.Parse(tempContent);
currentStatus = MULTIPLICATION;
tempContent += currentStatus;
inputContent.Text = tempContent;
firstNumLen = tempContent.Length;
break;
case PLUS:
if (tempContent.Length == 0)
{
return;
}
firstNum = double.Parse(tempContent);
currentStatus = PLUS;
tempContent += currentStatus;
inputContent.Text = tempContent;
firstNumLen = tempContent.Length;
break;
case SUBTRACTION:
if (tempContent.Length == 0)
{
return;
}
firstNum = double.Parse(tempContent);
currentStatus = SUBTRACTION;
tempContent += currentStatus;
inputContent.Text = tempContent;
firstNumLen = tempContent.Length;
break;
case EQUAL:
if (firstNum < 0 || currentStatus.Length == 0)
{
return;
}
Console.WriteLine(tempContent);
Console.WriteLine(firstNumLen);
Console.WriteLine(tempContent.Length);
string secondStr = tempContent.Substring(firstNumLen);
Console.WriteLine(secondStr);
secondNum = double.Parse(secondStr);
double result=0.0;
switch (currentStatus)
{
case PLUS:
result = firstNum + secondNum;
break;
case SUBTRACTION:
result = firstNum - secondNum;
break;
case MULTIPLICATION:
result = firstNum * secondNum;
break;
case DIVISION:
result = firstNum/secondNum;
break;
}
string showResult = result.ToString("f2");
firstNum = double.Parse(showResult);
inputContent.Text = showResult;
secondNum = -1;
currentStatus = "";
firstNumLen = tempContent.Length;
tempContent = showResult;
break;
case "C":
inputContent.Text = "";
currentStatus = "";
firstNum = -1;
secondNum = -1;
tempContent = "";
firstNumLen = 0;
break;
default:
if (firstNumLen > 0 && currentStatus.Length <= 0)
{
inputContent.Text = "";
currentStatus = "";
firstNum = -1;
secondNum = -1;
tempContent = "";
firstNumLen = 0;
}
if (value.Equals(".") && tempContent.Length<= 0)
{
return;
}
tempContent += value;
inputContent.Text = tempContent;
break;
}
}
}
}
}
效果图:
源码地址:https://download.csdn.net/download/qhs1573/11264243