453 lines
15 KiB
C
453 lines
15 KiB
C
#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 TAKE_SIGNED(x) (((2 * (x)) + 1) % 2)
|
|
#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'
|
|
|
|
//---------------------------------------------------------------------------------------
|
|
// 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);
|
|
}
|
|
|
|
//---------------------------------------------------------------------------------------
|
|
// 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 text[])
|
|
{
|
|
unsigned short counter = ZERO;
|
|
while (text[counter++]);
|
|
|
|
return (--counter);
|
|
}
|
|
|
|
//---------------------------------------------------------------------------------------
|
|
// 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 text1[], char text2[])
|
|
{
|
|
BOOLEAN equals = FALSE;
|
|
unsigned short counter = ZERO;
|
|
while ((text1[counter] == text2[counter]) * (text1[counter++]));
|
|
equals = (text1[--counter] == text2[counter]);
|
|
|
|
return equals;
|
|
}
|
|
|
|
//---------------------------------------------------------------------------------------
|
|
// 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 text[], char c)
|
|
{
|
|
unsigned short count = ZERO,
|
|
loop_counter = ZERO;
|
|
while (text[loop_counter])
|
|
{
|
|
count += (text[loop_counter++] == c);
|
|
}
|
|
|
|
return (count);
|
|
}
|
|
|
|
//---------------------------------------------------------------------------------------
|
|
// CutString
|
|
// ---------
|
|
//
|
|
// General : Cut the string.
|
|
//
|
|
// Parameters :
|
|
// text - string (char[])
|
|
// start - from where (unsigned short)
|
|
// size - the size of cut (unsigned short)
|
|
//
|
|
// Return value : If the equation is ok (BOOLEAN).
|
|
//
|
|
//---------------------------------------------------------------------------------------
|
|
// Programmer : Cohen Idan
|
|
// Student No : None
|
|
// Date : 04.10.2019
|
|
//---------------------------------------------------------------------------------------
|
|
void CutString(char text[], unsigned short start, unsigned short size)
|
|
{
|
|
unsigned short counter;
|
|
for (counter = ZERO; (counter < size) * text[counter + start]; counter++)
|
|
{
|
|
text[counter] = text[counter + start];
|
|
}
|
|
text[counter] = 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 text[], char copy[], unsigned short start)
|
|
{
|
|
unsigned short counter,
|
|
length = StringLenght(copy) + start;
|
|
for (counter = start; counter < length; counter++)
|
|
{
|
|
text[counter] = copy[counter - start];
|
|
}
|
|
text[counter] = 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 textA[], char textB[])
|
|
{
|
|
CopyString(textA, textB, StringLenght(textA));
|
|
}
|
|
|
|
//---------------------------------------------------------------------------------------
|
|
// 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 textA[], char textB[])
|
|
{
|
|
BOOLEAN exist = FALSE;
|
|
unsigned short textA_lenght = StringLenght(textA);
|
|
unsigned short textB_lenght = StringLenght(textB);
|
|
char temp_textA[textA_lenght];
|
|
unsigned short counter;
|
|
unsigned short loop_length = StringLenght(textA) - textB_lenght;
|
|
for (counter = ZERO; (counter < loop_length) * !exist; counter++)
|
|
{
|
|
CopyString(temp_textA, textA, ZERO);
|
|
CutString(temp_textA, counter, textB_lenght);
|
|
exist = StringCompare(temp_textA, textB);
|
|
}
|
|
|
|
return (exist);
|
|
}
|
|
|
|
//---------------------------------------------------------------------------------------
|
|
// StringBInStringA
|
|
// ----------------
|
|
//
|
|
// 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
|
|
//---------------------------------------------------------------------------------------
|
|
unsigned short IndexStringBInStringA(char textA[], char textB[])
|
|
{
|
|
BOOLEAN exist = FALSE;
|
|
unsigned short textA_lenght = StringLenght(textA);
|
|
unsigned short textB_lenght = StringLenght(textB);
|
|
char temp_textA[textA_lenght];
|
|
unsigned short counter;
|
|
unsigned short loop_length = StringLenght(textA) - textB_lenght + ONE;
|
|
for (counter = ZERO; (counter < loop_length) * !exist; counter++)
|
|
{
|
|
CopyString(temp_textA, textA, ZERO);
|
|
CutString(temp_textA, counter, textB_lenght);
|
|
exist = StringCompare(temp_textA, textB);
|
|
}
|
|
|
|
return (--counter);
|
|
}
|
|
|
|
//---------------------------------------------------------------------------------------
|
|
// CountStringBInStringA
|
|
// ---------------------
|
|
//
|
|
// General : Count string B in string A.
|
|
//
|
|
// Parameters :
|
|
// textA - first text (char[])
|
|
// textB - second text (char[])
|
|
//
|
|
// Return value : Count of string B in string A (unsigned short).
|
|
//
|
|
//---------------------------------------------------------------------------------------
|
|
// Programmer : Cohen Idan
|
|
// Student No : 211675038
|
|
// Date : 04.10.2019
|
|
//---------------------------------------------------------------------------------------
|
|
unsigned short CountStringBInStringA(char textA[], char textB[])
|
|
{
|
|
unsigned short count = ZERO;
|
|
unsigned short textA_lenght = StringLenght(textA);
|
|
unsigned short textB_lenght = StringLenght(textB);
|
|
char temp_textA[textA_lenght];
|
|
unsigned short counter;
|
|
unsigned short loop_length = StringLenght(textA) - textB_lenght;
|
|
for (counter = ZERO; (counter < loop_length); counter++)
|
|
{
|
|
CopyString(temp_textA, textA, ZERO);
|
|
CutString(temp_textA, counter, textB_lenght);
|
|
count += StringCompare(temp_textA, textB);
|
|
}
|
|
|
|
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 textA[], char textB[])
|
|
{
|
|
unsigned short textA_lenght = StringLenght(textA);
|
|
unsigned short textB_lenght = StringLenght(textB);
|
|
unsigned short index_textB_in_textA = IndexStringBInStringA(textA, textB);
|
|
char temp_textA[textA_lenght];
|
|
|
|
CopyString(temp_textA, textA, ZERO);
|
|
CutString(temp_textA, index_textB_in_textA + textB_lenght,
|
|
textA_lenght - (index_textB_in_textA + textB_lenght));
|
|
CopyString(textA, temp_textA, index_textB_in_textA);
|
|
}
|
|
|
|
//---------------------------------------------------------------------------------------
|
|
// 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 textA[], char textB[])
|
|
{
|
|
unsigned short loop_lenght = CountStringBInStringA(textA, textB);
|
|
unsigned short counter;
|
|
for (counter = ZERO; counter < loop_lenght; counter++)
|
|
{
|
|
RemoveStringBFromStringA(textA, textB);
|
|
}
|
|
}
|
|
|
|
//---------------------------------------------------------------------------------------
|
|
// 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);
|
|
}
|