All pastes #179040 Raw Edit

new

public cpp v1 · immutable
#179040 ·published 2006-09-21 20:59 UTC
rendered paste body
#include <iostream>#include <fstream>#include <vector>#include <string>#include <cctype>#include <iomanip>//#include "alpha.h"// TODO Tänk på...using namespace std;class List_Node ;class List{public:  //  List_Node *p;       ////// TODO: Pekaren ska vara DOLD!  List() : p(0) { cout << "En lista" << endl; }  List& operator=(List rhs);  ~List();  void insert(string ny_text);  void print(char direction);  void reverse();  void reverse_print(List_Node *first);  void set_list_pointer(List_Node *new_pointer);  List_Node *empty()	  {		 if(p) return p;		 return 0;	  }private:  List_Node *p;       ////// TODO: Pekaren ska vara DOLD!};void List::set_list_pointer(List_Node *new_pointer){  p = new_pointer;}class List_Node{public:  List_Node(string ny_text,List_Node *gammal_p); // : text(ny_text), p(0) {}  List_Node(List_Node *gammal_p, List_Node *ny_p);  List_Node& operator=(List_Node rhs);  ~List_Node();  string text;  void print(char direction);  void point_at(List_Node *self, List *list_pointer);  void reverse_print(List_Node *first);private:  //  string text;  List_Node *p;};List_Node::List_Node(string ny_text, List_Node *gammal_p){  text = ny_text;  p = gammal_p;  cout << "Element med texten " << ny_text << " har skapats." << endl;}List_Node::~List_Node(){  cout << text << " förstördes!";  delete p;}void List_Node::point_at(List_Node *self, List *list_pointer){  cout << text << " anmäler sig..." << endl;  if(p==0)  {    cout << "Plopp";     list_pointer->set_list_pointer(this);    p = self;    return;  }  cout << "Nu är det " << this->text << " tur." << endl;  p->point_at(this, list_pointer);  p = self;}void List::insert(string ny_text){  p = new List_Node(ny_text, p);}void List_Node::print(char direction){  if(direction=='R')  {    cout << endl << "Hej, jag heter " << this->text; // this onödig    if(p) p->print(direction);  }   else // // Gör rekusrsiv  {     if(p) p->print(direction);    cout << endl << "Hej, jag heter " << this->text;  }}void List_Node::reverse_print(List_Node *first){  if(first)    {      reverse_print(first->p);      cout << first->text << endl;    }}void List::reverse_print(List_Node *first){      reverse_print(p);}void List::print(char direction){  if(direction != 'R' && direction != 'O')   {    cout << "Du måste ange riktning. (R)ätt (O)mvänd." << endl;  }  else if(direction=='R')  {    cout << "Skriver ut lista:" << endl;    if(p) p->print(direction);  }   else if(direction=='O')  {    if(p) p->reverse_print(p);  }}List::~List(){  // Tag död på alla andra element först  cout << "Listan förstördes" << endl;  delete p;}void List::reverse(){  cout << "Tjockis!" << endl;  if(p) p->point_at(0,this);}List_Node& List_Node::operator=(List_Node rhs){  cout << "Lista_nod kopieras: " << &p << " till: " << &rhs << endl;  /*  if(p)     {     List_Node *nod_pekare;    *nodpekare = p;    rhs::set_list_pointer(nod_pekare);        }*/}List& List::operator=(List rhs){  cout << "Lista kopieras: " << &rhs << " till: " << &p << endl;  if(rhs.empty())     {      cout << "p != 0";     List_Node *nod_pekare = new List_Node(p->text,nod_pekare);    // List_Node(ny_text, p);    nod_pekare = p;    //    rhs->set_list_pointer(nod_pekare);      }  return *this;}int main (int argc, char *argv[]){  List *en_lista = new List;  /*  List_Node *fisk = new List_Node("fisk",0);    en_lista->insert("Bengan");  en_lista->insert("Totte");  en_lista->insert("Cebra");  en_lista->insert("Leif");  en_lista->print('O');  en_lista->reverse();  en_lista->print('O');  delete en_lista;  delete fisk;*/   ifstream listfilen;      if(argc != 2)   {	  cout << "Du måste ange vad listan heter, ex lab1-5 lista.txt" << endl;	  return 0; // kan bytas mot exit(...   }   else   {	  listfilen.open(argv[1]);	  if(!listfilen)	  {	  cout << "Kunde inte öppna den specifierade filen" << endl;	  return 0;	  }   }     // rulla genom listan   /*for(int i = 0; !listfilen.eof(); i++) // eof ??    {	  cout << "hopp";	  }*/   char c;   string post = "";   while(listfilen.get(c))     {       post += c;       if(listfilen.peek() == '\n')       {	 en_lista->insert(post);	 post ="";	 	 while(listfilen.peek() == '\n') listfilen.get(c);       }       //cout << c << "Plopp" << endl;     }    listfilen.close();   //en_lista->print('R');   //en_lista->reverse();   //en_lista->print('R');   cout << "Debug" << endl;   List *en_lista2 = new List;   en_lista2 = en_lista;      cout << "Tar bort första listan" << endl;   delete en_lista;   return 0;}