This commit is contained in:
2025-09-19 16:32:47 +03:00
parent e1b9f50edc
commit a92349ee38
12 changed files with 374 additions and 2 deletions

View File

@@ -0,0 +1,96 @@
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define SUITS 4
#define FACES 13
#define CARDS 52
void shuffle(int deck[][FACES]);
void deal(int deck[][FACES], const char *suits[], const char *faces[]);
void fiveCard(int deck[][FACES], const char *suits[], const char *faces[]);
int main(void)
{
int deck[SUITS][FACES] = {0};
srand((unsigned)time(NULL));
shuffle(deck);
const char *suits[SUITS] = {"Hearts", "Diamonds", "Clubs", "Spades"};
const char *faces[FACES] = {"Ace", "Deuce", "Three", "Four", "Five",
"Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"};
deal(deck, suits, faces);
fiveCard(deck, suits, faces);
return 0;
}
void shuffle(int deck[][FACES]) {
for(size_t card = 1; card <= CARDS; ++card) {
size_t row, column;
do {
row = rand() % SUITS;
column = rand() % FACES;
} while(deck[row][column] != 0);
deck[row][column] = (int)card;
}
}
void deal(int deck[][FACES], const char *suits[], const char *faces[]) {
for(size_t card = 1; card <= CARDS; ++card) {
for(size_t suit = 0; suit < SUITS; ++suit) {
for(size_t face = 0; face < FACES; ++face) {
if(deck[suit][face] == (int)card) {
/*printf("Card: %5s of %-8s%c",
faces[face],
suits[suit],
(card % 4 == 0) ? '\n' : '\t');*/
}
}
}
}
putchar('\n');
}
void fiveCard(int deck[][FACES], const char *suits[], const char *faces[]) {
int hand[5][2] = {0};
int count5 = 0;
while (count5 < 5) {
int row = rand() % SUITS;
int column = rand() % FACES;
if(hand[row][column] != 0) {
hand[count5][0] = row;
hand[count5][1] = column;
printf("Card: %d: %s, %s\n", count5, faces[column], suits[row]);
}
deck[row][column] = 0;
count5++;
}
checkHand(hand, suits, faces);
}
void checkHand(hand[][2], const char *suits[], const char *faces[]) {
int fourOfaKind = 0;
int threeOfaKind = 0;
int twoPairs = 0;
int onePair = 0;
int flush = 0;
int straight = 0;
int same = 0;
for(int i = 0; i < 5; i++) {
int sameVal[FACES] = {0};
int pairs = 0;
sameVal[hand[i][1]]++;
}
}

View File

@@ -8,6 +8,8 @@ int main(void)
int alice = 0;
int superAlice = 0;
int notSuperBob = 0;
int superBob = 0;
int notSuperAlice = 0;
srand(time(NULL));
for(int i = 0; i < 1000; ++i){
@@ -23,16 +25,30 @@ int main(void)
int tempDie = (die % 2) ? die - 1 : die;
if(tempDie % 2){
++notSuperBob;
++superBob;
} else {
++superAlice;
if(tempDie > 3) {
++notSuperAlice;
} else {
++superBob;
}
}
} else {
++notSuperBob;
++superBob;
++superAlice;
}
}
/* int bobtempDie = tempDie;
if (tempDie < 4 && tempDie % 2 != 0) {
bobtempDie = tempDie * 2;
(bobtempDie % 2) ? ++superBob : ++notSuperAlice;
printf("bobtempDie: %d\n", bobtempDie);
}
*/
printf("Normal Alice: %d, Normal Bob: %d\n", alice, bob);
printf("Super Alice: %d, NotSuperBob: %d\n", superAlice, notSuperBob);
printf("Super Bob: %d, notSuperAlice: %d\n", superBob, notSuperAlice);
return 0;
}

46
DeitelC/Chapter7/dieP.c Normal file
View File

@@ -0,0 +1,46 @@
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
void useAliceSuperPower(int *num);
void useBobSuperPower(int *num);
int main(void)
{
int bob = 0; //Bob
int alice = 0;
srand(time(NULL));
int die = 1 + rand() % 6;
printf("%d\n", die);
puts("Die rolled! Alice super power ? (1/x)");
scanf("%d", &alice);
puts("Bob super power? (1/x)");
scanf("%d", &bob);
if (alice == 1) useAliceSuperPower(&die);
if (bob == 1) useBobSuperPower(&die);
if(die % 2 == 0) {
printf("Winner Bob! Die: %d\n", die);
} else {
printf("Winner Alice! Die: %d\n", die);
}
return 0;
}
void useAliceSuperPower(int *num) {
if((*num) % 2 && (*num) != 1) {
(*num)--;
}
}
void useBobSuperPower(int *num) {
if((*num) < 4 && (*num) % 2 != 0) {
(*num) = (*num) * 2;
}
}

45
DeitelC/Chapter7/test2.c Normal file
View File

