Script started on Fri Feb 28 12:42:18 2003 uhunix2:~/212% cat makefile dictionary: dictionary.o wordsort.o gcc dictionary.o wordsort.o -o dictionary dictionary.o: dictionary.c dictionary.h gcc -c dictionary.c wordsort.o: wordsort.c dictionary.h gcc -c wordsort.c uhunix2:~/212% cat dictionary.h #include #include #include #define MAXWORD 38 //max size of a word, inc. terminating null. #define MAXWORDCOUNT 200 //max number of loaded words #define LINE (MAXWORD * 2 + 1) struct Word { char english[MAXWORD]; char japanese[MAXWORD]; char sortForm[MAXWORD]; }; #define Word struct Word /* in worsort.c */ int loadLine(char *line); int loadWord(Word newWord); void toSortForm(char *sortform); void printWordList(); uhunix2:~/212% cat dictionary.c /* dictionary.c */ /* A bilingual dictionary program. */ /* Takes a file with English and Japanese words, */ /* one per line, delimited by a tab. */ /* Sorts the file based on unmodified English form */ /* (initial parenthetical modifiers are removed) */ /* and prints it to the screen. */ /* Author: Zach Tomaszewski */ /* Date: 27 Feb 2003 */ #include "dictionary.h" int main(int argc, char *argv[]){ FILE *fileInp; char nextLine[LINE]; //if not given exactly one parameter or if help requested //print usage if (argc != 2 || strcmp(argv[1], "-h")==0 ) { puts("\nUsage:"); puts(" dictionary [text file to sort]\n"); return(0); //exit }else{ //open file fileInp = fopen(argv[1], "r"); //check for errors if (fileInp == NULL){ puts("Input error: could not read given file."); return(1); } //load word array while (fgets(nextLine, LINE, fileInp) != NULL) { loadLine(nextLine); } printWordList(); } } uhunix2:~/212% cat wordsort.c /* wordsort.c */ /* Part of a bilingual dictionary program. */ /* Loads an array of words, and sorts them based on */ /* their sort forms. */ /* Author: Zach Tomaszewski */ /* Date: 27 Feb 2003 */ #include "dictionary.h" Word wordList[MAXWORDCOUNT]; int wordListCount = 0; /* Takes a string of max length LINE, which must contain a tab. Splits the line on the tab, putting the first half in Word.english and the second half in Word.japanese. Copies Word.english to Word.sortform and removes any leading parethetical modifiers. Passes the new Word along to loadWord. If there is no tab, does nothing. Returns 1 on success, 0 on failure. */ int loadLine(char *line) { char *tab; Word newWord; tab = strchr(line, '\t'); if (tab != NULL) { *tab = '\0'; // set to null strcpy(newWord.english, line); strcpy(newWord.sortForm, line); strcpy(newWord.japanese, tab+1); toSortForm(newWord.sortForm); loadWord(newWord); return(1); }else{ return(0); } } /* Requires a pointer to a string. Edits that string in place by removing any preceding parethetical modifiers and converting to lowercase. */ void toSortForm(char *sortform){ char *cut; int i = 0; while (sortform[i] != '\0'){ sortform[i] = (char) tolower(sortform[i]); i++; } if (sortform[0] == '(') { cut = strpbrk(sortform, ")"); cut++; //to space just past found char strcpy(sortform, cut); toSortForm(sortform); //remove any more mods or spaces }else if (sortform[0] == '[') { cut = strpbrk(sortform, "]"); cut++; //to space just past found char strcpy(sortform, cut); toSortForm(sortform); }else if (sortform[0] == ' ') { strcpy(sortform, sortform + 1); //skip starting space toSortForm(sortform); } } /* Takes a Word with all three fields set. Inserts the Word into wordList and increases wordListCount. Returns 0 if successful, or 1 if array is full. */ int loadWord(Word newWord){ Word temp; int i = 0; if (wordListCount >= MAXWORDCOUNT) { return 1; //array full } while (i < wordListCount && strcmp(newWord.sortForm, wordList[i].sortForm) >= 0){ i++; } if (i == wordListCount) { //at end of array, just add wordList[wordListCount++] = newWord; }else{ do{ temp = wordList[i]; wordList[i] = newWord; newWord = temp; i++; }while (i <= wordListCount); wordList[++wordListCount]; //add last one to end } } /* Prints wordList to the screen. Formats it as Word.english first, at least one space, and then Word.japanese. One Word per line. */ void printWordList(){ register int i = 0; register int j = 0; int englishLength; for (i=0; i < wordListCount; i++){ printf(wordList[i].english); englishLength = strlen(wordList[i].english); for(j = 0; j <= (MAXWORD - englishLength + 1); j++){ putchar(' '); } printf(wordList[i].japanese); } } uhunix2:~/212% make gcc -c dictionary.c gcc -c wordsort.c gcc dictionary.o wordsort.o -o dictionary uhunix2:~/212% dictionary a5.txt A.M. gozen about (time) -koro -goro about ~ [Not used for time.] -kurai -gurai absent (o)yasumi (be) absent (from ~) (-o) yasumimasu address juusho after (not time) (-no) ato de after (time) -sugi again mata airplane hikooki all right daijoobu already moo (+aff.) always itsumo am desu America amerika amuse [not used for sports & music] asobimasu (Noun1) and (Noun2), etc. (N1) ya (N2) and [only at beginning of sentence] soshite and [used between two nouns] to And then sorekara (not) anything nanimo+Neg. (not to) anywhere dokoemo+Neg. April shigatsu are desu art bijutsu at (location) [with existence verb] ni at (place) [with action verb] de [with action verb]at (specific time) (specific time) ni [with existence verb](not) at all zenzen+Neg. August hachigatsu aunt obasan autumn aki bad warui bag baggu ball point pen boorupen balloon fuusen baseball yakyuu basketball basuketto(booru) bathroom (o)toire, (o)tearai beach umi beard hige beautiful utsukushii because (reason) kara, (result) bed beddo before mae before (not time) (-no) mae ni before (time) -mae - begin hajimemasu Besides [at beginning of a sentence] soreni bicycle jitensha big ookii bird tori birthday (o)tanjoobi (is) black kuroi black kuro blue ao (is) blue aoi boat fune body karada book hon bookstore honya boring tsumaranai born in (month) -gatsu umare - bow rei bowlful -pai - box lunch bentoo boy otoko no ko bread pan (a) break yasumijikan breakfast asagohan (is) brown chairoi brown chairo building tatemono bus basu busy isogashii But [at beginning of a sentence.] demo but [less formal than demo] (sentence 1) ga, (sentence 2) buy kaimasu by (tool) (tool) de by (transportation facility) (transportation) de cafeteria kafeteria camera kamera camp kyanpu can do well tokui can hear kikoemasu can see miemasu candy ame, kyandii cannot hear kikoemasen cannot see miemasen cap booshi car kuruma, jidoosha (playing) cards toranpu cat neko cent(s) -sento - chair isu cheap yasui child kodomo China chuugoku Chinese language chuugokugo chocolate chokoreeto chopsticks (o) hashi ( class jugyoo, kurasu classroom kyooshitsu clean kirei clock tokei (to) close shimemasu close, near chikai (Please) close. shimete kudasai cockroach gokiburi coffee koohii coffee shop kissaten cola koora (is) cold samui (a) cold kaze (is) cold (to the touch) tsumetai college daigaku college student daigakusei color iro come kimasu company kaisha company employee kaishain computer konpyuutaa concert konsaato Congratulations. omedetoo gozaimasu. cool [temperature] suzushii cousin itoko cup koppu cupful -pai - cute kawaii dance dansu uhunix2:~/212% exit uhunix2% ^D script done on Fri Feb 28 12:43:27 2003