small code refactor
This commit is contained in:
parent
ca827dcc71
commit
47470ba552
5 changed files with 91 additions and 9 deletions
34
.vscode/c_cpp_properties.json
vendored
Normal file
34
.vscode/c_cpp_properties.json
vendored
Normal file
|
@ -0,0 +1,34 @@
|
||||||
|
{
|
||||||
|
"configurations": [
|
||||||
|
{
|
||||||
|
"name": "Win32",
|
||||||
|
"includePath": [
|
||||||
|
"${workspaceFolder}/**"
|
||||||
|
],
|
||||||
|
"defines": [
|
||||||
|
"_DEBUG",
|
||||||
|
"UNICODE",
|
||||||
|
"_UNICODE"
|
||||||
|
],
|
||||||
|
"compilerPath": "C:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin\\gcc.exe",
|
||||||
|
"cppStandard": "gnu++14",
|
||||||
|
"intelliSenseMode": "windows-gcc-x64"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Linux",
|
||||||
|
"includePath": [
|
||||||
|
"${workspaceFolder}/**"
|
||||||
|
],
|
||||||
|
"defines": [
|
||||||
|
"_DEBUG",
|
||||||
|
"UNICODE",
|
||||||
|
"_UNICODE"
|
||||||
|
],
|
||||||
|
"compilerPath": "/usr/bin/gcc",
|
||||||
|
"cppStandard": "gnu++14",
|
||||||
|
"intelliSenseMode": "linux-gcc-x64",
|
||||||
|
"cStandard": "c17"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"version": 4
|
||||||
|
}
|
0
output.txt
Normal file
0
output.txt
Normal file
52
voronoi.c
52
voronoi.c
|
@ -1,11 +1,47 @@
|
||||||
|
/* voronoi.c
|
||||||
|
*
|
||||||
|
* Created by Rory Healy (healyr@student.unimelb.edu.au)
|
||||||
|
* 12th August 2021
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include "voronoi.h"
|
#include "voronoi.h"
|
||||||
|
|
||||||
|
#define OPEN_FILE_ERROR "Error: Unable to open file %s\n"
|
||||||
|
#define NUM_FILE_ERROR "Error: Incorrect number of inputs (3 required).\n"
|
||||||
|
#define MALLOC_ERROR "Error: Cannot allocate memory.\n"
|
||||||
|
|
||||||
int main(int argc, char **argv) {
|
int main(int argc, char **argv) {
|
||||||
FILE *dataset = NULL, *polygonData = NULL, *output = NULL;
|
FILE *dataset = NULL, *polygonData = NULL, *output = NULL;
|
||||||
checkInputArgs(argc, argv, &dataset, &polygonData, &output);
|
checkInputArgs(argc, argv, &dataset, &polygonData, &output);
|
||||||
|
|
||||||
|
watchtower_t *watchtowers = (watchtower_t *) malloc(sizeof(watchtower_t));
|
||||||
|
if (watchtowers == NULL) {
|
||||||
|
fputs(MALLOC_ERROR, stderr);
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
char *buffer;
|
||||||
|
size_t bufsize = 32;
|
||||||
|
size_t characters;
|
||||||
|
|
||||||
|
buffer = (char *)malloc(bufsize * sizeof(char));
|
||||||
|
if( buffer == NULL)
|
||||||
|
{
|
||||||
|
perror("Unable to allocate buffer");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("Type something: ");
|
||||||
|
characters = getline(&buffer,&bufsize,stdin);
|
||||||
|
printf("%zu characters were read.\n",characters);
|
||||||
|
printf("You typed: '%s'\n",buffer);
|
||||||
|
|
||||||
|
// Cleaning up data
|
||||||
|
free(watchtowers);
|
||||||
fclose(dataset);
|
fclose(dataset);
|
||||||
fclose(polygonData);
|
fclose(polygonData);
|
||||||
fclose(output);
|
fclose(output);
|
||||||
|
@ -14,29 +50,31 @@ int main(int argc, char **argv) {
|
||||||
|
|
||||||
void checkInputArgs(int argc, char **argv, FILE **datasetFile, \
|
void checkInputArgs(int argc, char **argv, FILE **datasetFile, \
|
||||||
FILE **polygonFile, FILE **outputFile) {
|
FILE **polygonFile, FILE **outputFile) {
|
||||||
char *openFileError = "Error: Unable to open file %s\n";
|
|
||||||
char *numFilesError = "Error: Incorrect number of inputs (3 required).\n";
|
|
||||||
|
|
||||||
if (argc != 4) {
|
if (argc != 4) {
|
||||||
fputs(numFilesError, stderr);
|
fputs(NUM_FILE_ERROR, stderr);
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
*datasetFile = fopen(argv[1], "r");
|
*datasetFile = fopen(argv[1], "r");
|
||||||
if (*datasetFile == NULL) {
|
if (*datasetFile == NULL) {
|
||||||
fprintf(stderr, openFileError, argv[1]);
|
fprintf(stderr, OPEN_FILE_ERROR, argv[1]);
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
*polygonFile = fopen(argv[2], "r");
|
*polygonFile = fopen(argv[2], "r");
|
||||||
if (*polygonFile == NULL) {
|
if (*polygonFile == NULL) {
|
||||||
fprintf(stderr, openFileError, argv[2]);
|
fprintf(stderr, OPEN_FILE_ERROR, argv[2]);
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
*outputFile = fopen(argv[3], "w");
|
*outputFile = fopen(argv[3], "w");
|
||||||
if (*outputFile == NULL) {
|
if (*outputFile == NULL) {
|
||||||
fprintf(stderr, openFileError, argv[3]);
|
fprintf(stderr, OPEN_FILE_ERROR, argv[3]);
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void readWatchtowers(watchtower_t *watchtowers, FILE* datasetFile) {
|
||||||
|
int numTowers = 1;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
10
voronoi.h
10
voronoi.h
|
@ -1,2 +1,12 @@
|
||||||
|
typedef struct {
|
||||||
|
char *id;
|
||||||
|
char *postcode;
|
||||||
|
char *manager;
|
||||||
|
int population;
|
||||||
|
double x;
|
||||||
|
double y;
|
||||||
|
} watchtower_t;
|
||||||
|
|
||||||
void checkInputArgs(int argc, char **argv, FILE **datasetFile, \
|
void checkInputArgs(int argc, char **argv, FILE **datasetFile, \
|
||||||
FILE **polygonFile, FILE **outputFile);
|
FILE **polygonFile, FILE **outputFile);
|
||||||
|
void readWatchtowers(watchtower_t *watchtowers, FILE* datasetFile);
|
||||||
|
|
BIN
voronoi1
BIN
voronoi1
Binary file not shown.
Loading…
Reference in a new issue