Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 16 additions & 15 deletions semester_1/homework1/substring.c

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

В PR не должно быть изменений в файлах, не связанных с задачей.

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include <stdio.h>
#include <stdbool.h>

int countingSubstrings(char *string, char *subString, int lengthOfString, int lengthOfSubstring);
int countSubstrings(const char *string, const char *subString, int lengthOfString, int lengthOfSubstring);
int readingLineFromConsole(char *string);
bool test(void);

Expand All @@ -13,18 +13,18 @@ int main()
return 1;
}
printf("Enter your string\n");
char S[1000];
int lengthOfS = readingLineFromConsole(S);
char s[1000] = { '\0' };
int lengthOfS = readingLineFromConsole(s);
printf("Enter your substring\n");
char S1[1000];
int lengthOfS1 = readingLineFromConsole(S1);
int numberOfSubstrings = countingSubstrings(S, S1, lengthOfS, lengthOfS1);
char s1[1000] = { '\0' };
int lengthOfS1 = readingLineFromConsole(s1);
int numberOfSubstrings = countSubstrings(s, s1, lengthOfS, lengthOfS1);

printf("The number of occurances of your substring in your string is %d\n", numberOfSubstrings);
return 0;
}

int countingSubstrings(char *string, char *subString, int lengthOfString, int lengthOfSubstring)
int countSubstrings(const char *string, const char *subString, int lengthOfString, int lengthOfSubstring)
{
int count = 0;
bool doWeCompareRightNow = false;
Expand All @@ -35,9 +35,10 @@ int countingSubstrings(char *string, char *subString, int lengthOfString, int le
{
if (doWeCompareRightNow)
{
if (++currentSubstringCharacter < lengthOfSubstring)
++currentSubstringCharacter;
if (currentSubstringCharacter < lengthOfSubstring)
{
doWeCompareRightNow = (string[currentCharacter] == subString[currentSubstringCharacter] ? true : false);
doWeCompareRightNow = string[currentCharacter] == subString[currentSubstringCharacter];
}

if (!doWeCompareRightNow)
Expand Down Expand Up @@ -65,7 +66,7 @@ int countingSubstrings(char *string, char *subString, int lengthOfString, int le
int readingLineFromConsole(char *string)
{
int c = 0;
char ch;
char ch = "\0";
do
{
ch = getchar();
Expand All @@ -86,7 +87,7 @@ bool test(void)
int testStringLength1 = 0;
int testSubStringLength1 = 6;
int answer1 = 0;
if (!(countingSubstrings(testString1, testSubstring1, testStringLength1, testSubStringLength1) == answer1))
if (!(countSubstrings(testString1, testSubstring1, testStringLength1, testSubStringLength1) == answer1))
{
printf("%d", 1);
return false;
Expand All @@ -97,7 +98,7 @@ bool test(void)
int testStringLength2 = 23;
int testSubStringLength2 = 3;
int answer2 = 4;
if (!(countingSubstrings(testString2, testSubstring2, testStringLength2, testSubStringLength2) == answer2))
if (!(countSubstrings(testString2, testSubstring2, testStringLength2, testSubStringLength2) == answer2))
{
return false;
}
Expand All @@ -107,7 +108,7 @@ bool test(void)
int testStringLength3 = 9;
int testSubStringLength3 = 0;
int answer3 = 0;
if (!(countingSubstrings(testString3, testSubstring3, testStringLength3, testSubStringLength3) == answer3))
if (!(countSubstrings(testString3, testSubstring3, testStringLength3, testSubStringLength3) == answer3))
{
return false;
}
Expand All @@ -116,7 +117,7 @@ bool test(void)
int testStringLength4 = 5;
int testSubStringLength4 = 1;
int answer4 = 5;
if (!(countingSubstrings(testString4, testSubstring4, testStringLength4, testSubStringLength4) == answer4))
if (!(countSubstrings(testString4, testSubstring4, testStringLength4, testSubStringLength4) == answer4))
{
return false;
}
Expand All @@ -126,7 +127,7 @@ bool test(void)
int testStringLength5 = 2;
int testSubStringLength5 = 2;
int answer5 = 1;
if (!(countingSubstrings(testString5, testSubstring5, testStringLength5, testSubStringLength5) == answer5))
if (!(countSubstrings(testString5, testSubstring5, testStringLength5, testSubStringLength5) == answer5))
{
return false;
}
Expand Down
198 changes: 145 additions & 53 deletions semester_1/homework2/fibonacci.c
Original file line number Diff line number Diff line change
@@ -1,91 +1,183 @@
#include <stdio.h>
#include <stdbool.h>
#include <time.h>

bool checkIfNumberIsCorrect(int n)
int iterativeFibonacci(int number, long long int *answer);
int recursiveFibonacci(int number, long long int *answer);
long long int recursiveFibonacciBody(int number);
bool correctInputCheckForRecursiveFunction(int number);
bool testIterativeFunction(void);
bool testRecursiveFunction(void);
bool test(void);

int main()
{
if (n < 1)

if (!test())
{
return false;
printf("\nSorry but the program does not work correctly\n");
return 1;
}
return true;

printf("\nEnter the number of fbonacci number you want to see: ");
int number = 0;
int scanfNumberOfElements = scanf("%d", &number);

if (scanfNumberOfElements != 1)
{
printf("\nSorry but something went wrong\n");
return 1;
}

long long int iterativeAnswer = 0;
int errorCode1 = iterativeFibonacci(number, &iterativeAnswer);
if (errorCode1 != 0)
{
printf("\nSorry but something went wrong\n");
return 1;
}

long long int recursiveAnswer = 0;
int errorCode2 = recursiveFibonacci(number, &recursiveAnswer);
if (errorCode2 != 0)
{
printf("\nSorry but something went wrong\n");
return 1;
}

if (iterativeAnswer != recursiveAnswer)
{
printf("\nSorry but something went wrong\n");
return 1;
}

printf("\nResult of work of iterative function: %lld", iterativeAnswer);
printf("\nResult of work of recursive function: %lld\n", recursiveAnswer);

return 0;
}

int iterativeFibonacci(int number)
int iterativeFibonacci(int number, long long int *answer)
{

if (number <= 3)
if (number < 1)
{
int firstThreeNumbers[3] = {1, 1, 2};
return firstThreeNumbers[number - 1];
*answer = -1;
return 1;
}

int previousNumber = 1;
int previousNumberForPreviousNumber = 1;
int currentNumber = 1;
int buffer = 0;
long long int currentNumbers[3] = {1, 1, 2};

if (number < 4)
{
*answer = currentNumbers[number - 1];
return 0;
}

for (int i = 2; i < number; i++)
for (int n = 2; n <= number; ++n)
{
previousNumberForPreviousNumber = previousNumber;
previousNumber = currentNumber;
currentNumber = previousNumber + previousNumberForPreviousNumber;
currentNumbers[2] = currentNumbers[1] + currentNumbers[0];
currentNumbers[0] = currentNumbers[1];
currentNumbers[1] = currentNumbers[2];
}

return currentNumber;
*answer = currentNumbers[0];
return 0;
}

int recursiveFibonacci(int number)
int recursiveFibonacci(int number, long long int *answer)
{
if (!correctInputCheckForRecursiveFunction(number))
{
return 1;
}

*answer = recursiveFibonacciBody(number);

return 0;
}

long long int recursiveFibonacciBody(int number)
{
if (number == 0)
{
return 0;
}

if (number == 1)
{
return 1;
}
return recursiveFibonacciBody(number - 1) + recursiveFibonacciBody(number - 2);
}

return recursiveFibonacci(number - 2) + recursiveFibonacci(number - 1);
bool correctInputCheckForRecursiveFunction(int number)
{
if (number < 1)
{
return false;
}
return true;
}

int timeComparison()
bool testIterativeFunction(void)
{
float recursiveFunctionTimeOfWork = 1;
float iterativeFunctionTimeOfWork = 1;
int i = 1;
while (recursiveFunctionTimeOfWork / iterativeFunctionTimeOfWork < 2.0)
{

float startTimeOfWorkIterative, finishTimeOfWorkIterative;
startTimeOfWorkIterative = clock();
int iterativeFunctionWorkResult = iterativeFibonacci(i);
finishTimeOfWorkIterative = clock();
iterativeFunctionTimeOfWork = finishTimeOfWorkIterative - startTimeOfWorkIterative;

float startTimeOfWorkRecursive, finishTimeOfWorkRecursive;
startTimeOfWorkRecursive = clock();
int recursiveFunctionWorkResult = recursiveFibonacci(i);
finishTimeOfWorkRecursive = clock();
recursiveFunctionTimeOfWork = finishTimeOfWorkRecursive - startTimeOfWorkRecursive;

if (iterativeFunctionWorkResult != recursiveFunctionWorkResult)
{
printf("Different answers for same arguments");
return 0;
}
printf("%f", recursiveFunctionTimeOfWork / iterativeFunctionTimeOfWork);

}
return i;
long long int test1 = 0;
int errorCode1 = iterativeFibonacci(-5, &test1);
if (errorCode1 != 1)
{
return false;
}

long long int test2 = 0;
int errorCode2 = iterativeFibonacci(3, &test2);
if (test2 != 2 || errorCode2 != 0)
{
return false;
}

long long int test3 = 0;
int errorCode3 = iterativeFibonacci(21, &test3);
if (test3 != 10946 || errorCode3 != 0)
{
return false;
}
return true;
}

int main()
bool testRecursiveFunction(void)
{
long long int test1 = 0;
int errorCode1 = recursiveFibonacci(-5, &test1);
if (errorCode1 != 1)
{
return false;
}

printf("%d", timeComparison());
long long int test2 = 0;
int errorCode2 = recursiveFibonacci(3, &test2);
if (test2 != 2 || errorCode2 != 0)
{
return false;
}

return 0;
long long int test3 = 0;
int errorCode3 = recursiveFibonacci(21, &test3);
if (test3 != 10946 || errorCode3 != 0)
{
return false;
}
return true;
}

bool test(void)
{
if (!testIterativeFunction())
{
return false;
}

if (!testRecursiveFunction())
{
return false;
}

return true;
}
Loading