Browse Source

merge feature/calculator-factorial to feature/display-menu-calculator and implement calculatorFactorial() and default case in displayMenuCalculator()

remotes/origin/feature/display-menu-calculator
fdai7514 2 years ago
parent
commit
373e100e28
  1. 18
      src/calculatorFactorial.c
  2. 8
      src/calculatorFactorial.h
  3. 6
      src/displayMenuCalculator.c
  4. 84
      tests/test_calculatorFactorial.c

18
src/calculatorFactorial.c

@ -0,0 +1,18 @@
#include "calculatorFactorial.h"
// Note:
/* This Function may or may not be implemented in actual program, even if it is merged to the main branch.
If it is temporarily not included in the main Program, then this has a role in future Development of the Project */
int calculatorFactorial(int num) //implement recursion. The function calls itself so many times, till the breaking condition is fulfilled.
{
if (num == 0) //breaking condition
{
return 1;
}
else
{
return num * calculatorFactorial(num - 1); //If its not breaking condition, then multiply the number with the same function implemented on the previous number. Eventually it will reach breaking condition.
}
}

8
src/calculatorFactorial.h

@ -0,0 +1,8 @@
#ifndef CALCULATORFACTORIAL_H
#define CALCULATORFACTORIAL_H
#include<stdio.h>
#include<stdlib.h>
int calculatorFactorial(int num);
#endif // CALCULATORFACTORIAL_H

6
src/displayMenuCalculator.c

@ -5,6 +5,7 @@
#include "calculatorSubtract.c"
#include "calculatorMultiply.c"
#include "calculatorDivide.c"
#include "calculatorFactorial.c"
// Note:
/* This Function may or may not be implemented in actual program, even if it is merged to the main branch.
If it is temporarily not included in the main Program, then this has a role in future Development of the Project */
@ -64,11 +65,12 @@ void displayMenuCalculator(char x) //Displays the correct output, only when x i
case 5:
calculatorGetUserInputFactorial(&num); //Created specially for factorial which gets a number from user.
//NOT YET IMPLEMENTED
answer = calculatorFactorial(num);
printf("Answer: %f\n", answer);
break;
default:
//NOT YET IMPLEMENTED
printf("Invalid choice\n");
return;
}
}

84
tests/test_calculatorFactorial.c

@ -0,0 +1,84 @@
#ifdef TEST
#include "unity.h"
#include "calculatorFactorial.h"
// Note:
/* This Function may or may not be implemented in actual program, even if it is merged to the main branch.
If it is temporarily not included in the main Program, then this has a role in future Development of the Project */
void setUp(void)
{
}
void tearDown(void)
{
}
void test1_calculatorFactorial(void)
{
int num, actual, expected; //Arrange
num = 1;
expected = 1;
actual = calculatorFactorial(num); //Act
TEST_ASSERT_EQUAL_INT(expected, actual); //Assert
}
void test2_calculatorFactorial(void)
{
int num, actual, expected; //Arrange
num = 0;
expected = 1;
actual = calculatorFactorial(num); //Act
TEST_ASSERT_EQUAL_INT(expected, actual); //Assert
}
void test3_calculatorFactorial(void)
{
int num, actual, expected; //Arrange
num = 3;
expected = 6;
actual = calculatorFactorial(num); //Act
TEST_ASSERT_EQUAL_INT(expected, actual); //Assert
}
void test4_calculatorFactorial(void)
{
int num, actual, expected; //Arrange
num = 5;
expected = 120;
actual = calculatorFactorial(num); //Act
TEST_ASSERT_EQUAL_INT(expected, actual); //Assert
}
void test5_calculatorFactorial(void)
{
int num, actual, expected; //Arrange
num = 8;
expected = 40320;
actual = calculatorFactorial(num); //Act
TEST_ASSERT_EQUAL_INT(expected, actual); //Assert
}
void test6_calculatorFactorial(void)
{
int num, actual, expected; //Arrange
num = 11;
expected = 39916800;
actual = calculatorFactorial(num); //Act
TEST_ASSERT_EQUAL_INT(expected, actual); //Assert
}
void test7_calculatorFactorial(void)
{
int num, actual, expected; //Arrange
num = 10;
expected = 3628800;
actual = calculatorFactorial(num); //Act
TEST_ASSERT_EQUAL_INT(expected, actual); //Assert
}
#endif // TEST
Loading…
Cancel
Save