Add today's solutions.
This commit is contained in:
parent
634f006569
commit
2ec83690ce
@ -15,8 +15,14 @@
|
||||
|
||||
char *inputName(const char *promptText, char *buffer, unsigned int bufferSize);
|
||||
|
||||
int main()
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
if(argc != 2)
|
||||
{
|
||||
printf("Usage: %s <path to names text file>\n", argv[0]);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
char buffer[MAX_BUFFER_LEN];
|
||||
|
||||
while((inputName("Namen hinzufuegen: ", buffer, MAX_BUFFER_LEN) != NULL) && (strlen(buffer) > 0))
|
||||
@ -30,6 +36,21 @@ int main()
|
||||
printf("\nNach Eingabe:\n");
|
||||
printNames();
|
||||
|
||||
/*printf("Speichere Namen ...\n");
|
||||
if(saveNames("namen.txt") == IO_ERROR)
|
||||
{
|
||||
printf("Fehler beim Schreiben ...\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}*/
|
||||
|
||||
printf("Lade Namen ...\n");
|
||||
if(loadNames(argv[1]) == IO_ERROR)
|
||||
{
|
||||
printf("Fehler beim Lesen ...\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
printNames();
|
||||
|
||||
/*printf("\nNach Sortierung:\n");
|
||||
sortNames();
|
||||
printNames();
|
||||
@ -49,7 +70,7 @@ int main()
|
||||
|
||||
while((inputName("Namen entfernen: ", buffer, MAX_BUFFER_LEN) != NULL) && (strlen(buffer) > 0))
|
||||
{
|
||||
if(removeName(buffer))
|
||||
if(removeName(buffer) == STATUS_OK)
|
||||
printf("%s wurde aus dem Archiv entfernt.\n", buffer);
|
||||
else
|
||||
printf("Fehler! %s konnte nicht gefunden werden.\n", buffer);
|
||||
|
||||
16
03_dateien/makefile
Normal file
16
03_dateien/makefile
Normal file
@ -0,0 +1,16 @@
|
||||
namesarchive: main.o libnamesarchive.so
|
||||
gcc -Wall -o namesarchive main.o libnamesarchive.so
|
||||
|
||||
main.o: main.c
|
||||
gcc -c -Wall main.c
|
||||
|
||||
namesarchive.o: namesarchive.c namesarchive.h
|
||||
gcc -c -Wall namesarchive.c
|
||||
|
||||
libnamesarchive.so: namesarchive.o
|
||||
gcc -shared -o libnamesarchive.so namesarchive.o
|
||||
|
||||
clean:
|
||||
del /f namesarchive.exe *.o *.so
|
||||
# Comment line above and uncomment line below for UNIX systems
|
||||
# rm -f namesarchive *.o *.so
|
||||
@ -5,23 +5,15 @@
|
||||
|
||||
#define INDEX_NOT_FOUND -1
|
||||
|
||||
typedef struct
|
||||
{
|
||||
char name[MAX_NAME_LEN+1];
|
||||
unsigned int id;
|
||||
} Student;
|
||||
|
||||
static char names[MAX_NAMES][MAX_NAME_LEN+1];
|
||||
static unsigned int numberOfEntries = 0;
|
||||
|
||||
/*
|
||||
static void cpy(char *dst, const char *src, unsigned int max)
|
||||
{
|
||||
int i;
|
||||
for(i = 0; i < max && *src != '\0'; i++)
|
||||
{
|
||||
*dst = *src;
|
||||
dst++;
|
||||
src++;
|
||||
}
|
||||
if(i < max)
|
||||
*dst = '\0';
|
||||
}*/
|
||||
|
||||
int addName(const char *name)
|
||||
{
|
||||
if(numberOfEntries < MAX_NAMES)
|
||||
@ -63,17 +55,57 @@ static void removeAt(int idx)
|
||||
numberOfEntries--;
|
||||
}
|
||||
|
||||
int removeName(const char *name)
|
||||
Status removeName(const char *name)
|
||||
{
|
||||
int idx = getNameIdx(name);
|
||||
|
||||
if(idx == INDEX_NOT_FOUND)
|
||||
{
|
||||
return 0;
|
||||
return REMOVE_ERROR;
|
||||
}
|
||||
else
|
||||
{
|
||||
removeAt(idx);
|
||||
return 1;
|
||||
return STATUS_OK;
|
||||
}
|
||||
}
|
||||
|
||||
Status saveNames(const char *path)
|
||||
{
|
||||
FILE *file = fopen(path, "w");
|
||||
|
||||
if(file != NULL)
|
||||
{
|
||||
for(int i = 0; i < numberOfEntries; i++)
|
||||
fprintf(file, "%s\n", names[i]);
|
||||
fclose(file);
|
||||
|
||||
return STATUS_OK;
|
||||
}
|
||||
else
|
||||
{
|
||||
return IO_ERROR;
|
||||
}
|
||||
}
|
||||
|
||||
Status loadNames(const char *path)
|
||||
{
|
||||
FILE *file = fopen(path, "r");
|
||||
|
||||
if(file != NULL)
|
||||
{
|
||||
char line[MAX_NAME_LEN+10];
|
||||
|
||||
while(fgets(line, MAX_NAME_LEN+10, file) != NULL)
|
||||
{
|
||||
line[strcspn(line, "\n")] = '\0';
|
||||
addName(line);
|
||||
}
|
||||
|
||||
return STATUS_OK;
|
||||
}
|
||||
else
|
||||
{
|
||||
return IO_ERROR;
|
||||
}
|
||||
}
|
||||
@ -7,16 +7,20 @@
|
||||
// Maximale Anzahl an Namen
|
||||
#define MAX_NAMES 100
|
||||
|
||||
typedef enum {IO_ERROR, STATUS_OK, REMOVE_ERROR} Status;
|
||||
|
||||
// Fügt einen Namen hinzu. Im Fehlerfall (Archiv ist voll) soll 0, ansonsten 1 zurückgegeben werden.
|
||||
// Ist der angegebene Name länger als die zulässige Namenslänge, wird der Name abgeschnitten hinzugefügt. Rückgabe ist dann 1.
|
||||
int addName(const char *name);
|
||||
// Wie addName. Fügt Namen aber direkt sortiert hinzu. Voraussetzung ist ein bereits sortiertes Archiv.
|
||||
int addNameSorted(const char *name);
|
||||
// Entfernt den angegebenen Namen. Gibt bei Erfolg 1 zurück, ansonsten 0 (Name konnte nicht gefunden werden).
|
||||
int removeName(const char *name);
|
||||
Status removeName(const char *name);
|
||||
// Sortiert die Namen im Archiv aufsteigend.
|
||||
void sortNames();
|
||||
// Gibt die Namen zeilenweise aus.
|
||||
void printNames();
|
||||
Status saveNames(const char *path);
|
||||
Status loadNames(const char *path);
|
||||
|
||||
#endif
|
||||
Loading…
x
Reference in New Issue
Block a user