//Quicksort implementation in C
//@author Daniel Latham
//input: $0=file with list of numbers on line 0
// $1=type of number(char, short, int, long, float, double)
// $2=# of numbers
//output: specified inputs and sorted array last
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#define TYPE char
#define READ strtol(buff, &end, 10)
#define PRINT printf("%c ", nums[i])
#include "gc_quicksort.h"
#undef TYPE
#undef PRINT
#define TYPE short
#define PRINT printf("%hi ", nums[i]);
#include "gc_quicksort.h"
#undef TYPE
#undef PRINT
#define TYPE int
#define PRINT printf("%d ", nums[i]);
#include "gc_quicksort.h"
#undef TYPE
#undef PRINT
#define TYPE long
#define PRINT printf("%li ", nums[i]);
#include "gc_quicksort.h"
#undef TYPE
#undef READ
#undef PRINT
#define READ strtod(buff, &end)
#define TYPE float
#define PRINT printf("%f ", nums[i]);
#include "gc_quicksort.h"
#undef TYPE
#undef PRINT
#define TYPE double
#define PRINT printf("%F ", nums[i]);
#include "gc_quicksort.h"
#undef TYPE
#undef PRINT
struct TYPE_VALUES {
int CHAR, SHORT, INT, LONG, FLOAT, DOUBLE;
};
typedef struct TYPE_VALUES type_v;
void build_type_values(type_v* t){
const char* types[] = {"char", "short", "int", "long", "float", "double" };
// Changes argv[2] type to lowercase
// Computes number associated with the addition of all chars in argv[2]
int identifier = 0;
const int PRIME = 3;
for (int j = 0; j < 6; j++){
for (int i = 0; types[j][i]; i++){
identifier = PRIME * (identifier + types[j][i]);
}
// J used to determine which array element we're on
// so, 0 should be char, from types array
switch (j){
case 0:
t->CHAR = identifier;
break;
case 1:
t->SHORT = identifier;
break;
case 2:
t->INT = identifier;
break;
case 3:
t->LONG = identifier;
break;
case 4:
t->FLOAT = identifier;
break;
case 5:
t->DOUBLE = identifier;
break;
}
// Reset identifier
identifier = 0;
}
}
int main(int argc, char** argv){
// Prints file name for user
printf("%s\n", argv[1]);
// Type of array
printf("Type: %s\n", argv[2]);
// Size of array
printf("Size: %s\n", argv[3]);
// Create struct
struct TYPE_VALUES t;
// Initialize values in t
// Create pointer to struct in memory
struct TYPE_VALUES* tt = &t;
build_type_values(tt);
// Changes argv[2] type to lowercase
// Computes number associated with the addition of all chars in argv[2]
int identifier = 0;
const int PRIME = 3;
for (int i = 0; argv[2][i]; i++){
argv[2][i] = tolower(argv[2][i]);
identifier = PRIME * (identifier + argv[2][i]);
}
if (identifier == tt->CHAR){
choose_type_char(argc, argv, 0);
return 1;
} else if (identifier == tt->SHORT){
choose_type_short(argc, argv, 1);
return 1;
} else if (identifier == tt->INT){
choose_type_int(argc, argv, 2);
return 1;
} else if (identifier == tt->LONG){
choose_type_long(argc, argv, 3);
return 1;
} else if (identifier == tt->FLOAT){
choose_type_float(argc, argv, 4);
return 1;
} else if (identifier == tt->DOUBLE){
choose_type_double(argc, argv, 5);
return 1;
} else {
printf("ERROR: Incorrect Number Type Specified%s", "\n");
return -1;
}
}
Comments