调和平均数(harmonic mean)是n个变量倒数的算术平均数的倒数。
几何平均数(geometric mean)是指n个变量连乘积的n次方根:
hMean函数和gMean函数分别实现两个变量的调和平均数和几何平均数的计算。如果变量的值不满足规定条件,则采用异常处理方法终止函数的执行,如代码清单17.3所示。
对程序进行编译和连接,没有出现错误。下面是程序运行的一个示例:
Exception Handle 1:
Enter two numbers:1 2
Harmonic mean of 1 and 2 is 1.33333
Geometric mean of 1 and 2 is 1.41421
Enter a new pair of numbers<Ctrl+z:quit>:1-1
(x+y)==0 not allowed.
Enter a new pair of numbers<Ctrl+z:quit>:1-2
Harmonic mean of 1 and-2 is 4
Negative values not allowed.
Enter a new pair of numbers<Ctrl+z:quit>:∧Z(www.daowen.com)
Exception Handle 2:
Enter two numbers:1 2
Harmonic mean of 1 and 2 is 1.33333
Geometric mean of 1 and 2 is 1.41421
Enter a new pair of numbers<Ctrl+z:quit>:1-1
(x+y)==0 not allowed.
Negative values not allowed.
Enter a new pair of numbers<Ctrl+z:quit>:1-2
Harmonic mean of 1 and-2 is 4
Negative values not allowed.
Enter a new pair of numbers<Ctrl+z:quit>:∧ Z
仔细分析上述程序运行示例,分析一下两种异常处理方案的差异,并利用以前实验介绍的调试方法跟踪程序的执行流程,理解异常处理的执行逻辑。
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
免责声明:以上内容源自网络,版权归原作者所有,如有侵犯您的原创版权请告知,我们将尽快删除相关内容。