/* Problem 3--Christmas Trees
   This was a straightforward ASCII art problem */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX(a,b) ((a)>(b)?(a):(b))

FILE *in, *out;

int main (int argc, char **argv);
void Process (int s1, int s2);
void AddLine (char *tstr, int s, int i);
void Trim (char *tstr);

int main (int argc, char **argv) {

  int s1, s2;

 in = fopen ("prob3.in","r");
 out = fopen ("prob3.out","w");
 while (fscanf (in,"%d %d",&s1,&s2),s1>0 && s2>0)
  Process (s1,s2);
 fclose (in);
 fclose (out);
 return EXIT_SUCCESS;
}

/* Process prints one pair of Christmas trees */

void Process (int s1, int s2) {

  char tstr[1000];
  int i;

 for (i=0; i < MAX (3*s1,3*s2); i++) { /* process one line of picture */
  tstr[0]=0;
  AddLine (tstr,s1,i); /* add left tree */
  strcat (tstr," "); /* add space */
  AddLine (tstr,s2,i); /* add right tree */
  Trim (tstr); /* get rid of trailing spaces */
  fprintf (out,"%s\n",tstr);
 }
 fprintf (out,"\n");
}

/* AddLine appends to str the ith. line of a tree of size s. */
void AddLine (char *tstr, int s, int i) {

  int ct;

 if (i >= 3*s) /* Beyond the tree */
  for (ct = 0; ct < 4*s-1; ct++) strcat (tstr," ");
 else if (i >= 2*s) { /* The stem */
  for (ct=0; ct < 2*s-1; ct++) strcat (tstr," ");
  strcat (tstr,"*");
  for (ct=0; ct < 2*s-1; ct++) strcat (tstr," ");
 } else { /* The branches */
  for (ct=0; ct < 2*s-i-1; ct++) strcat (tstr," ");
  for (ct=0; ct < 2*i+1; ct++) strcat (tstr,"*");
  for (ct=0; ct < 2*s-i-1; ct++) strcat (tstr," ");
 }
}

/* Trim removes all trailing spaces from tstr */
void Trim (char *tstr) {

  int i;

 for (i=strlen(tstr)-1;i>=0;i--) {
  if (tstr[i]!=' ') break;
  tstr[i] = 0;
 }
}
