奇怪的现象,求解释
程序代码:
[local]2[/local]程序就是读取数据库、检索。请先看注释吧。 我的其他文件在附件中,包括数据库文件,请用我用的文件(a.txt)进行测试。 #include <stdio.h> #include "strlib.h" #include "strlib.cpp" #include "file.h" #include "file.cpp" #include "scanner.h" #include "scanner.cpp" #include "genlib.h" #include "genlib.cpp" #define N 1000 //--------------------------------- typedef char *string; typedef struct { string name; string writers; string number; string mainwords; string publisher; string year; bool lend; }*bookT; typedef struct { bookT book[N+1]; }*bookDB; //-------------------------------- static void ReadDate(FILE *infile,bookDB books); static void CheckBook(bookDB books); static void SearchBook(string mainword,bookDB books); static void PrintBook(bookT book); main() { FILE *infile; bookDB books; printf("本程序将读入图书馆书库的数据库,并提供检索功能。\n"); infile=OpenFile("请输入图书馆数据库的文件名?"); books=(bookDB)malloc(sizeof(*books)*1); ReadDate(infile,books); fclose(infile); CheckBook(books); } static void ReadDate(FILE *infile,bookDB books) { string name; string writers; string number; string mainwords; string publisher; string year,tmp; int n=0; while(true){ books->book[n]=(bookT)malloc(sizeof(*(books->book[n]))); name=ReadLine(infile); books->book[n]->name=name; writers=ReadLine(infile); books->book[n]->writers=writers; number=ReadLine(infile); books->book[n]->number=number; mainwords=ReadLine(infile); books->book[n]->mainwords=mainwords; publisher=ReadLine(infile); books->book[n]->publisher=publisher; year=ReadLine(infile); books->book[n]->year=year; tmp=ReadLine(infile); if (StringEqual(tmp,"是")) { books->book[n]->lend=true; }else { books->book[n]->lend=false; } n++; tmp=ReadLine(infile); if (tmp==NULL) break; if (n==N) { printf("书籍过多,无法完全读取。\n"); break; } } books->book[n]=NULL; } static void CheckBook(bookDB books) { string mainword; printf("本程序将根据您所输入的关键词进行检索。\n"); printf("如果书中包含您所输入的关键词,那么书目的书名、第一作者姓名以及目录号码会显示出来。\n"); printf("请输出关键词:"); mainword=GetLine(); SearchBook(mainword,books); } static void SearchBook(string mainword,bookDB books) { int n=0; string tmp; while (books->book[n]!=NULL){ InitScanner(books->book[n]->mainwords); while(!StringEqual(tmp=GetNextToken(),"\0")){ if (StringEqual(mainword,tmp)) break; } if (StringEqual(tmp,"\0")){ n++; continue; }else{ PrintBook(books->book[n]); n++; } } } static void PrintBook(bookT book) { printf("书名:%s\n",book->name); InitScanner(book->writers); printf("第一作者:%s\n",GetNextToken()); printf("书号:%s\n\n",book->number); } 这是scanner.cpp的代码 #include <stdio.h> #include <ctype.h> #include <stdlib.h> #include <string.h> #include "scanner.h" static char *hang; static int jihao; static int chang; void InitScanner(char *line) { hang=line;//三个参数的初始化 chang=strlen(hang); jihao=0; } char *GetNextToken() { char dang; int kaishi; dang=hang[jihao]; if (isalnum(dang)){ kaishi=jihao; while ( isalnum(hang[jihao])){ jihao++; } return (SubString(hang,kaishi,jihao-1)); }else if (dang<0 || dang>128){ //汉字的编码在文件中是128以上,在机器中是负值,以此寻找汉字 kaishi=jihao; while ( hang[jihao]<0 ||hang[jihao]>128) ){ jihao++; } // printf("%s\n",SubString(hang,kaishi,jihao-1); //代码单独测试没有问题,但是在这个程序里,注释printf输出乱码,保留输出正确 return (SubString(hang,kaishi,jihao-1)); }else{ jihao++; return (CharToString(dang)); } } static char *SubString(char a[],int p1,int p2) { int i; char *s; if (p1<0) p1=0; if (p2>strlen(a)-1) p2=strlen(a)-1; if (p2<p1) { s=(char*)malloc(1); *s='\0'; return (s); } s=(char*)malloc(sizeof(char)*(p2-p1+2)); for(i=p1;i<=p2;i++){ s[i-p1]=a[i]; } s[i-p1]='\0'; return (s); } static char *CharToString(char a) { char *s; s=(char*)malloc(sizeof(char)*2); s[0]=a; s[1]='\0'; return (s); }
新建文件夹.rar
(6.22 KB)