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
|
||||
181
exam/main.c
Normal file
181
exam/main.c
Normal file
|
|
@ -0,0 +1,181 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "modules/list.h"
|
||||
|
||||
void gameMenu() {
|
||||
int iteration = 1;
|
||||
int option;
|
||||
char choice1[10];
|
||||
char choice2[10];
|
||||
int winner = 3; // Spielerzahl als Ergebnis (1 oder 2, 0 = unentschieden)
|
||||
|
||||
printf("Willkommen zu Schere-Stein-Papier! Wähle eine Option von 1 bis 4 zum Ausführen.\n");
|
||||
printf("(1): Spiel starten\n");
|
||||
printf("(2): Ausgabe der Anzahl und Details der bisher gespielten Partien\n");
|
||||
printf("(3): Ausgabe einer aktuellen Statistik (Punktesystem)\n");
|
||||
printf("(4): Beenden\n");
|
||||
printf("Auswahl: ");
|
||||
scanf("%d", &option);
|
||||
printf("\n");
|
||||
|
||||
switch (option) {
|
||||
case 1:
|
||||
// Spielstart mit Auswahlmöglichkeiten
|
||||
printf("Die Auswahlmöglichkeiten sind Schere, Stein oder Papier!\n");
|
||||
printf("Auswahl von Spieler 1:");
|
||||
scanf("%s", choice1);
|
||||
//printf("%s\n", choice1);
|
||||
printf("Auswahl von Spieler 2:");
|
||||
scanf("%s", choice2);
|
||||
//printf("%s\n", choice2);
|
||||
|
||||
/*
|
||||
// StringCompare-Werte testen, kommen sehr weirde Werte raus -> Anpassung der Abfrage
|
||||
printf("%d\n", strcmp("Stein", choice1));
|
||||
printf("%d\n", strcmp("Schere", choice1));
|
||||
printf("%d\n", strcmp("Papier", choice1));
|
||||
printf("%d\n", strcmp("Stein", choice2));
|
||||
printf("%d\n", strcmp("Schere", choice2));
|
||||
printf("%d\n", strcmp("Papier", choice2));
|
||||
printf("%d\n", strcmp(choice1, choice2));
|
||||
*/
|
||||
|
||||
//Spielentscheidung
|
||||
if(strcmp("Schere", choice1) == 0 && strcmp("Papier", choice2) == 0) {
|
||||
winner = 1;
|
||||
} else if(strcmp("Papier", choice1) == 0 && strcmp("Stein", choice2) == 0) {
|
||||
winner = 1;
|
||||
} else if(strcmp("Stein", choice1) == 0 && strcmp("Schere", choice2) == 0) {
|
||||
winner = 1;
|
||||
} else if(strcmp("Schere", choice2) == 0 && strcmp("Papier", choice1) == 0) {
|
||||
winner = 2;
|
||||
} else if(strcmp("Papier", choice2) == 0 && strcmp("Stein", choice1) == 0) {
|
||||
winner = 2;
|
||||
} else if(strcmp("Stein", choice2) == 0 && strcmp("Schere", choice1) == 0) {
|
||||
winner = 2;
|
||||
} else if(strcmp(choice1, choice2) == 0) {
|
||||
winner = 0;
|
||||
}
|
||||
|
||||
|
||||
// Ergebnisausgabe
|
||||
if(winner == 0) {
|
||||
printf("\nDas Spiel ging unentschieden aus!\n");
|
||||
} else if(winner == 1) {
|
||||
printf("\nSpieler 1 hat gewonnen!\n");
|
||||
} else if(winner == 2) {
|
||||
printf("\nSpieler 2 hat gewonnen!\n");
|
||||
} else {
|
||||
printf("\nKein Spielergebnis ist bisher vorhanden!\n");
|
||||
}
|
||||
|
||||
//Daten speichern
|
||||
addGame(choice1, choice2, winner);
|
||||
|
||||
iteration++;
|
||||
break;
|
||||
case 2:
|
||||
printf("\nAnzahl der gespielten Partien: %d\n", countGames());
|
||||
printGameData();
|
||||
iteration++;
|
||||
break;
|
||||
case 3:
|
||||
statistics();
|
||||
iteration++;
|
||||
break;
|
||||
case 4:
|
||||
break;
|
||||
default:
|
||||
printf("Bitte gib eine Zahl zwischen 1 und 4 ein!");
|
||||
break;
|
||||
}
|
||||
|
||||
while(iteration > 1) {
|
||||
printf("\nWeitere Option ausführen? \n");
|
||||
printf("(1): Spiel starten\n");
|
||||
printf("(2): Ausgabe der Anzahl und Details der bisher gespielten Partien\n");
|
||||
printf("(3): Ausgabe einer aktuellen Statistik (Punktesystem)\n");
|
||||
printf("(4): Beenden\n");
|
||||
printf("Auswahl: ");
|
||||
scanf("%d", &option);
|
||||
printf("\n");
|
||||
|
||||
switch (option) {
|
||||
case 1:
|
||||
// Spielstart mit Auswahlmöglichkeiten
|
||||
printf("Die Auswahlmöglichkeiten sind Schere, Stein oder Papier!\n");
|
||||
printf("Auswahl von Spieler 1:");
|
||||
scanf("%s", choice1);
|
||||
//printf("%s\n", choice1);
|
||||
printf("Auswahl von Spieler 2:");
|
||||
scanf("%s", choice2);
|
||||
//printf("%s\n", choice2);
|
||||
|
||||
/*
|
||||
// StringCompare-Werte testen, kommen sehr weirde Werte raus -> Anpassung der Abfrage
|
||||
printf("%d\n", strcmp("Stein", choice1));
|
||||
printf("%d\n", strcmp("Schere", choice1));
|
||||
printf("%d\n", strcmp("Papier", choice1));
|
||||
printf("%d\n", strcmp("Stein", choice2));
|
||||
printf("%d\n", strcmp("Schere", choice2));
|
||||
printf("%d\n", strcmp("Papier", choice2));
|
||||
printf("%d\n", strcmp(choice1, choice2));
|
||||
*/
|
||||
|
||||
//Spielentscheidung
|
||||
if(strcmp("Schere", choice1) == 0 && strcmp("Papier", choice2) == 0) {
|
||||
winner = 1;
|
||||
} else if(strcmp("Papier", choice1) == 0 && strcmp("Stein", choice2) == 0) {
|
||||
winner = 1;
|
||||
} else if(strcmp("Stein", choice1) == 0 && strcmp("Schere", choice2) == 0) {
|
||||
winner = 1;
|
||||
} else if(strcmp("Schere", choice2) == 0 && strcmp("Papier", choice1) == 0) {
|
||||
winner = 2;
|
||||
} else if(strcmp("Papier", choice2) == 0 && strcmp("Stein", choice1) == 0) {
|
||||
winner = 2;
|
||||
} else if(strcmp("Stein", choice2) == 0 && strcmp("Schere", choice1) == 0) {
|
||||
winner = 2;
|
||||
} else if(strcmp(choice1, choice2) == 0) {
|
||||
winner = 0;
|
||||
}
|
||||
|
||||
|
||||
// Ergebnisausgabe
|
||||
if(winner == 0) {
|
||||
printf("\nDas Spiel ging unentschieden aus!\n");
|
||||
} else if(winner == 1) {
|
||||
printf("\nSpieler 1 hat gewonnen!\n");
|
||||
} else if(winner == 2) {
|
||||
printf("\nSpieler 2 hat gewonnen!\n");
|
||||
} else {
|
||||
printf("\nKein Spielergebnis ist bisher vorhanden!\n");
|
||||
}
|
||||
|
||||
//Daten speichern
|
||||
addGame(choice1, choice2, winner);
|
||||
|
||||
iteration++;
|
||||
break;
|
||||
case 2:
|
||||
printf("\nAnzahl der gespielten Partien: %d\n", countGames());
|
||||
printGameData();
|
||||
iteration++;
|
||||
break;
|
||||
case 3:
|
||||
statistics();
|
||||
iteration++;
|
||||
break;
|
||||
case 4:
|
||||
return;
|
||||
default:
|
||||
iteration++;
|
||||
printf("Bitte gib eine Zahl zwischen 1 und 4 ein!");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
gameMenu();
|
||||
return 0;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue