Diskuze: Slovní fotbal
V předchozím kvízu, Online test znalostí C++, jsme si ověřili nabyté zkušenosti z kurzu.

Člen

Zobrazeno 6 zpráv z 6.
//= Settings::TRACKING_CODE_B ?> //= Settings::TRACKING_CODE ?>
V předchozím kvízu, Online test znalostí C++, jsme si ověřili nabyté zkušenosti z kurzu.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static char** g_words = NULL;
static int g_totalWords = 0;
{
FILE *fr;
fr = fopen("slovnik.txt", "r");
char buffer[1024];
int line = 0;
if (!file)
{
printf("Soubor se nepodarilo otevrit nebo neexistuje!");
return EXIT_FAILURE;
}
else
{
printf("Probiha nacitani slov do slovniku...\n");
}
while (fgets(buffer, sizeof(buffer), file))
{
size_t length = strlen(buffer);
if (length > 0)
*(buffer + length - 1) = '�';
if (!line)
{
g_words = (char **)calloc(atoi(buffer), sizeof(char ));
}
else if (length > 0)
{
printf("Load: %s\n", buffer);
*(g_words + g_totalWords) = (char *)calloc(length, sizeof(char));
memcpy((g_words + g_totalWords), buffer, length);
++g_totalWords;
}
++line;
}
fclose(file);
return EXIT_SUCCESS;
}
void unloadWords()
{
if (g_words)
{
int i;
for (i = 0; i < g_totalWords; ++i)
{
free(*(g_words + i));
}
free(g_words);
}
}
int constainsWord(const char* word)
{
int i;
if (!g_words || !word)
return EXIT_FAILURE;
for (i = 0; i < g_totalWords; ++i)
{
if (!strcmp(*(g_words + i), word))
return EXIT_SUCCESS;
}
}
int main(int argc, char** argv)
{
// deklarace a initializace promennych
char slovo [1024], temp [1024], p;
int error = 0;
if (loadWords("slovnik.txt") == EXIT_FAILURE)
return EXIT_FAILURE;
printf(":: SLOVNI FOTBAL ::\n");
printf("-> tady budou pravidla hry\n");
// nacitani znaku/slov z konzole a cela logika
printf("Zadej pismeno, jimz bude hra zacinat: ");
scanf("%s", temp);
p = temp[0];
int hrac = 1;
do {
printf("Hrac %d -> Zadej slovo: ", hrac);
scanf("%s", slovo);
if (p != slovo[0]) {
printf("Slovo nezacina znakem '%c'\n", p);
error++;
} else if (constainsWord("") != EXIT_FAILURE) {
printf("Slovo neni ve slovniku!\n");
error++;
}
if (error > 0) {
printf("Vitezem se stava hrac %d.\n", hrac == 1?2:1);
break;
} else {
p = slovo[strlen(slovo) - 1];
if (hrac == 1) {
hrac++;
} else {
hrac = 1;
}
}
} while (error <= 0);
printf("Pokracuj stiskem klavesy... ");
unloadWords();
return EXIT_SUCCESS;
}
Použij tlačítko vložit kód, tenhle blivajs nikdo číst nebude
Zapoměl jsi na ikonku Vložit zdrojový kód.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static char** g_words = NULL;
static int g_totalWords = 0;
{
FILE *fr;
fr = fopen("slovnik.txt", "r");
char buffer[1024];
int line = 0;
if (!file)
{
printf("Soubor se nepodarilo otevrit nebo neexistuje!");
return EXIT_FAILURE;
}
else
{
printf("Probiha nacitani slov do slovniku...\n");
}
while (fgets(buffer, sizeof(buffer), file))
{
size_t length = strlen(buffer);
if (length > 0)
*(buffer + length - 1) = '�';
if (!line)
{
g_words = (char **)calloc(atoi(buffer), sizeof(char *));
}
else if (length > 0)
{
printf("Load: %s\n", buffer);
*(g_words + g_totalWords) = (char *)calloc(length, sizeof(char));
memcpy(*(g_words + g_totalWords), buffer, length);
++g_totalWords;
}
++line;
}
fclose(file);
return EXIT_SUCCESS;
}
void unloadWords()
{
if (g_words)
{
int i;
for (i = 0; i < g_totalWords; ++i)
{
free(*(g_words + i));
}
free(g_words);
}
}
int constainsWord(const char* word)
{
int i;
if (!g_words || !word)
return EXIT_FAILURE;
for (i = 0; i < g_totalWords; ++i)
{
if (!strcmp(*(g_words + i), word))
return EXIT_SUCCESS;
}
}
int main(int argc, char** argv)
{
// deklarace a initializace promennych
char slovo [1024], temp [1024], p;
int error = 0;
if (loadWords("slovnik.txt") == EXIT_FAILURE)
return EXIT_FAILURE;
printf(":: SLOVNI FOTBAL ::\n");
printf("-> tady budou pravidla hry\n");
// nacitani znaku/slov z konzole a cela logika
printf("Zadej pismeno, jimz bude hra zacinat: ");
scanf("%s", temp);
p = temp[0];
int hrac = 1;
do {
printf("Hrac %d -> Zadej slovo: ", hrac);
scanf("%s", slovo);
if (p != slovo[0]) {
printf("Slovo nezacina znakem '%c'\n", p);
error++;
} else if (constainsWord("") != EXIT_FAILURE) {
printf("Slovo neni ve slovniku!\n");
error++;
}
if (error > 0) {
printf("Vitezem se stava hrac %d.\n", hrac == 1?2:1);
break;
} else {
p = slovo[strlen(slovo) - 1];
if (hrac == 1) {
hrac++;
} else {
hrac = 1;
}
}
} while (error <= 0);
printf("Pokracuj stiskem klavesy... ");
unloadWords();
return EXIT_SUCCESS;
}
Zobrazeno 6 zpráv z 6.