Skip to content
Snippets Groups Projects

Final commit

1 file
+ 10
6
Compare changes
  • Side-by-side
  • Inline
+ 64
42
@@ -3,18 +3,22 @@
#include <string.h>
#include "main.h"
#include "tree.h"
#define INPUTSIZE 20
// You are allowed to change anything about this function to fix it
int main() {
char* commandBuffer = (char*)malloc(sizeof(char) * 20);
int main()
{
char *commandBuffer = (char *)malloc(sizeof(char) * INPUTSIZE);
Tree *tree = tree_create();
for(;;) {
fgets(commandBuffer, 20, stdin);
for (;;)
{
fgets(commandBuffer, INPUTSIZE, stdin);
// Quit on EOF or 'q'
if (feof(stdin) || commandBuffer == "q"){
if (feof(stdin) || commandBuffer == "q")
{
break;
}
@@ -32,47 +36,56 @@ int main() {
* You are allowed to change anything about this function to fix it
* @param command The command string to handle
*/
Tree* handleString(char command[], Tree *tree){
if (command == NULL){
Tree *handleString(char command[], Tree *tree)
{
if (command == NULL)
{
fprintf(stderr, "Invalid command; null pointer\n");
return tree;
}
switch(command[0]){
case 'i':
insert(command, tree);
break;
case 'e':
erase(command, tree);
break;
case 'c':
check(command, tree);
break;
case 'p':
tree_print(tree, 1);
break;
case 'x':
tree_delete(tree);
return NULL;
default:
fprintf(stderr, "Invalid command string: %s\n", command);
break;
switch (command[0])
{
case 'i':
insert(command, tree);
break;
case 'e':
erase(command, tree);
break;
case 'c':
check(command, tree);
break;
case 'p':
tree_print(tree, 1);
break;
case 'o':
tree_fancy_print(tree, 1);
break;
case 'x':
tree_delete(tree);
break;
default:
fprintf(stderr, "Invalid command string: %s\n", command);
break;
}
return tree;
}
// You are allowed to change anything about this function to tix it
Tree* insert(char* command, Tree* tree) {
Tree *insert(char *command, Tree *tree)
{
int age;
char* name = malloc(sizeof(char) * 20);
char *name = malloc(sizeof(char) * INPUTSIZE);
if (2 != sscanf(command, "i %d %s", &age, name)){
if (2 != sscanf(command, "i %d %s", &age, name))
{
fprintf(stderr, "Failed to parse insert command: not enough parameters filled\n");
return NULL;
}
if (tree == NULL){
if (tree == NULL)
{
tree = tree_create();
}
@@ -81,29 +94,38 @@ Tree* insert(char* command, Tree* tree) {
}
// You are allowed to change anything about this function to fix it
void erase(char* command, Tree* tree) {
void erase(char *command, Tree *tree)
{
int age;
char* name = malloc(sizeof(char) * 20);
char *name = malloc(sizeof(char) * INPUTSIZE);
if (2 != sscanf(command, "e %d %s", &age, name)){
if (2 != sscanf(command, "e %d %s", &age, name))
{
fprintf(stderr, "Failed to parse erase command: not enough parameters filled\n");
}
tree_erase(tree, age, name);
}
// You are allowed to change anything about this function to fix it
void check(char* command, Tree* tree) {
void check(char *command, Tree *tree)
{
int age;
char* name = malloc(sizeof(char) * 20);
char *name = malloc(sizeof(char) * INPUTSIZE);
if (2 != sscanf(command, "c %d %s", &age, name)){
if (2 != sscanf(command, "c %d %s", &age, name))
{
fprintf(stderr, "Failed to parse check command\n");
}
Node* result = tree_find(tree, age, name);
if (result){
printf("y\n");
} else {
printf("n\n");
else
{
Node *result = tree_find(tree, age, name);
if (result)
{
printf("y\n");
}
else
{
printf("n\n");
}
}
}
Loading