mirror of
https://github.com/theoleuthardt/learningC.git
synced 2026-06-13 09:37:53 +00:00
Initial commit
This commit is contained in:
commit
826d4c8c9d
81 changed files with 7268 additions and 0 deletions
117
exam/exam_modules/list.c
Normal file
117
exam/exam_modules/list.c
Normal file
|
|
@ -0,0 +1,117 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "list.h"
|
||||
|
||||
|
||||
struct gameData *top = NULL;
|
||||
|
||||
int isEmpty(){
|
||||
return (top == NULL);
|
||||
}
|
||||
|
||||
int size(){
|
||||
if (isEmpty())
|
||||
return 0;
|
||||
|
||||
struct gameData *next = top->next;
|
||||
int size = 1;
|
||||
for (; next != NULL; next = next->next) {
|
||||
size++;
|
||||
}
|
||||
return size;
|
||||
}
|
||||
|
||||
void addGame(char choice1[10], char choice2[10], int winner){
|
||||
struct gameData *temp = (struct gameData *) malloc(sizeof(struct gameData));
|
||||
strcpy(temp->choice1, choice1);
|
||||
strcpy(temp->choice2, choice2);
|
||||
temp->winner = winner;
|
||||
|
||||
if(top == NULL){
|
||||
temp->next = NULL;
|
||||
top = temp;
|
||||
return;
|
||||
}
|
||||
|
||||
temp->next = top;
|
||||
top = temp;
|
||||
}
|
||||
|
||||
void *getGameData(int index){
|
||||
if (top == NULL)
|
||||
return NULL;
|
||||
|
||||
struct gameData *temp = top;
|
||||
for (int i = 0; i <= index; i++) {
|
||||
if (temp == NULL){
|
||||
printf("Element ist nicht enthalten!");
|
||||
return NULL;
|
||||
} else if(i == index){
|
||||
return temp;
|
||||
} else{
|
||||
temp = temp->next;
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int countGames() {
|
||||
int count = 0;
|
||||
|
||||
for (int i = 0; i < size(); ++i) {
|
||||
struct gameData *temp = getGameData(i);
|
||||
count++;
|
||||
}
|
||||
|
||||
return count;
|
||||
}
|
||||
|
||||
void printGameData() {
|
||||
printf("Ausgabe der Spieleergebnisse:\n");
|
||||
printf("\n");
|
||||
|
||||
for (int i = 0; i < size(); ++i) {
|
||||
struct gameData *temp = getGameData(i);
|
||||
printf("Spiel %d:\n", i+1);
|
||||
printf("Auswahl Spieler 1: %s\n", temp->choice1);
|
||||
printf("Auswahl Spieler 2: %s\n", temp->choice2);
|
||||
printf("Gewinner des Spiels: Spieler %d\n", temp->winner);
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
|
||||
void statistics() {
|
||||
double points1 = 0.0;
|
||||
double avgPoints1;
|
||||
double points2 = 0.0;
|
||||
double avgPoints2;
|
||||
double gameNum = (double)countGames();
|
||||
|
||||
printf("Ausgabe der Spielstatistik:\n");
|
||||
printf("\n");
|
||||
|
||||
// Zählung der Punkte
|
||||
for (int i = 0; i < size(); ++i) {
|
||||
struct gameData *temp = getGameData(i);
|
||||
|
||||
if(temp->winner == 0) {
|
||||
points1+=1.0;
|
||||
points2+=1.0;
|
||||
} else if(temp->winner == 1) {
|
||||
points1+=2.0;
|
||||
} else if(temp->winner == 2) {
|
||||
points2+=2.0;
|
||||
}
|
||||
}
|
||||
|
||||
// Berechnung der Punktedurchschnitte
|
||||
avgPoints1 = points1 / gameNum;
|
||||
avgPoints2 = points2 / gameNum;
|
||||
|
||||
// Ausgabe
|
||||
printf("Punkte von Spieler 1: %f\n", points1);
|
||||
printf("Punktedurchschnitt von Spieler 1: %f\n", avgPoints1);
|
||||
printf("Punkte von Spieler 2: %f\n", points2);
|
||||
printf("Punktedurchschnitt von Spieler 2: %f\n", avgPoints2);
|
||||
}
|
||||
19
exam/exam_modules/list.h
Normal file
19
exam/exam_modules/list.h
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
#ifndef EXAMTEMPLATE_LIST_H
|
||||
#define EXAMTEMPLATE_LIST_H
|
||||
|
||||
struct gameData{
|
||||
char choice1[10];
|
||||
char choice2[10];
|
||||
int winner; // Spielerzahl als Ergebnis (1 oder 2, 0 = unentschieden)
|
||||
struct gameData *next;
|
||||
};
|
||||
|
||||
void addGame(char choice1[10], char choice2[10], int winner);
|
||||
int size();
|
||||
void *getGameData(int index);
|
||||
int countGames();
|
||||
void printGameData();
|
||||
void statistics();
|
||||
|
||||
|
||||
#endif //EXAMTEMPLATE_LIST_H
|
||||
Loading…
Add table
Add a link
Reference in a new issue