aboutsummaryrefslogtreecommitdiff
path: root/cpp07
diff options
context:
space:
mode:
Diffstat (limited to 'cpp07')
-rw-r--r--cpp07/ex00/Makefile4
-rw-r--r--cpp07/ex00/main.cpp83
-rw-r--r--cpp07/ex00/whatever.hpp (renamed from cpp07/ex00/whatever.cpp)31
-rw-r--r--cpp07/ex01/Makefile4
-rw-r--r--cpp07/ex01/iter.hpp31
-rw-r--r--cpp07/ex01/main.cpp (renamed from cpp07/ex01/iter.cpp)30
-rw-r--r--cpp07/ex02/Array.hpp30
-rw-r--r--cpp07/ex02/main.cpp34
8 files changed, 149 insertions, 98 deletions
diff --git a/cpp07/ex00/Makefile b/cpp07/ex00/Makefile
index 660a45b..33feff3 100644
--- a/cpp07/ex00/Makefile
+++ b/cpp07/ex00/Makefile
@@ -6,7 +6,7 @@
# By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2020/04/14 19:48:34 by charles #+# #+# #
-# Updated: 2020/04/14 19:58:14 by charles ### ########.fr #
+# Updated: 2020/12/14 14:41:32 by cacharle ### ########.fr #
# #
# **************************************************************************** #
@@ -15,7 +15,7 @@ NAME = a_few_functions
CXX = clang++
CXXFLAGS = -std=c++98 -Wall -Wextra -Werror
-SRC = whatever.cpp
+SRC = main.cpp
OBJ = $(SRC:.cpp=.o)
all: $(NAME)
diff --git a/cpp07/ex00/main.cpp b/cpp07/ex00/main.cpp
new file mode 100644
index 0000000..1c04faf
--- /dev/null
+++ b/cpp07/ex00/main.cpp
@@ -0,0 +1,83 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* main.cpp :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: cacharle <me@cacharle.xyz> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2020/12/14 14:41:39 by cacharle #+# #+# */
+/* Updated: 2020/12/14 15:05:32 by cacharle ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include <iostream>
+#include <string>
+#include "whatever.hpp"
+
+int main()
+{
+ {
+ std::cout << "=================== SUBJECT MAIN ================" << std::endl;
+ int a = 2;
+ int b = 3;
+ ::swap(a, b);
+ std::cout << "a = " << a << ", b = " << b << std::endl;
+ std::cout << "min(a, b) = " << ::min( a, b ) << std::endl;
+ std::cout << "max(a, b) = " << ::max( a, b ) << std::endl;
+ std::string c = "chaine1";
+ std::string d = "chaine2";
+ ::swap(c, d);
+ std::cout << "c = " << c << ", d = " << d << std::endl;
+ std::cout << "min(c, d) = " << ::min( c, d ) << std::endl;
+ std::cout << "max(c, d) = " << ::max( c, d ) << std::endl;
+ }
+
+ std::cout << std::endl;
+
+ {
+ double ad = 3.4;
+ double bd = 4.5;
+ float af = 3.4f;
+ float bf = 4.5f;
+ char ac = 'a';
+ char bc = 'b';
+ std::cout << "=================== SWAP ================" << std::endl;
+ ::swap(af, bf);
+ std::cout << "af = " << af << ", bf = " << bf << std::endl;
+ ::swap(ad, bd);
+ std::cout << "ad = " << ad << ", bd = " << bd << std::endl;
+ ::swap(ac, bc);
+ std::cout << "ac = " << ac << ", bc = " << bc << std::endl;
+ }
+
+ std::cout << std::endl;
+
+ double ad = 3.4;
+ double bd = 4.5;
+ float af = 3.4f;
+ float bf = 4.5f;
+ char ac = 'a';
+ char bc = 'b';
+
+
+ int same1 = 4;
+ int same2 = 4;
+
+ std::cout << "=================== MIN ================" << std::endl;
+ std::cout << "min(af, bf) = " << ::min(af, bf) << std::endl;
+ std::cout << "min(ad, bd) = " << ::min(ad, bd) << std::endl;
+ std::cout << "min(ac, bc) = " << ::min(ac, bc) << std::endl;
+ int &minsame = ::min(same1, same2);
+ std::cout << "min(same1, same2) = " << minsame << ", address same2 = " << &same2 << ", address minsame = " << &minsame << std::endl;
+
+ std::cout << std::endl;
+
+ std::cout << "=================== MAX ================" << std::endl;
+ std::cout << "max(af, bf) = " << ::max(af, bf) << std::endl;
+ std::cout << "max(ad, bd) = " << ::max(ad, bd) << std::endl;
+ std::cout << "max(ac, bc) = " << ::max(ac, bc) << std::endl;
+ int &maxsame = ::max(same1, same2);
+ std::cout << "max(same1, same2) = " << maxsame << ", address same2 = " << &same2 << ", address maxsame = " << &maxsame << std::endl;
+
+ return 0;
+}
diff --git a/cpp07/ex00/whatever.cpp b/cpp07/ex00/whatever.hpp
index ebebf8c..8768a44 100644
--- a/cpp07/ex00/whatever.cpp
+++ b/cpp07/ex00/whatever.hpp
@@ -1,22 +1,22 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
-/* whatever.cpp :+: :+: :+: */
+/* whatever.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/04/14 19:46:56 by charles #+# #+# */
-/* Updated: 2020/04/14 20:01:41 by charles ### ########.fr */
+/* Updated: 2020/12/14 15:05:32 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
-#include <iostream>
-#include <string>
+#ifndef WHATEVER_HPP
+# define WHATEVER_HPP
template<typename T>
void swap(T& a, T& b)
{
- T tmp = a;
+ T tmp(a);
a = b;
b = tmp;
}
@@ -33,23 +33,4 @@ T& max(T& a, T& b)
return a > b ? a : b;
}
-int main()
-{
- int a = 2;
- int b = 3;
-
- ::swap(a, b);
- std::cout << "a = " << a << ", b = " << b << std::endl;
- std::cout << "min( a, b ) = " << ::min( a, b ) << std::endl;
- std::cout << "max( a, b ) = " << ::max( a, b ) << std::endl;
-
- std::string c = "chaine1";
- std::string d = "chaine2";
-
- ::swap(c, d);
- std::cout << "c = " << c << ", d = " << d << std::endl;
- std::cout << "min( c, d ) = " << ::min( c, d ) << std::endl;
- std::cout << "max( c, d ) = " << ::max( c, d ) << std::endl;
-
- return 0;
-}
+#endif
diff --git a/cpp07/ex01/Makefile b/cpp07/ex01/Makefile
index 45180bc..7bee119 100644
--- a/cpp07/ex01/Makefile
+++ b/cpp07/ex01/Makefile
@@ -6,7 +6,7 @@
# By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2020/04/14 19:48:34 by charles #+# #+# #
-# Updated: 2020/04/14 20:02:14 by charles ### ########.fr #
+# Updated: 2020/12/14 15:07:28 by cacharle ### ########.fr #
# #
# **************************************************************************** #
@@ -15,7 +15,7 @@ NAME = iter
CXX = clang++
CXXFLAGS = -std=c++98 -Wall -Wextra -Werror
-SRC = iter.cpp
+SRC = main.cpp
OBJ = $(SRC:.cpp=.o)
all: $(NAME)
diff --git a/cpp07/ex01/iter.hpp b/cpp07/ex01/iter.hpp
new file mode 100644
index 0000000..df1a37a
--- /dev/null
+++ b/cpp07/ex01/iter.hpp
@@ -0,0 +1,31 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* iter.hpp :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: cacharle <me@cacharle.xyz> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2020/12/14 15:06:48 by cacharle #+# #+# */
+/* Updated: 2020/12/14 15:18:09 by cacharle ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#ifndef ITER_HPP
+# define ITER_HPP
+
+# include <iostream>
+
+template<typename T>
+void iter(T* ptr, size_t len, void (*f)(T const& x))
+{
+ for (size_t i = 0; i < len; i++)
+ f(ptr[i]);
+}
+
+template<typename T>
+void timeTwo(T const& x)
+{
+ std::cout << "timeTwo function " << x << " * 2 = " << x * 2 << std::endl;
+}
+
+#endif
diff --git a/cpp07/ex01/iter.cpp b/cpp07/ex01/main.cpp
index 831286f..6ac8e82 100644
--- a/cpp07/ex01/iter.cpp
+++ b/cpp07/ex01/main.cpp
@@ -1,30 +1,17 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
-/* iter.cpp :+: :+: :+: */
+/* main.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/04/14 20:02:19 by charles #+# #+# */
-/* Updated: 2020/12/11 15:45:09 by charles ### ########.fr */
+/* Updated: 2020/12/14 15:16:32 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
#include <iostream>
-
-template<typename T>
-void iter(T* ptr, size_t len, void (*f)(T const& x))
-{
- for (size_t i = 0; i < len; i++)
- f(ptr[i]);
-}
-
-/* sel-melc says const& */
-template<typename T>
-void timeTwo(T const& x)
-{
- x *= 2;
-}
+#include "iter.hpp"
int main()
{
@@ -34,9 +21,7 @@ int main()
std::cout << intArray[i] << ", ";
std::cout << std::endl;
iter(intArray, intArraySize, timeTwo<int>);
- for (size_t i = 0; i < intArraySize; i++)
- std::cout << intArray[i] << ", ";
- std::cout << std::endl;
+
std::cout << "--------------------------------------" << std::endl;
float floatArray[] = {1.1, 2.2, 3.3, 4.3, 10.001, 20.9, 30.3, -1.2, -2.4};
@@ -45,9 +30,7 @@ int main()
std::cout << floatArray[i] << ", ";
std::cout << std::endl;
iter(floatArray, floatArraySize, timeTwo<float>);
- for (size_t i = 0; i < floatArraySize; i++)
- std::cout << floatArray[i] << ", ";
- std::cout << std::endl;
+
std::cout << "--------------------------------------" << std::endl;
unsigned int uintArray[] = {1, 2, 3, 4, 10, 20, 30, 100, 2000};
@@ -56,9 +39,6 @@ int main()
std::cout << uintArray[i] << ", ";
std::cout << std::endl;
iter(uintArray, uintArraySize, timeTwo<unsigned int>);
- for (size_t i = 0; i < uintArraySize; i++)
- std::cout << uintArray[i] << ", ";
- std::cout << std::endl;
return 0;
}
diff --git a/cpp07/ex02/Array.hpp b/cpp07/ex02/Array.hpp
index fec8698..0dc53c4 100644
--- a/cpp07/ex02/Array.hpp
+++ b/cpp07/ex02/Array.hpp
@@ -6,7 +6,7 @@
/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/04/14 20:14:07 by charles #+# #+# */
-/* Updated: 2020/04/14 20:41:25 by charles ### ########.fr */
+/* Updated: 2020/12/14 15:26:51 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -19,31 +19,30 @@ template<typename T>
class Array
{
public:
- Array() : m_under(new T[0]), m_size(0)
- {}
+ Array()
+ : m_under(new T[0]), m_size(0) {}
- Array(unsigned int n) : m_under(new T[n]()), m_size(n)
- {}
+ Array(unsigned int n)
+ : m_under(new T[n]()), m_size(n) {}
- Array(Array const& other) : m_under(new T[other.m_size]), m_size(other.m_size)
+ Array(Array const& other)
+ : m_under(new T[other.m_size]), m_size(other.m_size)
{
for (unsigned int i = 0; i < m_size; i++)
m_under[i] = other.m_under[i];
}
- void operator=(Array const& other)
+ Array& operator=(Array const& other)
{
delete [] m_under;
- m_size = other.m_size;
+ m_size = other.m_size;
m_under = new T[m_size];
for (unsigned int i = 0; i < m_size; i++)
m_under[i] = other.m_under[i];
+ return *this;
}
- ~Array()
- {
- delete [] m_under;
- }
+ ~Array() { delete [] m_under; }
T& operator[](unsigned int n)
{
@@ -59,13 +58,10 @@ public:
return m_under[n];
}
- unsigned int size() const
- {
- return m_size;
- }
+ unsigned int size() const { return m_size; }
private:
- T* m_under;
+ T* m_under;
unsigned int m_size;
};
diff --git a/cpp07/ex02/main.cpp b/cpp07/ex02/main.cpp
index 251d697..2f53f00 100644
--- a/cpp07/ex02/main.cpp
+++ b/cpp07/ex02/main.cpp
@@ -6,7 +6,7 @@
/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/04/14 20:25:44 by charles #+# #+# */
-/* Updated: 2020/04/14 20:41:40 by charles ### ########.fr */
+/* Updated: 2020/12/14 15:29:50 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -17,14 +17,8 @@ int main()
{
Array<int> a;
std::cout << a.size() << std::endl;
- try
- {
- std::cout << a[0] << std::endl;
- }
- catch (std::exception e)
- {
- std::cout << e.what() << std::endl;
- }
+ try { std::cout << a[0] << std::endl; }
+ catch (std::exception e) { std::cout << e.what() << std::endl; }
std::cout << "------------------------" << std::endl;
Array<int> b(3);
@@ -34,14 +28,8 @@ int main()
b[1] = 2;
b[2] = 3;
std::cout << b[0] << ", " << b[1] << ", " << b[2] << std::endl;
- try
- {
- std::cout << b[3] << std::endl;
- }
- catch (std::exception e)
- {
- std::cout << e.what() << std::endl;
- }
+ try { std::cout << b[3] << std::endl; }
+ catch (std::exception e) { std::cout << e.what() << std::endl; }
std::cout << "------------------------" << std::endl;
Array<float> c(3);
@@ -50,14 +38,8 @@ int main()
c[1] = 2.2;
c[2] = 3.1;
std::cout << c[0] << ", " << c[1] << ", " << c[2] << std::endl;
- try
- {
- std::cout << c[3] << std::endl;
- }
- catch (std::exception e)
- {
- std::cout << e.what() << std::endl;
- }
+ try { std::cout << c[3] << std::endl; }
+ catch (std::exception e) { std::cout << e.what() << std::endl; }
std::cout << "------------------------" << std::endl;
Array<float> d(c);
@@ -76,6 +58,4 @@ int main()
std::cout << f[0] << " <> " << c[0] << std::endl;
std::cout << f[1] << " <> " << c[1] << std::endl;
std::cout << f[2] << " <> " << c[2] << std::endl;
-
- return 0;
}