C-Kurs/Studentendatenbank/Musterlösung
< C-Kurs | Studentendatenbank
Version vom 21. September 2009, 18:00 Uhr von Alexander Kührmann (Diskussion | Beiträge)
Musterlösung
#include <stdlib.h> #include <stdio.h> #include <string.h> #define ANZAHL 10 #define NAME_LEN 25 #define NAME_TO_LONG (-1) #define MAT_NR_USED (-2) #define DB_FULL (-3) #define ADD_SUCCEED 1 #define NO_MAT_NR (-1) #define DEL_SUCCEED 1 /* Teil a */ typedef struct student_t { int mat_nr; char first_name[NAME_LEN]; char last_name[NAME_LEN]; } student; /* Teil b */ void print_student(student *studi) { printf("Matr.Nr.: %i, Vorname: %s, Nachname: %s\n", studi->mat_nr, studi->first_name, studi->last_name); } /* Teil c */ void print_students_db(student **db) { printf("===========================================================\n"); int i; for (i=0; i<ANZAHL; i++) { if (db[i] != NULL) { print_student(db[i]); } } printf("===========================================================\n"); } /* Teil e */ void clear_db(student **db) { int i; for (i=0; i<ANZAHL; i++) { if (db[i] != NULL) { free(db[i]); db[i] = NULL; } } } /* Teil f */ int get_student_index(int mat_nr, student **db) { int i; for (i=0; i<ANZAHL; i++) { if (db[i] != NULL) { if (mat_nr == db[i]->mat_nr) { return i; } } } return -1; } int add_student(int mat_nr, char *first_name, char *last_name, student **db) { if (strlen(first_name) >= NAME_LEN || strlen(last_name) >= NAME_LEN) { return NAME_TO_LONG; } if (get_student_index(mat_nr, db) >= 0) { return MAT_NR_USED; } int i; for (i=0; i<=ANZAHL; i++) { if (db[i] == NULL) { db[i] = malloc(sizeof(student)); db[i]->mat_nr = mat_nr; strcpy(db[i]->first_name, first_name); strcpy(db[i]->last_name, last_name); return ADD_SUCCEED; } } return DB_FULL; } /* Teil g */ int del_student(int mat_nr, student **db) { int del_student_index = get_student_index(mat_nr, db); if (del_student_index < 0) { return NO_MAT_NR; } free(db[del_student_index]); db[del_student_index] = NULL; return DEL_SUCCEED; } int main() { /* Teil b*/ printf(">>> Teil b\n"); student *one_student = malloc(sizeof(student)); one_student->mat_nr = 123456; strcpy(one_student->first_name, "Max"); strcpy(one_student->last_name, "Mustermann"); print_student(one_student); /* Teil c */ printf(">>> Teil c\n"); student *all_students[ANZAHL]; int i; for (i=0; i<ANZAHL; i++) { all_students[i] = NULL; } all_students[0] = one_student; print_students_db(all_students); /* Teil d */ printf(">>> Teil d\n"); all_students[1] = malloc(sizeof(student)); all_students[1]->mat_nr=653321; strcpy(all_students[1]->first_name, "Herbert"); strcpy(all_students[1]->last_name, "Meier"); all_students[2] = malloc(sizeof(student)); all_students[2]->mat_nr=765431; strcpy(all_students[2]->first_name, "Hubertine"); strcpy(all_students[2]->last_name, "Schulze"); free(all_students[0]); all_students[0] = NULL; print_students_db(all_students); /* Teil e */ printf(">>> Teil e\n"); clear_db(all_students); /* Teil f */ printf(">>> Teil f\n"); if (add_student(123456, "Peter", "Mueller", all_students) == ADD_SUCCEED) { printf("Student hinzugefuegt.\n"); } if (add_student(123456, "Hans", "Wurst", all_students) == MAT_NR_USED) { printf("Student nicht hinzugefuegt, da Mat.nr. schon benutzt wird.\n"); } if (add_student(123457, "Hans", "Wurst", all_students) == ADD_SUCCEED) { printf("Student hinzugefuegt..\n"); } print_students_db(all_students); /* Teil g */ printf(">>> Teil g\n"); if (del_student(123456, all_students) == DEL_SUCCEED) { printf("Student entfernt.\n"); } print_students_db(all_students); }
Kommentare
Wenn du Anmerkungen zur Aufgabe hast oder Lob und Kritik loswerden möchtest, ist hier die richtige Stelle dafür. Klicke einfach ganz rechts auf "bearbeiten" und schreibe deinen Kommentar direkt ins Wiki. Keine Scheu, es geht nichts kaputt ;)