Browse Source

Merge branch 'Pascal' into 'main'

Pascal erster Merge Versuche 2

See merge request fdai7760/the-quizstars!7
main
fdai7760 11 months ago
parent
commit
946641f9e3
  1. 200
      src/timequiz.c
  2. 4
      src/timequiz.h
  3. 118
      test/test_timequiz.c

200
src/timequiz.c

@ -0,0 +1,200 @@
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include "timequiz.h"
void timequiz();
int getRandomQuestionIndex(int askedQuestions[], int totalQuestions);
void displayQuestion(char* question, char* answers[], int correctIndex);
void processUserAnswer(int userAnswer, int correctIndex, int* score, int* totalCorrectAnswers, char* answers[]);
int isValidAnswer(int userAnswer);
void timequiz() {
printf("Welcome to our Time Quiz!\n");
printf("You have 60 seconds to answer the questions. Have fun!\n");
char* questions[] = {
"What is the capital of France?",
"Who developed the theory of relativity?",
"Who was the first President of the United States?",
"What is the chemical symbol for water?",
"Who wrote 'To Kill a Mockingbird'?",
"What is the largest planet in our solar system?",
"Who painted the Mona Lisa?",
"What is the tallest mountain in the world?",
"Which country is known as the Land of the Rising Sun?",
"Who was the first person to walk on the moon?",
"What year did World War I begin?",
"What is the currency of Japan?",
"Who is known as the 'Father of Computers'?",
"What is the capital of Canada?",
"Who discovered penicillin?",
"What is the chemical formula for table salt?",
"Which is the largest ocean on Earth?",
"Who is the author of '1984'?",
"What is the main ingredient in guacamole?",
"Who was the Greek god of the sea?",
"What is the square root of 144?",
"Who wrote 'Romeo and Juliet'?",
"What is the process by which plants make their food?",
"What is the boiling point of water in Celsius?",
"Who painted 'Starry Night'?",
"What is the chemical symbol for gold?",
"Which planet is known as the Red Planet?",
"Who invented the telephone?",
"What is the capital of Brazil?",
"What is the hardest natural substance on Earth?",
"Who was the first woman to win a Nobel Prize?",
"Who composed 'The Four Seasons'?",
"What is the largest mammal in the world?",
"What is the study of earthquakes called?",
"Who wrote 'Hamlet'?",
"What is the smallest prime number?",
"Who is known as the 'Queen of Pop'?",
"What is the chemical symbol for oxygen?",
"What is the capital of Australia?"
};
char* answers[][4] = {
{"Paris", "London", "Berlin", "Madrid"},
{"Albert Einstein", "Isaac Newton", "Galileo Galilei", "Stephen Hawking"},
{"George Washington", "Thomas Jefferson", "John Adams", "James Madison"},
{"H2O", "CO2", "NaCl", "C6H12O6"},
{"Harper Lee", "J.K. Rowling", "Ernest Hemingway", "George Orwell"},
{"Jupiter", "Saturn", "Mars", "Neptune"},
{"Leonardo da Vinci", "Michelangelo", "Pablo Picasso", "Vincent van Gogh"},
{"Mount Everest", "K2", "Kangchenjunga", "Lhotse"},
{"Japan", "China", "South Korea", "India"},
{"Neil Armstrong", "Buzz Aldrin", "Yuri Gagarin", "Alan Shepard"},
{"1914", "1918", "1923", "1939"},
{"Yen", "Euro", "Dollar", "Pound"},
{"Charles Babbage", "Alan Turing", "Ada Lovelace", "Bill Gates"},
{"Ottawa", "Toronto", "Vancouver", "Montreal"},
{"Alexander Fleming", "Louis Pasteur", "Marie Curie", "Albert Einstein"},
{"NaCl", "H2O", "CO2", "HCl"},
{"Pacific Ocean", "Atlantic Ocean", "Indian Ocean", "Arctic Ocean"},
{"George Orwell", "F. Scott Fitzgerald", "Aldous Huxley", "J.D. Salinger"},
{"Avocado", "Tomato", "Onion", "Lime"},
{"Poseidon", "Zeus", "Hades", "Apollo"},
{"12", "10", "16", "14"},
{"William Shakespeare", "Jane Austen", "Emily Bronte", "Leo Tolstoy"},
{"Photosynthesis", "Respiration", "Fermentation", "Transpiration"},
{"100", "0", "212", "50"},
{"Vincent van Gogh", "Pablo Picasso", "Leonardo da Vinci", "Claude Monet"},
{"Au", "Ag", "Hg", "Fe"},
{"Mars", "Jupiter", "Venus", "Mercury"},
{"Alexander Graham Bell", "Thomas Edison", "Nikola Tesla", "Galileo Galilei"},
{"Brasília", "Rio de Janeiro", "São Paulo", "Buenos Aires"},
{"Diamond", "Quartz", "Graphite", "Topaz"},
{"Marie Curie", "Rosalind Franklin", "Mother Teresa", "Malala Yousafzai"},
{"299,792,458 meters per second", "300,000,000 meters per second", "200,000,000 meters per second", "150,000,000 meters per second"},
{"Antonio Vivaldi", "Wolfgang Amadeus Mozart", "Ludwig van Beethoven", "Johann Sebastian Bach"},
{"Blue whale", "African elephant", "Giraffe", "Polar bear"},
{"Seismology", "Meteorology", "Geology", "Astronomy"},
{"William Shakespeare", "Charles Dickens", "Jane Austen", "Mark Twain"},
{"2", "3", "5", "7"},
{"Madonna", "Beyoncé", "Lady Gaga", "Taylor Swift"},
{"O", "Co", "O2", "O3"},
{"Canberra", "Sydney", "Melbourne", "Perth"}
};
int correctAnswers[] = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1 };
for (int i = 0; i < sizeof(questions) / sizeof(questions[0]); i++) {
int j = rand() % 4;
char* temp = answers[i][0];
answers[i][0] = answers[i][j];
answers[i][j] = temp;
if (j == 0) {
correctAnswers[i] = 1;
}
else if (j == 1) {
correctAnswers[i] = 2;
}
else if (j == 2) {
correctAnswers[i] = 3;
}
else {
correctAnswers[i] = 4;
}
}
int score = 0;
int totalAnsweredQuestions = 0;
int totalCorrectAnswers = 0;
time_t startTime = time(NULL);
time_t currentTime;
int elapsedTime = 0;
int totalQuestions = sizeof(questions) / sizeof(questions[0]);
int askedQuestions[40];
memset(askedQuestions, 0, sizeof(askedQuestions));
srand((unsigned int)time(NULL));
while (elapsedTime < 60 && totalAnsweredQuestions < totalQuestions) {
int questionIndex = getRandomQuestionIndex(askedQuestions, totalQuestions);
askedQuestions[questionIndex] = 1;
int correctIndex = correctAnswers[questionIndex] - 1;
displayQuestion(questions[questionIndex], answers[questionIndex], correctIndex);
int userAnswer;
printf("Answer (1-4): ");
scanf_s("%d", &userAnswer);
while (!isValidAnswer(userAnswer)) {
printf("Invalid input. Please enter a number between 1 and 4: ");
scanf_s("%d", &userAnswer);
}
processUserAnswer(userAnswer, correctIndex, &score, &totalCorrectAnswers, answers[questionIndex]);
totalAnsweredQuestions++;
currentTime = time(NULL);
elapsedTime = (int)difftime(currentTime, startTime);
}
printf("\nQuiz finished!\n");
printf("Your total score: %d out of %d\n", totalCorrectAnswers, totalAnsweredQuestions);
}
int getRandomQuestionIndex(int askedQuestions[], int totalQuestions) {
int questionIndex;
do {
questionIndex = rand() % totalQuestions;
} while (askedQuestions[questionIndex] == 1);
return questionIndex;
}
void displayQuestion(char* question, char* answers[], int correctIndex) {
printf("\nQuestion: %s\n", question);
for (int i = 0; i < 4; i++) {
printf("%d. %s\n", i + 1, answers[i]);
}
}
void processUserAnswer(int userAnswer, int correctIndex, int* score, int* totalCorrectAnswers, char* answers[]) {
if (userAnswer == correctIndex + 1) {
printf("Correct!\n");
(*score)++;
(*totalCorrectAnswers)++;
}
else {
printf("Wrong! The correct answer is: %d. %s\n", correctIndex + 1, answers[correctIndex]);
}
}
int isValidAnswer(int userAnswer) {
return (userAnswer >= 1 && userAnswer <= 4);
}

