_C PROGRAMMING COLUMN_ by Al Stevens Listing One #include #include #define __MINMAX_DEFINED #include #include #include int main() { int dim; // --- get the number of integers to sort cout << "How many integers?\n"; cin >> dim; // --- a vector of integers vector vct; // --- insert values into the vector for (int i = 0; i < dim; i++) vct.insert(vct.end(), rand()); // --- display the random integers cout << "\n----- unsorted -----\n"; for (i = 0; i < dim; i++) cout << setw(8) << vct[i]; // --- sort the array with the STL sort algorithm sort(vct.begin(), vct.end()); // --- display the sorted integers cout << "\n----- sorted -----\n"; for (i = 0; i < dim; i++) cout << setw(8) << vct[i]; return 0; } Listing Two /* bibsrch.c -- dll for bible windows application */ #include #include #include #include #include #include "textsrch.h" #include "table.c" HFILE fp = HFILE_ERROR; static HFILE fn = HFILE_ERROR; unsigned DocNo = FIRSTDOCNO - 1; char DatabasePath[255]; #ifndef DOS_VERSION HCURSOR WtCurs; HCURSOR PCurs; #endif static int RetrievalMode; #ifdef DOS_VERSION /* =================================================== DOS Query Program-specific code =================================================== */ static char *BookName[] = { "Genesis", "Exodus", "Leviticus", "Numbers", "Deuteronomy", "Joshua", "Judges", "Ruth", "I. Samuel", "II.Samuel", "I. Kings", "II. Kings", "I. Chronicles", "II. Chronicles", "Ezra", "Nehemiah", "Esther", "Job", "Psalms", "Proverbs", "Ecclesiates", "Song of Solomon", "Isaiah", "Jeremiah", "Lamentations", "Ezekiel", "Daniel", "Hosea", "Joel", "Amos", "Obadiah", "Jonah", "Micah", "Nahum", "Habakkuk", "Zephaniah", "Haggai", "Zechariah", "Malachi", "Matthew", "Mark", "Luke", "John", "The Acts", "The Romans", "I. Corinthians", "II. Corinthians", "Galatians", "Ephesians", "Philippians", "Colossians", "I. Thessalonians", "II. Thessalonians", "I. Timothy", "II. Timothy", "Titus", "Philemon", "To the Hebrews", "Epistle of James", "I. Peter", "II. Peter", "I.John", "II. John", "III. John", "Jude", "Revelation" }; int FAR _export PASCAL GetVerse( char far *Text, char far *Note, int Book, int Chapter, int Verse); void FAR _export PASCAL NextVerse( char far *Text, char far *Note, int *Book, int *Chapter, int *Verse); void FAR _export PASCAL PrevVerse( char far *Text, char far *Note, int *Book, int *Chapter, int *Verse); static void Display(int bk, int chap, int verse, char *text) { printf("\nDocNo: %u ", DocNo); printf("%s %d:%d\n", BookName[bk-1], chap, verse); printf(text); } static void doquery(void) { static char query[300]; unsigned hits; int c = 0, offset, phrase; int bk, chap, verse; static char text[512]; static char note[257]; while (c != 'x') { c = 0; while (c != 'p' && c != 'q' && c != 'n' && c != 'x') { printf("\nP-hrase, Q-uery, N-avigate, or e-X-it: "); c = getch(); putch(c); } if (c == 'n') { printf("\nN-ext, P-revious, B-ook/chapter/verse: "); c = 0; while (c != 'p' && c != 'b' && c != 'n') { c = getch(); putch(c); } if (c == 'b') { printf("\nBook/chapter/verse: "); scanf("%d %d %d", &bk, &chap, &verse); *text = '\0'; GetVerse(text, note, bk, chap, verse); Display(bk, chap, verse, text); } else if (c == 'n') { NextVerse(text, note, &bk, &chap, &verse); Display(bk, chap, verse, text); } else if (c == 'p') { PrevVerse(text, note, &bk, &chap, &verse); Display(bk, chap, verse, text); } } else if (c != 'x') { phrase = c == 'p'; if (phrase) printf("\nEnter Phrase:\n"); else printf("\nEnter Query:\n"); gets(query); if (!LexicalScan(query, &offset, &hits, phrase)) printf("\n\aSyntax error"); else { int c; printf("\n%d hits. Continue? ", hits); c = getch(); putch(c); if (tolower(c) == 'y') { int ct = 0; if (phrase && hits) { SearchPhrase(&hits); printf("\n%d more hits. Continue? ", hits); c = getch(); putch(c); if (tolower(c) != 'y') hits = 0; } while (hits--) { putchar('\n'); NextFoundVerse(&bk, &chap, &verse); *text = '\0'; GetVerse(text, note, bk, chap, verse); Display(bk, chap, verse, text); ++ct; if ((ct % 4) == 0) { printf("\n[more...]"); if (getch() == 27) break; } } } EndSearch(); } } } } int main() { strcpy(DatabasePath, "bible.dat"); fp = OpenDataFile(); if (fp != HFILE_ERROR) { strcpy(DatabasePath, ""); if ((fn = OpenNotes(DatabasePath)) != HFILE_ERROR) { doquery(); _lclose(fn); } } _lclose(fp); return 0; } /* =================================================== End of DOS Query Program-specific code =================================================== */ #else /* =================================================== Windows DLL-specific code =================================================== */ static HINSTANCE hInst = NULL; int FAR PASCAL LibMain(HINSTANCE hInstance, WORD wDatSeg, WORD cbHeapSize, LPSTR lpCmdLine) { if (hInst == NULL) { hInst = hInstance; if (cbHeapSize > 0) UnlockData(0); } return 1; } int FAR PASCAL WEP(int nParameter) { if (fp != HFILE_ERROR) _lclose(fp); if (fn != HFILE_ERROR) _lclose(fn); fp = fn = HFILE_ERROR; return 1; } int FAR _export PASCAL RegisterAppl(HANDLE hWnd, char *path) { static HANDLE semaphore = 0; HANDLE sem = semaphore; if (semaphore == 0) { int plen; semaphore = hWnd; strncpy(DatabasePath, path, 255); plen = strlen(DatabasePath); strcat(DatabasePath, "\\bible.dat"); fp = OpenDataFile(); if (fp != HFILE_ERROR) { *(DatabasePath+plen) = '\0'; fn = OpenNotes(DatabasePath); } } return sem; } /* =================================================== end of Windows DLL-specific code =================================================== */ #endif /* Compute the book/chapter/verse spec from a document number */ void DocNotoBCV(unsigned docno, int *book, int *chapter, int *verse) { unsigned char *tb = BCVtable; int chapterct; #ifdef DOS_VERSION DocNo = docno; #endif *book = *verse = 0; while (*verse == 0) { (*book)++; *chapter = 0; chapterct = *tb++; while (chapterct--) { (*chapter)++; if (docno <= *tb) { *verse = docno; break; } docno -= *tb++; } } } /* Compute a document number from a book/chapter/verse spec */ static int ComputeDocNo(int book, int chapter, int verse) { unsigned char *tb = BCVtable; int chapterct; unsigned int docno = 0; if (book == 0 || chapter == 0 || verse == 0) return 0; /* ---- get to the book ---- */ while (--book) { /* ---- bypass chapters in the preceding books ---- */ chapterct = *tb++; while (chapterct--) docno += *tb++; } chapterct = *tb++; /* ---- test valid chapter ---- */ if (chapter > chapterct) return 0; /* ------ get to the chapter ---- */ while (--chapter) docno += *tb++; /* ---- test valid verse ---- */ if (verse > *tb) return 0; /* ---- build document number ---- */ DocNo = docno + verse; return 1; } /* Read the verse text specified by the current document number */ void ReadVerseText(char far *Text) { /* ---- get offset and bit from DocNo ---- */ struct versercd vr; long offset = sizeof(struct versercd); offset *= DocNo-1-(FIRSTDOCNO-1); _llseek(fp, offset+BCVOFFSET, SEEK_SET); _lread(fp, &vr, sizeof(struct versercd)); GetLine(Text, vr.offset, vr.bit); } /* Read verse text and note text specified by the current document number */ static void ReadVerse(char far *Text, char far *Note) { ReadVerseText(Text); GetNote(Note); } /* Get the next verse for the user */ void FAR _export PASCAL NextVerse( char far *Text, char far *Note, int *Book, int *Chapter, int *Verse) { if (RetrievalMode) NextNotedDocNo(); else { if (DocNo == MAXVERSE) DocNo = FIRSTDOCNO; else ++DocNo; } if (DocNo != FIRSTDOCNO - 1) { ReadVerse(Text, Note); DocNotoBCV(DocNo, Book, Chapter, Verse); } } /* Get the previous verse for the user */ void FAR _export PASCAL PrevVerse( char far *Text, char far *Note, int *Book, int *Chapter, int *Verse) { if (RetrievalMode) PrevNotedDocNo(); else { if (DocNo > FIRSTDOCNO) --DocNo; else DocNo = MAXVERSE; } if (DocNo != FIRSTDOCNO - 1) { ReadVerse(Text, Note); DocNotoBCV(DocNo, Book, Chapter, Verse); } } /* Get the verse specified by the user in a book/chapter/verse spec */ int FAR _export PASCAL GetVerse( char far *Text, char far *Note, int Book, int Chapter, int Verse) { if (ComputeDocNo(Book, Chapter, Verse)) { ReadVerse(Text, Note); return 1; } return 0; } /* Set the retrieval mode (next note/next verse) for next/previous */ void FAR _export PASCAL SetRetrievalMode(int mode) { RetrievalMode = mode; }