Skip to content

Commit

Permalink
去除了 tsub_ok.c 中的 if 语句
Browse files Browse the repository at this point in the history
  • Loading branch information
aQua authored Jun 18, 2018
1 parent 0b46ff6 commit 0ff0085
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions chapter2/code/tsub-ok.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,19 @@
#include <limits.h>

/* Determine whether arguments can be substracted without overflow */
int tsub_ok(int x, int y) {
if (y == INT_MIN) {
return 0;
}
int tsub_ok(int x, int y)
{
int res = 1;

int neg_y = -y;
int sum = x + neg_y;
int pos_over = x > 0 && neg_y > 0 && sum < 0;
int neg_over = x < 0 && neg_y < 0 && sum >= 0;
(y == INT_MIN) && (res = 0);

return !(pos_over || neg_over);
int sub = x - y;
int pos_over = x > 0 && y < 0 && sub < 0;
int neg_over = x < 0 && y > 0 && sub > 0;

res = res && !(pos_over || neg_over);

return res;
}

int main(int argc, char* argv[]) {
Expand Down

0 comments on commit 0ff0085

Please sign in to comment.