Quelltext in Klasse schreiben



  • Ich hab einen funktionierenden Quelltext geschrieben. Wollte diesen jez in eine Klasse umwandeln und bekomme nur Fehlermeldungen beim kompilieren.

    // meine  main.cpp
    
    #include "CRS.h"
    #include<fstream>
    #include<iostream>
    
    int main(int argc, char** args) 
    {   
    
    	double** matrix_A; // Matrix wird deklariert
    	int matrix_A_n; 
    	// Container werden deklariert
    
    	if(argc!=2) {
    		cout << "Bitte Namen der einzulesenden Datei eingeben" << endl;
    		return 1;
    	}
    	//einlesen vom Dateinamen
    	infile = string(args[1]); 
    	cout << "The determinant of "<<endl;
    	// liest Matrix in CRS-Format ein
    	read(matrix_A, matrix_A_n, infile, alpha , kappa , rho);
        // gibt die Matrix im CRS_Format aus 
        //print( alpha, kappa, rho); 
    	dealloc(matrix_A, matrix_A_n); // Matrix wird gelöscht
    
    	int s;
        cin >> s;
    
    	return 0;
    }
    
    // CRS.cpp
    /**
     * Übung B 24.01.2010
     * ===============================================
     * 
     * Dieses Programm errechnet Determinanten im CRS-Format
     **/
    
    #include <iostream>
    #include <sstream>
    #include <fstream>
    #include <vector>
    #include <algorithm>
    #include <cmath>
    #include "CRS.h"
    
    using namespace std;
    
    /**
     * Aufgabenteil 1) Einlesen der Matrix im CRS-Format
     **/
    
    void CRS:: read(double**& mem, int& n, const string& fn, vector<double>&  alpha,vector<double>& kappa, vector<double>& rho) 
    {
    	double buffer;
    	ifstream ifs(fn.c_str());  // liest Textdatei aus dem Stream ein
    	if( ifs.is_open() ) {      // öffnet Testdatei
    		ifs >> n;              //liest Dimension aus
    //		alpha.reserve (n*n);   // reserviert Speicher
    //		kappa.reserve ((n)*(n)); 
    		alloc(mem, n, n);      // Speicher für Matrix wird reserviert
    		for(int i=0;!ifs.eof() && i<n;i++)    // Matrix wird gefüllt
    			for(int j=0;!ifs.eof() && j<n;j++) 
    				ifs >> mem[i][j];
    		ifs.close();           // Stream wird geschlossen
    	}
    	else {
    		cout << "error: the file "<<fn<<" cannot opened for reading."<<endl;
    	}
    	int position=0;
    
    	// Wandelt die Matrix in CRS Format um
       rho.push_back(0);
    
        for(int zeile=0; zeile <n; zeile++)
         {   
           for(int spalte=0; spalte< n; spalte++)
           {              
                 if( mem[zeile][spalte]!= 0)
                     { alpha.push_back(mem[zeile][spalte]);  // Alpha enthält nicht 0 Elemente der Matrix               
                       kappa.push_back(spalte);              // Kappa speichert die Position der Elemente in der Zeile 
    
                          position++;
                     }
           }
        rho.push_back(position);                             // rho enthält Zeilenumbrüche    
        }
    
      print( alpha, kappa, rho);                             //Matrix im CRS_Format wird ausgegeben 
      cout << "is: " << det(n, alpha, kappa , rho) << endl;  // Determinante wird berechnet
    
    }
    
    //Allocation of memory
    void CRS::alloc(double**& mem, int m, int n) 
    { 
    
    	mem = new double*[m];
    	for(int i=0;i<m;i++) {
    		mem[i] = new double[n];
    	}
    }
    
    // Deallocation of memory
    void CRS:: dealloc(double**& mem, const int n) 
    {
    	if(mem!=NULL) {
    		for(int i=0;i<n;i++)
    			delete [] mem[i];
    		delete [] mem;
    		mem=NULL;
    	}
    }
    
    //Ausgeben der Matrix im CRS-Format
    void CRS:: print(vector<double> alpha,vector<double> kappa, vector<double> rho) 
    {   
         vector<double>::const_iterator a;
    
         for (a=alpha.begin();a<alpha.end();a++) cout << *a << " ";
          cout << endl;
    
         for (a=kappa.begin();a<kappa.end();a++) cout << *a << " ";
          cout << endl;
    
         for (a=rho.begin();a<rho.end();a++) cout << *a << " ";
          cout << endl;  
    
          cout <<"" <<endl;      
    
    }
    
    //Erstellen der Untermatrix im CRS-Format
    void CRS::create_submatrix(vector<double> alpha,vector<double> kappa, vector<double> rho,
                          vector<double>& sub_alpha,vector<double>& sub_kappa, vector<double>& sub_rho, int knull)
    {
    
      // sub_kappa und sub_alpha berechnen:   
      // da wo kappa 0 ist wird alpha =0 und kappa =-1 gesetzt
      // anschließend werden die Nullen in alpha und die -1'er in kappa gelöscht
    for (int i= 0; i<kappa.size(); i++){
        if(kappa.at(i)==0)
        {
         kappa.at(i) =-1;
         alpha.at(i)=0;
        }
     } 
    
     // zeile zum"löschen" markieren
     if(rho.at(knull+1)== rho.at(rho.size()-1))
     {
      for(int i=knull;i<knull+(alpha.size()-knull-1) ; i++)
             {
                  alpha.at(i)=0; 
                  kappa.at(i)=-1;             
             }
    
         }
         else{
               for(int i=knull;i<rho.at(knull+1) ; i++)
               {
                  alpha.at(i)=0; 
                  kappa.at(i)=-1;             
               }     
             }
    
     // sub_kappa und sub_alpha füllen
         for(int i= 0; i<alpha.size(); i++)
         {
          if(alpha.at(i)!=0 && kappa.at(i)!=-1)
          {
            sub_alpha.push_back(alpha.at(i)) ;
            sub_kappa.push_back(kappa.at(i)-1) ;                       
          }            
         }         
    
         //rho bestimmen
        int nullzaehler; //zählt die Nullen in Alpha
        sub_rho.push_back(0);
    
        for(int i= 1; i<rho.size()-1; i++)
       {       
        if( alpha.at(rho.at(i))!=0 )
        {    
             nullzaehler=0;
             for(int j=0; j<rho.at(i);j++)
             { if(alpha.at(j)==0)
               { 
                nullzaehler++;
               }
              }
             sub_rho.push_back(rho.at(i)-nullzaehler);           
         }
    
         if(alpha.at(rho.at(i))==0 && rho.at(i)!=rho.at(i+1))
         {   
             nullzaehler=0;
             for(int j=0; j<rho.at(i);j++)
             { if(alpha.at(j)==0)
               { 
    
                nullzaehler++;
               }
             }
              sub_rho.push_back(rho.at(i)-nullzaehler);          
         }
    
         }
    
       /* for( int i=1; i< sub_rho.size(); i++)
              {
                  if(sub_rho.at(i)== sub_rho.at(i-1))
                  {
                     sub_rho.at(i-1)=-1;
                  }  
              } 
    
         vector<double> subsub_rho;  //temporäres Ausweich-Rho
         subsub_rho=sub_rho;
         sub_rho.clear();            //sub_rho wird geleert
         //es darf keine doppelten Einträge in sub_rho geben(außer möglicherweise am ende, aber
         // das letzte Element wird erst eingefügt, wenn der rest von sub_rho fertig ist)      
         for( int i=0; i< subsub_rho.size(); i++){
    
               if(subsub_rho.at(i)!=-1)
                  {
                     sub_rho.push_back(subsub_rho.at(i)) ;
    
                  }         
              }*/
         sub_rho.push_back(sub_alpha.size());   //Einfügen des letzten Elements in sub_rho, dass angibt,
                                                //wieviele Nicht-Null-Elemente in der Matrix existieren 
       print(sub_alpha, sub_kappa, sub_rho);  //Ausgeben der Untermatrix im CRS-Format 
    
    }
    
    //Methode zur Berechnung der Determinante
    double CRS::det( int n,vector<double> alpha,vector<double> kappa,vector<double> rho )
    {	
    	vector<double> sub_alpha; 
    //	sub_alpha.reserve ((n)*(n)); //Speicherreservierung für sub_alpha
        vector<double> sub_kappa;
    //   	sub_kappa.reserve ((n)*(n)); 
    	vector<double> sub_rho ;
    
    	double d=0.;
    	int j=0;
    	//Fallunterscheidung für verschiedene Dimensionen
    	switch(n) {
    		case 0: {
    			cerr << "Cannot compute determinant of matrix with dimension 0."<<endl;
    			exit(1);
    		}
    		case 1: {
    			d = alpha.at(0);
    			break;
    		}
    		case 2: {
                  // 0 oder 1 Element
                  cout << "fall 2";
                  if(alpha.size()<=1) 
                 {
                 d=0;
                 return d;
                 }           
    
                  // 2 Elemente in der Matrix
                  if(alpha.size()==2) 
                 { 
                  // Eine Nullzeile existiert
                   if(rho.at(1)==2)// ein Zeilenumbruch und 1mal Anzahl der Elemente
                   {
                    d=0;   return d;              
                   }
                       // Nullspalte
                   if(kappa.at(0)==kappa.at(1))
                   {
                    d=0; return d;
                   }
                   // 01  zum Beispiel
                   // 10
                  if(kappa.at(0)==1 && kappa.at(1)!=1)
                   {
                    d=-alpha.at(0)*alpha.at(1) ;   return d;           
                   }
                   // 10  zum Beispiel
                   // 01
                   else{d=alpha.at(0)*alpha.at(1) ; return d;}
                 }
    
                // 3Elemente in der Matrix 
                if(alpha.size() ==3)
                {
                    //rho an der 2.Stelle = 1 bedeutet das in der 1.Zeile nur 1 Element steht     
                    if( rho.at(1)== 1)
                    {  
                            if( kappa.at(0)== 1)
                            {
                             d=-alpha.at(0) * alpha.at(1); return d;
                            }
                            else
                            {
                              d=alpha.at(0) * alpha.at(2); return d;
                            }
                    }
                     //rho an der 2.Stelle = 2 bedeutet das in der 2.Zeile nur 1 Element steht
                    if( rho.at(1)== 2)
                    {
                            if( kappa.at(2)== 1)
                            {
                             d=alpha.at(0) * alpha.at(2); return d;
                            }
                            else
                            {
                              d=-alpha.at(1) * alpha.at(2); return d;
                            }
                    }
               } 
    
            // wenn die 2x2 Matrix voll besetzt ist, dann wird die determinate normal berechnet
                 if(rho.at(rho.size()-1) == 4)
                 { 	
    			   d = alpha.at(0) * alpha.at(3)-alpha.at(1) * alpha.at(2);  return d;		  
                  }	
                break;
            }
    
    		default: {
    
            double res;
    
    		for(int j=0;j<n;j++)
           {  
             res= pow(-1.0,(double)j) ;       
    
             //Untermatrizen sollen nur dann erstellt werden, wenn der Faktor, also die Zahl mit
             //der die Untermatrix multipliziert wird ungleich Null ist         	
    		 if (kappa.at(j)== 0)
             {                 
        	  int knull= j;
        	  create_submatrix(alpha, kappa, rho,sub_alpha, sub_kappa, sub_rho, knull);
        	  res*=alpha.at(j)*det(n-1, sub_alpha, sub_kappa, sub_rho);
              d+=res;      
             }//else{return d=0;}
    
           }
    			break;
    
    	  }
    
    	} 
      //print(sub_alpha, sub_kappa, sub_rho);
    	return d;
    
    }
    
    ///////////////////////////////////////////////////////////////////////////////
    

    // CRS.h

    #include <iostream>
    #include <vector>

    using namespace std;
    class CRS{

    public:
    CRS::CRS(){}
    CRS::~CRS() {}
    vector<double> alpha;
    vector<double> kappa;
    vector<double> rho ;

    void read(double**& mem, int& n, const string& fn,vector<double>& alpha,vector<double>& kappa, vector<double>& rho) ;

    int main(int argc, char** args) ;
    void alloc(double**& mem, int m, int n) ;
    void dealloc(double**& mem, const int n) ;

    double det(int n,vector<double> alpha,vector<double> kappa,vector<double> rho);
    void print(vector<double> alpha,vector<double> kappa, vector<double> rho);
    void create_submatrix(vector<double> alpha,vector<double> kappa, vector<double> rho,
    vector<double>& sub_alpha,vector<double>& sub_kappa, vector<double>& sub_rho , int knull);

    string infile;

    };
    [cpp]


Anmelden zum Antworten