4
src/timequiz.h

@ -2,5 +2,9 @@
#define TIMEQUIZ_H #define TIMEQUIZ_H
void timequiz(); void timequiz();
int getRandomQuestionIndex(int askedQuestions[], int totalQuestions);
void displayQuestion(char* question, char* answers[], int correctIndex);
void processUserAnswer(int userAnswer, int correctIndex, int* score, int* totalCorrectAnswers, char* answers[]);
int isValidAnswer(int userAnswer);
#endif //ende TIMEQUIZ_H #endif //ende TIMEQUIZ_H

118
test/test_timequiz.c

@ -0,0 +1,118 @@
#ifdef TEST
#include "unity.h"
#include "timequiz.h"
void setUp(void) {
}
void tearDown(void) {
}
void test_getRandomQuestionIndex(void) {
int askedQuestions[10] = {0};
int totalQuestions = 10;
int index = getRandomQuestionIndex(askedQuestions, totalQuestions);
TEST_ASSERT_TRUE(index >= 0 && index < totalQuestions);
}
void test_displayQuestion(void) {
char* question = "Test Question";
char* answers[] = {"A", "B", "C", "D"};
int correctIndex = 0;
printf("\nExpected Output:\n");
printf("Question: %s\n", question);
for (int i = 0; i < 4; i++) {
printf("%d. %s\n", i + 1, answers[i]);
}
printf("\n");
printf("Actual Output:\n");
displayQuestion(question, answers, correctIndex);
}
void test_processUserAnswer_correct(void) {
int score = 0;
int totalCorrectAnswers = 0;
char* answers[] = {"Paris", "London", "Berlin", "Madrid"};
int correctIndex = 0;
int userAnswer = 1;
processUserAnswer(userAnswer, correctIndex, &score, &totalCorrectAnswers, answers);
TEST_ASSERT_EQUAL_INT(1, score);
TEST_ASSERT_EQUAL_INT(1, totalCorrectAnswers);
}
void test_processUserAnswer_wrong(void) {
int score = 0;
int totalCorrectAnswers = 0;
char* answers[] = {"Paris", "London", "Berlin", "Madrid"};
int correctIndex = 0;
int userAnswer = 2; // Assuming user selects the second option
processUserAnswer(userAnswer, correctIndex, &score, &totalCorrectAnswers, answers);
TEST_ASSERT_EQUAL_INT(0, score);
TEST_ASSERT_EQUAL_INT(0, totalCorrectAnswers);
}
void test_processUserAnswer_correctAnswer_index3(void) {
int score = 0;
int totalCorrectAnswers = 0;
char* answers[] = {"A", "B", "C", "D"};
processUserAnswer(4, 3, &score, &totalCorrectAnswers, answers);
TEST_ASSERT_EQUAL_INT(1, score);
TEST_ASSERT_EQUAL_INT(1, totalCorrectAnswers);
}
void test_processUserAnswer_correctAnswer_index0(void) {
int score = 0;
int totalCorrectAnswers = 0;
char* answers[] = {"A", "B", "C", "D"};
processUserAnswer(1, 0, &score, &totalCorrectAnswers, answers);
TEST_ASSERT_EQUAL_INT(1, score);
TEST_ASSERT_EQUAL_INT(1, totalCorrectAnswers);
}
void test_processUserAnswer_wrongAnswer_index3(void) {
int score = 0;
int totalCorrectAnswers = 0;
char* answers[] = {"Paris", "London", "Berlin", "Madrid"};
int correctIndex = 0;
int userAnswer = 4; // Assuming user selects the fourth option
processUserAnswer(userAnswer, correctIndex, &score, &totalCorrectAnswers, answers);
TEST_ASSERT_EQUAL_INT(0, score);
TEST_ASSERT_EQUAL_INT(0, totalCorrectAnswers);
}
void test_processUserAnswer_correctAnswer_index1(void) {
int score = 0;
int totalCorrectAnswers = 0;
char* answers[] = {"Paris", "London", "Berlin", "Madrid"};
int correctIndex = 1;
int userAnswer = 2; // Assuming user selects the second option
processUserAnswer(userAnswer, correctIndex, &score, &totalCorrectAnswers, answers);
TEST_ASSERT_EQUAL_INT(1, score);
TEST_ASSERT_EQUAL_INT(1, totalCorrectAnswers);
}
void test_processUserAnswer_correctAnswer_index2(void) {
int score = 0;
int totalCorrectAnswers = 0;
char* answers[] = {"Paris", "London", "Berlin", "Madrid"};
int correctIndex = 2;
int userAnswer = 3; // Assuming user selects the third option
processUserAnswer(userAnswer, correctIndex, &score, &totalCorrectAnswers, answers);
TEST_ASSERT_EQUAL_INT(1, score);
TEST_ASSERT_EQUAL_INT(1, totalCorrectAnswers);
}
void test_processUserAnswer_wrongAnswer_index1(void) {
int score = 0;
int totalCorrectAnswers = 0;
char* answers[] = {"Paris", "London", "Berlin", "Madrid"};
int correctIndex = 2;
int userAnswer = 2; // Assuming user selects the second option
processUserAnswer(userAnswer, correctIndex, &score, &totalCorrectAnswers, answers);
TEST_ASSERT_EQUAL_INT(0, score);
TEST_ASSERT_EQUAL_INT(0, totalCorrectAnswers);
}
#endif //TEST
Loading…
Cancel
Save