From 60b625c6b39c2cd2ee70eb74024553667b21b411 Mon Sep 17 00:00:00 2001 From: Kaehvaman Date: Mon, 11 Nov 2024 21:36:35 +0400 Subject: [PATCH] swap and sort using only ->next --- lab20/lab20/main.c | 90 +++++++- poker/poker.sln | 31 +++ poker/poker/main.c | 354 ++++++++++++++++++++++++++++++ poker/poker/poker.vcxproj | 135 ++++++++++++ poker/poker/poker.vcxproj.filters | 22 ++ 5 files changed, 627 insertions(+), 5 deletions(-) create mode 100644 poker/poker.sln create mode 100644 poker/poker/main.c create mode 100644 poker/poker/poker.vcxproj create mode 100644 poker/poker/poker.vcxproj.filters diff --git a/lab20/lab20/main.c b/lab20/lab20/main.c index f1ec88a..830c419 100644 --- a/lab20/lab20/main.c +++ b/lab20/lab20/main.c @@ -306,7 +306,55 @@ void task10() { detectRepeats(); } -void swap(int i, int j) { +void swap_by_next(int i, int j) { + if (first == NULL) { + puts("first == NULL, no swap happened"); + return; + } + if (first->next == NULL) { + puts("first->next == NULL, no swap happened"); + return; + } + NODE* prev_ptr = first; + NODE* ptr = first->next; + + NODE* i_prev = prev_ptr; + NODE* i_ptr = ptr; + NODE* j_prev = prev_ptr; + NODE* j_ptr = ptr; + + int index = 1; + while (ptr != NULL) { + if (index == i) { + i_prev = prev_ptr; + i_ptr = ptr; + break; + } + prev_ptr = ptr; + ptr = ptr->next; + index++; + } + prev_ptr = first; + ptr = first->next; + index = 1; + while (ptr != NULL) { + if (index == j) { + j_prev = prev_ptr; + j_ptr = ptr; + break; + } + prev_ptr = ptr; + ptr = ptr->next; + index++; + } + j_prev->next = i_ptr; + i_prev->next = j_ptr; + NODE* tmp = i_ptr->next; + i_ptr->next = j_ptr->next; + j_ptr->next = tmp; +} + +void swap_by_data(int i, int j) { NODE* ptr = first; int i_data = 0, j_data = 0; int index = 0; @@ -334,7 +382,39 @@ void swap(int i, int j) { } } -void bubbleSort() { +NODE* getNode(int index) { + NODE* ptr = first; + int i = 0; + while (ptr != NULL) { + if (i == index) { + return ptr; + } + ptr = ptr->next; + i++; + } + puts("Node not found"); + return NULL; +} + +void bubbleSort_by_next() { + NODE* ptri = first; + int i = 0; + while (ptri->next != NULL) { + int j = 0; + NODE* ptrj = first; + while (ptrj->next != NULL) { + if (ptrj->data > ptrj->next->data) { + swap_by_data(j, j + 1); + } + j++; + ptrj = getNode(j); + } + i++; + ptri = getNode(i); + } +} + +void bubbleSort_by_data() { NODE* ptri = first; int i = 0; while (ptri->next != NULL) { @@ -342,7 +422,7 @@ void bubbleSort() { int j = 0; while (ptrj->next != NULL) { if (ptrj->data > ptrj->next->data) { - swap(j, j + 1); + swap_by_data(j, j + 1); } j++; ptrj = ptrj->next; @@ -356,7 +436,7 @@ void task12() { printf("\n====== task12 ======\n"); listTemplate(); printNodeList(); - swap(1, 4); + swap_by_next(1, 5); printNodeList(); } @@ -364,7 +444,7 @@ void task13() { printf("\n====== task13 ======\n"); listTemplate(); printNodeList(); - bubbleSort(); + bubbleSort_by_next(); printNodeList(); } diff --git a/poker/poker.sln b/poker/poker.sln new file mode 100644 index 0000000..07b58a9 --- /dev/null +++ b/poker/poker.sln @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.11.35327.3 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "poker", "poker\poker.vcxproj", "{AB8D4365-60B2-44E9-91C8-32FB3EF84F74}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 + Release|x64 = Release|x64 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {AB8D4365-60B2-44E9-91C8-32FB3EF84F74}.Debug|x64.ActiveCfg = Debug|x64 + {AB8D4365-60B2-44E9-91C8-32FB3EF84F74}.Debug|x64.Build.0 = Debug|x64 + {AB8D4365-60B2-44E9-91C8-32FB3EF84F74}.Debug|x86.ActiveCfg = Debug|Win32 + {AB8D4365-60B2-44E9-91C8-32FB3EF84F74}.Debug|x86.Build.0 = Debug|Win32 + {AB8D4365-60B2-44E9-91C8-32FB3EF84F74}.Release|x64.ActiveCfg = Release|x64 + {AB8D4365-60B2-44E9-91C8-32FB3EF84F74}.Release|x64.Build.0 = Release|x64 + {AB8D4365-60B2-44E9-91C8-32FB3EF84F74}.Release|x86.ActiveCfg = Release|Win32 + {AB8D4365-60B2-44E9-91C8-32FB3EF84F74}.Release|x86.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {AF256523-D607-4C77-81D0-0D6057E6CF36} + EndGlobalSection +EndGlobal diff --git a/poker/poker/main.c b/poker/poker/main.c new file mode 100644 index 0000000..c37fa3f --- /dev/null +++ b/poker/poker/main.c @@ -0,0 +1,354 @@ +// - B +// , 3- +// AppendixBpoker.c +/* + . , + , 1 5. + 5 , + , , + . + + +. + , + */ + +// +#define _CRT_SECURE_NO_WARNINGS +#include +#include +#include +#include +#include +// , +// . +#define FALSE 0 +#define TRUE 1 +// +void printGreeting(); +int getBet(); +char getSuit(int suit); +char getRank(int rank); +void getFirstHand(int cardRank[], int cardSuit[]); +void getFinalHand(int cardRank[], int cardSuit[], int finalRank[], int finalSuit[], int ranksinHand[], int suitsinHand[]); +int analyzeHand(int ranksinHand[], int suitsinHand[]); +main() +{ + SetConsoleCP(1251); + SetConsoleOutputCP(1251); + int bet; + int bank = 100; + int i; + int cardRank[5]; // 13 (-) + int cardSuit[5]; // 4 ( , , , ) + int finalRank[5]; + int finalSuit[5]; + int ranksinHand[13]; // + int suitsinHand[4]; // + int winnings; + time_t t; + char suit, rank, stillPlay; + // do...while, + // + // , + // main + // , , + // . + printGreeting(); + // , + // + do { + bet = getBet(); + srand(time(&t)); + getFirstHand(cardRank, cardSuit); + printf(" : \n"); + for (i = 0; i < 5; i++) + { + suit = getSuit(cardSuit[i]); + rank = getRank(cardRank[i]); + printf(" %d: %c%c\n", i + 1, rank, suit); + } + // , + // , + // . + // , + // . + for (i = 0; i < 4; i++) + { + suitsinHand[i] = 0; + } + for (i = 0; i < 13; i++) + { + ranksinHand[i] = 0; + } + getFinalHand(cardRank, cardSuit, finalRank, finalSuit, ranksinHand, suitsinHand); + printf(" : \n"); + for (i = 0; i < 5; i++) + { + suit = getSuit(finalSuit[i]); + rank = getRank(finalRank[i]); + printf(" %d: %c%c\n", i + 1, rank, suit); + } + winnings = analyzeHand(ranksinHand, suitsinHand); + printf(" %d!\n", bet * winnings); + bank = bank - bet + (bet * winnings); + printf("\n %d.\n", bank); + printf("\n ? "); + scanf(" %c", &stillPlay); + } while (toupper(stillPlay) == 'Y'); + return; +} +/****************************************************/ +// , +// +// +void printGreeting() +{ + printf("*************************************\n"); + printf("\n\t !\n\n"); + printf("\t \n\n"); + printf("*************************************\n"); + printf(" :\n"); + printf(" 100 , "); + printf(" 1 5 .\n"); + printf(" 5 , , "); + printf(" , - "); + printf(".\n"); + printf(" .\n"); + printf("\n ( "); + printf(" 1 ):"); + printf("\n\t\t\t\t1 "); + printf("\n \t\t\t2 "); + printf("\n \t\t3 "); + printf("\n\t\t\t\t4 "); + printf("\n\t\t\t\t5 "); + printf("\n\t\t\t\t8 "); + printf("\n \t10 "); + printf("\n-\t\t\t20 "); + printf("\n\n!!\n\n"); +} +// +void getFirstHand(int cardRank[], int cardSuit[]) +{ + int i, j; + int cardDup; + for (i = 0; i < 5; i++) + { + cardDup = 0; + do { + // 13 (2-10, , , K, ) + cardRank[i] = (rand() % 13); + // + // (, , , ) + cardSuit[i] = (rand() % 4); + // , + for (j = 0; j < i; j++) + { + if ((cardRank[i] == cardRank[j]) && + (cardSuit[i] == cardSuit[j])) + { + cardDup = 1; + } + } + } while (cardDup == 1); + } +} +// , +// , +char getSuit(int suit) +{ + switch (suit) + { + case 0: + return(''); + case 1: + return(''); + case 2: + return(''); + case 3: + return(''); + } +} +// , +// , +char getRank(int rank) +{ + switch (rank) + { + case 0: + return(''); + case 1: + return('2'); + case 2: + return('3'); + case 3: + return('4'); + case 4: + return('5'); + case 5: + return('6'); + case 6: + return('7'); + case 7: + return('8'); + case 8: + return('9'); + case 9: + return('10'); + case 10: + return(''); + case 11: + return(''); + case 12: + return('K'); + } +} +// ( 1 5) +int getBet() +{ + int bet; + do // , 0 - 5 + { + printf(" ? ( "); + printf("1 5 0 ): "); + scanf(" %d", &bet); + if (bet >= 1 && bet <= 5) + { + return(bet); + } + else if (bet == 0) + { + exit(1); + } + else + { + printf("\n\n 1 5 "); + printf("0 .\n"); + } + } while ((bet < 0) || (bet > 5)); +} +// +// +int analyzeHand(int ranksinHand[], int suitsinHand[]) +{ + int num_consec = 0; + int i, rank, suit; + int straight = FALSE; + int flush = FALSE; + int four = FALSE; + int three = FALSE; + int pairs = 0; + for (suit = 0; suit < 4; suit++) + if (suitsinHand[suit] == 5) + flush = TRUE; + rank = 0; + while (ranksinHand[rank] == 0) { + rank++; + } + for (; rank < 13 && ranksinHand[rank]; rank++) { + num_consec++; + } + if (num_consec == 5) { + straight = TRUE; + } + for (rank = 0; rank < 13; rank++) { + if (ranksinHand[rank] == 4) four = TRUE; + if (ranksinHand[rank] == 3) three = TRUE; + if (ranksinHand[rank] == 2) pairs++; + } + if (straight && flush) { + printf("-\n\n"); + return (20); + } + else if (four) { + printf(" \n\n"); + return (10); + } + else if (three && pairs == 1) { + printf("\n\n"); + return (8); + } + else if (flush) { + printf("\n\n"); + return (5); + } + else if (straight) { + printf("\n\n"); + return (4); + } + else if (three) { + printf(" \n\n"); + return (3); + } + else if (pairs == 2) { + printf(" \n\n"); + return (2); + } + else if (pairs == 1) { + printf("\n\n"); + return (1); + } + else { + printf(" \n\n"); + return (0); + } +} +// +// , +// . , . +void getFinalHand(int cardRank[], int cardSuit[], int + finalRank[], + int finalSuit[], int ranksinHand[], + int suitsinHand[]) +{ + int i, j, cardDup; + char suit, rank, ans; + for (i = 0; i < 5; i++) + { + suit = getSuit(cardSuit[i]); + rank = getRank(cardRank[i]); + printf(" %d: %c%c?", i + 1, rank, suit); + printf("\n, (Y/N): "); + scanf(" %c", &ans); + if (toupper(ans) == 'Y') + { + finalRank[i] = cardRank[i]; + finalSuit[i] = cardSuit[i]; + ranksinHand[finalRank[i]]++; + suitsinHand[finalSuit[i]]++; + continue; + } + else if (toupper(ans) == 'N') + { + cardDup = 0; + do { + cardDup = 0; + finalRank[i] = (rand() % 13); + finalSuit[i] = (rand() % 4); + // 5 + // + for (j = 0; j < 5; j++) + { + if ((finalRank[i] == cardRank[j]) && + (finalSuit[i] == cardSuit[j])) + { + cardDup = 1; + } + } + // , + // + // + for (j = 0; j < i; j++) + { + if ((finalRank[i] == finalRank[j]) && + (finalSuit[i] == finalSuit[j])) + { + cardDup = 1; + } + } + } while (cardDup == 1); + ranksinHand[finalRank[i]]++; + suitsinHand[finalSuit[i]]++; + } + } +} \ No newline at end of file diff --git a/poker/poker/poker.vcxproj b/poker/poker/poker.vcxproj new file mode 100644 index 0000000..5bd886f --- /dev/null +++ b/poker/poker/poker.vcxproj @@ -0,0 +1,135 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 17.0 + Win32Proj + {ab8d4365-60b2-44e9-91c8-32fb3ef84f74} + poker + 10.0 + + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + + + + + + + + + + + + + + + + + + + + Level3 + true + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + Level3 + true + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + + + + + \ No newline at end of file diff --git a/poker/poker/poker.vcxproj.filters b/poker/poker/poker.vcxproj.filters new file mode 100644 index 0000000..669bc4e --- /dev/null +++ b/poker/poker/poker.vcxproj.filters @@ -0,0 +1,22 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Исходные файлы + + + \ No newline at end of file