@@ -0,0 +1,45 @@
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
void useAliceSuperPower(int *num);
void useBobSuperPower(int *num);
int main(void)
{
int bob = 0;
int alice = 0;
srand(time(NULL));
for(int i = 0; i < 1000; ++i){
int die = 1 + rand() % 6;
useAliceSuperPower(&die);
useBobSuperPower(&die);
// Apply modulo 6 to keep within 1-6 range
die = (die - 1) % 6 + 1;
if(die % 2) {
++bob;
} else {
++alice;
}
}
printf("Alice: %d, Bob: %d\n", alice, bob);
return 0;
}
void useAliceSuperPower(int *num) {
if(*num % 2 && *num > 1) {
(*num)--;
}
}
void useBobSuperPower(int *num) {
// More reasonable power: add 1 to even numbers
if(*num % 2 == 0 && *num < 6) {
(*num)++;
}
}

47
DeitelC/Chapter7/test3.c Normal file
View File

@@ -0,0 +1,47 @@
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
void useAliceSuperPower(int *num);
void useBobSuperPower(int *num);
int main(void)
{
int bob = 0;
int alice = 0;
srand(time(NULL));
for(int i = 0; i < 1000; ++i){
int die = 1 + rand() % 6;
useAliceSuperPower(&die);
useBobSuperPower(&die);
// Keep within 1-6 range
if(die < 1) die = 1;
if(die > 6) die = 6;
if(die % 2) {
++bob;
} else {
++alice;
}
}
printf("Alice: %d, Bob: %d\n", alice, bob);
return 0;
}
void useAliceSuperPower(int *num) {
// Only subtract 1 if it's odd and greater than 1
if(*num % 2 && *num > 1) {
(*num)--;
}
}
void useBobSuperPower(int *num) {
// HALF the power: Only add 1 (not double)
if(*num < 6) {
(*num)++;
}
}

View File

@@ -0,0 +1,19 @@
#include <stdio.h>
int main(void) {
int m = 10;
int *z = &m;
int n, o;
printf("\n\n Pointer : Show the basic declaration of pointer :\n");
printf("-------------------------------------------------------\n");
printf(" Here is m=10, n and o are two integer variable and *z is an integer");
printf("\n\n z stores the address of m = %p\n", z); // Printing the address stored in z using %p
printf("\n *z stores the value of m = %i\n", *z); // Printing the value pointed to by z using *z
printf("\n &m is the address of m = %p\n", &m); // Printing the address of m using &m
printf("\n &n stores the address of n = %p\n", &n); // Printing the address of n using &n
printf("\n &o stores the address of o = %p\n", &o); // Printing the address of o using &o
printf("\n &z stores the address of z = %p\n\n", &z); // Printing the address of z using &z
return 0;
}

View File

@@ -0,0 +1,13 @@
#include <stdio.h>
int main(void) {
int m = 29;
int *ab = &m;
printf("m: %d, *ab: %d, ab: %d, address m: %d\n", m, *ab, ab, &m);
m = 32;
printf("m: %d, *ab: %d, ab: %d, address m: %d\n", m, *ab, ab, &m);
*ab = 7;
printf("m: %d, *ab: %d, ab: %d, address m: %d\n", m, *ab, ab, &m);
return 0;
}

View File

@@ -0,0 +1,19 @@
#include <stdio.h>
int main(void) {
int m = 300;
float fx = 300.600006;
char cht = 'z';
int *p1 = &m;
float *p2 = &fx;
char *p3 = &cht;
printf("The & operator: m=%p, fx=%p, cht=%p\n", &m, &fx, &cht);
printf("The & and * operator: m=%d, fx=%f, cht=%c\n", *(&m), *(&fx), *(&cht));
printf("Using only pointer variable: m=%p, fx=%p, cht=%p\n", p1, p2, p3);
printf("Using only pointer operator: m=%d, fx=%f, cht=%c\n", *p1, *p2, *p3);
return 0;
}

View File

@@ -0,0 +1,14 @@
#include <stdio.h>
int main(void) {
puts("Num adder with pointer!");
int num1, num2;
int *p1 = &num1;
int *p2 = &num2;
puts("Enter p1 and p2!");
scanf("%d", &num1);
scanf("%d", &num2);
printf("Total:%d\n", (*p1) + (*p2));
return 0;
}

View File

@@ -0,0 +1,24 @@
#include <stdio.h>
void sumOfNums(int *num1, int *num2);
int main(void)
{
int num1, num2;
puts("Enter num1 and num2");
scanf("%d %d", &num1, &num2);
int *p1 = &num1;
int *p2 = &num2;
sumOfNums(p1, p2);
return 0;
}
void sumOfNums(int *num1, int *num2) {
printf("Sum of Nums: %d\n", (*num1) + (*num2));
}

View File

@@ -0,0 +1,12 @@
#include <stdio.h>
int main(void)
{
int num1, num2;
int *p1 = &num1;
int *p2 = &num2;
puts("Enter nums!");
scanf("%d %d", p1, p2);
(*p1 > *p2) ? printf("%d > %d", *p1, *p2) : printf("%d <= %d", *p1, *p2);
return 0;
}

View File

@@ -0,0 +1,21 @@
#include <stdio.h>
int main(void)
{
puts("Input the number of elements to store in the array: ");
int arr = 0;
scanf("%d", &arr);
int nums[arr] = {};
for(size_t j = 0; j < arr; j++) {
printf("For Element %d enter num!\n", j);
int num = 0;
scanf("%d", &num);
nums[j] = num;
}
for(size_t j = 0; j < arr; j++) {
printf("Element %d = %d\n", j, nums[j]);
}
return 0;
}