1
0
Fork 0

Ex2.{1,2,3}: Copied and extended code from Intro CPP repo,

Working on Ex2.4
This commit is contained in:
Eric Teunis de Boone 2019-11-26 10:55:12 +01:00
parent 4c31b5a287
commit 82260a45db
11 changed files with 409 additions and 0 deletions

View file

@ -0,0 +1,90 @@
/*
* Bubble sort a list of 10 integers
*
* I'd rather have the method without pointers.
* As a user of the function, I don't have to think about passing pointers, which is also nice
*/
#include <iostream>
#define LIST_SIZE 10
#define USE_PTR 1
void sort( int* A, const int size );
#if !USE_PTR
void order( int& a, int& b );
#else
void orderptr( int* a, int* b );
#endif
int main() {
int list[LIST_SIZE] = { 2295, 14500, 9127, 23079, 20612, 5571, 21959, 10032, 18339, 16673 };
// Print the original
std::cout << "Original: ";
for( int i = 0; i < LIST_SIZE ; i++ )
{
if ( i != 0 )
{
std::cout << ", ";
}
std::cout << list[i];
}
std::cout << std::endl;
// sort it
sort( list, LIST_SIZE );
// Print it
std::cout << "Sorted: ";
for( int i = 0; i < LIST_SIZE ; i++ )
{
if ( i != 0 )
{
std::cout << ", ";
}
std::cout << list[i];
}
std::cout << std::endl;
return 0;
}
void sort( int* A, const int size ) {
for (int i = 0 ; i < size ; i++ ) {
for (int j = 0 ; j < size - 1 ; j++) {
#if !USE_PTR
order( A[ i ], A[ j ] );
#else
orderptr( &A[ i ], &A[ j ] );
#endif
}
}
}
#if !USE_PTR
void order( int& a, int& b ) {
// b is greater or equal to a, do nothing
if ( a < b ) {
return;
}
int tmp = a;
a = b;
b = tmp;
}
#else
void orderptr( int* a, int* b ) {
if ( *a < *b ) {
return;
}
int* tmp = a;
a = b;
b = tmp;
}
#endif

View file

@ -0,0 +1,126 @@
/*
* Bubble sort a list of 10 strings
*
* We want the strings to persist in memory, that is why we use a 'const char*'.
*
*/
#include <iostream>
#include <cstring>
#define LIST_SIZE 10
#define USE_PTR 1
#define VERBOSE 0
void sort( const char** A, const int size );
#if !USE_PTR
void order( const char* a, const char* b );
#else
void orderptr( const char* &a, const char* &b );
#endif
int main() {
const char* list[LIST_SIZE] = {
"b",
"cccccccccc",
"aaabcd",
"axefsaf",
"poqkud",
"6xnj439yu",
"nullieksj",
"bell",
"uuisdjk",
"aaaaaaa",
};
// Print the original
std::cout << "Original: ";
for( int i = 0; i < LIST_SIZE ; i++ )
{
if ( i != 0 )
{
std::cout << ", ";
}
std::cout << list[i];
}
std::cout << std::endl;
// sort it
sort( list, LIST_SIZE );
// Print it
std::cout << "Sorted: ";
for( int i = 0; i < LIST_SIZE ; i++ )
{
if ( i != 0 )
{
std::cout << ", ";
}
std::cout << list[i];
}
std::cout << std::endl;
return 0;
}
void sort( const char** A, const int size ) {
for ( int i = 0 ; i < size ; i++ ) {
for ( int j = 0 ; j < size - 1 ; j++ ) {
#if VERBOSE
std::cout << "SORT: " << A[i] << ", " << A[j]<< std::endl;
#endif
#if !USE_PTR
order( A[ i ], A[ j ] );
#else
orderptr( A[ i ], A[ j ] );
#endif
}
}
}
#if !USE_PTR
void order( const char* a, const char* b ) {
#if VERBOSE
std::cout << "Order: " << a << std::endl;
#endif
// b is greater or equal to a, do nothing
if ( strcmp(a, b) > 0) {
#if VERBOSE
std::cout << "Ordering OK" << std::endl;
#endif
return;
}
#if VERBOSE
std::cout << "Ordering ToDo" << std::endl;
#endif
const char* tmp = a;
a = b;
b = tmp;
}
#else
void orderptr( const char* &a, const char* &b ) {
#if VERBOSE
std::cout << "Order: " << a << ", " << b << std::endl;
#endif
if ( strcmp( a, b ) > 0 ) {
#if VERBOSE
std::cout << "Ordering OK" << std::endl;
#endif
return;
}
#if VERBOSE
std::cout << "Ordering ToDo" << std::endl;
#endif
const char* tmp = a;
a = b;
b = tmp;
}
#endif

55
ex2/ex2.2/overloading.cpp Normal file
View file

