First Upload
This commit is contained in:
1703
13 - Bits/PointersLibs.h
Normal file
1703
13 - Bits/PointersLibs.h
Normal file
File diff suppressed because it is too large
Load Diff
BIN
13 - Bits/ex1
Normal file
BIN
13 - Bits/ex1
Normal file
Binary file not shown.
12
13 - Bits/ex1.c
Normal file
12
13 - Bits/ex1.c
Normal file
@@ -0,0 +1,12 @@
|
||||
#include "PointersLibs.h"
|
||||
|
||||
BOOLEAN EvenNumber(int num)
|
||||
{
|
||||
return (num & !MASK_FIRST_BIT);
|
||||
}
|
||||
|
||||
void main(void)
|
||||
{
|
||||
int num = 2;
|
||||
printf("%d\n" ,EvenNumber(3));
|
||||
}
|
||||
BIN
13 - Bits/ex2
Normal file
BIN
13 - Bits/ex2
Normal file
Binary file not shown.
21
13 - Bits/ex2.c
Normal file
21
13 - Bits/ex2.c
Normal file
@@ -0,0 +1,21 @@
|
||||
#include "PointersLibs.h"
|
||||
|
||||
int RotateLeft(unsigned int num, int count)
|
||||
{
|
||||
unsigned short counter;
|
||||
unsigned int temp;
|
||||
for (counter = ZERO; counter < count; counter++)
|
||||
{
|
||||
temp = (num >> ((sizeof(num) * EIGHT) - ONE));
|
||||
num <<= ONE;
|
||||
num |= temp;
|
||||
}
|
||||
|
||||
return num;
|
||||
}
|
||||
|
||||
void main(void)
|
||||
{
|
||||
unsigned int x = 1000000000;
|
||||
printf("%u\n", RotateLeft(x,30));
|
||||
}
|
||||
BIN
13 - Bits/ex3
Normal file
BIN
13 - Bits/ex3
Normal file
Binary file not shown.
20
13 - Bits/ex3.c
Normal file
20
13 - Bits/ex3.c
Normal file
@@ -0,0 +1,20 @@
|
||||
#include "PointersLibs.h"
|
||||
|
||||
BOOLEAN EvenBits(byte b)
|
||||
{
|
||||
BOOLEAN even = TRUE;
|
||||
unsigned short loop_length;
|
||||
for (loop_length = sizeof(b) * EIGHT; loop_length; loop_length--)
|
||||
{
|
||||
even ^= b & MASK_FIRST_BIT;
|
||||
b >>= ONE;
|
||||
}
|
||||
|
||||
return (even);
|
||||
}
|
||||
|
||||
void main(void)
|
||||
{
|
||||
byte b = 3;
|
||||
printf("%hu\n", EvenBits(b));
|
||||
}
|
||||
BIN
13 - Bits/ex4
Normal file
BIN
13 - Bits/ex4
Normal file
Binary file not shown.
159
13 - Bits/ex4.c
Normal file
159
13 - Bits/ex4.c
Normal file
@@ -0,0 +1,159 @@
|
||||
#include "PointersLibs.h"
|
||||
|
||||
#define ROWS 4
|
||||
|
||||
void RemoveBits(byte *row, unsigned short count)
|
||||
{
|
||||
*row >>= count;
|
||||
}
|
||||
|
||||
unsigned short CountBits(byte b)
|
||||
{
|
||||
unsigned short counter = ZERO,
|
||||
loop_length = sizeof(b) * EIGHT;
|
||||
while (loop_length--)
|
||||
{
|
||||
counter += !EvenNumber(b);
|
||||
b >>= ONE;
|
||||
}
|
||||
|
||||
return (counter);
|
||||
}
|
||||
|
||||
void PrintGetRow()
|
||||
{
|
||||
printf("Enter row number (1-4): ");
|
||||
}
|
||||
|
||||
void PrintGetRemoveCount()
|
||||
{
|
||||
printf("Enter many bits: ");
|
||||
}
|
||||
|
||||
void PrintPlayerWon()
|
||||
{
|
||||
printf("You won!\n");
|
||||
}
|
||||
|
||||
void PrintBotWon()
|
||||
{
|
||||
printf("The bot won :(\n");
|
||||
}
|
||||
|
||||
unsigned short InputRow()
|
||||
{
|
||||
unsigned short row;
|
||||
scanf("%hu", &row);
|
||||
|
||||
return (row);
|
||||
}
|
||||
|
||||
unsigned short InputRemoveCount()
|
||||
{
|
||||
unsigned short remove_count;
|
||||
scanf("%hu", &remove_count);
|
||||
|
||||
return (remove_count);
|
||||
}
|
||||
|
||||
BOOLEAN EvenBits(byte b)
|
||||
{
|
||||
return (!(CountBits(b) % TWO));
|
||||
}
|
||||
|
||||
unsigned short SumBits(byte *vec, unsigned short length)
|
||||
{
|
||||
unsigned short sum = ZERO;
|
||||
while (length--)
|
||||
{
|
||||
sum += CountBits(*(vec++));
|
||||
}
|
||||
|
||||
return (sum);
|
||||
}
|
||||
|
||||
void PrintBits(byte b)
|
||||
{
|
||||
unsigned short length = sizeof(b) * EIGHT;
|
||||
while (length--)
|
||||
{
|
||||
printf("%hu,", !EvenNumber(b));
|
||||
b >>= ONE;
|
||||
}
|
||||
}
|
||||
|
||||
void PrintRows(byte *rows, unsigned short length)
|
||||
{
|
||||
while (length--)
|
||||
{
|
||||
PrintBits(*(rows++));
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
|
||||
byte * MaxBitsRow(byte *rows, unsigned short length)
|
||||
{
|
||||
unsigned short max_count = ZERO,
|
||||
temp_count;
|
||||
byte *max_row;
|
||||
while (length--)
|
||||
{
|
||||
temp_count = CountBits(*rows);
|
||||
if (temp_count > max_count)
|
||||
{
|
||||
max_count = temp_count;
|
||||
max_row = rows;
|
||||
}
|
||||
rows++;
|
||||
}
|
||||
|
||||
return (max_row);
|
||||
}
|
||||
|
||||
void Bot(byte *rows, unsigned short length)
|
||||
{
|
||||
byte *max_row = MaxBitsRow(rows, length);
|
||||
|
||||
RemoveBits(max_row, MAX(CountBits(*max_row) / TWO * TWO, ONE));
|
||||
}
|
||||
|
||||
void main(void)
|
||||
{
|
||||
byte rows[ROWS] = {0x7f, 0x1f, 0x7, 0x1};
|
||||
unsigned short row = -ONE, remove_count = ZERO;
|
||||
BOOLEAN player = FALSE;
|
||||
while (SumBits(rows, ROWS))
|
||||
{
|
||||
PrintRows(rows, ROWS);
|
||||
printf("\n\n");
|
||||
row = -ONE;
|
||||
remove_count = ZERO;
|
||||
player = !player;
|
||||
if (player)
|
||||
{
|
||||
while (!(row < ROWS))
|
||||
{
|
||||
PrintGetRow();
|
||||
row = InputRow();
|
||||
}
|
||||
while (!(ZERO < remove_count && remove_count <= CountBits(rows[row])))
|
||||
{
|
||||
PrintGetRemoveCount();
|
||||
remove_count = InputRemoveCount();
|
||||
}
|
||||
RemoveBits(&rows[row], remove_count);
|
||||
}
|
||||
else
|
||||
{
|
||||
Bot(rows, ROWS);
|
||||
}
|
||||
}
|
||||
if (player)
|
||||
{
|
||||
PrintPlayerWon();
|
||||
}
|
||||
else
|
||||
{
|
||||
PrintBotWon();
|
||||
}
|
||||
}
|
||||
BIN
13 - Bits/ex5
Normal file
BIN
13 - Bits/ex5
Normal file
Binary file not shown.
183
13 - Bits/ex5.c
Normal file
183
13 - Bits/ex5.c
Normal file
@@ -0,0 +1,183 @@
|
||||
#include "PointersLibs.h"
|
||||
|
||||
#define ROWS 3
|
||||
|
||||
void RemoveBits(byte *row, unsigned short count)
|
||||
{
|
||||
*row >>= count;
|
||||
}
|
||||
|
||||
unsigned short CountBits(byte b)
|
||||
{
|
||||
unsigned short counter = ZERO,
|
||||
loop_lenght = sizeof(b) * EIGHT;
|
||||
while (loop_lenght--)
|
||||
{
|
||||
counter += !EvenNumber(b);
|
||||
b >>= ONE;
|
||||
}
|
||||
|
||||
return (counter);
|
||||
}
|
||||
|
||||
void PrintGetRow()
|
||||
{
|
||||
printf("Enter row number (1-3): ");
|
||||
}
|
||||
|
||||
void PrintGetRemoveCount()
|
||||
{
|
||||
printf("Enter many bits: ");
|
||||
}
|
||||
|
||||
void PrintPlayerWon()
|
||||
{
|
||||
printf("You won!\n");
|
||||
}
|
||||
|
||||
void PrintBotWon()
|
||||
{
|
||||
printf("The bot won :(\n");
|
||||
}
|
||||
|
||||
unsigned short InputRow()
|
||||
{
|
||||
unsigned short row;
|
||||
scanf("%hu", &row);
|
||||
|
||||
return (row);
|
||||
}
|
||||
|
||||
unsigned short InputRemoveCount()
|
||||
{
|
||||
return (InputRow());
|
||||
}
|
||||
|
||||
unsigned short SumBits(byte *vec, unsigned short lenght)
|
||||
{
|
||||
unsigned short sum = ZERO;
|
||||
while (lenght--)
|
||||
{
|
||||
sum += CountBits(*(vec++));
|
||||
}
|
||||
|
||||
return (sum);
|
||||
}
|
||||
|
||||
void PrintBits(byte b)
|
||||
{
|
||||
unsigned short lenght = sizeof(b) * EIGHT;
|
||||
while (lenght--)
|
||||
{
|
||||
printf("%hu,", !EvenNumber(b));
|
||||
b >>= ONE;
|
||||
}
|
||||
}
|
||||
|
||||
void PrintRows(byte *rows, unsigned short lenght)
|
||||
{
|
||||
while (lenght--)
|
||||
{
|
||||
PrintBits(*(rows++));
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
byte * MaxBitsRow(byte *rows, unsigned short lenght)
|
||||
{
|
||||
unsigned short max_count = ZERO,
|
||||
temp_count;
|
||||
byte *max_row;
|
||||
while (lenght--)
|
||||
{
|
||||
temp_count = CountBits(*rows);
|
||||
if (temp_count > max_count)
|
||||
{
|
||||
max_count = temp_count;
|
||||
max_row = rows;
|
||||
}
|
||||
rows++;
|
||||
}
|
||||
|
||||
return (max_row);
|
||||
}
|
||||
|
||||
byte * OddMaxBitsRow(byte *rows, unsigned short length)
|
||||
{
|
||||
unsigned short max_count = ZERO,
|
||||
temp_count;
|
||||
byte *odd_max_row = MaxBitsRow(rows, length);
|
||||
while (length--)
|
||||
{
|
||||
if (!EvenBits(*rows))
|
||||
{
|
||||
temp_count = CountBits(*rows);
|
||||
if (temp_count > max_count)
|
||||
{
|
||||
max_count = temp_count;
|
||||
odd_max_row = rows;
|
||||
}
|
||||
}
|
||||
rows++;
|
||||
}
|
||||
|
||||
return (odd_max_row);
|
||||
}
|
||||
|
||||
void Bot(byte *rows, unsigned short lenght)
|
||||
{
|
||||
byte *max_row = OddMaxBitsRow(rows, lenght);
|
||||
|
||||
unsigned short sum_bits = SumBits(rows, lenght);
|
||||
BOOLEAN EvenArea = EvenNumber(sum_bits);
|
||||
unsigned short count_bits = CountBits(*max_row);
|
||||
unsigned short remove_count = EvenNumber(sum_bits - (count_bits / TWO)) ? (count_bits / TWO) : (count_bits / TWO) + ONE;
|
||||
|
||||
RemoveBits(max_row, remove_count); // EvenArea ? (CountBits(*max_row) / TWO * TWO - ONE) / TWO: (CountBits(*max_row) / TWO * TWO - ONE));
|
||||
}
|
||||
|
||||
void main(void)
|
||||
{
|
||||
byte rows[ROWS] = {0x7f, 0x1f, 0x7};
|
||||
|
||||
unsigned short row = -ONE, remove_count = ZERO;
|
||||
BOOLEAN player = FALSE;
|
||||
while (SumBits(rows, ROWS) != ONE)
|
||||
{
|
||||
PrintRows(rows, ROWS);
|
||||
printf("\n\n");
|
||||
row = -ONE;
|
||||
remove_count = ZERO;
|
||||
player = !player;
|
||||
if (player)
|
||||
{
|
||||
while (!(row < ROWS))
|
||||
{
|
||||
PrintGetRow();
|
||||
row = InputRow();
|
||||
}
|
||||
while (!(ZERO < remove_count && remove_count <= CountBits(rows[row])))
|
||||
{
|
||||
PrintGetRemoveCount();
|
||||
remove_count = InputRemoveCount();
|
||||
}
|
||||
RemoveBits(&rows[row], remove_count);
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("Bot:\n");
|
||||
Bot(rows, ROWS);
|
||||
}
|
||||
}
|
||||
PrintRows(rows, ROWS);
|
||||
printf("\n\n");
|
||||
if (player)
|
||||
{
|
||||
PrintPlayerWon();
|
||||
}
|
||||
else
|
||||
{
|
||||
PrintBotWon();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user