![]() |
Pages (2): [1] 2 » Show 150 posts per page |
.dsy:it. (http://www.dsy.it/forum/)
- Forum De Bell Tolls (http://www.dsy.it/forum/forumdisplay.php?forumid=7)
-- [INFORMATICA] conteggio stringhe ripetute (C) (http://www.dsy.it/forum/showthread.php?threadid=8361)
[INFORMATICA] conteggio stringhe ripetute (C)
Help...mi ritrovo un file di testo di lunghezza massima N, con un tot di stringhe ripetute;
devo contare per ogni parola, quante volte è ripetuta...
Non mi viene in mente un algoritmo decente...o per meglio dire non sò come non far ricontare la stessa parola quando con fseek ritorno al punto di partenza... uffff odio il C per queste cose!
Stò usando fgets fprintf su di un file in input (in sola lettura) e uno in output.
E' per uso personale, non fa parte di nessun progetto della nostra uni (almeno x quel che ne sò io!)
P.S: da evitare esplicitamente puntatori, array bidimensionali, alberi, il file non è ordinato e sopratutto non si possono usare funzioni del C++
(la ucciderei certa gente)
Non puoi salvare ogni stringa già contata in un array e prima di ogni fseek fare un bel strcmp? Non ho idea di come funzioni fseek perché non l'ho mai usata, quindi potrei dire una marea di str****te....
__________________
Bjarne Stroustrup: "I have always wished for my computer to be as easy to use as my telephone; my wish has come true because I can no longer figure out how to use my telephone."
Andrew S. Tanenbaum: "Never underestimate the bandwidth of a station wagon full of tapes hurtling down the highway."
Edsger Dijkstra: "Computer Science is no more about computers than astronomy is about telescopes."
Robert Firth: "One of the main causes of the fall of the Roman Empire was that, lacking zero, they had no way to indicate successful termination of their C programs."
Donald Knuth: "A mathematical formula should never be 'owned' by anybody! Mathematics belong to God."
Originally posted by Hamelin
strcmp?
Ma fseek cosa fa esattamente?
__________________
Bjarne Stroustrup: "I have always wished for my computer to be as easy to use as my telephone; my wish has come true because I can no longer figure out how to use my telephone."
Andrew S. Tanenbaum: "Never underestimate the bandwidth of a station wagon full of tapes hurtling down the highway."
Edsger Dijkstra: "Computer Science is no more about computers than astronomy is about telescopes."
Robert Firth: "One of the main causes of the fall of the Roman Empire was that, lacking zero, they had no way to indicate successful termination of their C programs."
Donald Knuth: "A mathematical formula should never be 'owned' by anybody! Mathematics belong to God."
Originally posted by Hamelin
Ma fseek cosa fa esattamente?
Ma se tu facessi così, invece?: apri il file in lettura e usando fgets di leggi una dopo
l'altra tutte le parole del file (senza doverlo leggere più volte)... memorizzi la prima
parola in una struct semplicissima che presenta due attributi (un char[] per memorizzare
la stringa di lettera della parola e un int per il numero di occorrenze); dopo di che
per la parola successiva, confronti quello che ti ritorna il fgets con la stringa salvata
nella struct: se è uguale aggiugi 1 al numero di occorrenze, se è diverso costruisci una
nuova struct contenente la nuova parola; alla lettura successiva dovrai usare strcmp con
entrambe le stringhe contenute nelle due struct e così via... naturalmente non è il
massimo della velocità perché è pieno di cicli... ma è semplice e credo che funzioni.
__________________
Bjarne Stroustrup: "I have always wished for my computer to be as easy to use as my telephone; my wish has come true because I can no longer figure out how to use my telephone."
Andrew S. Tanenbaum: "Never underestimate the bandwidth of a station wagon full of tapes hurtling down the highway."
Edsger Dijkstra: "Computer Science is no more about computers than astronomy is about telescopes."
Robert Firth: "One of the main causes of the fall of the Roman Empire was that, lacking zero, they had no way to indicate successful termination of their C programs."
Donald Knuth: "A mathematical formula should never be 'owned' by anybody! Mathematics belong to God."
Sono d' accordo con Hamelin.
Tuttavia non pensare troppo all' ottimizzazione, se il file che devi processare non è lungo diversi mega bytes.
Un' alternativa è usare un file che memorizza le parole e i relativi contatori.
non guardare la sintassi che è orribile e sbagliata, ma il concetto sottostante
Quindi (scrivo in un pseudo c )
while (!EOF)
{
String mystring = GiveMeNextString();
if (ThisIsNewString(mystring))
{
Myfileadd(mystring);
Myfileadd((char) 0); /* non mi ricordo molto del c come puoi notare... */
}
else {
int posizione = Myfileselect(mystring);
Myfileincrementcount (posizione);
}
/* ora leggiti il file */
}
boolean ThisIsNewString (String mystring)
{
/* scorri il file esaminando ogni stringa
e facendo una strcmp */
if... return true;
return false;
}
Originally posted by Hamelin
Ma se tu facessi così, invece?: apri il file in lettura e usando fgets di leggi una dopo l'altra tutte le parole del file (senza doverlo leggere più volte)
...sicuro che fgets si ferma a /n e non a /0? Fossi in te controllerei... bye!
__________________
Bjarne Stroustrup: "I have always wished for my computer to be as easy to use as my telephone; my wish has come true because I can no longer figure out how to use my telephone."
Andrew S. Tanenbaum: "Never underestimate the bandwidth of a station wagon full of tapes hurtling down the highway."
Edsger Dijkstra: "Computer Science is no more about computers than astronomy is about telescopes."
Robert Firth: "One of the main causes of the fall of the Roman Empire was that, lacking zero, they had no way to indicate successful termination of their C programs."
Donald Knuth: "A mathematical formula should never be 'owned' by anybody! Mathematics belong to God."
Openoffice ha mica una funzione di conteggio delle parole? Hum e avrà mica i sorgenti?[/B]
Originally posted by Hamelin
...sicuro che fgets si ferma a /n e non a /0? Fossi in te controllerei... bye!
Originally posted by Flyzone
Buona l'idea delle struct, ma la fregatura è che fgets piglia una stringa (si ferma al '\n') e non una parola, ed una riga è lunga all'incirca 5000 caratteri [...].
stavo pensando al fgetc, ma mi ritrovo in cicli e sottocicli cmq...sono a due pagine di roba ufff
Lo ammetto, in questo caso rimpiango amaramente di non poter usare il visual basic, in 10 minuti era fatto

__________________
~ get Debian! ~ get FreeBSD! ~ get OpenBSD! ~
una bella funzione di hash??? così se non c'è la parola la metti nella tabella di hash, se c'è aumenti un contatore di 1... (troppo complicato???)
E poi gli stream non sono oggetti legati al C++ (iostream per l'i/o standard e fstream per l'i/o con files???)
Gw@th
__________________
L'ombra si nasconde nella notte, laddove neanche l'occhio piu' allenato riesce a scorgerla...
Homo sapiens non urinat in ventum
Non c'è birra senza spina
La vita e` il dono piu` prezioso che Giove ci ha fatto (Ali G) 
Mastro Birromane della Falange Alcolica...
Gwath Lumbule... as darkness falls, he comes out! Dark Shadow's Blog
Tu mi dai la forza per andare avanti... Non fermarti continua a spingere!!!
Originally posted by Gwath
una bella funzione di hash??? così se non c'è la parola la metti nella tabella di hash, se c'è aumenti un contatore di 1... (troppo complicato???)

E poi gli stream non sono oggetti legati al C++ (iostream per l'i/o standard e fstream per l'i/o con files???)
__________________
~ get Debian! ~ get FreeBSD! ~ get OpenBSD! ~
Originally posted by DeepBlue
Deve cmq leggere il file parola per parola, o carattere per carattere e poi costruire l'hash table. Non gli conviene
mmm generalmente in C si definisce stream una sorgente o una destinazione di dati.
In C++ nin zo
__________________
L'ombra si nasconde nella notte, laddove neanche l'occhio piu' allenato riesce a scorgerla...
Homo sapiens non urinat in ventum
Non c'è birra senza spina
La vita e` il dono piu` prezioso che Giove ci ha fatto (Ali G) 
Mastro Birromane della Falange Alcolica...
Gwath Lumbule... as darkness falls, he comes out! Dark Shadow's Blog
Tu mi dai la forza per andare avanti... Non fermarti continua a spingere!!!
| All times are GMT. The time now is 13:35. | Pages (2): [1] 2 » Show all 21 posts from this thread on one page |
Powered by: vBulletin Version 2.3.1
Copyright © Jelsoft Enterprises Limited 2000 - 2002.