hab mein programm soweit hingebracht dass es zeichenweise einliest und dann den string vergleicht ob er schon vorgekommen ist. falls ja wordcounter erhöhen falls nicht wort hinten anhängen. nur klappt der algorithmus nicht ganz. wäre nett wenn ihr nen blick draufwerfen könnten. also ne kleine txt datei zum testen erstellen. nun zum code
Code:
#include <stdio.h>
#include <string.h>
#define MAXWORTCOUNT 100000
#define MAXWORTLEN 100
struct wordcount{
char word[MAXWORTLEN];
int wordcount;
};
struct wordcount wc[MAXWORTCOUNT];
void quicksort(struct wordcount a[], int li, int re){
int v, i, j, t;
char temp[100];
if (re > li){
v=a[re].wordcount; i=li-1; j=re;
for (;;){
while (a[++i].wordcount < v);
while (a[--j].wordcount > v);
if (i >= j) break;
t=a[i].wordcount; a[i].wordcount=a[j].wordcount; a[j].wordcount=t;
strcpy(temp,a[i].word); strcpy(a[i].word,a[j].word); strcpy(a[j].word,temp);
}
t=a[i].wordcount; a[i].wordcount=a[re].wordcount; a[re].wordcount=t;
strcpy(temp,a[i].word); strcpy(a[i].word,a[j].word); strcpy(a[j].word,temp);
quicksort(a, li, i-1);
quicksort(a, i+1, re);
}
}
int main(int argc, char *argv[])
{
int i,j=0;
char temp[MAXWORTLEN],buchstabe;
FILE *datei; /*Pointer auf FILE*/
if (argc == 1 || argc >= 4){
printf("\nstart mit abgabe_10.exe <infile.txt> [outfile.txt]\ninfile.txt\tname der einzulesenden datei\noutfile.txt\t(optional) datei fuer statistikausgabe\n\n");
return 0;
}
if ((datei = fopen(argv[1], "r")) == NULL){
printf("\n%s konnte nicht gefunden werden\n\n",argv[1]);
return 0;
}
while(!feof(datei)) /*Schleife zum einlesen der Textdatei und zum synchronen Verarbeiten des eingelesenen Buchstaben*/
{
buchstabe = fgetc(datei); /*Zuweisung des eingelesenen Zeichen auf Variable Buchstabe*/
if((buchstabe >= 'a' && buchstabe <= 'z') || (buchstabe >= 'A' && buchstabe <= 'Z')) /*Wenn ja, erhöhe wordcount um eins*/
{
temp[j]=buchstabe;
j++;
temp[j]='\0';
}else{
if(j!=0){
for(i=0;i<=MAXWORTCOUNT;i++){
if (strcmp(temp,wc[i].word)==0)
{
wc[i].wordcount++;
temp[0]='\0';
j=0;
break;
}
if (wc[i].word[0]==0)
{
strcpy(wc[i].word,temp);
wc[i].wordcount=1;
temp[0]='\0';
j=0;
break;
}
}
}
}
}
fclose(datei);
quicksort(wc,0,MAXWORTCOUNT);
if ((argc == 3) && ((datei = fopen(argv[2], "a+")) != NULL))
{
for(i=MAXWORTCOUNT;i>=0;i--){
if(wc[i].wordcount!=0)
fprintf(datei, "%s : %6d\n",wc[i].word,wc[i].wordcount);
}
fclose(datei);
}
else
{
for(i=MAXWORTCOUNT;i>=0;i--){
if(wc[i].wordcount!=0)
printf("%6d %s\n",wc[i].wordcount,wc[i].word);
}
}
return 0;
}
Zuletzt bearbeitet: