The scanf()
doesn't know if you would enter a division sign /
. You need to change its format:
scanf("%d/%d", &a, &b);
So that you could enter 28000/3
or similar inputs.
Here's the perfect code (notice comments):
#include <stdio.h>
#include <limits.h>
// Definitions must be global
int divide(int a, int b, int *result);
int main(void) {
int a, b;
// It's programmer's responsibility to ensure the input
if (scanf("%d/%d", &a, &b) != 2) {
puts("Arguments are incorrectly passed.");
return -1;
}
int c;
if (divide(a, b, &c))
printf("%d/%d=%d
", a, b, c);
else
printf("b is zero.
"); // also code the 'else' to print an error
// if b is zero
return 0;
}
int divide(int a, int b, int *result) {
int ret = 1;
if (b == 0 || (a == INT_MIN && b == -1))
ret = 0;
else
*result = a / b;
return ret;
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…