@ -0,0 +1,55 @@
/*
* Function Overloading
*
* generalisation of type is possible
* => not writing an int implementation means always use the double
* => => even when to ints are supplied, they are generalised to doubles
*/
#include <iostream>
//int min( int a, int b );
double min( double a, double b );
int min( int a[], int size);
int main() {
const int size = 5;
int list[size] = { 42, 37, 29, 23, 19 };
std::cout << "min(5, 1) = " << min(5,1) << std::endl;
std::cout << "min([42,37,29,23,19],5) = " << min(list,size) << std::endl;
std::cout << "min( 3.12345, 3.15432 ) = " << min(3.12345, 3.15432) << std::endl;
std::cout << "min( 3.12345, 4 ) = " << min(3.12345, 4) << std::endl;
return 0;
}
//int min( int a, int b ) {
// if ( a < b ) {
// return a;
// }
// return b;
//}
double min( double a, double b ) {
if ( a < b ) {
return a;
}
return b;
}
int min( int a[], int size ) {
int m = a[0];
for( int i = 1; i < size ; ++i ) {
m = min(a[i], m);
}
return m;
}

34
ex2/ex2.3/namespacing.cpp Normal file
View file

@ -0,0 +1,34 @@
#include <iostream>
namespace Black {
void print(int k) {
std::cout << "Black print" << std::endl;
};
}
namespace White {
void print(int k) {
std::cout << "White print" << std::endl;
};
}
void sub1() {
using White::print ;
print(5);
}
void print(int k) {
if ( k > 0 )
{
print(k-1);
}
}
int main() {
sub1();
print(7);
return 0;
}

11
ex2/ex2.3/namespacing.txt Normal file
View file

@ -0,0 +1,11 @@
a)
the `print(5)` line calls White::print
the `print(k-1)` line calls itself
b)
the line is allowed by itself,
however since we define a `void print(int k)` function
there is no way to distinguish which one is called.
thus it becomes illegal.
Including it in main would be OK

8
ex2/ex2.4/build.txt Normal file
View file

@ -0,0 +1,8 @@
d)
g++ -c max.cc;
g++ -c min.cc;
ar q libMinMax.a max.o min.o
g++ test.cpp -L. -llibMinMax.a

20
ex2/ex2.4/max.cc Executable file
View file

@ -0,0 +1,20 @@
//max.cc
namespace mylib {
double max( double a, double b ) {
if ( a < b ) {
return b;
}
return a;
}
int max( int a[], int size ) {
int m = a[0];
for( int i = 1; i < size ; ++i ) {
m = max(a[i], m);
}
return m;
}
}

8
ex2/ex2.4/max.hh Executable file
View file

@ -0,0 +1,8 @@
//max.hh
#ifndef MAX_HH
#define MAX_HH
namespace mylib {
double max( double a, double b );
int max( int a[], int size);
}
#endif

19
ex2/ex2.4/min.cc Executable file
View file

@ -0,0 +1,19 @@
//min.cc
namespace mylib {
double min( double a, double b ) {
if ( a < b ) {
return a;
}
return b;
}
int min( int a[], int size ) {
int m = a[0];
for( int i = 1; i < size ; ++i ) {
m = min(a[i], m);
}
return m;
}
}

8
ex2/ex2.4/min.hh Executable file
View file

@ -0,0 +1,8 @@
//min.hh
#ifndef MIN_HH
#define MIN_HH
namespace mylib {
double min( double a, double b );
int min( int a[], int size);
}
#endif

30
ex2/ex2.4/test.cpp Normal file
View file

@ -0,0 +1,30 @@
#include <iostream>
#include "max.hh"
#include "min.hh"
#define LIST_SIZE 5
int main() {
double dbl_a = 2.31233;
double dbl_b = 91.3239;
int list[ LIST_SIZE ] = { 0, 2 ,4 , 6, -9 };
std::cout << "Doubles" << std::endl;
std::cout << "max(" << dbl_a << ", " << dbl_b << ") = " << mylib::max(dbl_a, dbl_b ) << std::endl;
std::cout << "min(" << dbl_a << ", " << dbl_b << ") = " << mylib::min(dbl_a, dbl_b ) << std::endl;
std::cout << std::endl;
/*
std::cout << "Int & Doubles" << std::endl;
std::cout << "max(" << list[4] << ", " << dbl_b << ") = " << mylib::max(list[4], dbl_b ) << std::endl;
std::cout << "min(" << list[4] << ", " << dbl_b << ") = " << mylib::min(list[4], dbl_b ) << std::endl;
std::cout << std::endl;
*/
std::cout << "List" << std::endl;
//std::cout << "max( list, " << LIST_SIZE << ") = " << mylib::max ) << std::endl;
//std::cout << "min( list, " << dbl_a << ", " << dbl_b << ") = " << mylib::max(dbl_a, dbl_b ) << std::endl;
return 0;
}