1
0
Fork 0

Finished ex6.*

This commit is contained in:
Eric Teunis de Boone 2020-01-06 13:28:39 +01:00
parent fae6435f3c
commit 6591677706
7 changed files with 359 additions and 9 deletions

129
ex6.1/bubble_sort_int.cpp Normal file
View File

@ -0,0 +1,129 @@
/*
* Bubble sort some lists by exploiting templates
*
*/
#include <iostream>
#include <cstring>
#define LIST_SIZE 10
template<class T>
void display( T t[], int n );
template<class T>
void sort( T* A, const int size );
template<class T>
void order( T& a, T& b );
int main() {
int list[LIST_SIZE] = { 2295, 14500, 9127, 23079, 20612, 5571, 21959, 10032, 18339, 16673 };
float flist[LIST_SIZE] = { 835.2, 3248.8, 0.1213123, 8.234234, 8242.25823, 802348234.8, .9123812414, 0.00000001, 842, 10000000 };
const char* clist[LIST_SIZE] = {
"b",
"cccccccccc",
"aaabcd",
"axefsaf",
"poqkud",
"6xnj439yu",
"nullieksj",
"bell",
"uuisdjk",
"aaaaaaa",
};
// Print the original
std::cout << "Original: ";
display(list, LIST_SIZE);
std::cout << std::endl;
// sort it
sort( list, LIST_SIZE );
// Print it
std::cout << "Sorted: ";
display(list, LIST_SIZE);
std::cout << std::endl;
// Doing Floats
// Print the original
std::cout << "Original: ";
display(flist, LIST_SIZE);
std::cout << std::endl;
// sort it
sort( flist, LIST_SIZE );
// Print it
std::cout << "Sorted: ";
display(flist, LIST_SIZE);
std::cout << std::endl;
// Doing Chars
// Print the original
std::cout << "Original: ";
display(clist, LIST_SIZE);
std::cout << std::endl;
// sort it
sort( clist, LIST_SIZE );
// Print it
std::cout << "Sorted: ";
display(clist, LIST_SIZE);
std::cout << std::endl;
return 0;
}
template<class T>
void display( T t[], int n) {
for( int i = 0; i < n ; i++ )
{
if ( i != 0 )
{
std::cout << ", ";
}
std::cout << t[i];
}
}
template<class T>
void sort( T* A, const int size ) {
for (int i = 0 ; i < size ; i++ ) {
for (int j = 0 ; j < size - 1 ; j++) {
order( A[ i ], A[ j ] );
}
}
}
template<class T>
void order( T& a, T& b ) {
// b is greater or equal to a, do nothing
if ( a < b ) {
return;
}
T tmp = a;
a = b;
b = tmp;
}
template<>
void order( const char* &a, const char* &b ) {
if ( strcmp(a, b) <= 0 ) {
return ;
}
const char* tmp = a;
a = b;
b = tmp;
}

View File

