模拟苹果手机计算器
这是 利用 SunnyUi 可视化设计的 UI界面
接下来是简单的计算逻辑的,有些细节还是可以继续优化的
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Sunny.UI;
namespace EBear
{
public partial class F_Counter : UIForm
{
public F_Counter()
{
InitializeComponent();
}
string G_str1, G_str2;//输入值
//按钮输入方法
private void Input(string str)
{
if (G_str1 =="0")
{
if (str ==".")
{
G_str1 += str;
}
else
{
G_str1 = str;
}
}
else
{
G_str1 += str;
}
//格式显示问题
ShowView(G_str1);
}
private void ShowView(string str)
{
//格式显示问题
if (str.IndexOf(".") == -1) //整数
{
ShowText.Text = string.Format("{0:N0}", Convert.ToInt32(str));
}
else
{
string str1, str2 = "";
string[] arr = str.Split(".");
str1 = arr[0];
if (arr.Length > 1) str2 = arr[1];
ShowText.Text = string.Format("{0:N0}", Convert.ToInt32(str1)) + "." + str2;
}
}
//按 0 事件
private void Zern_Btn_Click(object sender, EventArgs e)
{
Input(Zern_Btn.TagString);
}
//按 . 事件
private void Spot_Btn_Click(object sender, EventArgs e)
{
if (G_str1.IndexOf(".") == -1)
{
Input(Spot_Btn.TagString);
}
}
//按 1 事件
private void One_Btn_Click(object sender, EventArgs e)
{
Input(One_Btn.TagString);
}
//按 2 事件
private void Two_Btn_Click(object sender, EventArgs e)
{
Input(Two_Btn.TagString);
}
//按 3 事件
private void Three_Btn_Click(object sender, EventArgs e)
{
Input(Three_Btn.TagString);
}
//按 4 事件
private void Four_Btn_Click(object sender, EventArgs e)
{
Input(Four_Btn.TagString);
}
//按 5 事件
private void Five_Btn_Click(object sender, EventArgs e)
{
Input(Five_Btn.TagString);
}
//按 6 事件
private void Six_Btn_Click(object sender, EventArgs e)
{
Input(Six_Btn.TagString);
}
//按 7 事件
private void Seven_Btn_Click(object sender, EventArgs e)
{
Input(Seven_Btn.TagString);
}
//按 8 事件
private void Eight_Btn_Click(object sender, EventArgs e)
{
Input(Eight_Btn.TagString);
}
//按 9 事件
private void Night_Btn_Click(object sender, EventArgs e)
{
Input(Night_Btn.TagString);
}
//按 AC 事件
private void AC_Btn_Click(object sender, EventArgs e)
{
G_str1 = "0";
G_str2 = "0";
Input("0");
}
//按 +/- 正负事件
private void Opp_Btn_Click(object sender, EventArgs e)
{
G_str1 = Convert.ToString(Convert.ToDouble(G_str1) *-1);
ShowView(G_str1);
}
//按 % 百分比事件
private void Per_Btn_Click(object sender, EventArgs e)
{
G_str1 = Convert.ToString(Convert.ToDouble(G_str1) / 100);
ShowView(G_str1);
}
//按 ÷ 除法事件
private void Div_Btn_Click(object sender, EventArgs e)
{
Operation("除");
}
//按 × 乘法事件
private void Mul_Btn_Click(object sender, EventArgs e)
{
Operation("乘");
}
//按 - 减法事件
private void Sub_Btn_Click(object sender, EventArgs e)
{
Operation("减");
}
//按 + 加法事件
private void Add_Btn_Click(object sender, EventArgs e)
{
Operation("加");
}
private void Operation(string str)
{
//赋值给第2operation
G_str2 = G_str1;
G_str1 = "0";
Input("0");
Res = str;
}
//按 = 等于事件
string Res;
private void Res_Btn_Click(object sender, EventArgs e)
{
switch (Res)
{
case "加": G_str2 = Convert.ToString(Convert.ToDouble(G_str2) + Convert.ToDouble(G_str1)); ShowView(G_str2);break;
case "减":G_str2= Convert.ToString(Convert.ToDouble(G_str2) - Convert.ToDouble(G_str1)); ShowView(G_str2); break;
case "除":G_str2= Convert.ToString(Convert.ToDouble(G_str2) / Convert.ToDouble(G_str1)); ShowView(G_str2); break;
case "乘":G_str2= Convert.ToString(Convert.ToDouble(G_str2) * Convert.ToDouble(G_str1)); ShowView(G_str2); break;
default:
break;
}
}
}
}