Add students archive.
This commit is contained in:
parent
2ec83690ce
commit
212c38a2f8
37
04_strukturen/main.c
Normal file
37
04_strukturen/main.c
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
/*
|
||||||
|
* Vervollständigen Sie das untenstehende Programm, indem Sie das Modul namesarchive in der
|
||||||
|
* namesarchive.h bzw. namesarchive.c implementieren.
|
||||||
|
* Die öffentlichen Schnittstellen mit genaueren Details entnehmen Sie der namesarchive.h.
|
||||||
|
* Das Programm soll das Modul als dynamische Laufzeitbibliothek nutzen.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include "namesarchive.h"
|
||||||
|
|
||||||
|
|
||||||
|
int main(int argc, char *argv[])
|
||||||
|
{
|
||||||
|
addStudent("Zorro", 999);
|
||||||
|
addStudent("Adam", 123);
|
||||||
|
addStudent("Fred", 789);
|
||||||
|
addStudent("Anna", 456);
|
||||||
|
addStudent("Zorro", 111);
|
||||||
|
|
||||||
|
printf("\nNach Eingabe:\n");
|
||||||
|
printStudents();
|
||||||
|
|
||||||
|
sortStudents();
|
||||||
|
printf("\nNach Sortierung:\n");
|
||||||
|
printStudents();
|
||||||
|
|
||||||
|
removeStudent("Anna");
|
||||||
|
removeStudent("Karl");
|
||||||
|
|
||||||
|
printf("\nNach Entfernen:\n");
|
||||||
|
printStudents();
|
||||||
|
|
||||||
|
return EXIT_SUCCESS;
|
||||||
|
}
|
||||||
16
04_strukturen/makefile
Normal file
16
04_strukturen/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
|
||||||
89
04_strukturen/namesarchive.c
Normal file
89
04_strukturen/namesarchive.c
Normal file
@ -0,0 +1,89 @@
|
|||||||
|
#include "namesarchive.h"
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#define INDEX_NOT_FOUND -1
|
||||||
|
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
char name[MAX_NAME_LEN+1];
|
||||||
|
unsigned int id;
|
||||||
|
} Student;
|
||||||
|
|
||||||
|
Student students[MAX_NAMES];
|
||||||
|
unsigned int numberOfEntries = 0;
|
||||||
|
|
||||||
|
int addStudent(const char *name, unsigned int id)
|
||||||
|
{
|
||||||
|
if(numberOfEntries < MAX_NAMES)
|
||||||
|
{
|
||||||
|
strncpy(students[numberOfEntries].name, name, MAX_NAME_LEN+1);
|
||||||
|
students[numberOfEntries].name[MAX_NAME_LEN] = '\0';
|
||||||
|
students[numberOfEntries].id = id;
|
||||||
|
numberOfEntries++;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void printStudents()
|
||||||
|
{
|
||||||
|
for(int i = 0; i < numberOfEntries; i++)
|
||||||
|
{
|
||||||
|
printf("%s: %u\n", students[i].name, students[i].id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static int getNameIdx(const char *name)
|
||||||
|
{
|
||||||
|
for(int i = 0; i < numberOfEntries; i++)
|
||||||
|
{
|
||||||
|
if(strcmp(name, students[i].name) == 0)
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
return INDEX_NOT_FOUND;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void removeAt(int idx)
|
||||||
|
{
|
||||||
|
for(int i = idx; i < numberOfEntries-1; i++)
|
||||||
|
{
|
||||||
|
students[i] = students[i+1];
|
||||||
|
}
|
||||||
|
numberOfEntries--;
|
||||||
|
}
|
||||||
|
|
||||||
|
Status removeStudent(const char *name)
|
||||||
|
{
|
||||||
|
int idx = getNameIdx(name);
|
||||||
|
|
||||||
|
if(idx == INDEX_NOT_FOUND)
|
||||||
|
{
|
||||||
|
return REMOVE_ERROR;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
removeAt(idx);
|
||||||
|
return STATUS_OK;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static int compareStudents(const void *arg1, const void *arg2)
|
||||||
|
{
|
||||||
|
const Student *student1 = (const Student *)arg1;
|
||||||
|
const Student *student2 = (const Student *)arg2;
|
||||||
|
|
||||||
|
int nameDiff = strcmp(student1->name, student2->name);
|
||||||
|
|
||||||
|
if(nameDiff == 0)
|
||||||
|
return student1->id - student2->id;
|
||||||
|
else
|
||||||
|
return nameDiff;
|
||||||
|
}
|
||||||
|
|
||||||
|
void sortStudents()
|
||||||
|
{
|
||||||
|
qsort(students, numberOfEntries, sizeof(Student), compareStudents);
|
||||||
|
}
|
||||||
25
04_strukturen/namesarchive.h
Normal file
25
04_strukturen/namesarchive.h
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
#ifndef NAMEARCHIVE_H
|
||||||
|
#define NAMEARCHIVE_H
|
||||||
|
|
||||||
|
// Maximale Namenslänge
|
||||||
|
#define MAX_NAME_LEN 10
|
||||||
|
|
||||||
|
// 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 addStudent(const char *name, unsigned int id);
|
||||||
|
// Wie addName. Fügt Namen aber direkt sortiert hinzu. Voraussetzung ist ein bereits sortiertes Archiv.
|
||||||
|
int addStudentSorted(const char *name, unsigned int id);
|
||||||
|
// Entfernt den angegebenen Namen. Gibt bei Erfolg 1 zurück, ansonsten 0 (Name konnte nicht gefunden werden).
|
||||||
|
Status removeStudent(const char *name);
|
||||||
|
// Sortiert die Namen im Archiv aufsteigend.
|
||||||
|
void sortStudents();
|
||||||
|
// Gibt die Namen zeilenweise aus.
|
||||||
|
void printStudents();
|
||||||
|
|
||||||
|
|
||||||
|
#endif
|
||||||
Loading…
x
Reference in New Issue
Block a user