@ -1,16 +1,16 @@
#ifndef ARRAY_HH
#define ARRAY_HH
template<class T>
class Array {
public:
Array(int size) : _size(size) {
_arr = new double[_size] ;
Array(int size, T default_value) : _size(size) {
_arr = new T[_size] ;
_default = default_value;
}
Array(const Array& other) : _size(other._size) {
_arr = new double[other._size] ;
_arr = new T[other._size] ;
// Copy elements
for (int i=0 ; i<_size ; i++) {
@ -35,10 +35,14 @@ public:
return *this ;
}
double& operator[](int index) {
T& operator[](int index) {
if ( index > _size ) {
resize(index+1);
}
return _arr[index] ;
}
const double& operator[](int index) const {
const T& operator[](int index) const {
return _arr[index] ;
}
@ -46,13 +50,17 @@ public:
void resize(int newSize) {
// Allocate new array
double* newArr = new double[newSize] ;
T* newArr = new T[newSize] ;
// Copy elements
for (int i=0 ; i<_size ; i++) {
newArr[i] = _arr[i] ;
}
for ( int i=_size ; i<newSize ; i++ ) {
newArr[i] = _default;
}
// Delete old array and install new one
delete[] _arr ;
_size = newSize ;
@ -62,7 +70,8 @@ public:
private:
int _size ;
double* _arr ;
T* _arr ;
T _default ;
} ;
#endif

29
ex6.2/main.cpp Normal file
View File

@ -0,0 +1,29 @@
#include <iostream>
#include "Array.hh"
int main() {
Array<int> intlist(1, 99);
Array<float> strlist(2, 9.9);
intlist[4] = 50;
std::cout << "Intlist" << std::endl;
for (int i=0; i<intlist.size(); i++) {
std::cout << i << ": " << intlist[i] << std::endl;
}
strlist[0] = 9;
strlist[5] = 10;
std::cout << std::endl;
std::cout << "Strlist" << std::endl;
for (int i=0; i<strlist.size(); i++) {
std::cout << i << ": " << strlist[i] << std::endl;
}
}

77
ex6.3/Array.hh Normal file
View File

@ -0,0 +1,77 @@
#ifndef ARRAY_HH
#define ARRAY_HH
template<class T>
class Array {
public:
Array(int size, T default_value) : _size(size) {
_arr = new T[_size] ;
_default = default_value;
}
Array(const Array& other) : _size(other._size) {
_arr = new T[other._size] ;
// Copy elements
for (int i=0 ; i<_size ; i++) {
_arr[i] = other._arr[i] ;
}
}
~Array() {
delete[] _arr ;
}
Array& operator=(const Array& other)
{
if (&other==this) return *this ;
if (_size != other._size) {
resize(other._size) ;
}
for (int i=0 ; i<_size ; i++) {
_arr[i] = other._arr[i] ;
}
return *this ;
}
T& operator[](int index) {
if ( index > _size ) {
resize(index+1);
}
return _arr[index] ;
}
const T& operator[](int index) const {
return _arr[index] ;
}
int size() const { return _size ; }
void resize(int newSize) {
// Allocate new array
T* newArr = new T[newSize] ;
// Copy elements
for (int i=0 ; i<_size ; i++) {
newArr[i] = _arr[i] ;
}
for ( int i=_size ; i<newSize ; i++ ) {
newArr[i] = _default;
}
// Delete old array and install new one
delete[] _arr ;
_size = newSize ;
_arr = newArr ;
}
private:
int _size ;
T* _arr ;
T _default ;
} ;
#endif

52
ex6.3/Stack.hh Normal file
View File

@ -0,0 +1,52 @@
//Stack.h
#include "Array.hh"
#ifndef STACK_H
#define STACK_H
template<class T>
class Stack {
// Interface
public:
Stack( int in_size ) {
count = 0;
_s = new Array<T>(in_size, 0);
}
int nitems() { return count ; }
bool full() { return (count == _s->size()) ; }
bool empty() { return (count == 0) ; }
void push(T c) {
if (full()) {
std::cout << "Stack::push() Error: stack is full" << std::endl ;
_s->resize( _s->size() + 10 );
}
_s[0][count++] = c ;
}
T pop() {
if (empty()) {
std::cout << "Stack::pop() Error: stack is empty" << std::endl ;
return 0 ;
}
return _s[0][--count] ;
}
void inspect() {
for ( int i = nitems() - 1; i >= 0; i-- )
{
std::cout << i << ": " << _s[0][i] << std::endl;
}
}
// Implementation
private:
Array<T>* _s ;
int count ;
};
#endif

52
ex6.3/main.cpp Normal file
View File

@ -0,0 +1,52 @@
#include <iostream>
#include "Stack.hh"
using namespace std ;
int main() {
Stack<double> s(5) ;// initialize Stack
s.push(5);
// Write doubles into Stack
int i ;
for (i=0 ; i<10 ; i++) {
cout << "pushing value " << i*i << " in stack" << endl ;
s.push(i*i) ;
}
cout << "Clone s to sclone" << endl;
Stack<double> sclone = s;
cout << "Inspect s's FIFO" << endl;
s.inspect();
cout << "Inspect sclone's FIFO" << endl;
sclone.inspect();
//The Same Data
while (!s.empty()) {
double val = s.pop() ;
cout << "popping value " << val << " from stack" << endl ;
}
cout << "Inspect s's FIFO" << endl;
s.inspect();
cout << "Inspect sclone's FIFO" << endl;
sclone.inspect();
//Not the Same Data after copy constructor
for (int i = 0; i < 5 ; i++ )
{
s.push(100*i) ;
}
cout << "Inspect s's FIFO" << endl;
s.inspect();
cout << "Inspect sclone's FIFO" << endl;
sclone.inspect();
//Not the Same Data after copy constructor
return 0 ;
}

2
ex6.3/remarks.txt Normal file
View File

@ -0,0 +1,2 @@
The implementation of Stack requires to use _s[0] whenever I want to operate on the array directly.
I don't understand why this is necessary.