Docsity
Docsity

Prepara tus exámenes
Prepara tus exámenes

Prepara tus exámenes y mejora tus resultados gracias a la gran cantidad de recursos disponibles en Docsity


Consigue puntos base para descargar
Consigue puntos base para descargar

Gana puntos ayudando a otros estudiantes o consíguelos activando un Plan Premium


Orientación Universidad
Orientación Universidad


2n Parcial Laboratori de Programació 2017-18, Apuntes de Ingeniería Infórmatica

Asignatura: Laboratori de Programació, Profesor: , Carrera: Enginyeria Informàtica, Universidad: UAB

Tipo: Apuntes

2017/2018

Subido el 09/02/2018

usuario desconocido
usuario desconocido 🇪🇸

1 / 7

Toggle sidebar

Esta página no es visible en la vista previa

¡No te pierdas las partes importantes!

bg1
Examen LP 2n parcial 2017-18
Nom……………………………………………. Cognoms………………………………………………………………………….…
NIU……………………………………………… Grup(41(11-13,43(9-11),45,51,52(17-19)……………………………
0
1
3
13
14
4
2
10
6
7
8
9
11
12
Exercici 1. (3 punts) Donat un arbre binari:
template <class T>
class BTree {
public:
BTree();
BTree(const BTree<T>& t);
virtual ~BTree();
virtual BTree<T>* newTree(T* pdata);
virtual BTree<T>& operator=(const BTree<T>& t);
virtual void read(string nomFitxer);
void print();
bool isLeave() const { return ((m_left == NULL) && (m_right == NULL)); }
bool isEmpty() const { return (m_data == NULL); }
virtual BTree<T>* getRight() { return m_right; }
virtual BTree<T>* getLeft() { return m_left; }
virtual BTree<T>* getFather(){return m_father;}
virtual T& getData() { return (*m_data); }
bool isLeftSon() { return (m_father->m_left == this); }
bool isRightSon(){ return (m_father->m_right == this); }
void eliminaLeft() {if (m_left != NULL) delete m_left; m_left = NULL;}
void eliminaRight() {if (m_right != NULL) delete m_right; m_right = NULL;}
protected:
BTree<T>* m_left;
BTree<T>* m_right;
BTree<T>* m_father;
T* m_data;
virtual void readTreeRec(ifstream& fitxerNodes, int h, BTree<T>* father);
virtual void readInfoNode(ifstream& fitxerNodes);
void printArbreRec(int n);
virtual void printInfoNode();
};
Implementa un mètode recursiu que imprimeixi el recorregut de l’arbre en amplada, és a dir,
per nivells. Per exemple, si l’arbre és:
El seu recorregut serà:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
pf3
pf4
pf5

Vista previa parcial del texto

¡Descarga 2n Parcial Laboratori de Programació 2017-18 y más Apuntes en PDF de Ingeniería Infórmatica solo en Docsity!

Nom……………………………………………. Cognoms………………………………………………………………………….…

NIU……………………………………………… Grup(41(11-13,43(9-11),45,51,52(17-19)……………………………

Exercici 1. (3 punts) Donat un arbre binari:

template class BTree { public: BTree(); BTree(const BTree& t); virtual ~BTree(); virtual BTree* newTree(T* pdata); virtual BTree& operator=(const BTree& t); virtual void read(string nomFitxer); void print(); bool isLeave() const { return ((m_left == NULL) && (m_right == NULL)); } bool isEmpty() const { return (m_data == NULL); } virtual BTree* getRight() { return m_right; } virtual BTree* getLeft() { return m_left; } virtual BTree* getFather(){return m_father;} virtual T& getData() { return (m_data); } bool isLeftSon() { return (m_father->m_left == this); } bool isRightSon(){ return (m_father->m_right == this); } void eliminaLeft() {if (m_left != NULL) delete m_left; m_left = NULL;} void eliminaRight() {if (m_right != NULL) delete m_right; m_right = NULL;} protected: BTree m_left; BTree* m_right; BTree* m_father; T* m_data; virtual void readTreeRec(ifstream& fitxerNodes, int h, BTree* father); virtual void readInfoNode(ifstream& fitxerNodes); void printArbreRec(int n); virtual void printInfoNode(); };

Implementa un mètode recursiu que imprimeixi el recorregut de l’arbre en amplada, és a dir,

per nivells. Per exemple, si l’arbre és:

El seu recorregut serà:

Nom……………………………………………. Cognoms………………………………………………………………………….…

NIU……………………………………………… Grup(41(11-13,43(9-11),45,51,52(17-19)……………………………

Pista : necessitaràs una funció auxiliar que faci la primera crida a la funció recursiva. Si ho

necessites pots utilitzar una cua per a la implementació de l’exercici:

template class Cua { public: Cua(); Cua(const Cua& c); ~Cua(); bool esBuida() const; Node* inserir(const TDada& valor); void treure(); Node* primer(); Cua& operator=(Cua& c); private: Node* m_primer; Node* m_ultim; };

SOLUCIÓ

template void BTree::ampladaRec() { Cua< BTree* > cNodes; if (m_data != NULL) { cout<< (m_data) << endl; if (m_left != NULL) cNodes.inserir(m_left); if (m_right != NULL) cNodes.inserir(m_right); ampRec(cNodes); } else cout<<"arbre buit"<<endl; } template void BTree::cRec(Cua< BTree >& cNodes) { if (!cNodes.esBuida()) { BTree* pNodeAct; pNodeAct = cNodes.primer()->getValor(); cout << pNodeAct->getData() << endl; if (pNodeAct->getLeft() != NULL) cNodes.inserir(pNodeAct->getLeft()); if (pNodeAct->getRight() != NULL) cNodes.inserir(pNodeAct->getRight()); cNodes.treure(); cRec(cNodes); } } class Node {public:Node(); ~Node(); Node* getNext(); TDada& getValor(); Node* getNode(); Node& operator=(const Node& n); ... private: TDada m_valor; Node* m_next; };

Nom……………………………………………. Cognoms………………………………………………………………………….…

NIU……………………………………………… Grup(41(11-13,43(9-11),45,51,52(17-19)……………………………

Exercici 3. (3 punts) Volem crear un mètode a la classe Graf per determinar si dos nodes són

veïns o no. Per fer-ho tenim definides les classes: Graf, InfoNodeGraf,

InfoArestaGraf, Llista i Node tal com les hem vist a classe (mostrades a continuació).

bool sonVeins(Node<InfoNodeGraph<T1, T2>>* pnode1,

Node<InfoNodeGraph<T1, T2>>* pnode2) ;

SOLUCIO

class Node {public:Node(); ~Node(); Node* getNext(); TDada& getValor(); Node* getNode(); Node& operator=(const Node& n); ... private: TDada m_valor; Node* m_next; }; template <class T1, class T2> class InfoArestaGraph {public:InfoArestaGraph(); ~InfoArestaGraph(); ... Node<InfoNodeGraph<T1,T2>>* getAdjacent(); ... private: T2 m_valor; Node<InfoNodeGraph<T1,T2>>* m_nodeAdjacent; } template <class T1, class T2> class InfoNodeGraph {public:InfoNodeGraph(); ~InfoNodeGraph(); Node<InfoArestaGraph<T1,T2>>* getFirstAdjacent(); Node<InfoArestaGraph<T1, T2>>* getLastAdjacent(); EstatNode getEstat(); int getPosicio(); ... private: T1 m_valor; int m_posicio; EstatNode m_estat; Llista<InfoArestaGraph<T1,T2>> m_adjacents; }; template template class Llista {public:Llista(); Llista(const Llista& l); ~Llista(); bool esBuida() const ; Node* getInici() const; Node* getFi() const; ... private: Node* m_primer; Node* m_ultim; }; template <class T1, class T2> class Graph { public:Graph(); ~Graph(); ... private: Llista<InfoNodeGraph<T1,T2>> m_nodes; int m_numNodes; int m_numArestes; T2 m_valorArestaInfinit; void setIndexNoVisitat(); void setNoVisitat(); };

Nom……………………………………………. Cognoms………………………………………………………………………….…

NIU……………………………………………… Grup(41(11-13,43(9-11),45,51,52(17-19)……………………………

template <class T1, class T2> bool Graph<T1, T2>::sonVeins(Node<InfoNodeGraph<T1, T2>>* pnode1, Node<InfoNodeGraph<T1, T2>>* pnode2) { Node<InfoArestaGraph<T1, T2>>* pArestaAct = pnode1->getValor().getFirstAdjacent(); bool trobat = false; while ((!trobat)&&(pArestaAct != NULL)) { if(pArestaAct->getValor().getAdjacent()==pnode2) { trobat = true; } else { pArestaAct = pArestaAct->getNext(); } } return trobat; }

Nom……………………………………………. Cognoms………………………………………………………………………….…

NIU……………………………………………… Grup(41(11-13,43(9-11),45,51,52(17-19)……………………………

Python is an easy to learn, powerful programming language. It is

Python is an easy to learn, powerful programming language. It is

SOLUCIO

def cercaParaulaSimple(paraula, index, nomFitxer):

if paraula not in index:

print "Error. Paraula inexistent"

else:

fitxer = open(nomFitxer, "rt")

linies = fitxer.readlines()

valor = index[paraula]

for linia in valor:

print linies[linia]