I do stuff and thingies... Try widening and reducing the number of small nooks and crannies to correct the problem.
Short description what I have to do as an exercise:
3 classes:
>> Matrix
-has 2 virtual functions: "write" and "print"
>> SMatrix (sparse matrix) (inheritance from Matrix)
-Constructor creates a matrix in a CRS (compact row storage) format reading a matrix in the standard format out of a file
-has "write" and "print"
>> DMatrix (inheritance from Matrix)
-reads a matrix in CRS format out of a file and stores it as a standard matrix (matrix[][])
-has "write" and "print"
########################
In the end I have to write a program main(...) that declares 2 pointers onto variables of the type Matrix and allocates them dynamically as SMatrix and DMatrix and use both functions: write and print.
########################
file: Matrix.hpp
Collapse Box
using namespace std;
class Matrix {
public:
virtual void print() = 0;
virtual void write(const char*) = 0;
};
file: SMatrix.hpp
Collapse Box
#include <vector>
#ifndef MATRIX_HPP
#define MATRIX_HPP
#include "Matrix.hpp"
#endif
class SMatrix : public Matrix {
public:
vector<float> a;
vector<int> k, p;
int hoehe, breite;
SMatrix(const char*);
void print();
void write(const char*);
};
file: SMatrix.cpp
Collapse Box
#include<iostream>
#include<fstream>
using namespace std;
#include "SMatrix.hpp"
SMatrix::SMatrix(const char* fname){
ifstream f(fname);
f >> hoehe; f >> breite;
if(hoehe <= 0 || breite <= 0)
cerr << "Fehler: Dimensionsgroessen muessen mindestens 1 sein. Hoehe = " <<hoehe<<", Breite = "<<breite<< endl;
p.reserve(hoehe+1);
double value;
int spaltenindex=0;
bool hadntFirstNonZero=true;
for(int i=0;i<hoehe*breite;i++,spaltenindex++){
if(spaltenindex >= breite){
spaltenindex=0;
hadntFirstNonZero=true;
}
f >> value;
if (value!=0.0){
a.push_back(value);
k.push_back(spaltenindex);
if(hadntFirstNonZero){
p.push_back(k.size()-1);
hadntFirstNonZero=false;
}
}
}
p.push_back(a.size());
f.close();
}
void SMatrix::print(){
vector<float>::const_iterator a_it=a.begin();
if(a.size()>0){
cout << *a_it;
a_it++;
}
for (int i=1; i<a.size();i++, a_it++)
cout << ", " << *a_it;
cout << endl;
vector<int>::const_iterator k_it=k.begin();
if(k.size()>0){
cout << *k_it;
k_it++;
}
for (int i=1; i<k.size(); i++, k_it++)
cout << ", " << *k_it;
cout << endl;
vector<int>::const_iterator p_it=p.begin();
if(p.size()>0){
cout << *p_it;
p_it++;
}
for (int i=1; i<p.size(); i++, p_it++)
cout << ", " << *p_it;
cout << endl;
}
void SMatrix::write(const char* fname){
ofstream f(fname);
vector<float>::const_iterator a_it=a.begin();
if(a.size()>0){
f << *a_it;
a_it++;
}
for (int i=1; i<a.size();i++, a_it++)
f << ", " << *a_it;
f << endl;
vector<int>::const_iterator k_it=k.begin();
if(k.size()>0){
f << *k_it;
k_it++;
}
for (int i=1; i<k.size(); i++, k_it++)
f << ", " << *k_it;
f << endl;
vector<int>::const_iterator p_it=p.begin();
if(p.size()>0){
f << *p_it;
p_it++;
}
for (int i=1; i<p.size(); i++, p_it++)
f << ", " << *p_it;
f << endl;
f.close();
}
file: DMatrix.hpp
Collapse Box
#ifndef MATRIX_HPP
#define MATRIX_HPP
#include "Matrix.hpp"
#endif
class DMatrix : public Matrix {
public:
int hoehe, breite;
float** m;
DMatrix(const char*);
void print();
void write(const char*);
~DMatrix();
};
file: DMatrix.cpp
Collapse Box
#include<iostream>
#include<fstream>
#include<vector>
using namespace std;
#include "DMatrix.hpp"
DMatrix::DMatrix(const char* fname){
ifstream f(fname);
float n;
char c='a';
int operationIndex = 0;
vector<float> a;
vector<int> k, p;
while( operationIndex<3 ){ //f.is_open()
n=0;
f >> c;
if(c >= '0' && c <= '9' || c == '-'){
f.unget();
f >> n;
switch(operationIndex){
case(0):
a.push_back(n); break;
case(1):
k.push_back((int) n); break;
case(2):
p.push_back((int) n);
}
f >> c;
if(c >= '0' && c <= '9'){
f.unget();
operationIndex++;
}
}
else{
cerr<<"ERROR: File's content has invalid CRS format {valid: float, float, float, float\nint, int, int, int, int\nint, int, int}."<<endl;
}
}
f.close();
int tmp;
hoehe = p.size()-1;
breite = 0;
for(int i=0;i<k.size();i++){
tmp = k[i];
if(tmp > breite)
breite = tmp;
}
tmp=0; breite++;
m = new float*[hoehe];
for(int i=0;i<hoehe;i++)
m[i] = new float[breite];
for(int i=0;i<hoehe;i++)
for(int j=0;j<breite;j++)
m[i][j]=0;
if(p.size() > 1){
for(int i=1;i<p.size();i++){
for(int j=p[i-1];j<p[i];j++){ //geht zwischenraeume in p durch
m[tmp][ k[j] ]= a[j];
}
tmp++; //gehe eine Zeile tiefer
}
}
else{
cerr << "Fehler: Nullmatrix?" << endl;
}
}
void DMatrix::print(){
cout<<hoehe<<endl<<breite<<endl;
for(int i=0;i<hoehe;i++){
for(int j=0;j<breite;j++)
cout<<m[i][j]<<" ";
cout<<endl;
}
}
void DMatrix::write(const char* fname){
ofstream f(fname);
f<<hoehe<<endl<<breite<<endl;
for(int i=0;i<hoehe;i++){
for(int j=0;j<breite;j++)
f<<m[i][j]<<" ";
f<<endl;
}
f.close();
}
DMatrix::~DMatrix(){
for(int i=0;i<hoehe;i++)
delete[] m[i];
delete[] m;
}
file: main.cpp
Collapse Box
using namespace std;
#include "SMatrix.hpp"
#include "DMatrix.hpp"
#include <iostream>
int main(int argc, char* argv[]){
if(argc != 5)
cerr<<"ERROR: parameter mismatch. Needed: Inputfilename_SparseMatrix, Inputfilename_Matrix, Outputfilename1, Outputfilename2"<<endl;
Matrix* a;
Matrix* b;
a = new SMatrix(argv[1]);
b = new DMatrix(argv[2]);
a->print();
b->print();
a->write(argv[3]);
b->write(argv[4]);
delete[] a;
delete[] b;
return 0;
}
What I type into console:
g++ main.cpp Matrix.hpp DMatrix.hpp SMatrix.hpp DMatrix.cpp SMatrix.cpp -o Uebung3.exe
What I get:
C:\MinGW\bin\..\lib\gcc\mingw32\3.4.5\..\..\..\..\mingw32\bin\ld.exe:Matrix.hpp: file format not recognized; treating as
linker script
C:\MinGW\bin\..\lib\gcc\mingw32\3.4.5\..\..\..\..\mingw32\bin\ld.exe:Matrix.hpp:1: syntax error
collect2: ld returned 1 exit status
matrix format example:
Collapse Box
standard matrix format (used in DMatrix):
4
5
0 1 0 1 0
1 2 0 7 8
0 0 3 0 2
1 2 3 4 1
CRS matrix format (used in SMatrix):
1, 1, 1, 2, 7, 8, 3, 2, 1, 2, 3, 4, 1
1, 3, 0, 1, 3, 4, 2, 4, 0, 1, 2, 3, 4
0, 2, 6, 8, 13
I hope somebody can help me.
Post has been edited 2 time(s), last time on Feb 2 2009, 11:25 am by Ahli.