First Upload
This commit is contained in:
479
11/IdanStringPointersLib.h
Normal file
479
11/IdanStringPointersLib.h
Normal file
@@ -0,0 +1,479 @@
|
||||
#include "PointersLibs.h"
|
||||
|
||||
#define DAYS_IN_MONTH 30
|
||||
#define MONTHS_IN_YEAR 12
|
||||
#define ONE_HUNDRED 100
|
||||
#define ONE_THOSEND 10000
|
||||
#define BOOLEAN unsigned short
|
||||
#define TRUE 1
|
||||
#define FALSE 0
|
||||
#define TEN 10
|
||||
#define TWELVE 12
|
||||
#define THIRTY 30
|
||||
#define ZERO 0
|
||||
#define ONE 1
|
||||
#define TWO 2
|
||||
#define THREE 3
|
||||
#define FOUR 4
|
||||
#define ABS(x) (x) * (((2 * (x)) + 1) % 2)
|
||||
#define MAX(x, y) (x > y) ? x : y
|
||||
#define MIN(x, y) (x < y) ? x : y
|
||||
#define BACKSLASH_ZERO '\0'
|
||||
#define MAX_SIZE_STRING 256
|
||||
|
||||
//---------------------------------------------------------------------------------------
|
||||
// CharInString
|
||||
// ------------
|
||||
//
|
||||
// General :CHecks if the char exist in string.
|
||||
//
|
||||
// Parameters :
|
||||
// text - string (char[])
|
||||
// c - char (char)
|
||||
//
|
||||
// Return value : If the char exist in string (BOOLEAN).
|
||||
//
|
||||
//---------------------------------------------------------------------------------------
|
||||
// Programmer : Cohen Idan
|
||||
// Student No : 211675038
|
||||
// Date : 04.10.2019
|
||||
//---------------------------------------------------------------------------------------
|
||||
BOOLEAN CharInString(char text[], char c)
|
||||
{
|
||||
BOOLEAN exist = FALSE;
|
||||
unsigned short counter = ZERO;
|
||||
while ((text[counter] != c) * text[counter++]);
|
||||
exist = (text[--counter] == c);
|
||||
|
||||
return (exist);
|
||||
}
|
||||
|
||||
char * LastCharOfString(char * start_string)
|
||||
{
|
||||
while (*(start_string++));
|
||||
|
||||
return (--start_string);
|
||||
}
|
||||
|
||||
//---------------------------------------------------------------------------------------
|
||||
// StringLenght
|
||||
// ------------
|
||||
//
|
||||
// General : Checks the lenght of string.
|
||||
//
|
||||
// Parameters :
|
||||
// text - string (char[])
|
||||
//
|
||||
// Return value : Lenght of string (unsigned short).
|
||||
//
|
||||
//---------------------------------------------------------------------------------------
|
||||
// Programmer : Cohen Idan
|
||||
// Student No : None
|
||||
// Date : 04.10.2019
|
||||
//---------------------------------------------------------------------------------------
|
||||
unsigned short StringLenght(char *start_string)
|
||||
{
|
||||
return (LastCharOfString(start_string) - start_string);
|
||||
}
|
||||
|
||||
//---------------------------------------------------------------------------------------
|
||||
// StringCompare
|
||||
// ------------------------
|
||||
//
|
||||
// General : Compare 2 strings.
|
||||
//
|
||||
// Parameters :
|
||||
// text1 - string (char[])
|
||||
// text2 - string (char[])
|
||||
//
|
||||
// Return value : If this 2 strings are equals (BOOLEAN).
|
||||
//
|
||||
//---------------------------------------------------------------------------------------
|
||||
// Programmer : Cohen Idan
|
||||
// Student No : 211675038
|
||||
// Date : 04.10.2019
|
||||
//---------------------------------------------------------------------------------------
|
||||
BOOLEAN StringCompare(char *start_stringA_index, char *start_stringB_index)
|
||||
{
|
||||
while ((*start_stringA_index) && (*(start_stringA_index++) == *(start_stringB_index++)));
|
||||
|
||||
return (*--start_stringA_index == *--start_stringB_index);
|
||||
}
|
||||
|
||||
//---------------------------------------------------------------------------------------
|
||||
// IndexCharInString
|
||||
// -----------------
|
||||
//
|
||||
// General : Find the index of char in the string.
|
||||
//
|
||||
// Parameters :
|
||||
// text - string (char[])
|
||||
// c - char (char)
|
||||
//
|
||||
// Return value : The first index of the char in the string (unsigned short).
|
||||
//
|
||||
//---------------------------------------------------------------------------------------
|
||||
// Programmer : Cohen Idan
|
||||
// Student No : 211675038
|
||||
// Date : 04.10.2019
|
||||
//---------------------------------------------------------------------------------------
|
||||
unsigned short IndexCharInString(char text[], char c)
|
||||
{
|
||||
unsigned short counter = ZERO;
|
||||
while (text[counter++] != c);
|
||||
|
||||
return (--counter);
|
||||
}
|
||||
|
||||
//---------------------------------------------------------------------------------------
|
||||
// CountCharInString
|
||||
// -----------------
|
||||
//
|
||||
// General : Count how many the char is in the string.
|
||||
//
|
||||
// Parameters :
|
||||
// text - string (char[])
|
||||
// c - char (char)
|
||||
//
|
||||
// Return value : Count of char in the string (unsigned short).
|
||||
//
|
||||
//---------------------------------------------------------------------------------------
|
||||
// Programmer : Cohen Idan
|
||||
// Student No : None
|
||||
// Date : 04.10.2019
|
||||
//---------------------------------------------------------------------------------------
|
||||
unsigned short CountCharInString(char *start_string_index, char *c)
|
||||
{
|
||||
unsigned short count = ZERO;
|
||||
while (*start_string_index)
|
||||
{
|
||||
count += (*(start_string_index++) == *c);
|
||||
}
|
||||
|
||||
return (count);
|
||||
}
|
||||
|
||||
//---------------------------------------------------------------------------------------
|
||||
// CutString
|
||||
// ---------
|
||||
//
|
||||
// General : Cut the string.
|
||||
//
|
||||
// Parameters :
|
||||
// text - string (char[])
|
||||
// start - from where (unsigned short)
|
||||
//
|
||||
// Return value : If the equation is ok (BOOLEAN).
|
||||
//
|
||||
//---------------------------------------------------------------------------------------
|
||||
// Programmer : Cohen Idan
|
||||
// Student No : None
|
||||
// Date : 04.10.2019
|
||||
//---------------------------------------------------------------------------------------
|
||||
void CutString(char *start_textA_index, char *end_textA_index, char *start_textB_index)
|
||||
{
|
||||
while (start_textA_index < end_textA_index)
|
||||
{
|
||||
*(start_textB_index++) = *(start_textA_index++);
|
||||
}
|
||||
*start_textB_index = BACKSLASH_ZERO;
|
||||
|
||||
}
|
||||
|
||||
//---------------------------------------------------------------------------------------
|
||||
// CopyString
|
||||
// ----------
|
||||
//
|
||||
// General : Copy second string into first string.
|
||||
//
|
||||
// Parameters :
|
||||
// text - first text (char[])
|
||||
// copy - second text (char[])
|
||||
// start - from where to start (unsigned short)
|
||||
//
|
||||
// Return value : None.
|
||||
//
|
||||
//---------------------------------------------------------------------------------------
|
||||
// Programmer : Cohen Idan
|
||||
// Student No : 211675038
|
||||
// Date : 04.10.2019
|
||||
//---------------------------------------------------------------------------------------
|
||||
void CopyString(char *start_string_index, char *start_copy_index)
|
||||
{
|
||||
while (*start_copy_index)
|
||||
{
|
||||
*(start_string_index++) = *(start_copy_index++);
|
||||
}
|
||||
*(start_string_index) = BACKSLASH_ZERO;
|
||||
}
|
||||
|
||||
//---------------------------------------------------------------------------------------
|
||||
// LinkingString
|
||||
// -------------
|
||||
//
|
||||
// General : Copy second string into end of first string.
|
||||
//
|
||||
// Parameters :
|
||||
// textA - first text (char[])
|
||||
// textB - second text (char[])
|
||||
//
|
||||
// Return value : None.
|
||||
//
|
||||
//---------------------------------------------------------------------------------------
|
||||
// Programmer : Cohen Idan
|
||||
// Student No : 211675038
|
||||
// Date : 04.10.2019
|
||||
//---------------------------------------------------------------------------------------
|
||||
void LinkingString(char *start_stringA_index, char *start_stringB_index)
|
||||
{
|
||||
CopyString(LastCharOfString(start_stringA_index), start_stringB_index);
|
||||
}
|
||||
|
||||
//---------------------------------------------------------------------------------------
|
||||
// ReverseString
|
||||
// -------------
|
||||
//
|
||||
// General : Chnage to oppsite the string.
|
||||
//
|
||||
// Parameters :
|
||||
// textA - first text (char[])
|
||||
//
|
||||
// Return value : None.
|
||||
//
|
||||
//---------------------------------------------------------------------------------------
|
||||
// Programmer : Cohen Idan
|
||||
// Student No : 211675038
|
||||
// Date : 04.10.2019
|
||||
//---------------------------------------------------------------------------------------
|
||||
void ReverseString(char textA[])
|
||||
{
|
||||
unsigned short textA_lenght = StringLenght(textA) - ONE,
|
||||
counter,
|
||||
loop_lenght = (textA_lenght + ONE) / TWO;
|
||||
char temp_char;
|
||||
for (counter = ZERO; counter < loop_lenght; counter++)
|
||||
{
|
||||
temp_char = textA[counter];
|
||||
textA[counter] = textA[textA_lenght - counter];
|
||||
textA[textA_lenght - counter] = temp_char;
|
||||
}
|
||||
}
|
||||
|
||||
//---------------------------------------------------------------------------------------
|
||||
// StringBInStringA
|
||||
// ----------------
|
||||
//
|
||||
// General : Check if string B exists in string A.
|
||||
//
|
||||
// Parameters :
|
||||
// textA - first text (char[])
|
||||
// textB - second text (char[])
|
||||
//
|
||||
// Return value : If string B exists in string A (BOOLEAN).
|
||||
//
|
||||
//---------------------------------------------------------------------------------------
|
||||
// Programmer : Cohen Idan
|
||||
// Student No : 211675038
|
||||
// Date : 04.10.2019
|
||||
//---------------------------------------------------------------------------------------
|
||||
BOOLEAN StringBInStringA(char *start_string_a, char *start_string_b)
|
||||
{
|
||||
char *ptr_string_a_temp = start_string_a;
|
||||
char temp = BACKSLASH_ZERO;
|
||||
BOOLEAN flag = FALSE;
|
||||
unsigned short textB_lenght = StringLenght(start_string_b);
|
||||
char *address = (ptr_string_a_temp + textB_lenght);
|
||||
while ((*(address - ONE)) * !flag)
|
||||
{
|
||||
SwapChars(&temp, address);
|
||||
flag = StringCompare(ptr_string_a_temp, start_string_b);
|
||||
SwapChars(&temp, address);
|
||||
address = ((++ptr_string_a_temp) + textB_lenght);
|
||||
}
|
||||
|
||||
return (flag);
|
||||
}
|
||||
|
||||
//---------------------------------------------------------------------------------------
|
||||
// IndexStringBInStringA
|
||||
// ---------------------
|
||||
//
|
||||
// General : Find the index of string B in string A.
|
||||
//
|
||||
// Parameters :
|
||||
// textA - first text (char[])
|
||||
// textB - second text (char[])
|
||||
//
|
||||
// Return value : Index of string B in string A (unsigned short).
|
||||
//
|
||||
//---------------------------------------------------------------------------------------
|
||||
// Programmer : Cohen Idan
|
||||
// Student No : 211675038
|
||||
// Date : 04.10.2019
|
||||
//---------------------------------------------------------------------------------------
|
||||
char * IndexStringBInStringA(char *start_string_a, char *start_string_b)
|
||||
{
|
||||
char *ptr_string_a_temp = start_string_a;
|
||||
char temp = BACKSLASH_ZERO;
|
||||
BOOLEAN flag = FALSE;
|
||||
unsigned short textB_lenght = StringLenght(start_string_b);
|
||||
char *address = (ptr_string_a_temp + textB_lenght);
|
||||
while ((*(address - ONE)) * !flag)
|
||||
{
|
||||
SwapChars(&temp, address);
|
||||
flag = StringCompare(ptr_string_a_temp, start_string_b);
|
||||
SwapChars(&temp, address);
|
||||
address = ((++ptr_string_a_temp) + textB_lenght);
|
||||
}
|
||||
|
||||
return (--ptr_string_a_temp);
|
||||
}
|
||||
|
||||
//---------------------------------------------------------------------------------------
|
||||
// CountStringBInStringA
|
||||
// ---------------------
|
||||
//
|
||||
// General : Counting how many times stringB in stringA.
|
||||
//
|
||||
// Parameters :
|
||||
// *start_string_a - Pointer of first string (char[])
|
||||
// *start_string_b - Pointer of second string (char[])
|
||||
//
|
||||
// Return value : Count second string in first string (unsigned int).
|
||||
//
|
||||
//---------------------------------------------------------------------------------------
|
||||
// Programmer : Cohen Idan
|
||||
// Student No : 211675038
|
||||
// Date : 04.10.2019
|
||||
//---------------------------------------------------------------------------------------
|
||||
unsigned int CountStringBInStringA(char *start_string_a, char *start_string_b)
|
||||
{
|
||||
char *ptr_string_a_temp = start_string_a;
|
||||
char temp = BACKSLASH_ZERO;
|
||||
unsigned short count = ZERO;
|
||||
unsigned short textB_lenght = StringLenght(start_string_b);
|
||||
char *address = (ptr_string_a_temp + textB_lenght);
|
||||
while (*(address - ONE))
|
||||
{
|
||||
SwapChars(&temp, address);
|
||||
count += StringCompare(ptr_string_a_temp, start_string_b);
|
||||
SwapChars(&temp, address);
|
||||
address = ((++ptr_string_a_temp) + textB_lenght);
|
||||
}
|
||||
|
||||
return (count);
|
||||
}
|
||||
|
||||
//---------------------------------------------------------------------------------------
|
||||
// CountStringBInStringA
|
||||
// ---------------------
|
||||
//
|
||||
// General : Remove first string B from string A.
|
||||
//
|
||||
// Parameters :
|
||||
// textA - first text (char[])
|
||||
// textB - second text (char[])
|
||||
//
|
||||
// Return value : None.
|
||||
//
|
||||
//---------------------------------------------------------------------------------------
|
||||
// Programmer : Cohen Idan
|
||||
// Student No : 211675038
|
||||
// Date : 04.10.2019
|
||||
//---------------------------------------------------------------------------------------
|
||||
void RemoveStringBFromStringA(char *start_string_a, char *start_string_b)
|
||||
{
|
||||
unsigned short textB_lenght = StringLenght(start_string_b);
|
||||
char *ptr_index_stringB_in_stringA = IndexStringBInStringA(start_string_a, start_string_b);
|
||||
CopyString(ptr_index_stringB_in_stringA, ptr_index_stringB_in_stringA + textB_lenght);
|
||||
}
|
||||
|
||||
//---------------------------------------------------------------------------------------
|
||||
// CountStringBInStringA
|
||||
// ---------------------
|
||||
//
|
||||
// General : Remove ALL string B from string A.
|
||||
//
|
||||
// Parameters :
|
||||
// textA - first text (char[])
|
||||
// textB - second text (char[])
|
||||
//
|
||||
// Return value : None.
|
||||
//
|
||||
//---------------------------------------------------------------------------------------
|
||||
// Programmer : Cohen Idan
|
||||
// Student No : 211675038
|
||||
// Date : 04.10.2019
|
||||
//---------------------------------------------------------------------------------------
|
||||
void RemoveAllStringBFromStringA(char *start_stringA_index, char *start_stringB_index)
|
||||
{
|
||||
unsigned short loop_lenght = CountStringBInStringA(start_stringA_index, start_stringB_index);
|
||||
unsigned short counter;
|
||||
for (counter = ZERO; counter < loop_lenght; counter++)
|
||||
{
|
||||
RemoveStringBFromStringA(start_stringA_index, start_stringB_index);
|
||||
}
|
||||
}
|
||||
|
||||
//---------------------------------------------------------------------------------------
|
||||
// ValidParenthesesTemplate
|
||||
// ------------------------
|
||||
//
|
||||
// General : checks if the string is a good equation.
|
||||
//
|
||||
// Parameters :
|
||||
// text - string (char[])
|
||||
//
|
||||
// Return value : If the equation is ok (BOOLEAN).
|
||||
//
|
||||
//---------------------------------------------------------------------------------------
|
||||
// Programmer : Cohen Idan
|
||||
// Student No : 211675038
|
||||
// Date : 04.10.2019
|
||||
//---------------------------------------------------------------------------------------
|
||||
BOOLEAN ValidParenthesesTemplate(char text[])
|
||||
{
|
||||
unsigned short counter = ZERO;
|
||||
short count_parenthesis = ZERO;
|
||||
while (text[counter] * (count_parenthesis + ONE))
|
||||
{
|
||||
count_parenthesis += (text[counter] == '(');
|
||||
count_parenthesis -= (text[counter] == ')');
|
||||
}
|
||||
|
||||
return (!count_parenthesis);
|
||||
}
|
||||
|
||||
unsigned short CharToNumber(char *c)
|
||||
{
|
||||
return (unsigned short)((*c) - '0');
|
||||
}
|
||||
|
||||
//---------------------------------------------------------------------------------------
|
||||
// MaxCountCharInString
|
||||
// --------------------
|
||||
//
|
||||
// General : Checks which character is most often in the string.
|
||||
//
|
||||
// Parameters :
|
||||
// *start_string_index - Pointer of string (char[])
|
||||
//
|
||||
// Return value : The number of times the character appears most often (unsigned int).
|
||||
//
|
||||
//---------------------------------------------------------------------------------------
|
||||
// Programmer : Cohen Idan
|
||||
// Student No : 211675038
|
||||
// Date : 12.11.19
|
||||
//---------------------------------------------------------------------------------------
|
||||
unsigned int MaxCountCharInString(char *start_string_index)
|
||||
{
|
||||
unsigned int max = ZERO,
|
||||
count_temp;
|
||||
while (*start_string_index)
|
||||
{
|
||||
count_temp = CountCharInString(start_string_index++, *start_string_index);
|
||||
max = (count_temp > max) ? count_temp : max;
|
||||
}
|
||||
|
||||
return (max);
|
||||
}
|
||||
1167
11/PointersLibs.h
Normal file
1167
11/PointersLibs.h
Normal file
File diff suppressed because it is too large
Load Diff
95
11/ex1.c
Normal file
95
11/ex1.c
Normal file
@@ -0,0 +1,95 @@
|
||||
#include "IdanStringPointersLib.h"
|
||||
#include <stdio.h>
|
||||
|
||||
//---------------------------------------------------------------------------------------
|
||||
// Highest CountTheLongestIdenticalCharacterSequence
|
||||
// -------------------------------------------------
|
||||
//
|
||||
// General : The function checks the highest sequence of chars in a string
|
||||
//
|
||||
// Parameters :
|
||||
// *ptr_start - Pointer to a place in a vector(char)
|
||||
//
|
||||
// Return Value : Highest sequence
|
||||
//
|
||||
//---------------------------------------------------------------------------------------
|
||||
unsigned short CountTheLongestIdenticalCharacterSequence(char * ptr_start)
|
||||
{
|
||||
unsigned short max_sum = ONE,
|
||||
temp_sum = ONE;
|
||||
ptr_start++;
|
||||
while (*(ptr_start++))
|
||||
{
|
||||
if (*ptr_start != *(ptr_start - ONE))
|
||||
{
|
||||
max_sum = (max_sum < temp_sum) ? temp_sum : max_sum;
|
||||
temp_sum = ZERO;
|
||||
}
|
||||
temp_sum++;
|
||||
}
|
||||
|
||||
return (max_sum);
|
||||
}
|
||||
|
||||
unsigned short IndexOfStringWithLongestIdenticalCharacterSequence(char *ptr_mat_start)
|
||||
{
|
||||
unsigned short max_sum = ZERO,
|
||||
temp_sum = ZERO,
|
||||
counter = ZERO,
|
||||
index = ZERO;
|
||||
unsigned int string_amount = (unsigned int)(ptr_mat_start) + (MAX_SIZE_STRING * TEN);
|
||||
for (counter = ZERO; (unsigned int)(ptr_mat_start) < string_amount; counter++)
|
||||
{
|
||||
temp_sum = CountTheLongestIdenticalCharacterSequence(ptr_mat_start);
|
||||
if (temp_sum > max_sum)
|
||||
{
|
||||
max_sum = temp_sum;
|
||||
index = counter;
|
||||
}
|
||||
ptr_mat_start += MAX_SIZE_STRING;
|
||||
}
|
||||
|
||||
return (index);
|
||||
}
|
||||
|
||||
//---------------------------------------------------------------------------------------
|
||||
// EX #1
|
||||
// -----
|
||||
//
|
||||
// General : The program gets 10 strings and checks what string is with the highest
|
||||
// sequence of chars
|
||||
//
|
||||
// Input : 10 strings
|
||||
//
|
||||
// Process : The program runs on a string and checks what is the highest sequence than
|
||||
// it compares it to the max
|
||||
//
|
||||
// Output : the highest sequence in a the 10 strings
|
||||
//
|
||||
//---------------------------------------------------------------------------------------
|
||||
// Programmer : Cohen Idan
|
||||
// Student No : 211675038
|
||||
// Date: 19.11.2019
|
||||
//---------------------------------------------------------------------------------------
|
||||
void main(void)
|
||||
{
|
||||
typedef char string[MAX_SIZE_STRING];
|
||||
|
||||
string s1 = "aaaabbbbbccccccccccccccc\0";
|
||||
string s2 = "aaaabbbbbccccvcccccccccc\0";
|
||||
string s3 = "aaaabbbbbccccycccccccccc\0";
|
||||
string s4 = "aaaabbbbbcccgccccccccccc\0";
|
||||
string s5 = "aaaabbbbbccccckccccccccc\0";
|
||||
string s6 = "aaaabbbbbccccchccccccccc\0";
|
||||
string s7 = "aaaabbbbbccccncccccccccc\0";
|
||||
string s8 = "aaaabbbbbccccjcccccccccc\0";
|
||||
string s9 = "aaaabbbbbcccmccccccccccc\0";
|
||||
string s10 = "aaaabbbbbccccucccccccccc\0";
|
||||
|
||||
string arr[10] = {*s5, *s2, *s3, *s4, *s1, *s6, *s7, *s8, *s9, *s10};
|
||||
|
||||
unsigned short max = PointerOfStringWithLongestIdenticalCharacterSequence(&arr[0]);
|
||||
|
||||
printf("%hu\n", max);
|
||||
|
||||
}
|
||||
84
11/ex2.c
Normal file
84
11/ex2.c
Normal file
@@ -0,0 +1,84 @@
|
||||
#include "IdanStringPointersLib.h"
|
||||
#include <stdio.h>
|
||||
|
||||
#define ROW 5
|
||||
#define COL 5
|
||||
|
||||
unsigned short SumValuesInPointers(BOOLEAN * ptr_start, BOOLEAN * ptr_end)
|
||||
{
|
||||
unsigned short sum;
|
||||
for (sum = ZERO; ptr_start <= ptr_end; sum += *(ptr_start++));
|
||||
|
||||
return (sum);
|
||||
}
|
||||
|
||||
unsigned short CountNeighbors(BOOLEAN *ptr_mat, BOOLEAN *ptr_player)
|
||||
{
|
||||
unsigned short neighbords_count = ZERO;
|
||||
unsigned short player_row = (ptr_player - ptr_mat) / ROW;
|
||||
unsigned short player_col = (ptr_player - ptr_mat) % COL;
|
||||
|
||||
// Add the sum of the vector above the cell
|
||||
neighbords_count += (player_row > ZERO) * SumValuesInPointers(ptr_mat + (MAX(player_row - ONE, ZERO)) * ROW + (MAX(player_col - ONE, ZERO)),
|
||||
ptr_mat + (MAX(player_row - ONE, ZERO)) * ROW + (MIN(player_col + ONE, COL - ONE)));
|
||||
|
||||
// Add the sum of the vector middle the cell
|
||||
neighbords_count += SumValuesInPointers(ptr_mat + (player_row) * ROW + (MAX(player_col - ONE, ZERO)),
|
||||
ptr_mat + (player_row) * ROW + (MIN(player_col + ONE, COL - ONE))) - *ptr_player;
|
||||
|
||||
// Add the sum of the vector below the cell
|
||||
neighbords_count += (player_row < ROW) * SumValuesInPointers(ptr_mat + (MIN(player_row + ONE, ROW - ONE)) * ROW + (MAX(player_col - ONE, ZERO)),
|
||||
ptr_mat + (MIN(player_row + ONE, ROW - ONE)) * ROW + (MIN(player_col + ONE, COL - ONE)));
|
||||
|
||||
return (neighbords_count);
|
||||
}
|
||||
|
||||
void OneLive(BOOLEAN *ptr_mat)
|
||||
{
|
||||
BOOLEAN *ptr_mat_start = ptr_mat;
|
||||
BOOLEAN *ptr_mat_end = ptr_mat_start + (ROW * COL);
|
||||
for (; ptr_mat_start < ptr_mat_end; *(ptr_mat_start++) = (CountNeighbors(ptr_mat, ptr_mat_start) == TWO));
|
||||
}
|
||||
|
||||
void GameLife(BOOLEAN *ptr_mat, unsigned short lives)
|
||||
{
|
||||
for (; lives; lives--)
|
||||
{
|
||||
OneLive(ptr_mat);
|
||||
PrintMatrix(ptr_mat);
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
|
||||
void ResetMatrix(BOOLEAN *ptr_mat)
|
||||
{
|
||||
BOOLEAN *end_mat = ptr_mat + (ROW * COL);
|
||||
for (; ptr_mat < end_mat; *(ptr_mat++) %= TWO);
|
||||
}
|
||||
|
||||
void PrintVector(BOOLEAN *ptr_vec) // Lenght = COL
|
||||
{
|
||||
BOOLEAN *end_vec = ptr_vec + COL;
|
||||
for (; ptr_vec < end_vec; ptr_vec++)
|
||||
{
|
||||
printf("%hu, ", *ptr_vec);
|
||||
}
|
||||
}
|
||||
|
||||
void PrintMatrix(BOOLEAN *ptr_mat) // Lenght = ROW
|
||||
{
|
||||
BOOLEAN *end_mat = ptr_mat + (ROW * COL);
|
||||
for (; ptr_mat < end_mat; ptr_mat += COL)
|
||||
{
|
||||
PrintVector(ptr_mat);
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
|
||||
void main(void)
|
||||
{
|
||||
BOOLEAN mat[ROW][COL] = {{1,1,1,0,1}, {1,1,1,1,1}, {1,1,1,1,1}, {1,0,1,0,1}, {1,0,1,0,1}};
|
||||
PrintMatrix(mat);
|
||||
printf("\n");
|
||||
GameLife(mat, THREE);
|
||||
}
|
||||
68
11/ex3.c
Normal file
68
11/ex3.c
Normal file
@@ -0,0 +1,68 @@
|
||||
#include "IdanStringPointersLib.h"
|
||||
#include <stdio.h>
|
||||
|
||||
#define ROW 2
|
||||
#define COL 2
|
||||
|
||||
|
||||
BOOLEAN StringIsSubstringInVec(char * ptr_vec, char * ptr_string) // Lenght vector = COL
|
||||
{
|
||||
char *strings_amount = ptr_vec + (COL * MAX_SIZE_STRING);
|
||||
for (;ptr_vec < strings_amount && !(ptr_vec != ptr_string) * StringBInStringA(ptr_vec, ptr_string); ptr_vec += MAX_SIZE_STRING);
|
||||
|
||||
return ((ptr_vec != ptr_string) * StringBInStringA(ptr_vec, ptr_string));
|
||||
}
|
||||
|
||||
BOOLEAN StringIsSubstringInMat(char * ptr_mat, char * ptr_string) // Lenght = ROW
|
||||
{
|
||||
unsigned int step = COL * MAX_SIZE_STRING;
|
||||
char *vectors_amount = ptr_mat + (ROW * step);
|
||||
for (; ptr_mat < vectors_amount && !((ptr_mat != ptr_string) * StringIsSubstringInVec(ptr_mat, ptr_string)); ptr_mat += step);
|
||||
|
||||
return ((ptr_mat != ptr_string) * StringIsSubstringInVec(ptr_mat, ptr_string));
|
||||
}
|
||||
|
||||
BOOLEAN HaveSubstringInVec(char * ptr_mat, char * ptr_vec) // בודק בווקטור אם אחד האיברים הוא תת מחרוזת במטריצה
|
||||
{
|
||||
char *strings_amount = ptr_vec + (COL * MAX_SIZE_STRING);
|
||||
for (; ptr_vec < strings_amount && !(StringIsSubstringInMat(ptr_mat, ptr_vec)); ptr_vec += MAX_SIZE_STRING);
|
||||
|
||||
return (StringIsSubstringInMat(ptr_mat, ptr_vec));
|
||||
}
|
||||
|
||||
BOOLEAN HaveSubstringInMat(char * ptr_mat) // בודק אם קיימת תת מחרוזת במטריצה למחרוזת במטריצה
|
||||
{
|
||||
unsigned int step = COL * MAX_SIZE_STRING;
|
||||
char *vectors_amount = ptr_mat + (ROW * step);
|
||||
char *ptr_temp_mat = ptr_mat;
|
||||
for (; ptr_mat < vectors_amount && !(HaveSubstringInVec(ptr_temp_mat, ptr_mat)); ptr_mat += step);
|
||||
|
||||
return (HaveSubstringInVec(ptr_temp_mat, ptr_mat));
|
||||
}
|
||||
|
||||
//---------------------------------------------------------------------------------------
|
||||
// EX #3
|
||||
// -----
|
||||
//
|
||||
// General : The program gets a matrix of string and checks if one of the string is a
|
||||
// substring to another string
|
||||
//
|
||||
// Input : matrix of string
|
||||
//
|
||||
// Process : The program runs on a string and if he is a substring of one of the other
|
||||
// string till it is out of string to check
|
||||
//
|
||||
// Output : True or False
|
||||
//
|
||||
//---------------------------------------------------------------------------------------
|
||||
// Programmer : Cohen Idan
|
||||
// Student No : 211675038
|
||||
// Date: 19.11.2019
|
||||
//---------------------------------------------------------------------------------------
|
||||
void main(void)
|
||||
{
|
||||
typedef char string[MAX_SIZE_STRING];
|
||||
string mat[ROW][COL] = { {"hello", "world"} , { "idan", "hel"}};
|
||||
|
||||
printf("%hu\n", HaveSubstringInMat(mat));
|
||||
}
|
||||
Reference in New Issue
Block a user