1
0
Fork 0

Finished ex7.*

This commit is contained in:
Eric Teunis de Boone 2020-01-06 15:20:05 +01:00
parent 6591677706
commit d9cbe495da
6 changed files with 205 additions and 0 deletions

31
ex7.1/main.cpp Normal file
View file

@ -0,0 +1,31 @@
#include <fstream>
#include <iostream>
#include <string>
#include <map>
int main () {
const char fname[] = "../ex5.3/example.txt";
std::ifstream fh(fname);
if ( !fh ) {
std::cout << "Error opening (hardcoded) File '" << fname << "'" << std::endl;
return 2;
}
std::map<std::string,int> myMap;
std::string word;
while ( fh >> word ) {
myMap[word] += 1; // Lookup the 'word' key and increase its counter;
}
auto iter = myMap.begin();
while ( iter != myMap.end() ) {
std::cout << iter->first << ", " << iter->second << std::endl;
iter++;
}
}

42
ex7.2/main.cpp Normal file
View file

@ -0,0 +1,42 @@
#include <iostream>
#include <vector>
template<class T>
bool isPalindrome(std::vector<T> v) {
// The Empty Vector is also a Palindrome I guess
if ( v.begin() == v.end() ) {
return 1;
}
auto iter_front = v.begin() ;
auto iter_end = v.end() - 1;
// If
while ( iter_front < iter_end ) {
// Values are not the same so it is not a palindrome
if ( *iter_front != *iter_end ) {
return 0;
}
// Move iterators
iter_front++;
iter_end--;
}
return 1;
}
int main() {
std::vector<int> l = {1, 2, 3, 2, 1};
if ( isPalindrome(l) ) {
std::cout << "This is a Palindrome" << std::endl;
}
else {
std::cout << "This is *not* a Palindrome" << std::endl;
}
return 0;
}

17
ex7.3/main_list.cpp Normal file
View file

@ -0,0 +1,17 @@
#include <list>
int main() {
std::list<int> l;
// Fill List
for (int i = 0; i < 10000000 ; i++) {
l.push_back(i);
}
for ( auto iter = l.begin(); iter != l.end(); iter++ ) {
if ( *iter % 3 == 0 ) {
iter = l.erase( iter );
}
}
}

17
ex7.3/main_vector.cpp Normal file
View file

@ -0,0 +1,17 @@
#include <vector>
int main() {
std::vector<int> l;
// Fill List
for (int i = 0; i < 10000 ; i++) {
l.push_back(i);
}
for ( auto iter = l.begin(); iter != l.end(); iter++ ) {
if ( *iter % 3 == 0 ) {
iter = l.erase( iter );
}
}
}

47
ex7.4/Stack.hh Normal file
View file

@ -0,0 +1,47 @@
//Stack.h
#include <deque>
#ifndef STACK_H
#define STACK_H
template<class T>
class Stack {
// Interface
public:
Stack() {
_s = new std::deque<T>();
}
int nitems() { return _s->size() ; }
bool empty() { return (_s->size() == 0) ; }
void push(T c) {
_s->push_back(c);
}
T pop() {
if (empty()) {
std::cout << "Stack::pop() Error: stack is empty" << std::endl ;
return 0 ;
}
T tmp = _s->back();
_s->pop_back();
return tmp;
}
void inspect() {
for ( auto iter = _s->begin(); iter != _s->end() ; iter++ )
{
std::cout << *iter << std::endl;
}
}
// Implementation
private:
std::deque<T>* _s ;
};
#endif

51
ex7.4/main.cpp Normal file
View file

@ -0,0 +1,51 @@
#include <iostream>
#include "Stack.hh"
using namespace std ;
int main() {
Stack<double> s ;// initialize Stack
// 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 ;
}