В позапрошлом семестре сделал людям кучу курсовиков, связанных со строками. Вот близкий тебе пример: Задание на курсовую работу : Написать программу, открывающую файл, подсчитывающую количество слов в нём и выводящую в некоторый другой файл строки, содержащие слово и количество вхождений слова в исходный файл. Строки должны быть упорядочены по длине слова. | Код | /* *********************************** | filename - "cours12.c" | | | | This program open text file and | | find diferent words and count | | their quantity in text. Result | | save in file by words and | | quantity. | | | \*********************************/
#include <stdio.h> #include <stdlib.h> #include <string.h>
#define IsLetter(x) (((x>64)&&(x<91))||((x>96)&&(x<123))) #define IsNotLetter(x) ((x<65)||((x>90)&&(x<97))||(x>122))
char *infName,*outfName,*strFText,*strTime,*strWords,chrAct; FILE *inFile,*outFile; int FLen,n,m,QW,*intPos,intTime;
void main(void) { printf ("\nEnter full pathname of input file \n \t-"); /* Input file open */ scanf ("%s",infName); /* for read in text */ inFile = fopen(infName,"rt"); /* format. */ if (inFile==NULL) { printf ("Error in name of input file! \n"); getch (); return; /* Check on wrong */ }
printf ("Enter full pathname of output file \n \t-"); scanf ("%s",outfName); /* Output file */ outFile = fopen (outfName,"wt"); /* creat in text */ if (outFile==NULL) /* format. */ { printf ("Error in name of output file! \n"); getch (); return; /* Check on wrong */ }
FLen=0; strFText=calloc(1024,sizeof(char)); while((chrAct=fgetc(inFile))!=EOF) { *(strFText+FLen)=chrAct; *(strFText+FLen+1)=0; FLen++; }
strWords=calloc(FLen,sizeof(char)); for(n=0;n<FLen;n++) { if ((IsLetter(*(strFText+n))&&(n==0))||(IsLetter(*(strFText+n))&&(IsNotLetter(*(strFText+n-1))))) { strTime=calloc(128,sizeof(char)); do { *(strTime+strlen(strTime)+1)=0; *(strTime+strlen(strTime))=*(strFText+n); n++; } while(IsLetter(*(strFText+n))); strcat(strTime," "); if(strstr(strWords,strTime)==NULL) { strcat(strWords,strTime); QW++; } free(strTime); } }
intPos=calloc(QW,sizeof(int)); m=1; while(m<QW) { *(intPos+m)=(strstr(strWords+*(intPos+m-1)," ")-strWords)+1; m++; }
for(m=0;m<QW;m++) { for(n=m;n<QW;n++) { if ((strstr(strWords+*(intPos+n)," ")-strWords-*(intPos+n))>(strstr(strWords+*(intPos+m)," ")-strWords-*(intPos+m))) { intTime=*(intPos+n); *(intPos+n)=*(intPos+m); *(intPos+m)=intTime; } } }
for(m=0;m<QW;m++) { strTime=calloc(128,sizeof(char)); strncpy(strTime,strWords+*(intPos+m),strstr(strWords+*(intPos+m)," ")-strWords-*(intPos+m)); intTime=n=0; while (strstr(strFText+n,strTime)!=NULL) { if (IsNotLetter(*(strstr(strFText+n,strTime)-1))&&IsNotLetter(*(strstr(strFText+n,strTime)+strlen(strTime)))) { intTime++; } n=strstr(strFText+n,strTime)-strFText+strlen(strTime); } fputs(strTime,outFile); itoa(intTime,strTime,10); fputs(" ",outFile); fputs(strTime,outFile); fputs("\n",outFile); free(strTime); }
free(intPos); free(strWords); free(strFText); fclose(inFile); fclose(outFile); return; }
|
Это сообщение отредактировал(а) usawal - 12.2.2006, 00:41
|