io scaffold
This commit is contained in:
parent
682bb557af
commit
2b222dda0b
3 changed files with 109 additions and 15 deletions
88
main.c
88
main.c
|
|
@ -1,7 +1,89 @@
|
|||
#include "record.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
|
||||
int main()
|
||||
#define MAX_RECORDS 1000
|
||||
#define END_PROGRAM -1
|
||||
#define SUCCESS 0
|
||||
#define ERROR -2
|
||||
|
||||
void print_usage()
|
||||
{
|
||||
printf("Hello World\n");
|
||||
return 0;
|
||||
printf("Usage: [-f filename] [-h]\n");
|
||||
printf("Options:\n");
|
||||
printf(" -f filename\tRead records from file\n");
|
||||
printf(" -h\tShow this help message");
|
||||
}
|
||||
|
||||
// stream is function output
|
||||
int process_args(int argc, char** argv, FILE** stream)
|
||||
{
|
||||
char* filename = NULL;
|
||||
int opt;
|
||||
while ((opt = getopt(argc, argv, "f:h")) != -1) {
|
||||
switch (opt) {
|
||||
case 'f':
|
||||
filename = optarg;
|
||||
break;
|
||||
case 'h':
|
||||
print_usage();
|
||||
return END_PROGRAM;
|
||||
case '?':
|
||||
printf("invalid option, terminating\n");
|
||||
return ERROR;
|
||||
}
|
||||
}
|
||||
if (filename != NULL) {
|
||||
printf("%s\n", filename);
|
||||
*stream = fopen(filename, "r");
|
||||
if (!*stream) {
|
||||
fprintf(stderr, "Error: Cannot open file '%s'\n", filename);
|
||||
return END_PROGRAM;
|
||||
}
|
||||
}
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
void process_input(FILE* input)
|
||||
{
|
||||
record records[MAX_RECORDS];
|
||||
int count = 0;
|
||||
while (count < MAX_RECORDS) {
|
||||
if (read_record(input, &records[count]) != SUCCESS) {
|
||||
break;
|
||||
}
|
||||
count++;
|
||||
}
|
||||
|
||||
printf("\nRead %d records\n", count);
|
||||
qsort(records, count, sizeof(record), compare_records);
|
||||
printf("\nSorted records:\n");
|
||||
for (int i = 0; i < count; i++) {
|
||||
printf("Record %d: ", i);
|
||||
print_record(stdout, &records[i]);
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
FILE* input = NULL;
|
||||
if (argc > 1) {
|
||||
int result = process_args(argc, argv, &input);
|
||||
if (result != SUCCESS) {
|
||||
return result;
|
||||
}
|
||||
if (input == NULL) {
|
||||
printf("ok\n");
|
||||
}
|
||||
}
|
||||
if (input == NULL) {
|
||||
printf("Enter records (format: a_0 a_1 a_2 a_3 a_4 x), or 'q' to quit:\n");
|
||||
input = stdin;
|
||||
}
|
||||
process_input(input);
|
||||
if (input != stdin) {
|
||||
fclose(input);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
30
record.h
30
record.h
|
|
@ -6,18 +6,6 @@ typedef struct {
|
|||
int x;
|
||||
} record;
|
||||
|
||||
static inline int read_record(FILE* stream, record* out)
|
||||
{
|
||||
int result = fscanf(stream, "%d %d %d %d %d %d", &out->a[0], &out->a[1], &out->a[2], &out->a[3], &out->a[4], &out->x);
|
||||
if (result == 6) {
|
||||
return 0;
|
||||
}
|
||||
if (result == EOF) {
|
||||
return EOF;
|
||||
}
|
||||
return -2;
|
||||
}
|
||||
|
||||
static inline int g(const record* x)
|
||||
{
|
||||
int out = 0;
|
||||
|
|
@ -37,3 +25,21 @@ static inline int compare_records(const void* a, const void* b)
|
|||
const int gb = g(rb);
|
||||
return (ga > gb) - (ga < gb);
|
||||
}
|
||||
|
||||
static inline void print_record(FILE* stream, const record* out)
|
||||
{
|
||||
fprintf(stream, "[%d %d %d %d %d] x=%d, g=%d",
|
||||
out->a[0], out->a[1], out->a[2], out->a[3], out->a[4], out->x, g(out));
|
||||
}
|
||||
|
||||
static inline int read_record(FILE* stream, record* out)
|
||||
{
|
||||
int result = fscanf(stream, "%d %d %d %d %d %d", &out->a[0], &out->a[1], &out->a[2], &out->a[3], &out->a[4], &out->x);
|
||||
if (result == 6) {
|
||||
return 0;
|
||||
}
|
||||
if (result == EOF) {
|
||||
return EOF;
|
||||
}
|
||||
return -2;
|
||||
}
|
||||
|
|
|
|||
6
tests/1.in
Normal file
6
tests/1.in
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
12312 312312 312 312 323 3
|
||||
543 3435 34 5342 532 54
|
||||
344 43534 222 435 2345 23
|
||||
2345 234 23423 455 44 4
|
||||
2345 23222 222 2245 4554 10
|
||||
2345 22 2 22 2 3
|
||||
Loading…
Add table
Add a link
Reference in a new issue