该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
#include
#include
typedef bool(*_fun_ptr)(int*, int*, int, int);
void fun(int *a, int* b, int size, int x, _fun_ptr do_fun, char* format, ...)
{
va_list args;
va_start(args, format);
for (int i = 0; i < size; ++i)
{
if (do_fun(a, b, i, x))
{
vprintf(format, args);
}
}
va_end(args);
}
#define cmp_name(name) \
cmp_##name
#define _cmp_fun(name, return_type, code) \
return_type cmp_name(name)\
code
#define cmp_fun(name, code) \
_cmp_fun(name , bool, (int* a, int* b, int i, int x) code)
cmp_fun(larger_than_x, {
if (a[i] + a[i + 1] > x) { printf("%d + %d = %d\n", a[i], a[i + 1], b[i]); return 1; }
return 0;
})
cmp_fun(less_than_x, {
if (a[i] + a[i + 1] < x) { printf("%d + %d = %d\n", a[i], a[i + 1], b[i]); return 1; }
return 0;
})
cmp_fun(b1_less_than_b2, {
if (b[i] < b[i + 1]){ printf("%d+%d < %d+%d\n", a[i], a[i + 1], a[i + 1], a[i + 2]); return 1; }
return 0;
})
int main()
{
#define _SIZE (10)
int a[_SIZE], b[_SIZE - 1], x;
for (int i = 0; i < _SIZE; i++)
scanf("%d", &a[i]);
for (int i = 0; i < _SIZE - 1; ++i)
b[i] = a[i] + a[i + 1];
scanf("%d", &x);
fun(a, b, _SIZE - 1, x, cmp_name(larger_than_x), "larger than x\n");
fun(a, b, _SIZE - 1, x, cmp_name(less_than_x), "less than x\n");
fun(a, b, _SIZE - 2, x, cmp_name(b1_less_than_b2), "b1 less than b2\n");
return 0;
}
咯,无聊写了一下。