Rozwiązanie z ?: się nie skaluje :P
#include <cstdio>
#include <cstdlib>
int main() {
#define a1 5
#define b1 -2
#define a2 5
#define b2 -8
#define a3 7
#define b3 -2
#define a4 5
#define b4 2
const char *formatString = "(%d)+(%d)+(%d)+(%d)+(%d)+(%d)+(%d)+(%d)="
#if (a1 + b1) >= 0
"%d + "
#else
"(%d) + "
#endif
#if (a2 + b2) >= 0
"%d + "
#else
"(%d) + "
#endif
#if (a3 + b3) >= 0
"%d + "
#else
"(%d) + "
#endif
#if (a4 + b4) >= 0
"%d"
#else
"(%d)"
#endif
;
printf(formatString, a1, b1, a2, b2, a3, b3, a4, b4, a1 + b1, a2 + b2, a3 + b3, a4 + b4);
return 0;
}
#include <cstdio>
#include <cstdlib>
int main() {
#if 5 > 8
#define ImieKota "Burek"
#else
#define ImieKota "Alik"
#endif
#if 6 < 4
#define ImiePsa "Alicja"
#else
#define ImiePsa "Burek"
#endif
#if 5 == 8
#define ImiePana "Dżeki"
#else
#define ImiePana "Maciek"
#endif
puts("Imie kota: " ImieKota ", imie psa: " ImiePsa ", imie pana: " ImiePana);
return 0;
}
Kolejny sposób, z użyciem %s w printfie:
#include <cstdio>
#include <cstdlib>
const char* ln(int v) {
if (v < 0) {
return "(";
} else {
return "";
}
}
const char* pn(int v) {
if (v < 0) {
return ")";
} else {
return "";
}
}
int main() {
int a = 5, b = -8;
printf("(%d)+(%d)=%s%d%s", a, b, ln(a + b), (a + b), pn(a + b));
return 0;
}