Pre and Post State from Stroustrup Book

In Chapter 5.10.1, “Programming: Principles and Practices,” using C ++, there is a “Try this” exercise for debugging for bad input of an area. Prerequisites: if the inputs for length and width are 0 or negative, and the post-condition checks if the region is 0 or negative. To quote the problem, "Find a pair of values ​​so that the precondition of this version of the region is satisfied, but the post-condition does not matter." Code so far:

#include <iostream>
#include "std_lib_facilities.h"

int area (int length, int width) {
    if (length <= 0 || width <= 0) { error("area() pre-condition"); }
    int a =  length * width;
    if(a <= 0) { error("area() post-condition"); }
    return a;
}

int main() {

int a;
int b;
while (std::cin >> a >> b) {
    std::cout << area(a, b) << '\n';
}

system("pause");
return 0;
}

, , , -. , ascii, 0, . - - ?

+4
3

, .

0

, , , , .

, , , , 4 8- , 4- .

0

I tried this and it looks like this works: area(1000000,1000000);

The output was: -727379968

0
source

Source: https://habr.com/ru/post/1696065/


All Articles