99 lines
2.5 KiB
C
99 lines
2.5 KiB
C
|
#include <stdio.h>
|
||
|
#include <unistd.h>
|
||
|
|
||
|
// #define p1
|
||
|
|
||
|
/* Bitmasks for different tree/decoration patterns
|
||
|
* use make <p1/p2/p3> to compile with different tree patterns
|
||
|
* default is p1
|
||
|
*/
|
||
|
#ifdef p1
|
||
|
#define pattern 0x942 // 100100100100
|
||
|
#endif
|
||
|
#ifdef p5
|
||
|
#define pattern 0xC2C2
|
||
|
#endif
|
||
|
#ifdef p2
|
||
|
#define pattern 0x1
|
||
|
#endif
|
||
|
#ifdef p3
|
||
|
#define pattern 0
|
||
|
#endif
|
||
|
#ifdef p4
|
||
|
#define pattern 0x333
|
||
|
#endif
|
||
|
|
||
|
void treeColor1() {
|
||
|
printf("\033[1;93;42m");
|
||
|
} // change terminal output color to green background, yellow text
|
||
|
void white() { printf("\033[0m"); } // change terminhl color to system default
|
||
|
void treeColor2() {
|
||
|
printf("\033[1;91;102m");
|
||
|
} // change terminal output color to redtext green background
|
||
|
void truncColor() {
|
||
|
printf("\033[31;43m");
|
||
|
} // change terminal output color to red(31)tesx and
|
||
|
void cls() { printf("\033[2J"); }
|
||
|
|
||
|
// print christmas tree with height n
|
||
|
void printTree(int n) {
|
||
|
for (int m = 0; 1; m++) {
|
||
|
// print leafy part of tree
|
||
|
for (int i = 0; i < n; i++) {
|
||
|
for (int j = 0; j < n - i; j++) {
|
||
|
printf(" "); // offset from left edge
|
||
|
}
|
||
|
for (int j = 0; j < (i * 2) + 1; j++) {
|
||
|
if (j & pattern) { // print tree pattern based on bitmask
|
||
|
(!(m & 1)) ? treeColor2() : treeColor1();
|
||
|
printf("%s", (!(m & 1)) ? "O" : "*");
|
||
|
white();
|
||
|
} else {
|
||
|
((m & 1)) ? treeColor2() : treeColor1();
|
||
|
printf("%s", (!(m & 1)) ? "*" : "O");
|
||
|
white();
|
||
|
}
|
||
|
}
|
||
|
printf("\n");
|
||
|
}
|
||
|
|
||
|
// print trunk with length and thickness based on tree size
|
||
|
for (int i = 0; i < (n / 6) || i < 1; i++) {
|
||
|
for (int j = 0; j < n - 2; j++) {
|
||
|
printf(" ");
|
||
|
}
|
||
|
truncColor();
|
||
|
printf("%s", (n < 8) ? "\033[0m \033[31;43m|#|"
|
||
|
: (n > 16) ? "|####|"
|
||
|
: "|###|");
|
||
|
white();
|
||
|
printf("\n");
|
||
|
}
|
||
|
sleep(1);
|
||
|
cls();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void useage(char *progName) {
|
||
|
printf("Useage:\n%s <tree_depth>\n <tree_depth> has to be greater than 3\n",
|
||
|
progName);
|
||
|
}
|
||
|
|
||
|
int main(int argc, char **argv) {
|
||
|
if (argc < 2) {
|
||
|
useage(argv[0]);
|
||
|
return 1;
|
||
|
}
|
||
|
// init treeDepth with invalid value as n would be undefined if sscanf fails
|
||
|
// this could cause crash if n is ised later
|
||
|
int treeDepth = -1;
|
||
|
int r = sscanf(argv[1], "%d", &treeDepth); // sscanf returns # charsscanned
|
||
|
if (r > 0 && treeDepth > 3) { // valid integer was scanned
|
||
|
printTree(treeDepth);
|
||
|
} else { // no valid integer detected display help message and quit
|
||
|
useage(argv[0]);
|
||
|
return 1;
|
||
|
}
|
||
|
return 0;
|
||
|
}
|