Skip to content

Commit 3ab2703

Browse files
Development (#5)
* add searches * add projecteuler * add dynamicprogramming * add conversions * remove unused * fixed build error
1 parent de55091 commit 3ab2703

File tree

9 files changed

+182
-0
lines changed

9 files changed

+182
-0
lines changed

CMakeLists.txt

+4
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,12 @@ project(Examples C)
44

55
set(CMAKE_C_STANDARD 99)
66

7+
add_subdirectory(conversions)
8+
add_subdirectory(dynamicprogramming)
79
add_subdirectory(maths)
810
add_subdirectory(sorts)
911
add_subdirectory(strings)
12+
add_subdirectory(searches)
13+
add_subdirectory(projecteuler)
1014

1115
add_executable(C main.c)

conversions/CMakeLists.txt

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# If necessary, use the RELATIVE flag, otherwise each source file may be listed
2+
# with full pathname. RELATIVE may makes it easier to extract an executable name
3+
# automatically.
4+
file(GLOB APP_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.c)
5+
# file( GLOB APP_SOURCES ${CMAKE_SOURCE_DIR}/*.c )
6+
# AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR} APP_SOURCES)
7+
foreach (testsourcefile ${APP_SOURCES})
8+
# I used a simple string replace, to cut off .cpp.
9+
string(REPLACE ".c" "" testname ${testsourcefile})
10+
add_executable(${testname} ${testsourcefile})
11+
install(TARGETS ${testname} DESTINATION "bin/conversions")
12+
13+
endforeach (testsourcefile ${APP_SOURCES})

conversions/binary_to_decimal.c

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#include <stdbool.h>
2+
#include <assert.h>
3+
#include <string.h>
4+
5+
/**
6+
* Convert binary string to decimal number.
7+
* @param binary the binary string.
8+
* @return decimal number.
9+
*/
10+
int toBinary(char *binary) {
11+
int sum = 0;
12+
bool isNegative = binary[0] == '-';
13+
int k = isNegative ? (int) strlen(binary) - 1 : (int) strlen(binary);
14+
for (int i = isNegative ? 1 : 0; binary[i] != '\0'; ++i) {
15+
--k;
16+
int power = 1;
17+
for (int j = 1; j <= k; j++) {
18+
power *= 2;
19+
}
20+
sum += (binary[i] - '0') * power;
21+
}
22+
return isNegative ? -sum : sum;
23+
}
24+
25+
void test() {
26+
assert(0 == toBinary("0"));
27+
assert(1 == toBinary("1"));
28+
assert(10 == toBinary("1010"));
29+
assert(-29 == toBinary("-11101"));
30+
}
31+
32+
int main() {
33+
test();
34+
return 0;
35+
}

dynamicprogramming/CMakeLists.txt

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# If necessary, use the RELATIVE flag, otherwise each source file may be listed
2+
# with full pathname. RELATIVE may makes it easier to extract an executable name
3+
# automatically.
4+
file( GLOB APP_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.c )
5+
# file( GLOB APP_SOURCES ${CMAKE_SOURCE_DIR}/*.c )
6+
# AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR} APP_SOURCES)
7+
foreach( testsourcefile ${APP_SOURCES} )
8+
# I used a simple string replace, to cut off .cpp.
9+
string( REPLACE ".c" "" testname ${testsourcefile} )
10+
add_executable( ${testname} ${testsourcefile} )
11+
install(TARGETS ${testname} DESTINATION "bin/dynamicprograming")
12+
13+
endforeach( testsourcefile ${APP_SOURCES} )

dynamicprogramming/factorial.c

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include <assert.h>
2+
3+
/**
4+
* Return the factorial of a number.
5+
* @param n the number.
6+
* @return the factorial of n.
7+
*/
8+
int factorial(int n) {
9+
int fact[n + 1];
10+
fact[0] = 1;
11+
for (int i = 1; i <= n; i++) {
12+
fact[i] = fact[i - 1] * i;
13+
}
14+
return fact[n];
15+
}
16+
17+
void test() {
18+
assert(1 == factorial(0));
19+
assert(1 == factorial(1));
20+
assert(2 == factorial(2));
21+
assert(6 == factorial(3));
22+
assert(24 == factorial(4));
23+
assert(120 == factorial(5));
24+
}
25+
26+
int main() {
27+
test();
28+
return 0;
29+
}

projecteuler/CMakeLists.txt

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# If necessary, use the RELATIVE flag, otherwise each source file may be listed
2+
# with full pathname. RELATIVE may makes it easier to extract an executable name
3+
# automatically.
4+
file( GLOB APP_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.c )
5+
# file( GLOB APP_SOURCES ${CMAKE_SOURCE_DIR}/*.c )
6+
# AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR} APP_SOURCES)
7+
foreach( testsourcefile ${APP_SOURCES} )
8+
# I used a simple string replace, to cut off .cpp.
9+
string( REPLACE ".c" "" testname ${testsourcefile} )
10+
add_executable( ${testname} ${testsourcefile} )
11+
install(TARGETS ${testname} DESTINATION "bin/projecteuler")
12+
13+
endforeach( testsourcefile ${APP_SOURCES} )

projecteuler/problem_01.c

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/** https://projecteuler.net/problem=1 */
2+
#include <assert.h>
3+
4+
int solution1(int n) {
5+
int sum = 0;
6+
for (int i = 1; i < n; ++i) {
7+
if (i % 3 == 0 || i % 5 == 0) {
8+
sum += i;
9+
}
10+
}
11+
return sum;
12+
}
13+
14+
void test() {
15+
assert(0 == solution1(-100));
16+
assert(0 == solution1(3));
17+
assert(3 == solution1(4));
18+
assert(23 == solution1(10));
19+
assert(233168 == solution1(1000));
20+
}
21+
22+
int main() {
23+
test();
24+
return 0;
25+
}
26+

searches/CMakeLists.txt

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# If necessary, use the RELATIVE flag, otherwise each source file may be listed
2+
# with full pathname. RELATIVE may makes it easier to extract an executable name
3+
# automatically.
4+
file( GLOB APP_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.c )
5+
# file( GLOB APP_SOURCES ${CMAKE_SOURCE_DIR}/*.c )
6+
# AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR} APP_SOURCES)
7+
foreach( testsourcefile ${APP_SOURCES} )
8+
# I used a simple string replace, to cut off .cpp.
9+
string( REPLACE ".c" "" testname ${testsourcefile} )
10+
add_executable( ${testname} ${testsourcefile} )
11+
install(TARGETS ${testname} DESTINATION "bin/searches")
12+
13+
endforeach( testsourcefile ${APP_SOURCES} )

searches/linear_search.c

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#include <assert.h>
2+
3+
/**
4+
* Search a element at an array and return index of element.
5+
* @param arr the array contains elements.
6+
* @param len the number of elements of array.
7+
* @param key the key value to be searched.
8+
* @return index if found, otherwise -1 is returned.
9+
*/
10+
int search(const int *arr, int len, int key) {
11+
for (int i = 0; i < len; ++i) {
12+
if (arr[i] == key) {
13+
return i;
14+
}
15+
}
16+
return -1;
17+
}
18+
19+
void test(){
20+
int arr[10];
21+
/* init array */
22+
for (int i = 0; i < 10; ++i) {
23+
arr[i] = i;
24+
}
25+
26+
for (int i = 0; i < 10; i++) {
27+
assert(search(arr, 10, arr[i]) == i);
28+
}
29+
assert(search(arr, 10, 66) == -1);
30+
assert(search(arr, 10, 10) == -1);
31+
32+
}
33+
int main(){
34+
test();
35+
return 0;
36+
}

0 commit comments

Comments
 (0)