clean lab09
This commit is contained in:
2 files changed
+92
-173
No files matched your search
+2
-2
@@ -3,9 +3,9 @@ CC = gcc
|
|||||||
CFLAGS = -Wall -std=c11
|
CFLAGS = -Wall -std=c11
|
||||||
|
|
||||||
#Input file
|
#Input file
|
||||||
INPUT = input5.txt
|
INPUT = input1.txt
|
||||||
OUTPUT = output.txt
|
OUTPUT = output.txt
|
||||||
REF = ref.txt
|
REF = ref1.txt
|
||||||
# Source file
|
# Source file
|
||||||
SRCS = lab9.c
|
SRCS = lab9.c
|
||||||
# Output
|
# Output
|
||||||
|
|||||||
+90
-171
@@ -8,6 +8,14 @@
|
|||||||
#define INPUT_FILE 1
|
#define INPUT_FILE 1
|
||||||
#define OUTPUT_FILE 2
|
#define OUTPUT_FILE 2
|
||||||
#define EXPECTED_INPUT 1
|
#define EXPECTED_INPUT 1
|
||||||
|
#define READING 1
|
||||||
|
#define ERROR_CODE 1
|
||||||
|
#define OK_CODE 0
|
||||||
|
#define MAX_STRING_LEN 256
|
||||||
|
#define IS_EQUAL 0
|
||||||
|
#define INDEX_OFFSET 1
|
||||||
|
#define BASE_TEN 10
|
||||||
|
#define DOUBLE 2
|
||||||
|
|
||||||
#define SHORT 1
|
#define SHORT 1
|
||||||
#define INTEGER 2
|
#define INTEGER 2
|
||||||
@@ -15,13 +23,6 @@
|
|||||||
#define CHAR 4
|
#define CHAR 4
|
||||||
#define STRING 5
|
#define STRING 5
|
||||||
|
|
||||||
#define MAX_STRING_LEN 256
|
|
||||||
#define READING 1
|
|
||||||
#define IS_EQUAL 0
|
|
||||||
#define INDEX_OFFSET 1
|
|
||||||
#define BASE_TEN 10
|
|
||||||
#define DOUBLE 2
|
|
||||||
|
|
||||||
typedef struct NODE
|
typedef struct NODE
|
||||||
{
|
{
|
||||||
void *data;
|
void *data;
|
||||||
@@ -57,106 +58,63 @@ void freeList(NODE *head)
|
|||||||
NODE* readFile(char *filepath, int *type, int *count)
|
NODE* readFile(char *filepath, int *type, int *count)
|
||||||
{
|
{
|
||||||
FILE *file;
|
FILE *file;
|
||||||
|
|
||||||
file = fopen(filepath, "r");
|
file = fopen(filepath, "r");
|
||||||
|
|
||||||
if (file == NULL)
|
if (file == NULL)
|
||||||
{
|
{
|
||||||
// printf("Failed to read file %s\n", filepath);
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (fscanf(file, "%d", type) != EXPECTED_INPUT)
|
if (fscanf(file, "%d", type) != EXPECTED_INPUT)
|
||||||
{
|
{
|
||||||
// printf("Error: failed to read type from file\n");
|
|
||||||
fclose(file);
|
fclose(file);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
int ch; // skip new line characters and EOF
|
int ch; // skip newlines characters and EOF
|
||||||
while ((ch = fgetc(file)) != '\n' && ch != EOF) {}
|
while ((ch = fgetc(file)) != '\n' && ch != EOF) {}
|
||||||
|
|
||||||
|
char *format;
|
||||||
NODE *head;
|
NODE *head;
|
||||||
char token[MAX_STRING_LEN];
|
size_t size;
|
||||||
|
|
||||||
head = NULL;
|
head = NULL;
|
||||||
*count = 0;
|
*count = 0;
|
||||||
|
|
||||||
|
format = *type == SHORT ? "%hd" :
|
||||||
|
*type == INTEGER ? "%d" :
|
||||||
|
*type == FLOAT ? "%f" :
|
||||||
|
" %c";
|
||||||
|
|
||||||
|
size = *type == SHORT ? sizeof(short) :
|
||||||
|
*type == INTEGER ? sizeof(int) :
|
||||||
|
*type == FLOAT ? sizeof(float) :
|
||||||
|
*type == CHAR ? sizeof(char) :
|
||||||
|
MAX_STRING_LEN;
|
||||||
|
|
||||||
while (READING)
|
while (READING)
|
||||||
{
|
{
|
||||||
// read till comma or new line
|
|
||||||
if (fscanf(file, " %255[^,\n]", token) != EXPECTED_INPUT)
|
|
||||||
{
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
// end of file
|
|
||||||
if (strcmp(token, "E") == IS_EQUAL)
|
|
||||||
{
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
void *data;
|
void *data;
|
||||||
|
|
||||||
data = NULL;
|
data = malloc(size);
|
||||||
|
|
||||||
switch (*type)
|
if (*type == STRING)
|
||||||
{
|
{
|
||||||
case SHORT:
|
if (fscanf(file, "%255[^,\n]", (char *)data) != EXPECTED_INPUT ||
|
||||||
|
strcmp((char *)data, "E") == IS_EQUAL)
|
||||||
{
|
{
|
||||||
long value;
|
free(data);
|
||||||
|
|
||||||
value = strtol(token, NULL, BASE_TEN);
|
|
||||||
|
|
||||||
if (value < SHRT_MIN || value > SHRT_MAX)
|
|
||||||
{
|
|
||||||
// printf("Error: %ld is out of range for SHORT type (%d to %d)\n",
|
|
||||||
// value, SHRT_MIN, SHRT_MAX);
|
|
||||||
|
|
||||||
fclose(file);
|
|
||||||
freeList(head);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
data = malloc(sizeof(short));
|
|
||||||
*(short *)data = (short)value;
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
case INTEGER:
|
|
||||||
{
|
{
|
||||||
data = malloc(sizeof(int));
|
if (fscanf(file, format, data) != EXPECTED_INPUT ||
|
||||||
*(int *)data = atoi(token);
|
*type == CHAR && *(char *)data == 'E')
|
||||||
|
{
|
||||||
|
free(data);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
case FLOAT:
|
|
||||||
{
|
|
||||||
data = malloc(sizeof(float));
|
|
||||||
*(float *)data = atof(token);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
case CHAR:
|
|
||||||
{
|
|
||||||
data = malloc(sizeof(char));
|
|
||||||
*(char *)data = token[0];
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
case STRING:
|
|
||||||
{
|
|
||||||
data = malloc(MAX_STRING_LEN);
|
|
||||||
strncpy((char *)data, token, MAX_STRING_LEN - INDEX_OFFSET);
|
|
||||||
((char *)data)[MAX_STRING_LEN - INDEX_OFFSET] = '\0';
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
default:
|
|
||||||
// printf("Error: Bad input type\n");
|
|
||||||
fclose(file);
|
|
||||||
return NULL;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
insertNode(&head, data);
|
insertNode(&head, data);
|
||||||
@@ -167,7 +125,6 @@ NODE* readFile(char *filepath, int *type, int *count)
|
|||||||
{
|
{
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ch == '\n')
|
if (ch == '\n')
|
||||||
{
|
{
|
||||||
continue;
|
continue;
|
||||||
@@ -178,31 +135,9 @@ NODE* readFile(char *filepath, int *type, int *count)
|
|||||||
return head;
|
return head;
|
||||||
}
|
}
|
||||||
|
|
||||||
void printList(NODE *head, int type)
|
|
||||||
{
|
|
||||||
while (head != NULL)
|
|
||||||
{
|
|
||||||
switch (type)
|
|
||||||
{
|
|
||||||
case SHORT: printf("%hd -> ", *(short *)head->data); break;
|
|
||||||
case INTEGER: printf("%d -> ", *(int *) head->data); break;
|
|
||||||
case FLOAT: printf("%.1f -> ", *(float *)head->data); break;
|
|
||||||
case CHAR: printf("%c -> ", *(char *) head->data); break;
|
|
||||||
case STRING: printf("%s -> ", (char *) head->data); break;
|
|
||||||
}
|
|
||||||
|
|
||||||
head = head->next;
|
|
||||||
}
|
|
||||||
|
|
||||||
printf("null\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
int compare(void *a, void *b, int type)
|
int compare(void *a, void *b, int type)
|
||||||
{
|
{
|
||||||
switch (type)
|
if (type == SHORT || type == INTEGER)
|
||||||
{
|
|
||||||
case SHORT:
|
|
||||||
case INTEGER:
|
|
||||||
{
|
{
|
||||||
int va;
|
int va;
|
||||||
int vb;
|
int vb;
|
||||||
@@ -213,10 +148,10 @@ int compare(void *a, void *b, int type)
|
|||||||
return (va > vb) - (va < vb);
|
return (va > vb) - (va < vb);
|
||||||
}
|
}
|
||||||
|
|
||||||
case FLOAT:
|
if (type == FLOAT)
|
||||||
{
|
{
|
||||||
int va;
|
float va;
|
||||||
int vb;
|
float vb;
|
||||||
|
|
||||||
va = *(float *)a;
|
va = *(float *)a;
|
||||||
vb = *(float *)b;
|
vb = *(float *)b;
|
||||||
@@ -224,7 +159,7 @@ int compare(void *a, void *b, int type)
|
|||||||
return (va > vb) - (va < vb);
|
return (va > vb) - (va < vb);
|
||||||
}
|
}
|
||||||
|
|
||||||
case CHAR:
|
if (type == CHAR)
|
||||||
{
|
{
|
||||||
char va;
|
char va;
|
||||||
char vb;
|
char vb;
|
||||||
@@ -235,11 +170,7 @@ int compare(void *a, void *b, int type)
|
|||||||
return (va > vb) - (va < vb);
|
return (va > vb) - (va < vb);
|
||||||
}
|
}
|
||||||
|
|
||||||
case STRING:
|
|
||||||
return strcmp((char *)a, (char *)b);
|
return strcmp((char *)a, (char *)b);
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
NODE* split(NODE *head)
|
NODE* split(NODE *head)
|
||||||
@@ -305,69 +236,70 @@ NODE* mergeSort(NODE *head, int type)
|
|||||||
return merge(head, second, type);
|
return merge(head, second, type);
|
||||||
}
|
}
|
||||||
|
|
||||||
void writeOutput(char *filePath, char *data)
|
void printValue(FILE *file, void *data, int type)
|
||||||
|
{
|
||||||
|
if (type == SHORT)
|
||||||
|
{
|
||||||
|
short v;
|
||||||
|
v = *(short *)data;
|
||||||
|
fprintf(file, "%hd", v);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (type == INTEGER)
|
||||||
|
{
|
||||||
|
int v;
|
||||||
|
v = *(int *)data;
|
||||||
|
fprintf(file, "%d", v);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (type == FLOAT)
|
||||||
|
{
|
||||||
|
float v;
|
||||||
|
v = *(float *)data;
|
||||||
|
fprintf(file, "%.1f", v);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (type == CHAR)
|
||||||
|
{
|
||||||
|
char v;
|
||||||
|
v = *(char *)data;
|
||||||
|
fprintf(file, "%c", v);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
fprintf(file, "%s", (char *)data);
|
||||||
|
}
|
||||||
|
|
||||||
|
void writeOutput(char *filepath, NODE *head, int type, int isError)
|
||||||
{
|
{
|
||||||
FILE *file;
|
FILE *file;
|
||||||
|
|
||||||
file = fopen(filePath, "w");
|
file = fopen(filepath, "w");
|
||||||
|
|
||||||
fputs(data, file);
|
|
||||||
fputs("\n", file);
|
|
||||||
|
|
||||||
|
if (isError == 1)
|
||||||
|
{
|
||||||
|
fputs("Error\n", file);
|
||||||
fclose(file);
|
fclose(file);
|
||||||
}
|
return;
|
||||||
|
}
|
||||||
char* toString(NODE *head, int type)
|
|
||||||
{
|
|
||||||
size_t capacity;
|
|
||||||
char *result;
|
|
||||||
|
|
||||||
capacity = MAX_STRING_LEN;
|
|
||||||
result = malloc(capacity);
|
|
||||||
result[0] = '\0';
|
|
||||||
|
|
||||||
char temp[MAX_STRING_LEN];
|
|
||||||
|
|
||||||
while (head != NULL)
|
while (head != NULL)
|
||||||
{
|
{
|
||||||
switch (type)
|
printValue(file, head->data, type);
|
||||||
{
|
|
||||||
case SHORT:
|
|
||||||
snprintf(temp, sizeof(temp), "%hd", *(short *)head->data);
|
|
||||||
break;
|
|
||||||
|
|
||||||
case INTEGER:
|
|
||||||
snprintf(temp, sizeof(temp), "%d", *(int *)head->data);
|
|
||||||
break;
|
|
||||||
|
|
||||||
case FLOAT:
|
|
||||||
snprintf(temp, sizeof(temp), "%.1f", *(float *)head->data);
|
|
||||||
break;
|
|
||||||
|
|
||||||
case CHAR:
|
|
||||||
snprintf(temp, sizeof(temp), "%c", *(char *)head->data);
|
|
||||||
break;
|
|
||||||
|
|
||||||
case STRING:
|
|
||||||
snprintf(temp, sizeof(temp), "%s", (char *)head->data);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
while (strlen(result) + strlen(temp) + 2 >= capacity)
|
|
||||||
{
|
|
||||||
capacity *= DOUBLE;
|
|
||||||
result = realloc(result, capacity);
|
|
||||||
}
|
|
||||||
|
|
||||||
strcat(result, temp);
|
|
||||||
|
|
||||||
if (head->next != NULL)
|
if (head->next != NULL)
|
||||||
strcat(result, ",");
|
{
|
||||||
|
fputc(',', file);
|
||||||
|
}
|
||||||
|
|
||||||
head = head->next;
|
head = head->next;
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
fputc('\n', file);
|
||||||
|
fclose(file);
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
@@ -386,26 +318,13 @@ int main(int argc, char *argv[])
|
|||||||
|
|
||||||
if (head == NULL)
|
if (head == NULL)
|
||||||
{
|
{
|
||||||
writeOutput(argv[OUTPUT_FILE], "Error");
|
writeOutput(argv[OUTPUT_FILE], head, type, ERROR_CODE);
|
||||||
// printf("Failed to read list.\n");
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("\nOriginal:\n");
|
|
||||||
printList(head, type);
|
|
||||||
printf("\n");
|
|
||||||
|
|
||||||
head = mergeSort(head, type);
|
head = mergeSort(head, type);
|
||||||
|
|
||||||
char *listString;
|
writeOutput(argv[OUTPUT_FILE], head, type, OK_CODE);
|
||||||
|
|
||||||
listString = toString(head, type);
|
|
||||||
|
|
||||||
writeOutput(argv[OUTPUT_FILE], listString);
|
|
||||||
|
|
||||||
printf("Sorted:\n");
|
|
||||||
printList(head, type);
|
|
||||||
printf("\n");
|
|
||||||
|
|
||||||
freeList(head);
|
freeList(head);
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user