/* file P11_1.c */ #include #include #include #include #include "declarations.h" int main(int argc, char* argv[]) { FILE *quelle; FILE *ziel; int i,n; if ((argc < 2) || (argc > 3)) { printf("Ungueltige Parameter.Programm wird beendet\n8"); return 1; } quelle = fopen(argv[1],"r"); if(quelle == NULL) { printf("Die Datei konnte nicht geoeffnet werden. Programm wird beendet\n"); return 1; }; if (argc == 2) ziel = stdout; else { ziel = fopen(argv[2],"w+"); if (quelle == NULL) { printf("Die Datei konnte nicht geoeffnet werden. Programm wird beendet."); return 1; } } if (char_statistics(quelle, ziel) == 1){ printf("\nEs sind keine Buchstaben in der Datei enthalten"); return 1; } fclose(quelle); fclose(ziel); return 0; } //--------------------------------------------------------------------------- /* file charstat.c */ #include #include #include #include #include "declarations.h" int char_statistics(FILE *quelle, FILE *ziel) { int i,nSize, summe = 0, has_alphas = 0; char zeile[MAX_LINE]; char *pZeile; struct buchstabe *buchstaben; buchstaben = malloc(26 * sizeof(struct buchstabe)); // array initialisieren for (i=0; i < 26; i++) { buchstaben[i].der_buchstabe = (char)(0x41 + i); buchstaben[i].anzahl = 0; } while(!feof(quelle)) { fgets(zeile,26 - 2,quelle); pZeile = strupr(zeile); for (i=0; i < (int)strlen(pZeile); i++) { if(isalpha(pZeile[i])){ has_alphas = 1; buchstaben[pZeile[i] - 0x41].anzahl++; buchstaben[26].anzahl++; } } } if (has_alphas == 0) return 1; bubble_sort(buchstaben); nSize = 0; while (buchstaben[nSize].anzahl != 0) nSize++; buchstaben = realloc(buchstaben,nSize * sizeof(struct buchstabe)); if (buchstaben == NULL) printf("Feldgroesse konnte nicht angepasst werden\n"); summe = 0; for (i = 0; i < nSize; i++) summe += buchstaben[i].anzahl; //Haeufigkeiten berechnen for (i = 0; i < nSize; i++) buchstaben[i].proz = (double)buchstaben[i].anzahl / ((double)summe / 100); plot_table(quelle,ziel,buchstaben,nSize); // free(buchstaben) <-- ?? -.-; return 0; } //--------------------------------------------------------------------------- //--------------------------------------------------------------------------- /*FILE plot.c*/ #include #include #include #include #include "declarations.h" int plot_table(FILE *quelle,FILE *ziel,struct buchstabe buchstaben[],int nSize) { int i; char zeile[MAX_LINE]; fseek(quelle,0L,SEEK_SET); while(!feof(quelle)){ fgets(zeile,26 - 2,quelle); fprintf(ziel,zeile); } fprintf(ziel,"\n------------------------------------------------------------------------"); fprintf(ziel,"\nNr | Buchstabe | Anzahl | Haeufigkeit in %"); for(i = 0;i < nSize; i++){ fprintf(ziel,"\n%d\t|\t%c\t|\t%d\t|\t%lf",i, buchstaben[i].der_buchstabe, buchstaben[i].anzahl, buchstaben[i].proz); } return 0; } //------------------------------------------------------------------------------------------------------- //------------------------------------------------------------------------------------------------------- /* FILE bubblesort */ #include #include #include #include #include "declarations.h" int bubble_sort(struct buchstabe buchstaben[]) { int s = 0,i; struct buchstabe temp; while (s==0) { s = 1; for (i=0; i < 26 - 2; i++) { if (buchstaben[i].anzahl < buchstaben[i+1].anzahl) { temp = buchstaben[i]; buchstaben[i] = buchstaben[i+1]; buchstaben[i+1] = temp; s = 0; } } } return 0; } //-------------------------------------------------------------------------------------------------------------- //-------------------------------------------------------------------------------------------------------------- /* declarations.h */ #ifndef DEC #define DEC #define MAX_CHARS 26 #define MAX_LINE 257 #warning Header body is being read! struct buchstabe{ char der_buchstabe; int anzahl; float proz; }; int bubble_sort(struct buchstabe buchstaben[]); int plot_table(FILE *quelle,FILE *ziel,struct buchstabe buchstaben[],int nSize); int char_statistics(FILE *quelle, FILE *ziel); #endif //----------------------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------------------- # Tool Definition CC=gcc CCFLAGS= LNKOPT= # file definition OBJECTS= P11_1.o charstat.o plot.o bubblesort.o # rules monte: $(OBJECTS) $(CC) $(LNKOPT) -o charstatistic $(OBJECTS) P11_1.o: P11_1.c declarations.h $(CC) -c P11_1.c charstat.o: charstat.c declarations.h $(CC) -c $< plot.o: plot.c declarations.h $(CC) -c $< bubblesort.o: bubblesort.c declarations.h $(CC) -c $< clean: rm *.o charstatistic.exe