C ordening
This commit is contained in:
55
C/C(extra) ExtraAssignments/Skater/product/main.c
Normal file
55
C/C(extra) ExtraAssignments/Skater/product/main.c
Normal file
@@ -0,0 +1,55 @@
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
// Add your includes here:
|
||||
#include "terminal_io.h"
|
||||
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
MenuOptions choice = MO_SHOW_ICESKATERS;
|
||||
printProgramHeader();
|
||||
|
||||
while (choice != MO_QUIT)
|
||||
{
|
||||
choice = getMenuChoice();
|
||||
|
||||
switch (choice)
|
||||
{
|
||||
case MO_SHOW_ICESKATERS:
|
||||
fprintf(stderr, "not yet implemented\n");
|
||||
break;
|
||||
case MO_ADD_ICESKATER:
|
||||
fprintf(stderr, "not yet implemented\n");
|
||||
break;
|
||||
case MO_REMOVE_ICESKATER:
|
||||
fprintf(stderr, "not yet implemented\n");
|
||||
break;
|
||||
case MO_ADD_TIME:
|
||||
fprintf(stderr, "not yet implemented\n");
|
||||
break;
|
||||
case MO_SHOW_CLASSIFICATION:
|
||||
fprintf(stderr, "not yet implemented\n");
|
||||
break;
|
||||
case MO_SHOW_FINAL_CLASSIFICATION:
|
||||
fprintf(stderr, "not yet implemented\n");
|
||||
break;
|
||||
case MO_LOAD:
|
||||
fprintf(stderr, "not yet implemented\n");
|
||||
break;
|
||||
case MO_SAVE:
|
||||
fprintf(stderr, "not yet implemented\n");
|
||||
break;
|
||||
case MO_QUIT:
|
||||
// nothing to do here
|
||||
break;
|
||||
default:
|
||||
fprintf(stderr, "ERROR: invalid choice: %d\n", choice);
|
||||
break;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
92
C/C(extra) ExtraAssignments/Skater/product/terminal_io.c
Normal file
92
C/C(extra) ExtraAssignments/Skater/product/terminal_io.c
Normal file
@@ -0,0 +1,92 @@
|
||||
#include <limits.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "terminal_io.h"
|
||||
|
||||
|
||||
#define MAX_STRLEN 80
|
||||
|
||||
static const char* MenuStrings[] =
|
||||
{
|
||||
"Show Iceskaters",
|
||||
"Add Iceskater",
|
||||
"Remove Iceskater",
|
||||
"Add time",
|
||||
"Show classification",
|
||||
"Show final classification",
|
||||
"Load ...",
|
||||
"Save ...",
|
||||
"Quit"
|
||||
};
|
||||
static const size_t NrMenuStrings =
|
||||
sizeof(MenuStrings) / sizeof(MenuStrings[0]);
|
||||
|
||||
void clearScreen()
|
||||
{
|
||||
printf("\033[2J\033[1;1H");
|
||||
}
|
||||
|
||||
int getInt(const char* message)
|
||||
{
|
||||
char line[MAX_STRLEN];
|
||||
char* result = NULL;
|
||||
int value = -1;
|
||||
|
||||
printf("%s", message);
|
||||
result = fgets(line, sizeof(line), stdin);
|
||||
if (result != NULL)
|
||||
{
|
||||
sscanf(result, "%d", &value);
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
int getLimitedInt(const char* message, const char* items[], int nrItems)
|
||||
{
|
||||
int choice = -1;
|
||||
do
|
||||
{
|
||||
if (items != NULL)
|
||||
{
|
||||
for (int i = 0; i < nrItems; i++)
|
||||
{
|
||||
printf(" [%d] %s\n", i, items[i]);
|
||||
}
|
||||
}
|
||||
choice = getInt(message);
|
||||
} while (choice < 0 || choice >= nrItems);
|
||||
|
||||
return choice;
|
||||
}
|
||||
|
||||
void getStr(const char* message, char* str, int maxLength)
|
||||
{
|
||||
char line[maxLength];
|
||||
char* result = NULL;
|
||||
|
||||
printf("%s", message);
|
||||
result = fgets(line, sizeof(line), stdin);
|
||||
if (result != NULL)
|
||||
{
|
||||
if (result[strlen(result) - 1] == '\n')
|
||||
{
|
||||
result[strlen(result) - 1] = '\0';
|
||||
}
|
||||
strncpy(str, result, maxLength);
|
||||
str[maxLength - 1] = '\0';
|
||||
}
|
||||
}
|
||||
|
||||
MenuOptions getMenuChoice()
|
||||
{
|
||||
printf("\n\nMENU\n====\n");
|
||||
return (MenuOptions)getLimitedInt("choice: ", MenuStrings, NrMenuStrings);
|
||||
}
|
||||
|
||||
void printProgramHeader()
|
||||
{
|
||||
printf("PRC2 opdracht 'Schaatsen'\n"
|
||||
"-------------------------");
|
||||
}
|
||||
27
C/C(extra) ExtraAssignments/Skater/product/terminal_io.h
Normal file
27
C/C(extra) ExtraAssignments/Skater/product/terminal_io.h
Normal file
@@ -0,0 +1,27 @@
|
||||
#ifndef TERMINAL_IO_H
|
||||
#define TERMINAL_IO_H
|
||||
|
||||
#include "skater.h"
|
||||
|
||||
typedef enum
|
||||
{
|
||||
MO_SHOW_ICESKATERS,
|
||||
MO_ADD_ICESKATER,
|
||||
MO_REMOVE_ICESKATER,
|
||||
MO_ADD_TIME,
|
||||
MO_SHOW_CLASSIFICATION,
|
||||
MO_SHOW_FINAL_CLASSIFICATION,
|
||||
MO_LOAD,
|
||||
MO_SAVE,
|
||||
MO_QUIT
|
||||
} MenuOptions;
|
||||
|
||||
void clearScreen();
|
||||
int getInt(const char* message);
|
||||
int getLimitedInt(const char* message, const char* items[], int nrItems);
|
||||
void getStr(const char* message, char* str, int maxLength);
|
||||
MenuOptions getMenuChoice();
|
||||
|
||||
void printProgramHeader();
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user