【发布时间】:2015-12-31 11:01:05
【问题描述】:
我正在通过阅读 Stroustrup 的“使用 C++ 的原理和实践”来学习 C++。
在关于前置条件和后置条件的部分中,有以下函数示例:
int area(int length, int width)
// calculate area of a rectangle;
// pre-conditions: length and width are positive
// post-condition: returns a positive value that is the area
{
if (length<=0 || width <=0)
error("area() pre-condition");
int a = length*width;
if (a<=0)
error("area() post-condition");
return a;
}
让我困惑的是这段代码的任务:
找到一对值使得这个版本的前提条件 面积成立,但后置条件不成立。
是否存在前置条件可以但后置条件不行的整数值?
【问题讨论】:
-
不,没有,除了导致未定义行为的值。幸运的是你有办法检查这个stackoverflow.com/questions/199333/…