Skip to content

Instantly share code, notes, and snippets.

@jalvarado91
Created October 22, 2017 04:36
Show Gist options
  • Save jalvarado91/773904c4ee79a51ea42f7e3c90e5f808 to your computer and use it in GitHub Desktop.
Save jalvarado91/773904c4ee79a51ea42f7e3c90e5f808 to your computer and use it in GitHub Desktop.
/*
Author: Juan Alvarado
Panther ID: 3367805
Description: Write a C program that sorts the lines of some input
Usage: bstsort [-c] [-o output_file_name] [input_file_name]
Originality: I affirm that this program is entirely my own work and
none of it is the work of any other person
*/
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <ctype.h>
#include <string.h>
/***********************
* Type Declarations
*/
typedef struct TreeNode {
char key[100];
struct TreeNode* left;
struct TreeNode* right;
} TreeNode;
/***********************
* Function Signature Declarations
*/
int read_input_from_stdin_to_tree();
int read_input_from_file_to_tree(char* in_file_name);
int comp_str_case(char string1[], char string2[]);
int comp_str_ignore_case(char string1[], char string2[]);
TreeNode* add(TreeNode* node, char key[]);
void printInorder(TreeNode* node);
void freePostorder(TreeNode* root);
/***********************
* Main Program
*/
int case_sensitive_flag = 0;
char outfileflag = 0;
FILE* outfile;
TreeNode* root_node;
int
main(int argc, char **argv)
{
extern char *optarg;
extern int optind;
int c, err = 0;
char *outfilename;
static char usage[] = "Usage: %s [-c] [-o output_file_name] [input_file_name]\n";
TreeNode* node;
while ((c = getopt(argc, argv, "co:")) != -1)
switch (c) {
case 'c':
case_sensitive_flag = 1;
break;
case 'o':
outfileflag = 1;
outfilename = optarg;
outfile = fopen(outfilename, "w");
break;
case '?':
err = 1;
break;
}
// no input_file_name given
if((optind+1) > argc) {
// Process from stdin
int stdinread = read_input_from_stdin_to_tree();
} else if (err) {
// incorrect use of bsort. Print usage.
fprintf(stderr, usage, argv[0]);
exit(1);
}
else {
// Process from file
char* input_file_name;
input_file_name = argv[optind];
int fileread = read_input_from_file_to_tree(input_file_name);
}
printInorder(root_node);
if(outfileflag == 1) fclose(outfile);
freePostorder(root_node);
exit(0);
}
/**************************************
* FUNCTION DEFINITIONS
**************************************/
/***************
* Reading Functions
*/
int read_input_from_stdin_to_tree() {
char strbuffer[100];
while(fgets(strbuffer, sizeof(strbuffer), stdin) != NULL && strbuffer[0] != '\n') {
root_node = add(root_node, strbuffer);
}
return 0;
}
int read_input_from_file_to_tree(char* in_file_name) {
char strbuffer[100];
FILE* infile;
infile = fopen(in_file_name, "r");
if (infile == NULL) {
fprintf(stderr, "Error opening file.\n");
return(-1);
}
while(fgets(strbuffer, sizeof(strbuffer), infile) != NULL && strbuffer[0] != '\n') {
root_node = add(root_node, strbuffer);
}
fclose(infile);
return 0;
}
/***********************
* Binary Search Tree Implementation
*/
TreeNode* add(TreeNode* node, char key[]) {
if (node == NULL) {
node = (TreeNode*) malloc(sizeof (TreeNode));
if (node == NULL) {
return NULL;
}
strcpy(node->key, key);
node->left = NULL;
node->right = NULL;
return node;
}
// Comparison Based on case setting;
int comparison;
if (case_sensitive_flag > 0)
comparison = comp_str_case(node->key, key);
else
comparison = comp_str_ignore_case(node->key, key);
if (comparison > 0) {
node->left = add(node->left, key);
}
else {
node->right = add(node->right, key);
}
return node;
}
void printInorder(TreeNode* node) {
if (node == NULL) {
return;
}
printInorder(node->left);
if (outfileflag == 1) {
fprintf(outfile, "%s", node->key);
}
else {
printf("%s", node->key);
}
printInorder(node->right);
}
void freePostorder(TreeNode *root) {
if (root == NULL) {
return;
}
freePostorder(root->left);
freePostorder(root->right);
free(root);
}
/******************
* Utilites
*/
int comp_str_case(char string1[], char string2[]) {
int i = 0;
while(string1[i] == string2[i]) {
if(string1[i] == '\0' || string2[i] == '\0') break;
i++;
}
if(string1[i] == '\0' && string2[i]=='\0')
return 0;
else if(string1[i] > string2[i])
return 1;
else if(string1[i] < string2[i])
return -1;
}
int comp_str_ignore_case(char string1[], char string2[]) {
int i = 0;
while(tolower(string1[i]) == tolower(string2[i])) {
if(string1[i] == '\0' || string2[i] == '\0') break;
i++;
}
if(string1[i] == '\0' && string2[i]=='\0')
return 0;
else if(tolower(string1[i]) > tolower(string2[i]))
return 1;
else if(tolower(string1[i]) < tolower(string2[i]))
return -1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment