Hallo Community,
folgender Quellcode soll mit alleiniger Nutzung des Templates queue der STL
und der dazugehörigen Methoden (front(), push(), pop(), empty(), size(), ...)
implementiert werden.
ich hab jetzt das Template so eingebunden...
Die Klasse elem wird jetzt sicher überflüssig ...
Allerdings habe ich keine Ahnung wie ich weiter vorgehen muss, kann ich die klasse queue im Grunde so lassen und muss nur die Operatoren überladen?
hat vllt jemand nen Tipp für Buchmaterial, Google liefert viel, aber auch sehr viel Schrott ;/
(Arbeite momentan mit Data Becker c++ Auflage 1999, an sich nicht schlecht, aber doch nicht aktuell)
folgender Quellcode soll mit alleiniger Nutzung des Templates queue der STL
und der dazugehörigen Methoden (front(), push(), pop(), empty(), size(), ...)
implementiert werden.
ich hab jetzt das Template so eingebunden...
Code:
template < class T, class Container = deque<T> > class queue;
Die Klasse elem wird jetzt sicher überflüssig ...
Allerdings habe ich keine Ahnung wie ich weiter vorgehen muss, kann ich die klasse queue im Grunde so lassen und muss nur die Operatoren überladen?
hat vllt jemand nen Tipp für Buchmaterial, Google liefert viel, aber auch sehr viel Schrott ;/
(Arbeite momentan mit Data Becker c++ Auflage 1999, an sich nicht schlecht, aber doch nicht aktuell)
Code:
#include "stdafx.h"
#include <iostream>
#include <math.h>
#include <iomanip>
#include <string>
#define N 3
using namespace std ;
class prod
{
private:
string pn;
double preis;
unsigned long anz ;
public:
prod(string,double,unsigned long);
~prod();
void ein();
void out();
double get_preis() {return preis;} //durch const ist zzr schreibgeschützt
unsigned long get_anz() {return anz;}
string get_pn() {return pn;}
void set_pn(string pn = "") {this->pn = pn ;}
void set_preis(double preis) {this->preis = preis;}
void set_anz (unsigned long anz) {this->anz = anz;}
};
prod::prod(string pn="",double preis=0.0, unsigned long anz=0ul):pn(pn),preis(preis),anz(anz)
{
cout<<"Produktname: "<<pn<<endl;
cout<<"Preis: "<<preis<<endl;
cout<<"Anzahl: "<<anz<<endl;
}
void prod::out()
{
cout<< "\n\nWerte von prod\n";
cout<<"Produktname: "<<pn<<endl;
cout<<"Preis: "<<preis<<endl;
cout<<"Anzahl: "<<anz<<endl;
}
prod::~prod()
{
cout<< "\n\nDestruktor\n";
cout<<"Produktname: "<<pn<<endl;
cout<<"Preis: "<<preis<< endl;
cout<<"Anzahl: "<<anz<<endl;
}
void prod::ein () { //Eingaben
cout<< "Eingabe Produktname: ";
getline(cin, pn);
do { //Eingabe preis + abfangen von fehleingaben
cout<< "Preis = "; cin>>preis;
if(cin.eof()) break;
if(cin.fail() || (cin.peek() != '\n')) {
cin.clear(); cin.ignore (INT_MAX, '\n');
}
break;
} while (preis <=0.0f);
if(cin.eof()) { cin.clear(); cout <<"eof\n"; }
else {
cin.clear (); cin.ignore(INT_MAX, '\n');
}
do { //Eingabe Anzahl + abfangen von fehleingaben
cout<< "Anzahl = "; cin>>anz;
if(cin.eof()) break;
if(cin.fail() || (cin.peek() != '\n')) {
cin.clear(); cin.ignore (INT_MAX, '\n');
}
break;
}while(anz <= 0.0f);
if(cin.eof()) { cin.clear(); cout <<"eof\n"; }
else {
cin.clear (); cin.ignore(INT_MAX, '\n');
}
}
class Queue
{
//#### Innere klasse Elem ####
class elem
{
public:
prod p; //Instanz von p
elem *next; //Zeiger auf nächstes listen element
elem(prod p): p(p), next(0){} //Standard Konstruktor für initialisierung p vom typ prod und next 0
};
// Alles was auf Elem zugreift ->
elem *first;
elem *last;
public:
Queue():last(0), first(0) {} //Konstruktor zeiger vom typ elem wird 0 initialisiert und zeiger last vom typ elem ebenfalls 0
~Queue(){
elem *tmp=first;
while(first){
tmp=tmp->next;
delete first;
first=tmp;
}
first = 0;
last = 0;
}
void enqueue(prod p) //Methode um elemente hinzuzufügen
{
elem *tmp = new elem(p); //Neue instanz wird tmp zugewiesen
if(empty()){ //prüfen ob kette leer
first=tmp; //Danninstanz auf erstes element sonst ->
last=first;
}
else { //an letztes element hängen und zeiger ein element weiter setzen
last->next=tmp;
last=tmp;
}
}
bool empty(){ //Prüfe ob first leer
return first==0;
}
unsigned long anzahl()
{
elem *tmp = first;
int anz = 0;
while(tmp) {
tmp=tmp->next;
anz+=1;
}
return anz;
}
prod dequeue()
{
elem *tmp = first;
if(empty())
return 0;
first = first->next;
prod p = tmp->p;
delete tmp;
tmp=0;
return p;
}
void print_queue() {
elem *tmp = first;
while(tmp) { //Solange tmp != 0
tmp->p.out();
tmp=tmp->next;
}
}
};
void main()
{
prod p("H20", 4.1, 20);
Queue q;
q.enqueue(p);
while(1){
p.ein();
if(p.get_pn()=="")break;
q.enqueue(p);
}
cout<<"Anzahl = "<<q.anzahl()<<endl;
q.print_queue();
while(!q.empty()){
p=q.dequeue();
p.out();
}
cin.get();
system("PAUSE");
}