|
|||||||
|
|
|
![]() |
|
|
Strumenti |
|
|
#81 |
|
Senior Member
Iscritto dal: Dec 2005
Città: Istanbul
Messaggi: 1817
|
nota 1 : probabilmente prima il programma crashava perche' la seconda linea veniva letta solo in parte (sforavi quindi il vector).
nota 2 : ovviamente a questo punto possiamo pure fare a meno delle eccezioni. Non cambia comunque poi molto Codice:
void read_matrix( const string& filename, vector< vector<float> >& matrix, int line_size )
{
string s;
ifstream input( filename.c_str() );
while ( getline( input, s ) )
{
istringstream is(s);
matrix.push_back( vector<float>(line_size) );
for ( int i=0 ; i<line_size ; ++i )
{
if ( ! (is >> matrix.back()[i]) )
{
matrix.pop_back();
continue;
}
}
// Be sure that there isn't anything left on the line...
string s;
if ( is >> s )
{
matrix.pop_back();
}
}
}
__________________
One of the conclusions that we reached was that the "object" need not be a primitive notion in a programming language; one can build objects and their behaviour from little more than assignable value cells and good old lambda expressions. —Guy Steele |
|
|
|
|
|
#82 |
|
Bannato
Iscritto dal: Mar 2008
Città: Villabate(PA)
Messaggi: 2515
|
Marco,
purtroppo crasha ancora. Forse non ho capito bene l'utilizzo del terzo parametro della funzione. Devo passargli 3 nel caso di un array con tre colonne, giusto? Ciao Codice:
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <ctime>
using namespace std;
void read_matrix1( const string& filename, vector< vector<float> >& matrix )
{
string s;
ifstream input( filename.c_str() );
while ( getline( input, s ) )
{
matrix.push_back( vector<float>() );
istringstream is(s);
copy( istream_iterator<float>( is ),
istream_iterator<float>(),
back_inserter( matrix.back() ) );
}
}
void read_matrix2( const string& filename, vector< vector<float> >& matrix )
{
string s;
ifstream input( filename.c_str() );
while ( getline( input, s ) )
{
try
{
matrix.push_back( vector<float>() );
istringstream is(s);
is.exceptions( ifstream::badbit | ifstream::failbit );
copy( istream_iterator<float>( is ),
istream_iterator<float>(),
back_inserter( matrix.back() ) );
if ( matrix.back().size() != 365 )
matrix.pop_back();
} catch( ifstream::failure e )
{ /* Ignore wrong line */ }
}
}
void read_matrix3( const string& filename, vector< vector<float> >& matrix, int line_size )
{
string s;
ifstream input( filename.c_str() );
while ( getline( input, s ) )
{
istringstream is(s);
matrix.push_back( vector<float>(line_size) );
for ( int i=0 ; i<line_size ; ++i )
{
if ( ! (is >> matrix.back()[i]) )
{
matrix.pop_back();
continue;
}
}
// Be sure that there isn't anything left on the line...
string s;
if ( is >> s )
{
matrix.pop_back();
}
}
}
int main()
{
clock_t c_start, c_end;
c_start = clock();
vector< vector<float> > matrix;
read_matrix3("C:\\Temp\\test1.txt", matrix, 3);
cout << endl;
cout << "v[0][0] -> " << matrix[0][0] << endl;
cout << "v[1][1] -> " << matrix[1][1] << endl;
c_end = clock();
printf("\nTempo impiegato -> %5.5f secondi\n", (double)(c_end - c_start) / CLOCKS_PER_SEC);
return 0;
}
|
|
|
|
|
|
#83 |
|
Senior Member
Iscritto dal: Dec 2005
Città: Istanbul
Messaggi: 1817
|
occhio che quello e' il codice che avevo scritto come esempio per gestire il primo caso che avevo esposto (ovvero quello in cui si preferisce saltare la linea errata piuttosto che rinunciare alla lettura dell'intero file). Se fai un controllo molto probabilmente ti trovi che matrix.size() e' 1, per cui con il secondo output sfori. Ovviamente ci sono anche altre alternative, tipo lasciare una riga vuota, copiare la precedente etc., dipende come vuoi gestirlo.
__________________
One of the conclusions that we reached was that the "object" need not be a primitive notion in a programming language; one can build objects and their behaviour from little more than assignable value cells and good old lambda expressions. —Guy Steele |
|
|
|
|
|
#84 | |
|
Senior Member
Iscritto dal: Dec 2005
Città: Istanbul
Messaggi: 1817
|
Quote:
Codice:
void read_matrix( const string& filename, vector< vector<float> >& matrix, int line_size )
{
string s;
ifstream input( filename.c_str() );
while ( getline( input, s ) )
{
istringstream is(s);
matrix.push_back( vector<float>() );
copy( istream_iterator<float>( is ),
istream_iterator<float>(),
back_inserter( matrix.back() ));
// Did we read the right amount of values, and consumed all the input ?
string s;
if ( matrix.back().size() != line_size || (is >> s) )
{
matrix.pop_back();
}
}
}
, devo fare le cose con meno fretta
__________________
One of the conclusions that we reached was that the "object" need not be a primitive notion in a programming language; one can build objects and their behaviour from little more than assignable value cells and good old lambda expressions. —Guy Steele |
|
|
|
|
|
|
#85 | |
|
Senior Member
Iscritto dal: Oct 2007
Città: Padova
Messaggi: 4131
|
Quote:
__________________
As long as you are basically literate in programming, you should be able to express any logical relationship you understand. If you don’t understand a logical relationship, you can use the attempt to program it as a means to learn about it. (Chris Crawford) |
|
|
|
|
|
|
#86 |
|
Senior Member
Iscritto dal: Apr 2000
Città: Vicino a Montecatini(Pistoia) Moto:Kawasaki Ninja ZX-9R Scudetti: 29
Messaggi: 53971
|
Però ad esempio risulterebbe complicato segnalare la posizione dell'errore
|
|
|
|
|
|
#87 |
|
Senior Member
Iscritto dal: Dec 2005
Città: Istanbul
Messaggi: 1817
|
Perche' ? La posizione dell'errore e' la lunghezza del vector appena letto, per la riga basta tenere un contatore.
__________________
One of the conclusions that we reached was that the "object" need not be a primitive notion in a programming language; one can build objects and their behaviour from little more than assignable value cells and good old lambda expressions. —Guy Steele |
|
|
|
|
|
#88 | |
|
Senior Member
Iscritto dal: Dec 2005
Città: Istanbul
Messaggi: 1817
|
Quote:
__________________
One of the conclusions that we reached was that the "object" need not be a primitive notion in a programming language; one can build objects and their behaviour from little more than assignable value cells and good old lambda expressions. —Guy Steele |
|
|
|
|
|
|
#89 |
|
Bannato
Iscritto dal: Mar 2008
Città: Villabate(PA)
Messaggi: 2515
|
Ohé Cionci,
ciao. Ho modificato i programmi in modo da fargli calcolare la media dei numeri dell'intero file senza caricare l'array in memoria. Questo è il mio codice: Codice:
#include <iostream>
#include <ctime>
using namespace std;
#define BUFFER_SIZE 4096
typedef enum tagStati
{
S_ERROR = -1, S0 = 0, S1, S2, S3, S4, S5, S6, S7, S8, S9
} Stati;
Stati DFA(const char *szFileName, double *media)
{
Stati stato = S0;
FILE *fp;
unsigned char buffer[BUFFER_SIZE];
int numblocks;
int numread;
unsigned char c;
int k, x, j;
int riga, colonna;
char szNum[256];
double sum = 0;
int count = 0;
unsigned char byteCR = 0xD; // Carriage Return
unsigned char byteLF = 0xA; // Line Feed
*media = 0;
fp = fopen(szFileName, "rb"); // Warning C4996 -> Usare fopen_s
if ( fp == NULL )
{
cout << "Errore nell'apertura del file " << szFileName << endl;
return S_ERROR;
}
if ( fseek(fp, 0, SEEK_END) )
return S_ERROR;
numblocks = ftell(fp)/BUFFER_SIZE;
if ( numblocks == 0 )
{
numblocks = 1;
}
else
{
if ( ftell(fp) % BUFFER_SIZE != 0 )
numblocks++;
}
fseek(fp, 0, SEEK_SET);
numread = fread(buffer, 1, BUFFER_SIZE, fp);
riga = colonna = 0;
x = k = j = 0;
while ( x < numblocks )
{
c = *(buffer + k++);
if ( c == byteCR )
{
if ( k >= numread )
{
numread = fread(buffer, 1, BUFFER_SIZE, fp);
k = 0;
x++;
}
c = *(buffer + k++);
}
switch (stato)
{
case S0:
j = 0;
if ( c == '-' || c == '+' )
{
szNum[j++] = c;
stato = S1;
}
else if ( c >= '0' && c <= '9' )
{
szNum[j++] = c;
stato = S2;
}
else if ( c == '.' )
{
szNum[j++] = c;
stato = S3;
}
else if ( c == ' ' || c == '\t' )
{
while ( c == ' ' || c == '\t' )
{
c = *(buffer + k++);
if ( k >= numread )
break;
}
k--;
}
else
{
cout << "Errore: stato S0 << riga " << riga << " colonna " << colonna << endl;
fclose(fp);
return S_ERROR;
}
break;
case S1:
if ( c >= '0' && c <= '9' )
{
szNum[j++] = c;
stato = S2;
}
else if ( c == '.' )
{
szNum[j++] = c;
stato = S3;
}
else
{
cout << "Errore: stato S1 riga " << riga << " colonna " << colonna << endl;
fclose(fp);
return S_ERROR;
}
break;
case S2:
if ( c >= '0' && c <= '9' )
{
szNum[j++] = c;
}
else if ( c == '.' )
{
szNum[j++] = c;
stato = S4;
}
else if ( c == 'E' || c == 'e' )
{
szNum[j++] = c;
stato = S5;
}
else if ( c == ' ' || c == '\t' || c == byteLF )
{
szNum[j] = '\0';
sum += atof(szNum);
count++;
if ( c == byteLF )
{
riga++;
colonna = 0;
}
else
{
colonna++;
}
stato = S0;
}
else
{
cout << "Errore: stato S2 riga " << riga << " colonna " << colonna << endl;
fclose(fp);
return S_ERROR;
}
break;
case S3:
if ( c >= '0' && c <= '9' )
{
szNum[j++] = c;
stato = S6;
}
else
{
cout << "Errore: stato S3 riga " << riga << " colonna " << colonna << endl;
fclose(fp);
return S_ERROR;
}
break;
case S4:
if ( c >= '0' && c <= '9' )
{
szNum[j++] = c;
stato = S6;
}
else if ( c == 'E' || c == 'e' )
{
szNum[j++] = c;
stato = S7;
}
else if ( c == ' ' || c == '\t' || c == byteLF )
{
szNum[j] = '\0';
sum += atof(szNum);
count++;
if ( c == byteLF )
{
riga++;
colonna = 0;
}
else
{
colonna++;
}
stato = S0;
}
else
{
cout << "Errore: stato S4 riga " << riga << " colonna " << colonna << endl;
fclose(fp);
return S_ERROR;
}
break;
case S5:
if ( c >= '0' && c <= '9' )
{
szNum[j++] = c;
stato = S9;
}
else if ( c == '-' || c == '+' )
{
szNum[j++] = c;
stato = S8;
}
else
{
cout << "Errore: stato S5 riga " << riga << " colonna " << colonna << endl;
fclose(fp);
return S_ERROR;
}
break;
case S6:
if ( c >= '0' && c <= '9' )
{
szNum[j++] = c;
stato = S6;
}
else if ( c == 'E' || c == 'e' )
{
szNum[j++] = c;
stato = S7;
}
else if ( c == ' ' || c == '\t' || c == byteLF )
{
szNum[j] = '\0';
sum += atof(szNum);
count++;
if ( c == byteLF )
{
riga++;
colonna = 0;
}
else
{
colonna++;
}
stato = S0;
}
else
{
cout << "Errore: stato S6 riga " << riga << " colonna " << colonna << endl;
fclose(fp);
return S_ERROR;
}
break;
case S7:
if ( c >= '0' && c <= '9' )
{
szNum[j++] = c;
stato = S9;
}
else if ( c == '-' || c == '+' )
{
szNum[j++] = c;
stato = S8;
}
else
{
cout << "Errore: stato S7 riga " << riga << " colonna " << colonna << endl;
fclose(fp);
return S_ERROR;
}
break;
case S8:
case S9:
if ( c >= '0' && c <= '9' )
{
szNum[j++] = c;
stato = S9;
}
else if ( c == ' ' || c == '\t' || c == byteLF )
{
szNum[j] = '\0';
sum += atof(szNum);
count++;
if ( c == byteLF )
{
riga++;
colonna = 0;
}
else
{
colonna++;
}
stato = S0;
}
else
{
cout << "Errore: stato S9 riga " << riga << " colonna " << colonna << endl;
fclose(fp);
return S_ERROR;
}
break;
}
if ( k >= numread )
{
numread = fread(buffer, 1, BUFFER_SIZE, fp);
k = 0;
x++;
}
}
fclose(fp);
if ( stato == S2 || stato == S4 || stato == S6 || stato == S9 )
{
szNum[j] = '\0';
sum += atof(szNum);
count++;
}
*media = sum/count;
return stato;
}
int main()
{
Stati stato;
double media;
clock_t c_start, c_end;
char *szFileName = "C:\\Temp\\test3.txt";
c_start = clock();
stato = DFA(szFileName, &media);
c_end = clock();
if ( stato == S0 || stato == S2 || stato == S4 || stato == S6 || stato == S9 )
cout << endl << "media -> " << media << endl;
else
cout << "L'automa ha restituito un errore." << endl;
printf("\nTempo impiegato -> %5.5f secondi\n", (double)(c_end - c_start) / CLOCKS_PER_SEC);
return 0;
}
Ho tentato di modificare il tuo codice: Codice:
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <ctime>
using namespace std;
class MalformedLineException
{
int row;
int column;
string message;
public:
MalformedLineException(int row, int column)
{
this->row = row;
this->column = column;
ostringstream oss;
oss << "Malformed line at row " << row << " and column " << column;
message = oss.str();
}
string getMessage()
{
return message;
}
int getRow()
{
return row;
}
int getColumn()
{
return column;
}
};
int countColumns(string line)
{
istringstream iss(line);
int columnCounter = 0;
while (1)
{
float tmp;
iss >> tmp;
if (iss.fail() && !iss.eof())
throw MalformedLineException(1, columnCounter + 1);
columnCounter++;
if (iss.eof())
break;
}
return columnCounter;
}
int main()
{
clock_t c_start, c_end;
int riga, colonna;
double media, sum;
int count;
int colonne;
c_start = clock();
ifstream inputFile("C:\\Temp\\test3.txt");
if (inputFile.fail())
{
cout << "Error opening file" << endl;
return 1;
}
try
{
string line;
riga = colonna = 0;
sum = 0;
count = 0;
while (!inputFile.fail())
{
getline(inputFile, line);
riga++;
colonne = countColumns(line);
istringstream iss(line);
for (int i = 0; i < colonne; ++i)
{
float f;
iss >> f;
if (iss.fail())
throw MalformedLineException(riga, i + 1);
sum += f;
//cout << "num -> " << f << endl;
count++;
sum += f;
}
}
media = sum/count;
//cout << "count -> " << count << endl;
cout << endl << "media -> " << media << endl;
}
catch (MalformedLineException ex)
{
cout << ex.getMessage();
}
c_end = clock();
printf("\nTempo impiegato -> %5.5f secondi\n", (double)(c_end - c_start) / CLOCKS_PER_SEC);
return 0;
}
Sembra che tenti di leggere oltre l'ultima riga del file. Cosa ho sbagliato? Ciao. |
|
|
|
|
|
#90 |
|
Senior Member
Iscritto dal: Apr 2000
Città: Vicino a Montecatini(Pistoia) Moto:Kawasaki Ninja ZX-9R Scudetti: 29
Messaggi: 53971
|
Sinceramente non capisco perché tu gli voglia far calcolare la media. Diventa chiaro che in quel caso c'è un altro collo di bottiglia, cioè il vector che contiene le righe.
Edit: ho visto ora che stai tentando di creare la media in tempo reale. Ora guardo Ultima modifica di cionci : 18-09-2008 alle 13:08. |
|
|
|
|
|
#91 | |
|
Bannato
Iscritto dal: Mar 2008
Città: Villabate(PA)
Messaggi: 2515
|
Quote:
|
|
|
|
|
|
|
#92 | |
|
Bannato
Iscritto dal: Mar 2008
Città: Villabate(PA)
Messaggi: 2515
|
Quote:
|
|
|
|
|
|
|
#93 |
|
Senior Member
Iscritto dal: Apr 2000
Città: Vicino a Montecatini(Pistoia) Moto:Kawasaki Ninja ZX-9R Scudetti: 29
Messaggi: 53971
|
|
|
|
|
|
|
#94 |
|
Bannato
Iscritto dal: Mar 2008
Città: Villabate(PA)
Messaggi: 2515
|
Ho provato su un file di piccole dimensioni:
Codice:
5.23 7.28 21.5 8.9 10.5 8.3 edit: l'automa funziona sia col new-line che senza Ultima modifica di Vincenzo1968 : 18-09-2008 alle 13:34. |
|
|
|
|
|
#95 |
|
Bannato
Iscritto dal: Mar 2008
Città: Villabate(PA)
Messaggi: 2515
|
|
|
|
|
|
|
#96 |
|
Senior Member
Iscritto dal: Apr 2000
Città: Vicino a Montecatini(Pistoia) Moto:Kawasaki Ninja ZX-9R Scudetti: 29
Messaggi: 53971
|
Codice:
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <ctime>
using namespace std;
class MalformedLineException
{
int row;
int column;
string message;
public:
MalformedLineException(int row, int column)
{
this->row = row;
this->column = column;
ostringstream oss;
oss << "Malformed line at row " << row << " and column " << column;
message = oss.str();
}
string getMessage()
{
return message;
}
};
int countColumns(string line)
{
istringstream iss(line);
int columnCounter = 0;
while (1)
{
float tmp;
iss >> tmp;
if (iss.fail() && !iss.eof())
throw MalformedLineException(1, columnCounter + 1);
columnCounter++;
if (iss.eof())
break;
}
return columnCounter;
}
void parseLine(string line, int columns, double &sum)
{
istringstream iss(line);
for (int i = 0; i < columns; ++i)
{
float f;
iss >> f;
if (iss.fail())
throw MalformedLineException(0, i + 1);
sum += f;
}
}
int main()
{
clock_t c_start, c_end;
c_start = clock();
ifstream inputFile("/media/deposito/CartellaVbox/test.txt");
if (inputFile.fail())
{
cout << "Error opening file" << endl;
return 1;
}
try
{
string line;
getline(inputFile, line);
int columns = countColumns(line);
double sum = 0.0;
int counter = 0;
while (!inputFile.fail())
{
parseLine(line, columns, sum);
counter += columns;
getline(inputFile, line);
}
cout << endl;
cout << "Sum: " << sum << endl;
cout << "Average: " << sum / counter << endl;
}
catch (MalformedLineException ex)
{
cout << ex.getMessage();
}
c_end = clock();
printf("\nTempo impiegato -> %5.5f secondi\n", (double)(c_end - c_start) / CLOCKS_PER_SEC);
return 0;
}
In ogni caso continuo a non vedere quale utilità abbia questa prova |
|
|
|
|
|
#97 |
|
Bannato
Iscritto dal: Mar 2008
Città: Villabate(PA)
Messaggi: 2515
|
Sulla mia macchina:
Codice:
AMD Athlon(tm) 64 X2 Dual Core Processor 4800+ 2.50 GHz 896 MB di RAM
|
|
|
|
|
|
#98 |
|
Bannato
Iscritto dal: Mar 2008
Città: Villabate(PA)
Messaggi: 2515
|
|
|
|
|
|
|
#99 | |
|
Senior Member
Iscritto dal: Apr 2000
Città: Vicino a Montecatini(Pistoia) Moto:Kawasaki Ninja ZX-9R Scudetti: 29
Messaggi: 53971
|
Quote:
|
|
|
|
|
|
|
#100 | |
|
Bannato
Iscritto dal: Mar 2008
Città: Villabate(PA)
Messaggi: 2515
|
Quote:
E se, invece, debbo elaborare un centinaio di file per delle statiche, per esempio, l'automa sbriga il lavoro in un terzo del tempo |
|
|
|
|
|
| Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 22:08.












, devo fare le cose con meno fretta








