コード例 #1
0
ファイル: animal_game.c プロジェクト: megabobo/Assignment11
void play(FILE* inputFile){
  if(inputFile == NULL){
    printf("IN HERE\n");
    inputFile = fopen("src/animals.tree", "w+");
    fputs("Aelephant", inputFile);
    rewind(inputFile);
  }
  binary_tree* tree = binary_tree_create_f(inputFile);
  if(inputFile != NULL){
  printf("Welcome to the Animals game!\n"); //starts the game
  printf("\n");
  printf("Shall we play a game? "); //asks to play
  char c;
  scanf(" %c", &c); //scans for either a 'y' or 'n' char
  while(c == 'y'){
    char* val;
    tree = playRound(inputFile, tree); //plays the game
    printf("\n");
    printf("Shall we play a game? "); //asks to play
    scanf(" %c", &c); //scans for either a 'y' or 'n' char
  }
  printf("Bye!\n"); //quits the game
  freopen("src/animals.tree", "w", inputFile); //makes a blank file to be written to
  binary_tree_write(tree, inputFile); //writes the new information to the file
  rewind(inputFile); //rewinds to the beginning of the file
  fclose(inputFile); //closes the file
  binary_tree_destroy(tree); //free's the malloc'ed memory
  return;
}
else{
  printf("File is null or not in the correct format\n");
}
}
コード例 #2
0
//print string of tree in pre order recursively
void binary_tree_write(binary_tree* self, FILE* stream){

// string to output text with
char *str = NULL;

// if it's a leaf
  if (binary_tree_is_leaf(self)){
    str = binary_tree_get_string(self, str);
    fprintf(stream, "A%s", str);
    return;
  } else {
    // it's not a leaf
    str = binary_tree_get_string(self, str);
    fprintf(stream, "Q%s", str);
    binary_tree_write(binary_tree_get_left(self), stream);
    binary_tree_write(binary_tree_get_right(self), stream);

  }
}