运用WPF技术,模仿Windows 10系统中计算机器(Calculator)程序,开发一个类似程序。
注:
- MS:记忆当前显示的数字(Memory Save)
- MC:清除记忆的数字(Memory Clean)
- MR:显示记忆的数字(Memory Recall)
- M-:记忆的数字减去当前数字,并保存
- M+:记忆的数字加上当前数字,并保存
Metro风格来美化:https://www.cnblogs.com/ouyangkai/p/12759466.html
App.xaml
MainWindow.xaml
历史记录
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.Navigation;
using System.Windows.Shapes;
using MahApps.Metro.Controls;
using System.Windows.Controls.Primitives;
namespace Homework19
{
///
/// MainWindow.xaml 的交互逻辑
///
public partial class MainWindow : MetroWindow
{
public MainWindow()
{
InitializeComponent();
B_MC.IsEnabled = false;
B_M__1.IsEnabled = false;
B_M__.IsEnabled = false;
B_MR.IsEnabled = false;
}
//temp用于临时存储操作数,oper存储最近一次的运算符
private double temp, op1, op2, memory, result;
private string history=null,oper = null;
//next用来表示是否要输入新的操作数
private bool next;
//连续计算时先计算前面的结果
private void Aut()
{
op2 = Convert.ToDouble(Out_1.Text);
switch (oper)
{
case "+":
result = op1 + op2;
break;
case "-":
result = op1 - op2;
break;
case "×":
result = op1 * op2;
break;
case "÷":
result = op1 / op2;
break;
default:
break;
}
Out_1.Text = Convert.ToString(result);
//Out_2.Text = Convert.ToString(result);
}
private void Button_Click(object sender, RoutedEventArgs e)
{
//目标
this.contextMenu.PlacementTarget = this.btnMenu;
//位置
this.contextMenu.Placement = PlacementMode.Top;
//显示菜单
this.contextMenu.IsOpen = true;
}
private void B_NUM_Click(object sender, RoutedEventArgs e)
{
Button numBtn = sender as Button;
string numstr = numBtn.Content.ToString();
if (numstr != "0")
{
if (Out_1.Text == "0" || next)
{
Out_1.Text = numstr;
if (next)
{
next = false;
}
}
else
Out_1.Text += numstr;
}
else {
if (Out_1.Text != "0")
{
Out_1.Text += "0";
}
if (next)
{
Out_1.Text = "0";
next = false;
}
}
}
private void B_C_Click(object sender, RoutedEventArgs e)
{
Out_1.Text = "0";
Out_2.Text = "";
oper = null;
}
private void B_CE_Click(object sender, RoutedEventArgs e)
{
Out_1.Text = "0";
Out_2.Text = "";
oper = null;
}
private void operater_Button_Click(object sender, RoutedEventArgs e)
{
Button operaterBtn = sender as Button;
string operater = operaterBtn.Content.ToString();
Out_2.Text += Out_1.Text + operater;
if (oper != null)
{
Aut();
}
op1 = Convert.ToDouble(Out_1.Text);
oper = operater;
next = true;
}
private void B_delete_Click(object sender, RoutedEventArgs e)
{
if (!next)
{
if (Out_1.Text.Length == 1)
{
Out_1.Text = "0";
}
else
{
Out_1.Text = Out_1.Text.Substring(0, Out_1.Text.Length - 1);
}
}
}
private void B_reciprocal_Click(object sender, RoutedEventArgs e)
{
try
{
temp = Convert.ToDouble(Out_1.Text);
temp = 1 / temp;
Out_1.Text = Convert.ToString(temp);
}
catch (Exception ee)
{
Out_1.Text = "除数不能为0";
}
}
private void B_sq_Click(object sender, RoutedEventArgs e)
{
temp = Convert.ToDouble(Out_1.Text);
temp = Math.Pow(temp, 2);
Out_1.Text = Convert.ToString(temp);
}
private void B_cu_Click(object sender, RoutedEventArgs e)
{
temp = Convert.ToDouble(Out_1.Text);
temp = Math.Pow(temp, 3);
Out_1.Text = Convert.ToString(temp);
}
private void B_radical_Click(object sender, RoutedEventArgs e)
{
temp = Convert.ToDouble(Out_1.Text);
temp = Math.Pow(temp, 0.5);
Out_1.Text = Convert.ToString(temp);
}
private void B_per_Click(object sender, RoutedEventArgs e)
{
temp = Convert.ToDouble(Out_1.Text);
temp = temp / 100;
Out_1.Text = Convert.ToString(temp);
}
private void B_MR_Click(object sender, RoutedEventArgs e)
{
Out_1.Text = Convert.ToString(memory);
}
private void B_M__1_Click(object sender, RoutedEventArgs e)
{
memory += Convert.ToDouble(Out_1.Text);
}
private void B_M___Click(object sender, RoutedEventArgs e)
{
memory -= Convert.ToDouble(Out_1.Text);
}
private void B_MC_Click(object sender, RoutedEventArgs e)
{
memory = 0;
B_MC.IsEnabled = false;
B_M__1.IsEnabled = false;
B_M__.IsEnabled = false;
B_MR.IsEnabled = false;
}
private void B_MS_Click(object sender, RoutedEventArgs e)
{
memory = Convert.ToDouble(Out_1.Text);
B_MC.IsEnabled = true;
B_M__1.IsEnabled = true;
B_M__.IsEnabled = true;
B_MR.IsEnabled = true;
}
private void clear_Btn_Click(object sender, RoutedEventArgs e)
{
historyPanel.Children.Clear();
}
private void B_eq_Click(object sender, RoutedEventArgs e)
{
op2 = Convert.ToDouble(Out_1.Text);
switch (oper)
{
case "+":
result = op1 + op2;
break;
case "-":
result = op1 - op2;
break;
case "×":
result = op1 * op2;
break;
case "÷":
result = op1 / op2;
break;
default:
break;
}
oper = null;
history = Out_2.Text + Out_1.Text + "=";
Out_1.Text = Convert.ToString(result);
Out_2.Text = "";
Add_History(history, Convert.ToString(result));
}
private void B_opp_Click(object sender, RoutedEventArgs e)
{
temp = Convert.ToDouble(Out_1.Text);
if (temp != 0)
{
temp = 0 - temp;
}
Out_1.Text = Convert.ToString(temp);
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
Out_1.Text += ".";
}
private void Add_History(string cal, string result){
Border border = new Border();
border.CornerRadius = new CornerRadius(5);
border.Background = new SolidColorBrush(Colors.White);
border.BorderBrush = new SolidColorBrush(Colors.LightGray);
border.BorderThickness = new Thickness(1);
border.Height = 100;
Grid gird = new Grid();
TextBlock textBlock1 = new TextBlock();
textBlock1.HorizontalAlignment = HorizontalAlignment.Left;
textBlock1.Margin = new Thickness(10, 10, 0, 0);
textBlock1.TextWrapping = TextWrapping.Wrap;
textBlock1.Text = cal;
textBlock1.VerticalAlignment = VerticalAlignment .Top;
textBlock1.Width = 260;
textBlock1.Height = 28;
textBlock1.TextAlignment = TextAlignment.Right;
textBlock1.FontSize = 24;
TextBlock textBlock2 = new TextBlock();
textBlock2.HorizontalAlignment = HorizontalAlignment.Left;
textBlock2.Margin = new Thickness(10, 30, 0, 0);
textBlock2.TextWrapping = TextWrapping.Wrap;
textBlock2.Text = result;
textBlock2.VerticalAlignment = VerticalAlignment.Top;
textBlock2.Width = 260;
textBlock2.Height = 64;
textBlock2.TextAlignment = TextAlignment.Right;
textBlock2.FontSize = 54;
textBlock2.FontFamily = new FontFamily("Microsoft YaHei");
gird.Children.Add(textBlock1);
gird.Children.Add(textBlock2);
border.Child = gird;
historyPanel.Children.Add(border);
}
}
}
https://github.com/Microsoft/calculator
https://github.com/BuXianShan/Calculator
https://blog.csdn.net/weixin_43272781/article/details/106750837
https://blog.csdn.net/qq_43479622/article/details/91599602
https://blog.csdn.net/OneWord233/article/details/80755284
https://www.cnblogs.com/lonelyxmas/p/12117453.html
https://blog.csdn.net/weixin_42009898/article/details/80921151
https://www.cnblogs.com/ouyangkai/p/12759466.html