|
|
|
![]() |
|
Strumenti |
![]() |
#1 |
Senior Member
Iscritto dal: Sep 2006
Città: Bologna/Milano
Messaggi: 525
|
[c++] sto scrivendo una classe per la gestione di matrici...
sto scrivendo una classe per la gestione delle matrici...
matrici.h Codice:
#include <cstdlib> #include <iostream> #include <stdio.h> #include <stdlib.h> #include <math.h> #ifndef _MATRICI_H #define _MATRICI_H 1 //**************** class ******************// class matrice { public: int r; int c; double ** m; //costruttore e distruttore matrice(); ~matrice(); //crea matrice void create(int r, int c); void create_with_value(int r, int c, double value); //visualizza matrice void print(); //caricamento - salvataggio matrici void load_from_file(char * file); void save_in_file(char * file); //operazioni su matrici friend matrice operator+(matrice, matrice); }; #endif Codice:
#include "matrici.h" //costruttore matrice::matrice(){ r = 0; c = 0; } //distruttore matrice::~matrice(){ } //creazione matrice void matrice::create(int r, int c){ // r = righe, c = colonne int x,y; this->r = r; this->c = c; m = new double *[r]; for(x=0;x<r;x++) m[x] = new double[c]; } void matrice::create_with_value(int r, int c, double value){ int x,y; this->r = r; this->c = c; m = new double *[r]; for(x=0;x<r;x++) m[x] = new double[c]; for(x=0;x<r;x++) for(x=0;x<r;x++) m[x][y] = value; } //visualizza matrice void matrice::print(){ int x,y; for(x=0;x<r;x++){ for(y=0;y<c;y++) printf("%12.6f\x20",m[x][y]); printf("\n"); } printf("\n"); } //caricamento - salvataggio matrici void matrice::load_from_file(char * file){ FILE *stream; char *buffer, *number; long filesize, readed; long x, y=0, mr=0, mc=0, r=0, c=0; stream = fopen(file,"rb"); fseek(stream,0,SEEK_END); filesize = ftell(stream); rewind(stream); buffer = (char *) malloc(filesize * sizeof(char)); number = (char *) malloc(100 * sizeof(char)); readed = fread(buffer,1,filesize,stream); for(x=0; x<filesize ;x++){ //conta colonne if(buffer[x] == '\x09') mc++; if(buffer[x] == '\x0D'){ mc++; break; } } for(x=0; x<filesize ;x++){ //conta righe if(buffer[x] == '\x0D') mr++; } create(mr, mc); for(x=0; x<filesize ;x++){ if(buffer[x] == '\x0A') continue; if(buffer[x] == '\x09' || buffer[x] == '\x0D'){ number[y] = '\x00'; this->m[r][c] = atof(number); y=0; c++; if(buffer[x] == '\x0D'){ r++; c=0; } } else{ number[y] = buffer[x]; y++; } } } void matrice::save_in_file(char * file){ int x,y; FILE * stream; stream = fopen(file, "w+"); for(x=0;x<r;x++){ for(y=0;y<c;y++){ fprintf(stream,"%.6f",m[x][y]); if(y<c-1) fprintf(stream,"\x09"); } fprintf(stream,"\n"); } fclose(stream); } //operazioni su matrici matrice operator+(matrice a,matrice b){ matrice c; int x,y,i; if(a.r != b.r || a.c != b.c) exit(0); c.create(a.r, a.c); for(x=0;x<c.r;x++) for(y=0;y<c.c;y++) c.m[x][y] = a.m[x][y]+b.m[x][y]; return c; } Codice:
#include "matrici.h" int main(int argc, char *argv[]) { long x; matrice a,b; a.load_from_file("a.txt"); a.save_in_file("b.txt"); a.print(); for(x=0;x<1000000000;x++) b = a + a; b.print(); getchar(); return 1; } Solo che non so come fare... grazie ![]()
__________________
http://mamo139.altervista.org |
![]() |
![]() |
![]() |
#2 |
Member
Iscritto dal: Sep 2008
Città: Milano
Messaggi: 126
|
credo proprio che tu debba implementare l'overload dell'operatore di assegnamento ( e quindi anche il costruttore di copia e il distruttore)
ciao! british |
![]() |
![]() |
![]() |
#3 |
Senior Member
Iscritto dal: Oct 2006
Città: Roma
Messaggi: 1383
|
banalmente: quel programma contiene un tot di operatori new e nemmeno un delete, l'effetto collaterale che esponi mi sembra il minimo.
hint: il distruttore della classe matrice é vuoto. riempilo ![]() |
![]() |
![]() |
![]() |
#4 | ||
Senior Member
Iscritto dal: Sep 2006
Città: Bologna/Milano
Messaggi: 525
|
Quote:
![]() Quote:
grazie a tutti e due ![]()
__________________
http://mamo139.altervista.org |
||
![]() |
![]() |
![]() |
Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 12:11.