C# 学习之旅(3) --- 会说话的简易计算器
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Windows.Forms; 9 using System.Speech; 10 using System.Speech.Synthesis; 11 12 13 namespace calculator 14 { 15 public partial class Form1 : Form 16 { 17 SpeechSynthesizer sp = new SpeechSynthesizer(); 18 19 public Form1() 20 { 21 InitializeComponent(); 22 } 23 24 private void Add_Click(object sender, EventArgs e) 25 { 26 Compute compute = new Compute(); 27 compute.set(number1, number2); 28 answer.Text = string.Format("{0}",compute.add()); 29 sp.SpeakAsync(answer.Text); 30 } 31 32 private void Subtract_Click(object sender, EventArgs e) 33 { 34 Compute compute = new Compute(); 35 compute.set(number1, number2); 36 answer.Text = string.Format("{0}",compute.subtract()); 37 sp.SpeakAsync(answer.Text); 38 } 39 40 private void Multiply_Click(object sender, EventArgs e) 41 { 42 Compute compute = new Compute(); 43 compute.set(number1, number2); 44 answer.Text = string.Format("{0}", compute.multiply()); 45 sp.SpeakAsync(answer.Text); 46 } 47 48 private void Divide_Click(object sender, EventArgs e) 49 { 50 Compute compute = new Compute(); 51 compute.set(number1, number2); 52 answer.Text = string.Format("{0}", compute.divide()); 53 sp.SpeakAsync(answer.Text); 54 } 55 56 private void Power_Click(object sender, EventArgs e) 57 { 58 Compute compute = new Compute(); 59 compute.set(number1, number2); 60 answer.Text = string.Format("{0}", compute.power()); 61 sp.SpeakAsync(answer.Text); 62 } 63 64 private void number1_TextChanged(object sender, EventArgs e) 65 { 66 string str = number1.Text.Trim(); 67 68 for (int i = 0; i < str.Length; i++) 69 { 70 if (!Char.IsNumber(str[i])) 71 { 72 number1.Text = string.Empty; 73 number1.BackColor = Color.Red; 74 } 75 else 76 { 77 number1.BackColor = Color.Empty; 78 } 79 } 80 } 81 82 private void number2_TextChanged(object sender, EventArgs e) 83 { 84 string str = number2.Text.Trim(); 85 for (int i = 0; i < str.Length; i++) 86 { 87 if (!Char.IsNumber(str[i])) 88 { 89 number2.Text = string.Empty; 90 number2.BackColor = Color.Red; 91 } 92 else 93 { 94 number2.BackColor = Color.Empty; 95 } 96 } 97 } 98 99 } 100 101 class Compute { 102 private double a; 103 private double b; 104 private double x; 105 106 public void set(object a, object b) 107 { 108 TextBox tempa = (TextBox)a; 109 TextBox tempb = (TextBox)b; 110 this.a = double.Parse(tempa.Text); 111 this.b = double.Parse(tempb.Text); 112 } 113 114 public double add() 115 { 116 this.x = this.a + this.b; 117 return this.x; 118 } 119 120 public double subtract() 121 { 122 this.x = this.a - this.b; 123 return this.x; 124 } 125 126 public double multiply() 127 { 128 this.x = this.a * this.b; 129 return this.x; 130 } 131 132 public double divide() 133 { 134 this.x = this.a / this.b; 135 return this.x; 136 } 137 138 public double power() 139 { 140 this.x = Math.Pow(this.a, this.b); 141 return this.x; 142 } 143 } 144 }
看到这里您辛苦了,谢谢 : )
—————————————————————————————————————————————————————————————————————————————
声明:
本文为 大Yi巴狼 对自己所学的知识整理和实现。
本文档欢迎自由转载,但请务必保持本文档完整或注明来之本文档。本文档未经 大Yi巴狼 同意,不得用于商业用途。最后,如果您能从这个简单文档里获得些许帮助,大Yi巴狼 将对自己的一点努力感到非常高兴;由于作者本人水平有限,如果本文档中包含的错误给您造成了不便,在此提前说声抱歉。
祝身体健康,工作顺利。