Electronic dictionary--two scan file Send/Link list method

Source: Internet
Author: User
Tags time in milliseconds

Electronic dictionary of two-scan file method

#define _crt_secure_no_warnings#include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h>struct Dict{char *key;char *content;}; int get_dict_size (file *pfile)//Get the total number of entries in the dictionary file {if (pfile = = NULL) return 0;int i = 0;char buf[2048];while (!feof (pfile)) {Fget S (buf, sizeof (BUF), pfile), fgets (buf, sizeof (BUF), pfile), i++;//read two lines, counter plus 1}return i;} Open the dictionary file and read the file contents int open_dict (struct dict **p, const char *dict_filename) {File *pfile = fopen (Dict_filename, "R"); if (PFI Le = = NULL) return 0;//Open file failed, function returned int size = Get_dict_size (pfile);//Get total number of entries in dictionary if (size = = 0) return 0;*p = (struct dict *) m  Alloc (sizeof (struct dict) * size);//Allocate Memory memset (*p, 0, sizeof (struct dict) * size) According to the total number of entries in the dictionary file, or initialize the allocated memory to 0struct dict *PD = *P;//PD points to the first address of the array p char buf[2048] = {0};size_t len = 0;int i = 0;fseek (Pfile, 0L, seek_set);//Set read location for dictionary file start while (!feof (PF ile)//loop through the file until the end of the file {memset (buf, 0, sizeof (BUF)), fgets (buf, sizeof (BUF), pfile);//Read the file one line len = strlen (BUF);// Get read to String length if (Len > 0) {pd[i].kEY = (char *) malloc (len);//Allocate Memory memset (pd[i].key, 0, Len) based on string length, strcpy (Pd[i].key, &buf[1]),//Copy the read to key} memset (buf, 0, sizeof (BUF)), fgets (buf, sizeof (BUF), pfile), Len = strlen (BUF), if (Len > 0) {pd[i].content = (char *) Mallo C (Len); memset (pd[i].content, 0, Len); strcpy (Pd[i].content, &buf[6]);} i++;} Fclose (pfile);//close the dictionary file return i;//returns the number of dictionary entries read}//according to the keyword key, look in the dictionary for content int search_dict (const struct DICT *p, int size, const Char *key, char *content) {int i = 0;for (i = 0; i < size; i++)//Traverse Dictionary {if ((P[i].key = = NULL) | | (p[i].content = = NULL)) Continue;if (strncmp (P[i].key, Key, strlen (key)) = = 0) {strcpy (content, p[i].content); return 1;//find a qualifying record, return 1}}return 0 ;//did not find a matching condition record, return 0}//free memory void free_dict (struct dict *p, int size) {int i = 0;for (i = 0; i < size; i++)//Loop release key and content if (P[i].key) free (P[i].key), if (p[i].content) free (p[i].content);} Free (P);//release P memory}int main (int argc, char *args[]) {if (ARGC < 2) {printf ("Usage:%s dict-filename\n", Args[0]); return 0;/ /Insufficient parameters, program exit}long STARt_ms = 0;//record function execution start time long End_ms = 0;//record function execution end time struct Dict *p = Null;start_ms = Clock (); int size = Open_dict (&p, ARGS[1]);//According to the first parameter of the command line as the dictionary file name, open the dictionary files if (size = = 0) return 0;//Open the dictionary file failed, the program exits End_ms = Clock ();p rintf ("Open_dict used%ld ms \ n ", End_ms-start_ms);//print function execution time, unit: Ms Char Key[2048];char content[2048];while (1) {memset (key, 0, sizeof (key)); memset (content, 0, sizeof (content)); scanf ("%s", key);//get user input from the keyboard if (STRNCMP (Key, "Command=exit", "n") = = 0) Break;start_ms = Clock (); if (Search_dict (p, size, key, content)//Retrieve {printf ("%s", content) in the dictionary according to user input;} else{printf ("not found\n");} End_ms = Clock ();p rintf ("Search_dict used%ld ms\n", End_ms-start_ms);//print function execution time, unit: milliseconds};start_ms = clock (); Free_dict (P , size);//release Memory End_ms = Clock ();p rintf ("Free_dict used%ld ms\n", End_ms-start_ms);//print function execution time in milliseconds return 0;}


The electronic dictionary realized by the chain list method

#define _crt_secure_no_warnings#include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h>struct Dict{char *key;char *content;struct dict *next;//Pointer to the next node of the list};//open the dictionary file and read the file contents int Open_dict ( struct Dict **p, const char *dict_filename)//open Dict.txt,and read Dict{file *pfile = fopen (Dict_filename, "R");//read-only open the text  if (pfile = = NULL) return 0;//failed to open the file, the function returns char buf[2048] = {0};size_t len = 0;int i = 0;//counter, recording the total number of entries read to *p = (struct dict *) malloc (sizeof (struct dict));//Allocation list first node memory memset (*p, 0, sizeof (struct dict)), struct dict *pd = *p;//pd point to the first address of the list while (! Feof (Pfile))//loop through the file until the end of the file {memset (buf, 0, sizeof (BUF)), fgets (buf, sizeof (BUF), pfile);//Read the file one line len = strlen (BUF);// Get read to String length if (Len > 0) {pd->key = (char *) malloc (len);//Allocate Memory memset (pd->key, 0, Len) based on string length; strcpy (Pd->key, & AMP;BUF[1]);//Copy the contents of the read to key}memset (buf, 0, sizeof (BUF)), fgets (buf, sizeof (BUF), pfile), Len = strlen (BUF), if (Len > 0) {pd->content = (char *) malloc (len); Memset (Pd->contENT, 0, Len); strcpy (Pd->content, &buf[6]);} Pd->next = (struct dict *) malloc (sizeof (struct dict));//Allocate memory for the next node of the list memset (pd->next, 0, sizeof (struct dict));pD = pd->next;//The PD refers to the position of the next node i++;} Fclose (pfile);//close the dictionary file return i;//returns the number of dictionary entries read}//according to the keyword key, look in the dictionary for content int search_dict (const struct DICT *p, int size, const Char *key, char *content) {const struct Dict *pd = p;while (PD)//Traversal Dictionary {if ((Pd->key) && (pd->content)) {if (str NCMP (Pd->key, Key, strlen (key)) = = 0) {strcpy (content, pd->content); return 1;//find a qualifying record, returning 1}}PD = pd->next;// Pointing to the next node position of the list}return 0;//did not find a matching condition record, return 0}//release list memory void free_dict (struct dict *p, int size) {struct Dict *pd = P;while (PD) {if ( Pd->key)//delete the key member in the linked list node memory free (pd->key); if (pd->content)//delete content member memory free (pd->content) in the linked list node; struct Dict *tmp = pd->next;//Save the address of the next node of the list free (PD);//Delete list current node PD = tmp;//p refers to the position of the downward node}}int main (int argc, char *args[]) {if ( ARGC < 2) {printf ("Usage:%s dict-filename\n", Args[0]), return 0;//parameter is insufficient, program exits}long Start_ms = 0;//Record the start time of the function execution long End_ms = 0;//The end time of the record function execution Start_ms = clock (); struct Dict *p = null;int size = open_dict (&p, arg S[1]);//based on the first argument of the command line as a dictionary file name, open dictionary files if (size = = 0) return 0;//Open dictionary file failed, program exits End_ms = Clock ();p rintf ("Open_dict used%ld ms\n" , End_ms-start_ms);//print function execution time, unit: Ms Char Key[2048];char content[2048];while (1) {memset (key, 0, sizeof (key)); Memset ( Content, 0, sizeof (content)), scanf ("%s", key);//get user input from the keyboard if (STRNCMP (Key, "Command=exit", "n") = = 0) Break;start_ms = Clock (); if (Search_dict (p, size, key, content)//Retrieve {printf ("%s", content) in the dictionary according to user input;} else{printf ("not found\n");} End_ms = Clock ();p rintf ("Search_dict used%ld ms\n", End_ms-start_ms);//print function execution time, unit: milliseconds};start_ms = clock (); Free_dict (P , size);//release list memory End_ms = Clock ();p rintf ("Free_dict used%ld ms\n", End_ms-start_ms);//print function execution time, unit: milliseconds return 0;}


Electronic dictionary--two scan file Send/Link list method

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.