Opgave om dynamisk array C++
Hver eneste linie i koden skal kommenteres:sender cpp filen:
#include <iostream>
#include <string>
#include "Array.h"
using namespace std;
Dynaarray::Dynaarray(int s)
{
// Memory allocation for Dynamic Array in the heap.
anArray = new double[s];
for(int i =0; i <s;i++)
{
anArray[i]=NULL;
}
counter = 0;
num = s;
}
// array element added to the end of the container
void Dynaarray::push_back(itemType item)
{
// The temp has type int and has been declared -1.
int temp=-1;
if (counter >= num)
{
/* An array name is a pointer to the array’s first element
* The size of a dynamically allocated array can be increased
*/
double *original = anArray;
// Save old
double oldSize = num;
num *= 2;
anArray = new double[num];
for(int i =0; i <num;i++)
{
anArray[i]=NULL;
}
for ( int j = 0; j < oldSize; ++j )
anArray[j] = original[j];
// Release memory of Dynamic Array
delete [ ] original;
}
for (int i =0; i<num;i++)
{
if (anArray[i] !=NULL)
{
temp = i;
}
}
anArray[temp+1]= item;
// increments the sizeof int
counter++;
}
itemType Dynaarray::pop_back(){
if (counter != 0)
{
int temp=0;
double temp2;
for (int i =0; i<num;i++)
{
if (anArray[i] !=NULL)
{
temp = i;
}
}
temp2 = anArray[temp];
anArray[temp]=NULL;
counter--;
return temp2;
}
else
return NULL;
}
void Dynaarray::insert(itemType item, int pos)
{
if (pos < num)
{
if (counter >= num || pos == num-1)
{
double *original = anArray;
double oldSize = num;
num *= 2;
anArray = new double[num];
for(int i =0; i <num;i++)
{
anArray[i]=NULL;
}
for ( int j = 0; j < oldSize; ++j )
anArray[j] = original[j];
delete [ ] original;
}
for (int i = num;i>pos;i--)
{
anArray[i]= anArray[i-1];
}
anArray[pos]=item;
counter++;
}
}
/*
* Check whether the array is empty or not.
*/
itemType Dynaarray::erase(int pos)
{
if(pos <= num)
{
int temp=-1;
int temp2;
for (int i =0; i<num;i++)
{
if (anArray[i] !=NULL)
{
temp = i;
}
}
anArray[pos]=NULL;
for (int i =pos+1;i<=temp;i++)
{
anArray[i-1]= anArray[i];
}
anArray[temp]= NULL;
counter--;
return 1;
}
else
return 0;
}
void Dynaarray::clear(){
for (int i =0; i<counter;i++)
{
anArray[i]= NULL;
}
counter = 0;
}
int Dynaarray::size() const{
return num;
}
bool Dynaarray::empty() const{
if (counter == 0)
{
return true;
}
else
return false;
}
itemType & Dynaarray::at(int pos) const{
if (pos < num)
{
if (anArray[pos] != NULL)
return anArray[pos];
}
}
int main()
{
char c;
Dynaarray *ar = new Dynaarray();
cout<<" Total size "<<ar->size()<<endl;
ar->push_back(40);
ar->push_back(20);
cout<<"This is test 1"<<endl;
cout<<"position 0 "<<ar->at(0)<<endl;
cout<<"position 1 "<<ar->at(1)<<endl;
cout<<"This is test 2"<<endl;
cout<<" Total size "<<ar->size()<<endl;
ar->pop_back();
cout<<"This is test 3"<<endl;
cout<<"position 0 "<<ar->at(0)<<endl;
cout<<"position 1 "<<ar->at(1)<<endl;
ar->insert(60,1);
ar->insert(70.5,1);
ar->insert(75,1);
cout<<"This is test 4"<<endl;
cout<<" position 0 "<<ar->at(0)<<endl;
cout<<" position 1 "<<ar->at(1)<<endl;
cout<<"position 2 "<<ar->at(2)<<endl;
cout<<"position 3 "<<ar->at(3)<<endl;
ar->erase(2);
cout<<"This is test 5"<<endl;
cout<<"position 0 "<<ar->at(0)<<endl;
cout<<"position 1 "<<ar->at(1)<<endl;
cout<<"position 2 "<<ar->at(2)<<endl;
cout<<"position 3 "<<ar->at(3)<<endl;
cin>>c;
return 0;
}
ostream &operator<<( ostream &Out, const Dynaarray &rhs ) {
Out << "";
return Out;
}