Skip to content

Ringpuffer - const #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Compiled Object files
*.slo
*.lo
*.o

# Compiled Dynamic libraries
*.so

# Compiled Static libraries
*.lai
*.la
*.a

# Output files
*.exe
*.d

#Eclipse Folder
.settings
Debug
.cproject
.project
135 changes: 135 additions & 0 deletions Ringpuffer/ringpuffer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
#include <iostream>

using namespace std;

class Ringpuffer {
public:
bool push(double newelement);
double pop();
double element(int number) const;
Ringpuffer(int size);
~Ringpuffer();

private:
int NumberOfElements;
int ElementsInUse;
double* ptr;
double* pushptr;
double* popptr;
};


Ringpuffer::Ringpuffer(int size):NumberOfElements(size) {
ptr=new double[NumberOfElements];
pushptr=ptr;
popptr=ptr;
ElementsInUse=0;
}

bool Ringpuffer::push(double newelement)
{
if (ElementsInUse>=NumberOfElements) //Zu viele Elemente
{
return 0;
}

*pushptr=newelement; //Element schreiben

if (pushptr==ptr+NumberOfElements-1) //�berpr�fen, ob letztes Element
{
pushptr=ptr; //Zur�ck zum 1. Element
}
else
{
pushptr++; //Weiterspringen falls nicht am Ende
}

ElementsInUse++; //Erh�he Zahl der Elemente
return 1;
}

double Ringpuffer::pop()
{
if (ElementsInUse==0)
{
return 0;
}

double returnvalue=*popptr;

if (popptr==ptr+NumberOfElements-1) //�berpr�fen, ob letztes Element
{
popptr=ptr; //Zur�ck zum 1. Element
}
else
{
popptr++; //Weiterspringen falls nicht am Ende
}

ElementsInUse--; //Senke Zahl der Elemente
return returnvalue;
}


double Ringpuffer::element(int number) const
{
if ((number<0)||(number>=NumberOfElements))
{
return 0;
}
return *(ptr+number);
}


Ringpuffer::~Ringpuffer() {
delete[] ptr;
}


int main()
{
int size;
cout << "Bitte Groesse des Puffers angeben: " << flush;
cin >> size;
Ringpuffer puffer(size);


int s=0;
while (s!=4)
{
cout << "push=>1 pop=>2 read=>3 quit=>4 " << flush;
cin >> s;

if (s==1) //push
{
double add;
cout << "Zahl eingeben: " << flush;
cin >> add;
if (puffer.push(add)==1)
{
cout << "Zahl erfolgreich hinzugefuegt" << endl;
}
else
{
cout << "Zahl konnte nicht hinzugefuegt werden" << endl;
}
}

if (s==2) // pop
{
cout << "Die Zahl ist " << puffer.pop() << " und wurde geloescht" << endl;
}

if (s==3) //read
{
int n;
cout << "Element eingeben (zwischen 0 und " << size-1 << "): " << flush;
cin >> n;
cout << "Der Zahl ist " << puffer.element(n) << endl;
}

cout << endl;
}

return 0;
}