記錄一個c#計算器程序
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;
namespace 計算器
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
/// <summary>
/// 用來將按鍵所展示的數字顯示在屏幕上
/// </summary>
/// <param name="num"></param>
public void Write_in(int num)
{
textBox1.Text = textBox1.Text + num.ToString();
}
/// <summary>
/// 按鍵1
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
Write_in(1);
}
/// <summary>
/// 按鍵2
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button2_Click(object sender, EventArgs e)
{
Write_in(2);
}
/// <summary>
/// 按鍵3
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button3_Click(object sender, EventArgs e)
{
Write_in(3);
}
/// <summary>
/// 按鍵4
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button4_Click(object sender, EventArgs e)
{
Write_in(4);
}
/// <summary>
/// 按鍵5
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button5_Click(object sender, EventArgs e)
{
Write_in(5);
}
/// <summary>
/// 按鍵6
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button6_Click(object sender, EventArgs e)
{
Write_in(6);
}
/// <summary>
/// 按鍵7
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button7_Click(object sender, EventArgs e)
{
Write_in(7);
}
/// <summary>
/// 按鍵8
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button8_Click(object sender, EventArgs e)
{
Write_in(8);
}
/// <summary>
/// 按鍵9
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button9_Click(object sender, EventArgs e)
{
Write_in(9);
}
/// <summary>
/// 按鍵0
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button0_Click(object sender, EventArgs e)
{
Write_in(0);
}
/// <summary>
/// 加法運算
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button_add_Click(object sender, EventArgs e)
{
textBox1.Text += "+";
}
/// <summary>
/// 減法運算
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button_sub_Click(object sender, EventArgs e)
{
textBox1.Text += "-";
}
/// <summary>
/// 乘法運算
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button_mul_Click(object sender, EventArgs e)
{
textBox1.Text += "×";
}
/// <summary>
/// 除法運算
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button_div_Click(object sender, EventArgs e)
{
textBox1.Text += "÷";
}
/// <summary>
/// 等號運算
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button_equal_Click(object sender, EventArgs e)
{
string temp = textBox1.Text;
if (temp.Contains("+"))
{
int index = temp.IndexOf("+"); // 加號的位置
string lStr = temp.Substring(0, index);
string rStr = temp.Substring(index + 1);
double result = Convert.ToDouble(lStr) + Convert.ToDouble(rStr);
textBox1.Text = result.ToString();
}
if (temp.Contains("-")&&temp.IndexOf("-")!=0)
{
int index = temp.IndexOf("-"); // 減號的位置
string lStr;
string rStr;
lStr = temp.Substring(0, index);
rStr = temp.Substring(index + 1);
double result = Convert.ToDouble(lStr) - Convert.ToDouble(rStr);
textBox1.Text = result.ToString();
}
if(temp.Contains("×"))
{
int index = temp.IndexOf("×"); // 乘號的位置
string lStr = temp.Substring(0, index);
string rStr = temp.Substring(index + 1);
double result = Convert.ToDouble(lStr) * Convert.ToDouble(rStr);
textBox1.Text = result.ToString();
}
if(temp.Contains("÷"))
{
int index = temp.IndexOf("÷"); // 除號的位置
string lStr = temp.Substring(0, index);
string rStr = temp.Substring(index + 1);
double result = Convert.ToDouble(lStr) / Convert.ToDouble(rStr);
textBox1.Text = result.ToString();
}
}
/// <summary>
/// 按鍵C,清空輸入
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button_delete_Click(object sender, EventArgs e)
{
textBox1.Text = "";
}
/// <summary>
/// 按鍵".",浮點運算
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button_point_Click(object sender, EventArgs e)
{
textBox1.Text += ".";
}
/// <summary>
///按鍵<-, 刪除尾部的一個字符
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button_back_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text.Remove(textBox1.Text.Length - 1);
}
/// <summary>
/// 關閉窗口OFF
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button_symbol_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
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;
namespace 計算器
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
/// <summary>
/// 用來將按鍵所展示的數字顯示在屏幕上
/// </summary>
/// <param name="num"></param>
public void Write_in(int num)
{
textBox1.Text = textBox1.Text + num.ToString();
}
/// <summary>
/// 按鍵1
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
Write_in(1);
}
/// <summary>
/// 按鍵2
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button2_Click(object sender, EventArgs e)
{
Write_in(2);
}
/// <summary>
/// 按鍵3
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button3_Click(object sender, EventArgs e)
{
Write_in(3);
}
/// <summary>
/// 按鍵4
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button4_Click(object sender, EventArgs e)
{
Write_in(4);
}
/// <summary>
/// 按鍵5
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button5_Click(object sender, EventArgs e)
{
Write_in(5);
}
/// <summary>
/// 按鍵6
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button6_Click(object sender, EventArgs e)
{
Write_in(6);
}
/// <summary>
/// 按鍵7
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button7_Click(object sender, EventArgs e)
{
Write_in(7);
}
/// <summary>
/// 按鍵8
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button8_Click(object sender, EventArgs e)
{
Write_in(8);
}
/// <summary>
/// 按鍵9
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button9_Click(object sender, EventArgs e)
{
Write_in(9);
}
/// <summary>
/// 按鍵0
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button0_Click(object sender, EventArgs e)
{
Write_in(0);
}
/// <summary>
/// 加法運算
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button_add_Click(object sender, EventArgs e)
{
textBox1.Text += "+";
}
/// <summary>
/// 減法運算
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button_sub_Click(object sender, EventArgs e)
{
textBox1.Text += "-";
}
/// <summary>
/// 乘法運算
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button_mul_Click(object sender, EventArgs e)
{
textBox1.Text += "×";
}
/// <summary>
/// 除法運算
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button_div_Click(object sender, EventArgs e)
{
textBox1.Text += "÷";
}
/// <summary>
/// 等號運算
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button_equal_Click(object sender, EventArgs e)
{
string temp = textBox1.Text;
if (temp.Contains("+"))
{
int index = temp.IndexOf("+"); // 加號的位置
string lStr = temp.Substring(0, index);
string rStr = temp.Substring(index + 1);
double result = Convert.ToDouble(lStr) + Convert.ToDouble(rStr);
textBox1.Text = result.ToString();
}
if (temp.Contains("-")&&temp.IndexOf("-")!=0)
{
int index = temp.IndexOf("-"); // 減號的位置
string lStr;
string rStr;
lStr = temp.Substring(0, index);
rStr = temp.Substring(index + 1);
double result = Convert.ToDouble(lStr) - Convert.ToDouble(rStr);
textBox1.Text = result.ToString();
}
if(temp.Contains("×"))
{
int index = temp.IndexOf("×"); // 乘號的位置
string lStr = temp.Substring(0, index);
string rStr = temp.Substring(index + 1);
double result = Convert.ToDouble(lStr) * Convert.ToDouble(rStr);
textBox1.Text = result.ToString();
}
if(temp.Contains("÷"))
{
int index = temp.IndexOf("÷"); // 除號的位置
string lStr = temp.Substring(0, index);
string rStr = temp.Substring(index + 1);
double result = Convert.ToDouble(lStr) / Convert.ToDouble(rStr);
textBox1.Text = result.ToString();
}
}
/// <summary>
/// 按鍵C,清空輸入
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button_delete_Click(object sender, EventArgs e)
{
textBox1.Text = "";
}
/// <summary>
/// 按鍵".",浮點運算
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button_point_Click(object sender, EventArgs e)
{
textBox1.Text += ".";
}
/// <summary>
///按鍵<-, 刪除尾部的一個字符
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button_back_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text.Remove(textBox1.Text.Length - 1);
}
/// <summary>
/// 關閉窗口OFF
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button_symbol_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.