計算器程序是自己開始學習c#時自己編寫的一小程序,自己開始一直在摸索,在這其中也遇到了很多問題,比如正負號的問題,清零的操作等等。后來在同事的幫忙下順利完成了。畫面有些粗糙,望大家見諒哈~~~
using System;
using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;namespace WindowsFormsApplication45
{ public partial class Form1 : Form { static string mystring;//接受的字符串 bool val1=false;//是否點擊操作符 string leftstring;//操作符左邊的操作數 string rightstring;//操作符右邊的操作數 string strOp;//操作符 public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { mystring += (this.button1.Text).ToString(); mystring = Convert.ToDouble(mystring).ToString(); this.textBox1.Text = mystring; }//數字1 private void button2_Click(object sender, EventArgs e) { mystring += (this.button2.Text).ToString(); mystring = Convert.ToDouble(mystring).ToString(); this.textBox1.Text = mystring; }//數字2 private void button3_Click(object sender, EventArgs e) { mystring += (this.button3.Text).ToString(); mystring = Convert.ToDouble(mystring).ToString(); this.textBox1.Text = mystring; }//數字3 private void button4_Click_1(object sender, EventArgs e) { mystring += (this.button4.Text).ToString(); mystring = Convert.ToDouble(mystring).ToString(); this.textBox1.Text = mystring; }//數字4 private void button5_Click_1(object sender, EventArgs e) { mystring += (this.button5.Text).ToString(); mystring = Convert.ToDouble(mystring).ToString();this.textBox1.Text = mystring;
}//數字5 private void button6_Click_1(object sender, EventArgs e) { mystring += (this.button6.Text).ToString(); mystring = Convert.ToDouble(mystring).ToString(); this.textBox1.Text = mystring; }//數字6 private void button7_Click_1(object sender, EventArgs e) { mystring += (this.button7.Text).ToString(); mystring = Convert.ToDouble(mystring).ToString(); this.textBox1.Text = mystring; }//數字7 private void button8_Click_1(object sender, EventArgs e) { mystring += (this.button8.Text).ToString(); mystring = Convert.ToDouble(mystring).ToString(); this.textBox1.Text = mystring; }//數字8 private void button9_Click_1(object sender, EventArgs e) { mystring += (this.button9.Text).ToString(); mystring = Convert.ToDouble(mystring).ToString(); this.textBox1.Text = mystring; }//數字9 private void button10_Click(object sender, EventArgs e) { mystring += (this.button10.Text).ToString(); this.textBox1.Text = mystring; }//數字0private void button12_Click(object sender, EventArgs e)
{ if (mystring.Contains(".")) { } else { mystring += (this.button12.Text).ToString(); } this.textBox1.Text = mystring; }//小數點操作 private void button11_Click(object sender, EventArgs e) { if (this.textBox1.Text.Contains("-")) { this.textBox1.Text = this.textBox1.Text.TrimStart('-'); } else { this.textBox1.Text = "-" + this.textBox1.Text; } }//正負號操作private void button18_Click(object sender, EventArgs e) { mystring = string.Empty; this.textBox1.Text = ""; val1 = false; }//清零操作
private void button13_Click(object sender, EventArgs e)
{ strOp = "+"; if (val1==false) { leftstring = mystring; mystring = string.Empty; } else {leftstring = (this.textBox1.Text).ToString();
} val1 = true; }//加發操作private void button14_Click(object sender, EventArgs e)
{ strOp = "-"; if (val1 == false) { leftstring = mystring; mystring = string.Empty; } else { leftstring = (this.textBox1.Text).ToString(); } val1 = true; }//減發操作private void button15_Click(object sender, EventArgs e)
{ strOp = "*"; if (val1 == false) { leftstring = mystring; mystring = string.Empty; } else { leftstring = (this.textBox1.Text).ToString(); } val1 = true; }//乘發操作private void button16_Click(object sender, EventArgs e)
{ strOp = "/"; if (val1 == false) { leftstring = mystring; mystring = string.Empty; } else { leftstring = (this.textBox1.Text).ToString(); } val1 = true; }//除法操作private void button17_Click(object sender, EventArgs e)
{ rightstring = mystring; switch(strOp) { case "+": this.textBox1.Text = (Convert.ToDouble(leftstring) + Convert.ToDouble(rightstring)).ToString(); mystring = string.Empty; break; case "-": this.textBox1.Text = (Convert.ToDouble(leftstring) - Convert.ToDouble(rightstring)).ToString(); mystring = string.Empty; break; case "*": this.textBox1.Text = (Convert.ToDouble(leftstring) * Convert.ToDouble(rightstring)).ToString(); mystring = string.Empty; break; case "/": this.textBox1.Text = (Convert.ToDouble(leftstring) / Convert.ToDouble(rightstring)).ToString(); mystring = string.Empty; break; case "sin": this.textBox1.Text = Math.Sin(Math.PI*(Convert.ToDouble(this.textBox1.Text))/180).ToString(); break; case "cos": this.textBox1.Text = Math.Sin(Math.PI * (Convert.ToDouble(this.textBox1.Text)) / 180).ToString(); break; case "tan": this.textBox1.Text = Math.Sin(Math.PI * (Convert.ToDouble(this.textBox1.Text)) / 180).ToString(); break; case "sqrt": this.textBox1.Text = Math.Sqrt(Convert.ToDouble(this.textBox1.Text)).ToString(); mystring = this.textBox1.Text; break; default: break;}
}//計算操作
private void button19_Click(object sender, EventArgs e)
{ if (this.textBox1.Text !=string.Empty) { this.textBox1.Text = this.textBox1.Text.Remove(this.textBox1.Text.Length -1); mystring = this.textBox1.Text; } else { MessageBox.Show("您好,現在所顯示的數據已經為空了,請您再次輸入正確的數據!"); } }//退格操作private void button20_Click(object sender, EventArgs e)
{ strOp = "sin";if (val1 == false)
{ leftstring = mystring; mystring = string.Empty; } else { } val1 = true; }// sin 操作private void button24_Click(object sender, EventArgs e)
{ Help.ShowHelp(this, "C:\\WINDOWS\\system32\\calc.exe");// 显示XP系统自带计算器}
private void button21_Click(object sender, EventArgs e)
{ strOp = "cos";if (val1 == false)
{ leftstring = mystring; mystring = string.Empty; } else {}
val1 = true; }// cos 操作private void button22_Click(object sender, EventArgs e)
{ strOp = "tan";if (val1 == false)
{ leftstring = mystring; mystring = string.Empty; } else {}
val1 = true; }// tan 操作private void button23_Click(object sender, EventArgs e)
{ strOp = "sqrt";if (val1 == false)
{ leftstring = mystring; mystring = string.Empty; } else {}
val1 = true; }// sqrt 操作 }}