Warning: Undefined array key "del" in /customers/7/3/b/codenode.nl/httpd.www/skole/stuff/H10/Programmering/Ovinger/print.php on line 6 Ståle Nestås, Ørjan Bauge - TOD070 - Øving 4
Øving 4 // Øving 5 Del 1 // Øving 5 Del 2
Oppgave1Dictionary.h
#include <iostream>
#include <string>
#include <map>

#include "Entry.h"

using namespace std;

class Dictionary {

public:
Dictionary() { }
void leggTilDef( string ref, string def );
Entry* operator []( string ref );
private:
map<string, Entry*> dict;

};
Entry.h
#include "string"

using namespace std;

class Entry {
public:
string ref, def;
Entry( string refword, string defword ) : ref( refword ), def( defword ) { }
};
Dictionay.cpp
#include "Dictionary.h"

void Dictionary::leggTilDef( string ref, string def ) {
Entry* ptr = new Entry( ref, def );
dict.insert( pair<string,Entry*>(ref, ptr) );
}

Entry* Dictionary::operator []( string ref ) {

map<string, Entry*>::iterator it = dict.find( ref );
if( it != dict.end() )
return it->second;

return 0;
}
main.cpp
#include <iostream>
#include <string>
#include <map>

#include "Dictionary.h"

using namespace std;

int main() {

Dictionary dict;
dict.leggTilDef( "LOL", "Lord Of Life" );
dict.leggTilDef( "OMG", "The OMG is a non-profit consortium created in 1989 to promote the theory and practice of object technology for the development for distributed operating systems.");

cout << dict["LOL"]->def << endl;


return 0;

}
Kjøring:
Lord Of Life
Oppgave2Motor_vehicle.h
#ifndef GUARD_MOTOR_VEHICLE_H
#define GUARD_MOTOR_VEHICLE_H


#include <string>
#include <iostream>

#include "Vehicle.h"

using namespace std;

class Motor_vehicle : public Vehicle {
public:
Motor_vehicle() {}
Motor_vehicle(const string& no, const int we) : reg_num(no), weight(we) {}
const string& number() { return reg_num; }
const int getWeight() { return weight; }
void write_info() const;
Motor_vehicle* max(Vehicle* veh);
protected:
string reg_num;
int weight;
};

#endif
Owner.h
#include "Person.h"
#include "Motor_vehicle.h"

#include <iostream>
#include <string>
#include <cstring>

using namespace std;

class Owner : public Person {
public:
Owner(const string& n) : Person(n), capasity(1), used(0) { carCollection = new Motor_vehicle*[1]; }
~Owner();
void add(Motor_vehicle& vehicle); // “add”
void erase(Motor_vehicle& vehicle);//”erase”
void virtual write_info() const;
private:
Motor_vehicle** carCollection; // dynamisk tabell, alt: bruke vector, da skriver
// kun vector<Motor_vehicle *> carCollection;
int capasity;
int used;
};
Person.h
#ifndef GUARD_PERSON_H
#define GUARD_PERSON_H

#include <string>
#include <iostream>

using namespace std;

class Person {
public:
Person() {}
Person(const string& n) : name(n) { }
const string getName() const;
void setName(const string& n);
void virtual write_info() const;
private:
string name;
};

#endif
Private_car.h
#ifndef GUARD_PRIVATE_CAR_H
#define GUARD_PRIVATE_CAR_H

#include <string>
#include <iostream>

#include "Motor_vehicle.h"

using namespace std;

class Private_car : public Motor_vehicle {
public:
Private_car(const string& no, const int weight, int n) : Motor_vehicle(no, weight), num_seats(n) { }
void write_info() const;
protected:
int num_seats;
};

#endif
Vehicle.h
#ifndef GUARD_VEHICLE_H
#define GUARD_VEHICLE_H

class Vehicle {
public:
virtual void write_info() const = 0;
virtual Vehicle* max(Vehicle* Pveh) = 0;
};

#endif
Motor_vehicle.cpp
#include "Motor_vehicle.h"

void Motor_vehicle::write_info() const {

cout << "Reg. nr: " << reg_num << endl;
}

Motor_vehicle* Motor_vehicle::max(Vehicle* veh){
Motor_vehicle* mveh = dynamic_cast<Motor_vehicle*>(veh);
if( mveh == 0 ) {
//exit(1);
cout << "Kan ikke gjøre downcaste!";
}
return (weight > mveh->getWeight())?this:mveh;

}
Owner.cpp
#include "Owner.h"

void Owner::add(Motor_vehicle& vehicle) {
if( used >= capasity ) {
capasity += 10;

Motor_vehicle** tmp = new Motor_vehicle*[ capasity ];

for( int i = 0; i < used; i++ )
tmp[i] = carCollection[i];

//delete [] carCollection;

carCollection = new Motor_vehicle*[ capasity ];

for( int i = 0; i < used; i++ )
carCollection[i] = tmp[i];

//delete [] tmp;

}
carCollection[used++] = &vehicle;
}


void Owner::erase(Motor_vehicle& vehicle) {
for( int i = 0; i < used; i++ ) {
if( strcmp( carCollection[i]->number().c_str(), vehicle.number().c_str() ) == 0 ){

used--;
i++;
//delete carCollection[i];

for( int u = i; u < used; u++ )
carCollection[u-1] = carCollection[u];
}
}

}


void Owner::write_info() const {
cout << getName() << " har " << used << " biler" << endl;
for( int i = 0; i < used; i++ )
carCollection[i]->write_info();
}

Owner::~Owner() {
for( int i = 0; i < capasity; i++ )
delete carCollection[i];
delete [] carCollection;
}
Person.cpp
#include "Person.h"

const string Person::getName() const {
return name;
}

void Person::setName(const string& n) {
name = n;
}

void Person::write_info() const {
cout << getName();
}
Private_car.cpp
#include "Private_car.h"

void Private_car::write_info() const {
cout << "Reg nummer: " << reg_num << " Antall seter: " << num_seats << endl;
}
main.cpp

#include <iostream>
#include <string>
#include "Owner.h"
#include "Private_car.h"

using namespace std;


int main() {

Owner personA( "Åge Sørås" );
Private_car* ny = new Private_car( "C-108", 2000, 4 );
Private_car* ny2 = new Private_car( "C-109", 1998, 4 );
Private_car* ny3 = new Private_car( "C-111", 1999, 4 );
Private_car eksisterende( "C-111", 0, 0 );

personA.add( *ny );
personA.add( *ny2 );
personA.add( *ny3 );
personA.erase( eksisterende );

personA.write_info();


Motor_vehicle* mest;
Motor_vehicle* mv1 = new Motor_vehicle("131", 500);
Motor_vehicle* mv2 = new Motor_vehicle("132", 725);

mest = mv1->max( mv2 );

cout << "Bilen som veier mest er: ";
mest->write_info();


return 0;
}
Kjøring:
Åge Sørås har 2 biler
Reg nummer: C-108 Antall seter: 4
Reg nummer: C-109 Antall seter: 4
Bilen som veier mest er: Reg. nr: 132
Oppgave2cCircle.h
#ifndef GUARD_CIRCLE_H
#define GUARD_CIRCLE_H

#include "Figure.h"

class Circle : public Figure {
public:
Circle( Point a, int rad ) : Figure(), A(a), r(rad) { }
int area() const;
private:
Point A;
int r;

};

#endif
Figure.h
#ifndef GUARD_FIGURE_H
#define GUARD_FIGURE_H

#include "Point.h"

class Figure {
public:
Figure() { }
virtual int area() const = 0;

};

#endif
Point.h
#ifndef GUARD_POINT_H
#define GUARD_POINT_H

class Point {
public:
Point(int nx, int ny) : x(nx), y(ny) {}
int x,y;

};

#endif
Rectangle.h
#ifndef GUARD_RECTANGLE_H
#define GUARD_RECTANGLE_H

#include "Figure.h"

using namespace std;

class Rectangle : public Figure{
public:
Rectangle( Point a, Point c ) : Figure(), A(a), C(c) { }
int area() const;
private:
Point A,C;
};

#endif

Triangle.h
#ifndef GUARD_TRIANGLE_H
#define GUARD_TRIANGLE_H

#include "Figure.h"

class Triangle : public Figure {
public:
Triangle( Point a, Point b, Point c ) : Figure(), A(a), B(b), C(c) { }
int area() const;
private:
Point A,B,C;
};

#endif
Circle.cpp
#include "Circle.h"

int Circle::area() const {
return ( 2 * 3.14 * r );
}
Rectangle.cpp
#include "Rectangle.h"

int Rectangle::area() const {
return (( C.y - A.y ) * ( C.x - A.x ) );
}
Triangle.cpp
#include "Triangle.h"

int Triangle::area() const {

return ( ( B.x - A.x ) * ( C.y - A.y ) );
}
main.cpp
#include "iostream"
#include "Rectangle.h"
#include "Triangle.h"
#include "Circle.h"

using namespace std;

int main() {


Figure** figurer = new Figure*[5];


Rectangle* fig1 = new Rectangle( Point(1,1), Point( 3,3 ) );
Circle* fig2 = new Circle( Point(1,1), 4 );
Triangle* fig3 = new Triangle( Point(1,1), Point( 4,1 ), Point(3,2) );
Triangle* fig4 = new Triangle( Point(1,1), Point( 4,1 ), Point(1,6) );
Rectangle* fig5 = new Rectangle( Point(0,1), Point( 3,7 ) );

figurer[0] = fig1;
figurer[1] = fig2;
figurer[2] = fig3;
figurer[3] = fig4;
figurer[4] = fig5;


for( int i = 0; i < 5; i++ )
cout << "Areal for figur #" << i+1 << ": " << figurer[i]->area() << endl;

return 0;

}
Kjøring:
Areal for figur #1: 4
Areal for figur #2: 25
Areal for figur #3: 3
Areal for figur #4: 15
Areal for figur #5: 18
Oppgave3amain.cpp
#include <iostream>
#include <limits>
#include <stdexcept>

using namespace std;

float nfac( int n ) throw ( range_error );

int main() {

int tall = 0;
cout << "Skriv inn tall: ";
while( cin >> tall ) {


try {
float fak = nfac( tall );
cout << "Fakultet: " << fak << endl;
}
catch( range_error &e ) {
cout << e.what() << endl;
}

cout << "Skriv inn tall: ";
}

return 0;

}


float nfac( int n ) throw ( range_error ) {
if( n <= 0 )
return 1;

float sum = n*nfac(n-1);
if( sum > numeric_limits<float>::max() )
throw range_error( "Tallet blir for stort!" );
return sum;

}
Kjøring:
Skriv inn tall: 33
Fakultet: 8.68332e+36
Skriv inn tall: 34
Fakultet: 2.95233e+38
Skriv inn tall: 35
Tallet blir for stort!
Skriv inn tall: ^D
Oppgave3bpfarrayd.h
#ifndef PFARRAYD_H
#define PFARRAYD_H

#include <string>
using std::string;
class OutOfRange
{
public:
OutOfRange(string thisMessage, int thisIndex) : message( thisMessage ), index( thisIndex ) { }
string getMessage() const { return message; }
int getIndex() const { return index; }
private:
string message;
int index;
};

//Objects of this class are partially filled arrays of doubles.
class PFArrayD
{
public:
PFArrayD( );
PFArrayD( int );
//constructors
void addElement(double element) throw ( OutOfRange );
//Precondition: The array is not full.
//Postcondition: The element has been added.
bool full( ) const { return (capacity == used); }
//Returns true if the array is full, false otherwise.
int getCapacity( ) const { return capacity; }
int getNumberUsed( ) const { return used; }
void emptyArray( ){ used = 0; }
//Empties the array.
double& operator[](int index) throw ( OutOfRange );
//Read and change access to elements 0 through numberUsed - 1.
PFArrayD& operator =(const PFArrayD& rightSide);
virtual ~PFArrayD( );
private:
double *a; //for an array of doubles.
int capacity; //for the size of the array.
int used; //for the number of array positions currently in use.
};
#endif //PFARRAYD_H
pfarrayd.cpp
//This is the implementation file pfarrayd.cpp.
#include <iostream>
using std::cout;
#include "pfarrayd.h"

PFArrayD::PFArrayD( ) : capacity(50), used(0)
{
a = new double[capacity];
}

PFArrayD::PFArrayD(int size) : capacity(size), used(0)
{
a = new double[capacity];
}

double& PFArrayD::operator[](int index) throw ( OutOfRange )
{
if ( index >= used || index < 0 )
throw OutOfRange( "Illegal index in PFArrayD ", index );

return a[index];
}

PFArrayD& PFArrayD::operator =(const PFArrayD& rightSide)
{
if (capacity != rightSide.capacity)
{
delete [] a;
a = new double[rightSide.capacity];
}

capacity = rightSide.capacity;
used = rightSide.used;
for (int i = 0; i < used; i++)
a[i] = rightSide.a[i];
return *this;
}

PFArrayD::~PFArrayD( )
{
delete [] a;
}

void PFArrayD::addElement(double element) throw ( OutOfRange )
{
if (used >= capacity)
throw OutOfRange( "Attempt to exceed capacity in PFArrayD.", 0 );

a[used] = element;
used++;
}


main.cpp
// bmain.cpp

#include <iostream>

#include "pfarrayd.h"

using std::cin;
using std::cout;
using std::endl;

void testPFArrayD();

int main()
{
cout << "This program tests the class PFArrayD. \n";

char ans;
do
{
testPFArrayD();
cout << "Test again? (y/n) ";
cin >> ans;
}
while((ans == 'y') || (ans == 'Y'));

return 0;
}

void testPFArrayD()
{
int cap;
cout << "Enter capacity of this super array: ";
cin >> cap;
PFArrayD temp(cap);

cout << "Enter up to " << cap << " nonnefative numbers.\n";

double next;
cin >> next;

try {
while ((next >= 0) ) //&& (!temp.full())
{
temp.addElement(next);
cin >> next;
}
}
catch ( OutOfRange &e ) {
cout << e.getMessage() << endl;
}

cout << "You entered the following "
<< temp.getNumberUsed() << " numbers:\n";
int index;
int count = temp.getNumberUsed();
for(index = 0; index < count+1; index++) {
try {
cout << temp[index] << " ";
} catch ( OutOfRange &e ) {
cout << e.getMessage() << " for indeks " << e.getIndex() << endl;
}
}
cout << endl;
cout << "(plus a sentinel value.)\n";
}
Kjøring:
This program tests the class PFArrayD.
Enter capacity of this super array: 2
Enter up to 2 nonnefative numbers.
1 4 5
Attempt to exceed capacity in PFArrayD.
You entered the following 2 numbers: 1 4
1 4 Illegal index in PFArrayD for indeks 2

(plus a sentinel value.)
Test again? (y/n)
y
Enter capacity of this super array: 3
Enter up to 3 nonnefative numbers.
5 2 52 23
Attempt to exceed capacity in PFArrayD.
You entered the following 3 numbers:
5 2 52 Illegal index in PFArrayD for indeks 3

(plus a sentinel value.)
Test again? (y/n)
n
Oppgave4amain.cpp
#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
#include <string>
#include <algorithm>


using namespace std;

int main() {

string aktuell, sokord;
ifstream les( "words.txt" );

string storstOrd;

//ofstream skriv( "likbakfrem.txt" );

while( !les.eof() ) {
getline(les, aktuell );

if( aktuell == "" )
continue;

if (aktuell.size() > 0 && aktuell[aktuell.size() - 1] == '\r')
aktuell.resize(aktuell.size() - 1);
if (aktuell.size() > 0 && aktuell[aktuell.size() - 1] == '\n')
aktuell.resize(aktuell.size() - 1);

aktuell[0] = tolower( aktuell[0] );

sokord = aktuell;
reverse (sokord.begin(), sokord.end());


ifstream sok( "words.txt" );

while( !sok.eof() ) {

getline( sok, aktuell );
aktuell[0] = tolower( aktuell[0] );

if (aktuell.size() > 0 && aktuell[aktuell.size() - 1] == '\r')
aktuell.resize(aktuell.size() - 1);
if (aktuell.size() > 0 && aktuell[aktuell.size() - 1] == '\n')
aktuell.resize(aktuell.size() - 1);



if( strcmp( aktuell.c_str(), sokord.c_str() ) == 0) {
aktuell[0] = toupper( aktuell[0] );

if( aktuell.size() > storstOrd.size() ) {
storstOrd = aktuell;
}
//skriv << aktuell << endl;
}
}



sok.close();

}

les.close();
//skriv.close();

cout << "Det største ordet som er lik fremlengs og baklengs er: " << storstOrd << " med en kolossal lengde på hele " << storstOrd.size() << endl;






return 0;

}
Kjøring:
Det største ordet som er lik fremlengs og baklengs er: Stressed med en kolossal lengde på hele 8
Oppgave4bFilmarkiv.h
#include <iostream>
#include <fstream>
#include <string>
#include <map>

#include "Film.h"

using namespace std;


class Filmarkiv {
public:
map<string, int> filmer;

Filmarkiv( string filnavn ) { lesInn( filnavn ); }
~Filmarkiv();
void skrivUt();

private:
Film** filmptrs;
int antallFilmer;

void lesInn( string filnavn );
};
Film.h
#include <string>

using namespace std;

class Film {
public:
Film( string filmnavn ) : navn( filmnavn ) { sumKarakterer=0; antKarakterer=0;}
string getNavn() { return navn; }
double getAvg() { return sumKarakterer/static_cast<double>(antKarakterer); }
void leggTilKarakter( int karakter ) { sumKarakterer+=karakter; antKarakterer++; }

private:
string navn;
int sumKarakterer;
int antKarakterer;

};
Filmarkiv.cpp
#include "Filmarkiv.h"

void Filmarkiv::lesInn( string filnavn ) {

ifstream leser( filnavn.c_str() );

if( leser.good() ) {

string asd; // temp

leser >> antallFilmer;
getline( leser, asd ); // pga carriage return og newline drit

filmptrs = new Film*[ antallFilmer ];

int atm = 0;

while( !leser.eof() || atm >= antallFilmer ) {

int karakter;
string navn;

getline( leser, navn );
leser >> karakter;
getline( leser, asd ); // pga carriage return og newline drit

//DBUG
//cout << "navn: " << navn << endl << "kar: " << karakter << endl;

if( filmer.find( navn ) == filmer.end() ) {
filmer.insert( pair<string,int>(navn, atm) );
filmptrs[atm] = new Film( navn );
atm++;
}

filmptrs[ filmer.find(navn)->second ]->leggTilKarakter( karakter );

}



}

}

Filmarkiv::~Filmarkiv() {

for( int i = 0; i < antallFilmer; i++ )
delete filmptrs[ i ];
delete [] filmptrs;

}

void Filmarkiv::skrivUt() {

map<string,int>::iterator it;
int old = cout.precision();
cout.precision(2);

for ( it=filmer.begin() ; it != filmer.end(); it++ )
cout << it->first << " ( " << filmptrs[ it->second ]->getAvg() << " )" << endl;

cout.precision( old );
}
main.cpp
#include <iostream>
#include <string>
#include "Filmarkiv.h"

using namespace std;

int main() {

Filmarkiv filmer("filmer.txt");

filmer.skrivUt();


return 0;

}
Kjøring:
filmen ( 3.7 )
filmto ( 5 )
filmtre ( 2 )