Może inaczej, mam swój kod z lista dynamiczna...
#include <iostream>
#include <time.h>
#include <stdlib.h>
#include <iomanip>
using namespace std;
#define LICZBA 5
//template<typename T>
//class Kalkulator
//{
// enum { ssize = 100 };
// T stack[ssize];
// int top;
//public:
// Kalkulator() : top(0) {}
// void push(T i) {
// assert(top < ssize); stack[top++] = i;
// };
// T pop() {
// assert(top > 0); return stack[--top];
// };
// int rozmiar() { return top; };
//
// friend class iterator;
//
// double objetosc2(listConten<double> &p, listConten<double> &d)
// {
// listConten<double>::iterator i1 = p.begin();
// listConten<double>::iterator i2 = p.end();
// listConten<double>::iterator i3 = d.begin();
// trensformuj(i1, i2, i3);
// }
//};
template <typename TypDanych>
class listConten {
struct Node {
TypDanych wartosc;
Node * poprz;
Node * nast;
};
Node * head;
Node * tail;
public:
listConten() : head(NULL), tail(NULL) {};
bool isEmpty() {
if (head == NULL)
return true;
else
return false;
}
void push(TypDanych element) {
if (head != NULL) {
Node * temp = new Node;
temp->wartosc = element;
temp->poprz = tail;
temp->nast = NULL;
tail->nast = temp;
tail = temp;
}
else {
Node * temp = new Node;
temp->wartosc = element;
temp->poprz = NULL;
temp->nast = NULL;
head = temp;
tail = head;
}
}
void display() {
if (!isEmpty()) {
Node * current = tail;
cout << "Zawartosc listy: \n";
while (current != NULL) {
cout << setprecision(3) <<current->wartosc << endl;
current = current->poprz;
}
}
else cout << "Pusta lista." << endl;
cout << endl;
}
void pop() {
if (!isEmpty()) {
Node * tmp = tail->poprz;
delete tail;
tail = tmp;
tail->nast = NULL;
}
else cout << "Nie ma zadnych elemntow w liscie.\n";
}
class iterator {
Node * listelement;
public:
iterator(Node * _listelement) : listelement(_listelement) {};
iterator(const iterator& x) : listelement(x.listelement) {};
iterator& operator=(const iterator& second)
{
listConten = second.listelement; return *this;
}
iterator& operator++()
{
listelement = listelement->nast;
return *this;
}
iterator operator++(int)
{
iterator tmp(*this); listelement = listelement->nast; return tmp;
}
TypDanych & operator*() { return listelement->wartosc; }
bool operator==(const iterator& x) const
{
return listelement == x.listelement;
}
bool operator!=(const iterator& x) const
{
return listelement != x.listelement;
}
};
iterator begin() { return iterator(head); };
iterator end() { return iterator(NULL); };
};
double randdouble()
{
return rand()%10 + rand() / (double(RAND_MAX) + 10);
}
template <typename iter>
void transformuj(iter start1, iter end1, iter start2)
{
while (start1 != end1) {
if (*start1< *start2) {
*start1 = *start1 + *start2;
*start2 = *start1 - *start2;
*start1 = *start1 - *start2;
/*double temp = *start1;
*start1 = *start2;
*start2 = temp;*/
}
start1++;
start2++;
}
}
template <typename iter>
void roznicuj(iter start1, iter end1, iter start2)
{
while (start1 != end1) {
*start1 = *start1 - *start2;
start1++;
start2++;
}
}
template <typename iter>
void objetosc(iter start1, iter end1, double & a)
{
while (start1 != end1) {
a = a*(*start1);
start1++;
}
}
int main()
{
srand(time(NULL));
listConten<double> p_values;
listConten<double> q_values;
//Kalkulator<double> K;
for (int i = 0; i < LICZBA; i++) {
p_values.push(randdouble());
q_values.push(randdouble());
}
cout << "Zawartosc 1 kontenera." << endl;
p_values.display();
cout << "Zawartosc 2 kontenera." << endl;
q_values.display();
listConten<double>::iterator i1 = p_values.begin();
listConten<double>::iterator i2 = p_values.end();
listConten<double>::iterator i3 = q_values.begin();
transformuj(i1, i2, i3);
cout << "Zawartosc 1 kontenera po transforamcji." << endl;
p_values.display();
cout << "Zawartosc 2 kontenera po transforamcji." << endl;
q_values.display();
roznicuj(i1, i2, i3);
cout << "Zawartosc 1 kontenera po roznicowaniu." << endl;
p_values.display();
cout << "Zawartosc 2 kontenera po roznicowaniu." << endl;
q_values.display();
double a = 1;
objetosc(i1, i2, a);
cout << "Objetosc wynosi: " << a << endl;
cout << "----------------------------" << endl;
//K.objetosc2(p_values, q_values);
return 0;
}
I do tego musze zrobić to : (załącznik chyba)