码迷,mamicode.com
首页 > Windows程序 > 详细

C#平均值计算器具体实现

时间:2018-11-09 20:54:03      阅读:245      评论:0      收藏:0      [点我收藏+]

标签:sys   set   .text   res   文章   rgs   编写   stat   有关   

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. 实际效果

 

技术分享图片

 

C#平均值计算器具体实现

标签:sys   set   .text   res   文章   rgs   编写   stat   有关   

原文地址:https://www.cnblogs.com/sunriseblogs/p/9936803.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!