1. 程式人生 > >C#平均值計算器具體實現

C#平均值計算器具體實現

1. 題目及要求

 

2. Avg.cs

 

在直接編寫視窗程式之前,我們需要建立一個Avg類,我們可以在類庫中編輯,也可以像java一樣直接在專案中新建類。

 

有關類庫的建立與連線方法,我們在上一次的《C#四則運算器(多型方法實現)》中已經詳細講述過,這裡就不再贅述,我這裡是直接專案中新建類編寫的

 

 

Avg.cs的具體程式碼如下:

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  
4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace cs平均值計算器_20181024 8 { 9 public class Avg //宣告平均數的類 10 { 11 private double num1; //宣告運算元A 12 private double num2; //宣告運算元B 13 private double weight1; //宣告權重A 14 private double weight2; //
宣告權重B 15 16 //宣告四個變數的索引器 17 public double Num1 18 { 19 get 20 { 21 return num1; 22 } 23 set 24 { 25 num1 = value; 26 } 27 } 28 public double Num2
29 { 30 get 31 { 32 return num2; 33 } 34 set 35 { 36 num2 = value; 37 } 38 } 39 public double Weight1 40 { 41 get 42 { 43 return weight1; 44 } 45 set 46 { 47 weight1 = value; 48 } 49 } 50 public double Weight2 51 { 52 get 53 { 54 return weight2; 55 } 56 set 57 { 58 weight2 = value; 59 } 60 } 61 62 //檢查輸入的字串是否能夠轉換為數字 63 public static bool CheckNum(string s1, string s2, string s3, string s4) 64 { 65 double num; 66 bool flag = true; //宣告標誌訊號flag 67 68 //四個字串中若有任何一個無法轉換為數字,flag都為假 69 if (!double.TryParse(s1, out num)) 70 flag = false; 71 else if (!double.TryParse(s2, out num)) 72 flag = false; 73 else if (!double.TryParse(s3, out num)) 74 flag = false; 75 else if (!double.TryParse(s4, out num)) 76 flag = false; 77 78 return flag; //返回flag的值 79 } 80 //只檢查兩個數字的CheckNum 81 public static bool CheckNum(string s1, string s2) 82 { 83 double num; 84 bool flag = true; //宣告表示訊號flag 85 86 //兩個數中的任意一個無法轉換為數字,flag都為假 87 if (!double.TryParse(s1, out num)) 88 flag = false; 89 else if (!double.TryParse(s2, out num)) 90 flag = false; 91 92 return flag; //返回flag的值 93 } 94 95 //得到結果字串 96 public virtual string GetResult() 97 { 98 return "兩數沒有做任何操作"; 99 } 100 } 101 102 public class CalAvg:Avg //宣告計算算術平均值的類 103 { 104 //CalAvg的建構函式 105 public CalAvg(double num1, double num2) 106 { 107 Num1 = num1; 108 Num2 = num2; 109 } 110 111 //過載父方法中的GetResult()方法 112 public override string GetResult() 113 { 114 string result = "兩數的算術平均值為:"; 115 double num = (Num1 + Num2) / 2; 116 result += string.Format("{0:f}", num); //將結果變為字串並保留兩位小數 117 return result; 118 } 119 } 120 121 public class CalWAvg : Avg //宣告計算加權平均值的類 122 { 123 //CalWAvg的建構函式 124 public CalWAvg(double num1, double num2, double weight1, double weight2) 125 { 126 Num1 = num1; 127 Num2 = num2; 128 Weight1 = weight1; 129 Weight2 = weight2; 130 } 131 132 //過載父方法中的GetResult()方法 133 public override string GetResult() 134 { 135 string result = "兩數的加權平均值為:"; 136 double num = (Num1 * Weight1 + Num2 * Weight2) / 2; //計算兩數的加權平均值 137 result += string.Format("{0:f}", num); //將結果變為字串並保留兩位小數 138 return result; 139 } 140 } 141 }

 

 

3. Form1.cs

接下來我們在視覺化視窗上拖動控制元件並程式設計,關於視覺化視窗控制元件的操作這裡也不再詳細講解了,不懂的同學請自行翻閱《C#四則運算器(多型實現方法)》的文章。

頁面佈局及部分控制元件命名如下:

Form1.cs的程式碼如下:

 

 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.Threading.Tasks;
 9 using System.Windows.Forms;
10 
11 namespace cs平均值計算器_20181024
12 {
13     public partial class Form1 : Form
14     {
15         public Form1()
16         {
17             InitializeComponent();
18             this.Text = "平均數計算器";   //設定視窗標題
19         }
20 
21         private void ButtonAvg_Click(object sender, EventArgs e)
22         {
23             string n1 = TextBoxNum1.Text;   //得到第一個數的字串
24             string n2 = TextBoxNum2.Text;   //得到第二個數的字串
25             if(!Avg.CheckNum(n1,n2))    //若輸入的數字不符合規範,發出警告並返回
26             {
27                 MessageBox.Show("請輸入符合規範的數字!", "警告",
28                     MessageBoxButtons.OK, MessageBoxIcon.Warning);
29                 return;
30             }
31 
32             //建立CalAvg例項物件avg
33             Avg avg = new CalAvg(Convert.ToDouble(n1), Convert.ToDouble(n2));
34 
35             LabelResult.Text = avg.GetResult(); //將結果顯示到視窗上
36         }
37 
38         private void ButtonWAvg_Click(object sender, EventArgs e)
39         {
40             string n1 = TextBoxNum1.Text;   //得到第一個數的字串
41             string n2 = TextBoxNum1.Text;   //得到第二個數的字串
42             string w1 = TextBoxWeight1.Text;//得到第一個權重的字串
43             string w2 = TextBoxWeight2.Text;//得到第二個權重的字串
44             if(!Avg.CheckNum(n1, n2, w1, w2))   //若輸入的數字不符合規範,發出警告並返回
45             {
46                 MessageBox.Show("請輸入符合規範的數字!", "警告",
47                     MessageBoxButtons.OK, MessageBoxIcon.Warning);
48                 return;
49             }
50 
51             //建立CalWAvg例項物件avg
52             Avg avg = new CalWAvg(Convert.ToDouble(n1), Convert.ToDouble(n2),
53                 Convert.ToDouble(w1), Convert.ToDouble(w2));
54 
55             LabelResult.Text = avg.GetResult(); //將結果顯示到視窗上
56         }
57     }
58 }

 

 

4. 實際效果