/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/*
* File: classe lista dinamica
* Author: alessandrobarazzuol
*
* Created on 26 gennaio 2020, 16.53
*/
#include <cstdlib>
#include<iostream>
#include<ctime>
#include<string.h>
using namespace std;
template <class T>
class object
{
public:
object(){}
/*caratteristica dell'oggetto*/
T val;
object *next;
/*ritorna la caratteristica dell'oggetto*/
virtual T get_val()
{
return val;
};
};
/*crea la classe lista*/
template <class T>
class lista
{
public:
int lunghezza;
object<T> *o;
lista()
{
o=nullptr;
lunghezza=0;
}
void push(object<T> *obj)
{
lunghezza++;
obj->next=nullptr;
if(o==nullptr)
{
o=obj;
}
else
{
object<T> *temp=o;
o=obj;
obj->next=temp;
}
}
void ordina()
{
int css=1;
object<T> *temp=nullptr;
object<T> *aux=nullptr;
object<T> *app=new object<T>;
while(css)
{
css=0;
aux=o;
temp=o->next;
while(temp!=nullptr)
{
if(aux->get_val()>temp->get_val())
{
app->val=aux->val;
aux->val=temp->val;
temp->val=app->val;
css=1;
}
aux=temp;
temp=temp->next;
}
}
}
void print()
{
cout<<endl;
object<T> *temp=o;
while(temp!=nullptr)
{
cout<<" "<<temp->get_val();
temp=temp->next;
}
cout<<endl;
}
};
class rettangolo:public object<int>
{
public:
rettangolo(int a,int b)
{
val=a*b;
}
int get_val()
{
return val;
}
};
class interi:public object<int>
{
public:
interi(int a)
{
val=a;
}
int get_val()
{
return val;
}
};
class carattere:public object<char>
{
public:
carattere(char a)
{
val=a;
}
char get_val()
{
return val;
}
};
int main(int argc, char** argv) {
srand(time(0));
/*oggetto generalizzato dal quale derivano tutti gli oggetti che andrò a creare*/
object<int> *o=nullptr;
/*creo una lista di rettangoli*/
lista<int> *li=new lista<int>;
for(int i=0;i<5;i++)
{
int a=rand()%10;
int b=rand()%20;
o=new rettangolo(a,b);
li->push(o);
}
li->print();
li->ordina();
li->print();
delete o,li;
/*rinizializzo o*/
o=nullptr;
/*creo una lista di interi*/
li=new lista<int>;
for(int i=0;i<5;i++)
{
int a=rand()%10;
o=new interi(a);
li->push(o);
}
li->print();
li->ordina();
li->print();
delete o,li;
/*rinizializzo o*/
o=nullptr;
li=nullptr;
/*creo una lista di caratteri*/
lista<char> *lic=new lista<char>;
object<char> *ob=nullptr;
for(int i=0;i<5;i++)
{
char b=(char)(rand()%26+65);
ob=new carattere(b);
lic->push(ob);
}
lic->print();
lic->ordina();
lic->print();
return 0;
}
//out
48 66 35 105 2
2 35 48 66 105
3 4 7 7 2
2 3 4 7 7
M O T Z Z
M O T Z Z
Post Views: 815