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. 實際效果

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM