문제가요...
파일 이름면을 입력받으면 그 파일을 찾아서 출력하고요..
그 파일에는 문자열이 저장되어 있어요.
그 문자열의 단어들을 사전순으로 정렬하고 그 단어가 있는
행도 같이 출력하는 거예요...
그리고 단어 하나를 입력 받아서 그 단어가 포함된
문장을 모두 출력하는 건데요...
제가 짰는데
자꾸 오류가 뜨고...뭐가 잘못된건지;;;
#include<stdio.h>
#include<string.h>
#include<conio.h>
#include<stdlib.h>
struct Data
{
int x;
char y;
};
//x는 행 y는 글자
struct Data binarysearch( char *[], char *);
// 발견된 부분의 행과 단어를 struct Data를 반환
// char *[] 는 message 를 넘겨받고 char *는 원하는 단어를 넘겨 받는다.
char *message[10];
char *word[50];
void main()
{
FILE *inFile;
int i;
char inkey;
inFile = fopen("input.txt", "r");
if (inFile == NULL )
{
printf("\n Failed to open the file.\n");
exit(1);
}
// 파일이 존재하지 않을 경우 출력
while (fscanf(inFile,"%s",&message) != EOF){
printf("%-s\n",message);
fscanf(inFile,"r",&word);
fclose(inFile);
}
//문장의 끝표시(^d)나타날때까지 inFile 에 있는
// 문장들 message에 저장
// 그리고 message에 저장된 문자열 출력
// fscanf를 이용하여 inFile에 있는 단어들 word배열로 전달
struct Data wordindex;
for(i=0;i<10;++i){
wordindex = binarysearch(message,word);
printf("\n%-5d %-20s\n", wordindex.x,wordindex.y);
}
// binarysearch함수에 넣는다.
//그리고 binarysearch 함수에서 그 단어가 어느 줄에 있는지
//리턴값으로 전달 받는다.
printf("\n\n Enter a word:");
scanf("%s", &inkey);
struct Data wordindex2;
wordindex2 = binarysearch( message, inkey);
//wordindex2 이진검색 함수로 정의
printf("%-10s", message[wordindex2.x]);
// 리턴 받음 라인값을 받은후 message문자열에 해당하는 그라인 출력
getch[];
fclose(inFile);
}
struct Data binarysearch(char *message[10], char *key)
//이진검색
{
struct Data temp; //발견된 위치 정보를 가지는 임시변수
int i,j;
char *cptr,*count; //cptr문자열에서 문자가 발견되는 위치의 주소
for( i=0; i< 10; ++i){
if((cptr = strstr(message[i],key)) != NULL)
// message[i][j]에서 key에 맞는 단어가 있는지 확인한다.
// 이때 결과 주소를 cptr에 대입한다.
//만약 NULL,즉 단어가 발견되었으?.........?▼?
{
temp.x =i+1;
//발견된 행은 ,i=0이므로 +1를 해주어야 한다.
temp.y = key;
// 구조체에 y에 검색한key를 넘긴다.
count = message[i];
// count에 message[i]의 시작주소를 넘긴다.
while(count != cptr){
//단어가 발견된 주속 cptr하고 같을때 까지 count를 증기시키면서
//검사한다.
count++;
}
}
}
return(temp);
// 발견된 행과 검색한 단어를 넘긴다.
}
|