L'esercizio l'ho svolto in C++ con le classi e con le
liste dinamiche:
classe.h
#ifndef __CPCINFO__
#define __CPCINFO__
class MPCList;
class PCList
{
public:
PCList(char *name, char *proc, float prezzo);
~PCList();
private:
PCList* next;
PCList* prev;
char* m_name;
char* m_proc;
float m_prezzo;
friend MPCList;
};
class MPCList
{
public:
MPCList();
~MPCList();
void Insert(char* name,char* proc, float prezzo);
void Print();
void ViewProc(char* proc);
int count();
private:
PCList* list;
void toLast();
void toFirst();
};
#endif
class.cpp //corpo della classe
#include <iostream>
#include <cstring>
#include "pcinfo.h"
using namespace std;
MPCList::MPCList()
{
list =0;
}
MPCList::~MPCList()
{
}
PCList::PCList(char *name, char *proc, float prezzo)
{
m_name = new char[strlen(name)];
m_proc = new char[strlen(proc)];
strcpy(m_name,name);
strcpy(m_proc,proc);
m_prezzo = prezzo;
next=prev=NULL;
}
PCList::~PCList()
{
delete next;
delete prev;
}
void MPCList::Insert(char *name, char *proc, float prezzo)
{
PCList* p=0;
while(list)
{
p=list;
list = list->next;
}
if(p)
{
list = p;
list->next =new PCList(name,proc,prezzo);
list = list->next;
list->prev = p;
}
else
list = new PCList(name,proc,prezzo);
}
void MPCList::Print()
{
PCList*p=0;
PCList*list_a=0;
toFirst();
while(list)
{
if(list->next)
list_a = list->next;
else
break ;
while(list_a)
{
if(list->m_prezzo>list_a->m_prezzo)
{
char *n,*pc;
int pr;
n = new char[strlen(list->m_name)];
pc = new char[strlen(list->m_proc)];
strcpy(n,list->m_name);
strcpy(pc,list->m_proc);
pr = list->m_prezzo;
strcpy(list->m_name,list_a->m_name);
strcpy(list->m_proc,list_a->m_proc);
list->m_prezzo = list_a->m_prezzo;
strcpy(list_a->m_name,n);
strcpy(list_a->m_proc,pc);
list_a->m_prezzo = pr;
}
list_a = list_a->next;
}
list = list->next;
}
toFirst();
cout << "ID |\t" << "Nome\t" << "Processore\t" << "Prezzo" << endl;
cout << "-----------------------------------------------------" << endl;
int id=1;
while(list)
{
p = list;
cout << id++ << " |\t";
cout << list->m_name << "\t";
cout << list->m_proc << "\t\t";
cout << list->m_prezzo << endl;
list = list->next;
}
list = p;
}
void MPCList::ViewProc(char* proc)
{
PCList* tmp=0;
PCList* p=0;
cout << "ID |\t" << "Nome\t" << "Processore\t" << "Prezzo" << endl;
cout << "-----------------------------------------------------" << endl;
int id=0;
toFirst();
while(list)
{
p = list;
id++;
if(!strcmp(list->m_proc,proc))
{
cout << id << " |\t";
cout << list->m_name << "\t";
cout << list->m_proc << "\t\t";
cout << list->m_prezzo << endl;
}
list = list->next;
}
list = p;
}
void MPCList::toLast()
{
PCList*p=0;
while(list)
{
p = list;
list = list->next;
}
list = p;
}
void MPCList::toFirst()
{
PCList*p=0;
while(list)
{
p = list;
list = list->prev;
}
list = p;
}
la main che richiama la classe:
#include "class.h" //header file dove sono dichiarate le classi
#include <cstring>
int main()
{
MPCList a;
char num[256]={"tt"};
char proc[256]={"tyu"};
a.Insert(num,proc,1256);
strcpy(num,"art");
strcpy(proc,"Corel7");
a.Insert(num,proc,7521);
strcpy(num,"efxc");
strcpy(proc,"dfrfg");
a.Insert(num,proc,1154);
strcpy(num,"sfc");
strcpy(proc,"Corel7");
a.Insert(num,proc,4528);
strcpy(num,"rdgd");
strcpy(proc,"Corel7");
a.Insert(num,proc,112);
a.Print();
a.ViewProc(proc);
return 1;
}
dovrebbe funzionare.