030430511C语言课设

030430511C语言课设

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 030430511 0 <¶ R C ¨ ªz <¶ R z ¨ ª { 1„ EÎ E p Ñ C .a 'ƒ @ II 0 10 <Ô Ù Ð Î 0 20 E < @Î 0 3<¶ R z ¨ 0 ª 0 4<¶ R z ¨ 0 ª 0 50 0 6<¶ R z ¨ 0 ª 0 7<¶ R z ¨ 0 ª C-8 0 ³ 80 €o ³ 2€o 6+ * ª °¶ª< ÀT ïàH + p88 < ÎÊ á…C µª< struct word *creat(void); <ª*¶ & / ±å “ diction.txt” <<xá< Î C…Î& ªµ Ê */ struct word *insert_a_word(struct word *head, char *wo); &8 + /* x <Î 2 6 I */ struct word *insert(struct word *head, char *wo); <ª < ± <åÎ Ô ð Ù Ñ ¶ /* 0 insert_a_word()˜ */ struct word *insert_words(struct word *head, char *filename); /* *ˆ ³ <Ô Î insert()Ù Ð */ struct word *del(struct word *head, char *wo); <ª ¶ <å /Ô*Ñ ˜ ± Î ðÙ */ void search(struct word *head, char *wo); /* *ˆ */ void list(struct word *head); <</±Î Ô*ðÙå Ñ ˜ ª¶ */ struct word *delall(struct word *head); /* *ˆ ³ */ void write_to_file(struct word *head, char *filename); < /* Î Ô Ù Ð <ª ± å filename ¶ & diction.txt0 , ³ˆ * 0 */ void Menu_f(); </¶*ç x ª± 0 main @ Î 8 2 < + x X#include <stdlib.h> #include <stdio.h> #include <string.h> #include <math.h> #define LEN sizeof(struct word) #define PAUSE system("pause"); #define CLS system("cls"); int n; struct word {char w[20]; struct word *next; }; struct word *create() {FILE *fp; struct word *head,*p1,*p2; head=NULL; n=0; if((fp=fopen("DICTION.txt","r+"))==NULL) {printf("no find"); return(head);} p1=p2=(struct word )malloc(LEN); fscanf(fp,"%s",p1->w); while(!feof(fp)) {n=n+1; if(n==1) head=p1; else p2->next=p1; p2=p1; fscanf(fp,"%s",p1->w); } p2->next=NULL; <ª ¶ ± /*x ç */ ª ¶± /x ç /*p2 0 p1 0 < ±ç p2->next 0 ª ¶ p1 x < ±ç ,0 ª ¶ p1 x */ */ <Î Ô Ù / Ð /* /* {Š Ð s */ */ */ */ ±ç /m w¶ord* xª <¶ /*x ª ±ç < ¶ord x ª w± ç */ <Î /Ù Ð Ô /* /* /* {Œˆ {Œˆ */ */ word 0 */ */ */ ªç #define PR printf("********************\n") < ¶ ± /*x */ p1=(struct word *)malloc(LEN); fclose(fp);/*0 return(head); } */ struct word *insert_a_word(struct word *head,char *wo) {struct word *p1,*p2,*p; if(head==NULL) {head=(struct word *)malloc(LEN); n=n+1; strcpy(head->w,wo); head->next=NULL; return(head); } p1=p2=head; p=(struct word )malloc(LEN); strcpy(p->w,wo); {p2=p1; p1=p2->next;} if(strcmp(wo,p1->w)==0) <ä {printf("\nword %s has already exist!\n\n",wo); ª ¶ ± /*x PAUSE; return(head); } if(strcmp(wo,p1->w)<0&&p1!=NULL) if(p1==head) {head=p; p->next=p1; } else p2->next=p; p->next=p1; if(p1==NULL) {p2->next=p; p->next=NULL; } n=n+1; return(head); } struct word *insert_words(struct word *head) {FILE *fp; char str[20]; CLS; <ª ¶ /*x ±ä */ /*0 */ <ª ¶ /*x ±ä */ */ /* /m word 0 ð <ª ¶ ± p x ä */ */ */ Î Î << µ… Ê á Î while(strcmp(wo,p1->w)>0&&p1!=NULL) < <ªC/* PR; printf("\nplease input a filename(*.txt):"); scanf("%s",str); if((fp=fopen(str,"r"))==NULL) PAUSE; return(head); } while(!feof(fp)) {fscanf(fp,"%s",str); head=insert_a_word(head,str); } if(!feof(fp)) {printf("\ninsert words error!"); PAUSE; fclose(fp); return(head); } else {printf("\ninsert words from the text successfully!\n\n"); PAUSE; fclose(fp); return(head); } } struct word *del(struct word *head,char *wo) {struct word *p1,*p2; p1=head; while(strcmp(p1->w,wo)&&p1!=NULL) {p2=p1; p1=p2->next; } if(p1==NULL) return(head); if(p1==head) head=p1->next; if(p1->next==NULL) p2->next=NULL; else p2->next=p1->next; n=n-1; return(head); } <ªÎ<¶ Ô R /*˜ Ùz < Ñ */ /* ¶ ±I ¬ */ /*0 <¶ R ª inserty è */ /* Ð /* */ Ð */ {printf("\n%s file does not exist!\n",str); void search(struct word *head) {struct word *p; char str[20]; p=head; CLS; PR; printf("\nplease enter the word you want to search:"); scanf("%s",str); while(strcmp(p->w,str)&&p!=NULL) {p=p->next;} if(p==NULL) {printf("\nthe word you want to search not found!\n"); PAUSE; return; } else PAUSE; return; } } void list(struct word *head) {struct word *p; int k=0; p=head; CLS; if(head==NULL) {PR; printf("\nDictionary is null!\n"); PAUSE;return; } PR; printf("now,these words are:\n"); while(p!=NULL) {k=k+1; printf("\t%s\n",p->w); p=p->next; if(k%10==0){ PAUSE; CLS; PR; } < ª 0¶ ± k ˜ /* ç 10 0 */ <Î /* Ð ÔÙ */ ª µ< /*0 < pÎCáÎ…Ê < Î */ {printf("\nword found:\t%s\n",p->w); } printf("\n"); PAUSE; } struct word *delall(struct word *head) {head=NULL; n=0; CLS; PR; printf("\nDictionary is null!\n"); PAUSE; return(head); } struct word *reverse (struct word *head) {struct word *p1,*p2,*NEWhead; p1=head; if(head==NULL) {CLS; PR; printf("\ndictionary is null!\n"); PAUSE; return(head); } while (p1->next!=NULL) {p2=p1;p1=p2->next;} ª Ñ NEWhead=p1; <¶ R <ÎÊz Ô Ù /*˜ p1->next=p2; p2->next=NULL; flag:p1=head; while (p1->next!=NULL) {p2=p1;p1=p2->next;} if(p1==head) {p1->next=NULL; CLS; PR; printf("\nDictionary has been reversed!\n"); PAUSE; return(NEWhead); } else {p1->next=p2; < ÔÙ p2->next=NULL; Î /* Ð goto flag; /*0 FLAG ±® ˜ NEXT 0 */ <Î Ô Ù NULL Ð */ /*0 */ */ <¶ R z <Î Ô Ù /*˜ ª Ñ word 0 */ } } void write_to_file (struct word *head) {FILE *fp; struct word *p; int i; p=head; fp=fopen("words.txt","w+"); while(p!=NULL) /* p=p->next; } fclose(fp); return; } struct word *insert_a_wordMenu(struct word *head) {char str[20]; int k; k=n; CLS; PR; printf("\nplease input a new word:"); scanf("%s",str); head=insert_a_word(head,str); if(n==k) goto end; printf("\nyou succeed in inserting the word:%s!\n",str); PAUSE; end:return(head); } struct word *delMenu(struct word *head) {char str[20]; int k; k=n; CLS; PR; printf("\nplease enter the word you want delete:"); scanf("%s",str); head=del(head,str); if(n==k) {printf("\nword not been found!\n"); PAUSE; ¶ ³ /¨ */ {fprintf(fp,"%s ",p->w); return(head); } printf("\nsuccessfully delete the word:%s!\n",str); PAUSE; return(head); } void Menu_f() {CLS; printf("\n"); printf("*************************Dictionary management system***************************\n"); printf("\n"); printf("\n\t1.Insert word"); printf("\n\t2.Insert words from a text file"); printf("\n\t3.Delete word"); printf("\n\t4.Search word"); printf("\n\t5.List all words"); printf("\n\t6.Delete all words"); printf("\n\t7.Reverse the word list"); printf("\n\t0.Exit"); printf("\n\tEnter you Choice:"); } void mainmenu (struct word *head) {int n; do{Menu_f(); scanf("%d",&n); getchar(); switch(n) { case 1:head=insert_a_wordMenu(head);break; case 2:head=insert_words(head);break; case 3:head=delMenu(head);break; case 4:search(head);break; case 5:list(head);break; case 6:head=delall(head);break; case 7:head=reverse(head);break; case 0:write_to_file(head);return; default:printf("\n\t\tInput Error!Enter your choice again!\n\t\t"); PAUSE; } } while(n!=0); } main() {struct word *head; head=create(); mainmenu(head); } ª ¶± ç ˜ À * ª ´{¶ T ž{à ÎH ÿ3 i= ¶ª<ª ‡ ¯ Ø v ð ° •ð 68 I <Î 2 + I u AI 8 6 + 68 I <Î 2 + ³ MAIN ¶ vp 3 p1 •ð p2 ¶ ³ ;à <z Ù Ñ R VC 0 <ª ¶ DÎIÔIC.C˜ <ª ¶ ± ç DIC.C ˜ 0 ª¶ ± ç ˜ II I a6 + p8 ÑÎ< T+ ƒL 8 ª ¶ ±ç ˜ 0 ª¶± Insertç ˜ 0 ª Del ˜ ¶± ç <ª ¶ <Î z IÔ Ù Ñ ˜ R <Ô Î DeallÙ Ð <Î C…ª µ á < Î <Ê Î strcmp(); strcmp(); •ð ð p2 0 p1 ³ (X <ª p1,p2 ˜ <ÎR IÙ Ñ ¶ Ôz < ±ç ,0 p2->next=p1->next 0 ª ¶ p1 ˜ f Creat 0 <Ô Î head,Ù Ð p2 •ð I u p1 A 8 6 I + 3 ±ç p2 0 < ª ¶ p1 ˜ ؇{¶ °ðv¯ª< Î 08 Reverse [<Î 5 à Ì 0 0 DICTION.TXT,ZHOU,KNN. ð exam,knn,language,student,study,zhou 0 zhou,study,student,language,knn,exam <¶ ± ç ˜ ª words.TXT ª ¶± ç ˜ .0 ...
View Full Document

This note was uploaded on 11/08/2009 for the course ISYS 2350 taught by Professor Joserf during the Three '08 term at University of Sydney.

Ask a homework question - tutors are online