strcmp(list[middle].name,list[search].name)이거 왜안되죠?
갤원
/*
Filename : 4.c
Written by : 2006920017 KimJinSoo
Date : 2008.03.13
*/
#includestdio.h
#includestdlib.h
#includestring.h
//Global Declarations
typedef struct
{
char name[10];
int id;
int math;
int eng;
}STUDENT;
int binsearch(STUDENT *list, int search,int left, int right);
//main start
void main()
{
//Local Definitions
int n=10;//the numbers of all student loading from the data.txt
int i;
int search;//get search from the user console
int result;//get result from the binsearch()
float sum1=0,sum2=0;
STUDENT *pArray=(STUDENT*)calloc(n,sizeof(STUDENT));//pArray which has 5 Struct Person
//read the file
FILE *fp;//fp means filepointer
fp=fopen(data.txt,r);//r means read
if(fp==0)
{
printf(CANNOT OPEN THE FILE, CHEAK PLEASE);//exception handling
exit(0);
}
fscanf(fp,%d,&n);//The fist number means the struct PERSON Numbers
//the number of all students in the dataprintf(The Number of Student is %d \n,n);
for(i=0;in;i++)//load the data from the file
{
fscanf(fp,%s,&pArray[i].name);//fscanf take fp into variable.
fscanf(fp,%d,&pArray[i].id);
fscanf(fp,%d,&pArray[i].math);
fscanf(fp,%d,&pArray[i].eng);
}
for(i=0;in;i++)
{
fprintf(stdout,%s ,pArray[i].name);
fprintf(stdout,%d ,pArray[i].id);
fprintf(stdout,%d ,pArray[i].math);
fprintf(stdout,%d ,pArray[i].eng);
printf(\n);
}
printf(Input the name (ex)Ann \n : );
scanf(%s,&search);
result = binsearch(pArray,search,0,n-1);//binsearch()
printf(Name:%s\nStudentID:%d\nEnglish:%d\nMath:%d\n\n,
pArray[result].name, pArray[result].id, pArray[result].math, pArray[result].eng);
//close the file pointer
fclose(fp);
free(pArray);
}//main end
int binsearch(STUDENT *list,int search, int left, int right){
int middle=(left+right)/2;
if(left=right)
{
switch(strcmp(list[middle].name,list[search].name))
{
case -1 :return binsearch(list,search,middle+1,right);
case +1 : return binsearch(list,search,left,middle-1);
case 0 :return middle;
}
}
return -1;
}access error가 나는데, 구조체를 가리키는 포인터에 문제가 있는건지;-_-
제가 switch 안에를 1로 했을땐 됬는데,
문제는 strcmp인거 같아염;; 근데 왜 그런건지 모르겠음.ㅜㅠ
-
남자
list의 범위 문제겠네요.
번호 | 제 목 | 글쓴이 | 날짜 |
---|---|---|---|
2693387 | 배열문제입니다 수정오류캡쳐했습니다 (6) | 연하얀 | 2025-05-01 |
2693356 | text 입출력 내림차순 질문입니다 ㅠ | 빛글 | 2025-05-01 |
2693328 | C언어를이용해서 .txt파일 외에 다른 확장자 파일 삭제가 가능한지.. (2) | 대나무 | 2025-05-01 |
2693299 | 파일입출력 바이너리파일 | 독특한 | 2025-04-30 |
2693273 | 오류 (1) | 귀1여운렩 | 2025-04-30 |
2693080 | visual studio 2008 express edition 등록키 말인데요 | 얀별 | 2025-04-28 |
2693053 | 배열, 구조체 관련 프로그래밍 질문드립니다. | 싸리 | 2025-04-28 |
2693025 | 프로그램을 짜봤는데요 ㅠㅠ | 상처입은마음 | 2025-04-28 |
2693001 | 워닝문제, 세그멘트결함문제 (1) | 월식 | 2025-04-28 |
2692979 | 라인한줄 이랑.. 소스 설명좀 부탁드려요.. | 이루리 | 2025-04-27 |
2692947 | 이 문제좀 풀어 주세요..ㅜㅜ (1) | 소리 | 2025-04-27 |
2692889 | 함수의 구조체 인자로 받아서 그 인자로 데이터 넣기... | 한뎃집 | 2025-04-27 |
2692862 | 성적 출력 하는 프로그램인데요~!!!도움좀 주세욤.ㅠ | 두빛나래 | 2025-04-26 |
2692831 | if 문 간단해요 빨리좀 ㅠㅠ | 이플 | 2025-04-26 |
2692805 | 실행파일이 이상해요 | 푸헷 | 2025-04-26 |
2692750 | 퀵정렬 질문이요 . | 동생몬 | 2025-04-25 |
2692700 | 이 소스코드에 문제 있나요?? (2) | 초코초코해 | 2025-04-25 |
2692596 | 오류좀 잡아주세요 | 하나 | 2025-04-24 |
2692510 | 함수형 중에서.. (6) | 한란 | 2025-04-23 |
2692483 | C언어 함수, Header | 떠나간그놈 | 2025-04-23 |