Compile an Electronic Dictionary (including the creation of custom header files and linked lists)

Source: Internet
Author: User
Tags random seed

Loading editor...

The function of this electronic dictionary is to query words, review words, learn words, and fill in the blanks,
Word storm with time prompt

Part 1: Compile the main function
# Include <stdio. h>
# Include <string. h>
# Include "list. h"
# Include "tool. h"
# Include "game. h"
# Include <stdlib. h>
Int main ()
{
Int m = 0, n = 7950, c;
Char ch [130];
Struct wordnode * head = NULL;

Head = Create (head, m );

Printf ("welcome to this electronic dictionary: \ n ");
Printf ("\ n electronic dictionary function: \ n ");
Printf ("0 quit ");
Printf ("");
Printf ("1 QUERY \ n ");
Printf ("2 help ");
Printf ("");
Printf ("3 Review \ n ");
Printf ("4 Learning ");
Printf ("");
Printf ("5 blank \ n ");
Printf ("6 ");
Printf ("");
Printf ("7 fast memory \ n ");
Printf ("8 detailed introduction to the electronic dictionary \ n ");
Printf ("9 clear screen \ n ");
While (1)
{
Printf ("* 007 brand electronic dictionary * \ n ");
Printf ("************************************* **************************************** ** \ n ");
Printf ("input command (0 exit, 1 query, 2 help, 3 Review, 4 learning, 5 blank, 6 translation, 7 memory, 8 Introduction, 9 clear screen) \ n ");
Printf ("************************************* **************************************** ** \ n ");
If (scanf ("% d", & c) = 1)
{
If (c = 2)
{
Printf ("\ n electronic dictionary function: \ n ");
Printf ("0 quit ");
Printf ("");
Printf ("1 QUERY \ n ");
Printf ("2 help ");
Printf ("");
Printf ("3 Review \ n ");
Printf ("4 Learning ");
Printf ("");
Printf ("5 blank \ n ");
Printf ("6 ");
Printf ("");
Printf ("7 fast memory \ n ");
Printf ("9 clear screen \ n ");
}
Else if (c = 9)
System ("cls ");
Else if (c = 6)
{
Game (head, n); // In list. h
}
Else if (c = 8) // description file
{
Printf ("Regular Expression: \ n ");
Printf ("input: Regular Expression string to be matched string \ n ");
Printf ("output: If the matching is successful, the entire string \ n" is output ");
Printf ("^ represents the start of the string \ n. represents any character \ n $ represents the end of the string \ n ");
Printf ("Example: \ n ");
Printf ("input: ads \ n ");
Printf ("output: croseroads n. intersection, crosstab \ n ");
Printf ("select the 3-Word Learning option, and seven words will be randomly selected for your memory \ n ");
Printf ("1 query a word (Regular Expression supported) \ n ");
Printf ("0 out of the electronic dictionary \ n ");
Printf ("2 print the Help file \ n ");
Printf ("3 word review options \ n ");
Printf ("4 word learning options \ n ");
Printf ("5 words fill in the blanks \ n ");
Printf ("6 write English \ n according to Chinese ");
Printf ("7 fast memory \ n ");
Printf ("9 clear screen \ n ");
}
Else if (c = 0)
{
Printf ("welcome to use this dictionary \ n" next time ");
Break;
}
Else if (c = 5)
{
Fill (head, n); // fill in the blanks
}
Else if (c = 1)
{
Printf ("Enter the word to search: \ n ");
Getchar ();
Gets (ch );
N = LookUp (head, ch); // n is the number of words.
}
Else if (c = 7) // fast memory
{
Storm (head, n); // in game. h
}
Else if (c = 3) // word review options
{
Review (); // In list. h
}
Else if (c = 4) // Word Learning Option
{
MemWord (head, n); // In list. h
}
Else
Printf ("invalid operation! \ N ");
}
Else
Printf ("invalid operation! \ N ");
}

Return 0;
}

Part 2:

Game. c file

# Include "game. h"
# Include <stdio. h>
# Include "list. h"
# Include "tool. h"
# Include <time. h>
# Include <stdlib. h>
# Include <string. h>
# Include <windows. h>
Int game (wordnode * head, int n) // spell according to Chinese Characters
{
Int count, m, c, flag;
Char ch [32];
Wordnode * p = head;

If (head = NULL)
Return 0;

Srand (time (NULL ));
While (1)
{
Flag = 0;
Count = 0;
M = rand () % n;
P = head;
While (p! = NULL)
{
If (count = m)
{
Printf ("% s \ n", p-> Chinese );
While (1)
{
Printf ("Enter the English Translation of Chinese :");
Scanf ("% s", ch );
If (strcmp (ch, p-> Eword) = 0)
{
Printf ("spelling correct \ n ");
Break;
}
Else
{
Printf ("misspelled ,");
Printf ("continue to input. Please input 1 ,");
Printf ("exit this word, please enter any number \ n ");

Scanf ("% d", & c );
If (c! = 1)
{
Printf ("% s \ n", p-> Eword, p-> Chinese );
Break;
}
}
}
}
Count ++;
P = p-> next;
}
Printf ("if you want to continue the Chinese-English Translation test, enter 1 and exit. Please enter any number \ n ");
Scanf ("% d", & c );
If (c! = 1)
Break;
}
Return 0;
}

Int fill (wordnode * head, int n) // randomly spot check words, empty at random, check word spelling
{
Int count, I, x [2], m, c, len, q;
Char ch [32], str [32];
Wordnode * p = head;

While (1)
{
Memset (x, 0, sizeof (x); // initialization, cleared
Count = 0;
Srand (time (NULL ));
M = rand () % n;
P = head;
While (p! = NULL)
{
If (count = m)
{
Strcpy (ch, p-> Eword );
Len = strlen (p-> Eword );
If (len> 4) // a word with a length greater than four letters
{
Srand (time (NULL); // Random Seed
For (I = 0; I <2; I ++)
X [I] = rand () % len;
BubbleSort (x, 2 );
Q = 0;
For (I = 0; I <len; I ++)
{
If (I = x [q])
{
Ch [I] = '_';
Q ++;
}
Printf ("% c", ch [I]);
}
Printf ("\ n ");
Ch [len] = '\ 0 ';
While (1)
{
Scanf ("% s", str );
If (strcmp (str, p-> Eword) = 0)
{
Printf ("correct! \ N % s \ n ", p-> Eword, p-> Chinese );
Break;
}
Else
{
Printf ("error, re-input: 1; Chinese prompt: 2; exit the word and display the answer: Any number \ n ");
Scanf ("% d", & c );
If (c = 2)
{
Printf ("% s \ n", p-> Chinese );
Printf ("Please enter \ n ");
}
Else if (c! = 1 & c! = 2)
{
Printf ("% s \ n", p-> Eword, p-> Chinese );
Break;
}
Else
Continue;
}
}
}

Else // words with a length less than 5 letters
{
Srand (time (NULL ));
X [0] = rand () % len;
For (I = 0; I <len; I ++)
{
If (I = x [0])
Ch [I] = '_';
Printf ("% c", ch [I]);
}
Ch [len] = '\ 0 ';
Printf ("\ n ");
While (1)
{
Scanf ("% s", str );
If (strcmp (str, p-> Eword) = 0)
{
Printf ("correct! \ N % s \ n ", p-> Eword, p-> Chinese );
Break;
}
Else
{
Printf ("error, re-input: 1, Chinese prompt: 2, exit the word and display the answer: Any number \ n ");
Scanf ("% d", & c );
If (c = 2)
{
Printf ("% s \ n", p-> Chinese );
}
Else if (c! = 1 & c! = 2)
{
Printf ("% s \ n", p-> Eword, p-> Chinese );
Break;
}
Else
Continue;
}
}
}
}
Count ++;
P = p-> next;
}
Printf ("to continue, please enter the number 1, exit, please enter any number \ n ");
Scanf ("% d", & c );
If (c! = 1)
Break;
}
Return 0;
}
Int storm (wordnode * head, int n)
{
Int per, x, t, I, j, flag, count, m;
Char str1 [100] [31], str2 [100] [101], str3 [31];
Wordnode * p = head;
If (head = NULL)
Return 0;

Printf ("Enter the number of words to be tested and the time limit for each word \ n ");
Scanf ("% d", & x, & per );
T = x;
X = x * per;
I = 0;
While (x)
{
Srand (time (NULL ));
Flag = 0;
Count = 0;
Printf ("\ r"); // place the time cursor in the same place
Printf ("Remaining Time: % d seconds", x --); // Output Time
M = rand () % n;
Sleep (1000); // Time Interval
P = head;
While (p! = NULL)
{
If (count = m & (x + 1) % per = 0)
{
Printf ("% s", p-> Eword, p-> Chinese); // outputs both Chinese and English characters.
Flag ++; // record the number of existing words
Strcpy (str1 [I], p-> Eword); // The following four statements are used to save the word that appears.
Str1 [I] [strlen (p-> Eword)] = '\ 0'; // end the Array
Strcpy (str2 [I], p-> Chinese );
Str2 [I ++] [strlen (p-> Chinese)] = '\ 0 ';
}
Count ++;
P = p-> next;
}
If (flag = x) // when the output is consistent with the number of input words, the loop exists.
Break;
}

System ("cls"); // clear screen
For (I = 0; I <t; I ++)
{
Printf ("test now: \ n ");
Printf ("% d % s \ n", I + 1, str2 [I]); // output Chinese prompt
Scanf ("% s", str3); // enter the word
If (strcmp (str3, str1 [I]) = 0) // you can determine whether the request is consistent.
Printf ("correct \ n ");
Else
Printf ("error, answer: % s \ n", str1 [I]);
If (I <t-1) // when not all words are output, ask whether to continue the test. if the output is complete, do not execute this statement
{
Printf ("continue to complete the following words, enter 1; otherwise, press any number key \ n ");
Scanf ("% d", & j );
If (j! = 1)
Break;
}
}
Return 0;
}

Part 3:

Game. h file

# Ifndef _ GAME_H
# Define _ GAME_H
# Include "list. h"
# Include "tool. h"
Int game (wordnode * head, int n );
Int fill (wordnode * head, int n );
Int storm (wordnode * head, int n );
# Endif

Part 4

List. c file

# Include <stdio. h>
# Include <string. h>
# Include <stdlib. h>
# Include "list. h"
# Include "tool. h"
# Include <time. h>
Int n;
Int RegSearch (char re [], char target [])
{
Int len_re, len_target;
Int I, j;

Len_re = strlen (re );
Len_target = strlen (target );

If (re [0] = '^' & re [len_re-1] = '$ ')
{
If (len_re-2 )! = Len_target)
Return 0;
Else
{
For (I = 1; I <len_re-1; I ++)
If (re [I]! = Target [I-1] & re [I]! = '.')
Return 0;
If (I = len_re-1)
Return 1;
}
}

If (re [0] = '^' & re [len_re-1]! = '$ ')
{
If (len_re-1> len_target)
Return 0;
Else
{
For (I = 1; I <len_re; I ++)
If (re [I]! = Target [I-1] & re [I]! = '.')
Return 0;
If (I = len_re)
Return 1;
}
}

If (re [0]! = '^' & Re [len_re-1] = '$ ')
{
If (len_re-1> len_target)
Return 0;
Else
{
For (I = len_re-2, j = len_target-1; I> = 0; I --, j --)
If (re [I]! = Target [j] & re [I]! = '.')
Return 0;
If (I =-1)
Return 1;
}
}

If (re [0]! = '^' & Re [len_re-1]! = '$ ')
{
If (len_re> len_target)
Return 0;
Else
{
For (j = 0; j <= len_target-len_re; j ++)
{
For (I = 0; I <len_re; I ++)
{
If (re [I]! = Target [j + I] & re [I]! = '.')
{
Break;
}
}
If (I = len_re)
Return 1;
}
If (j = len_target-len_re + 1)
Return 0;
}
}
Return 0;
}

Wordnode * Create (struct wordnode * head, int m) // Create a linked list
{
Char ch [133];
Char str1 [31], str2 [1, 101];

Struct wordnode * p = NULL, * pr = head;

FILE * f1 = NULL;
F1 = fopen ("dict. TXT", "r ");
If (f1 = NULL)
Return 0;

While (fgets (ch, 300, f1 )! = NULL)
{
M ++;
Trim (ch );
Seg (ch, str1, str2 );
P = (struct wordnode *) malloc (sizeof (struct wordnode ));
Strcpy (p-> Eword, str1 );
Strcpy (p-> Chinese, str2 );

If (head = NULL)
{
Head = p;
Pr = head;
}
Else
{
While (pr-> next! = NULL)
Pr = pr-> next;
Pr-> next = p;
}
P-> next = NULL;
}
N = m;
Printf ("Total Number of words included in this electronic dictionary % d \ n", m );
Fclose (f1 );
Return head;
}

Int LookUp (wordnode * head, char word []) // search for words
{
Int count = 0;
Wordnode * p = head;
If (head = NULL)
Return 0;

While (p! = NULL)
{
If (RegSearch (word, p-> Eword) = 1)
{
Printf ("% s \ n", p-> Eword, p-> Chinese );
Count ++;
}
P = p-> next;
}
Printf ("% d matched \ n", count );
Return n;
}

Int MemWord (wordnode * head, int num)
{
Int I, nums [7];
FILE * f1 = NULL;
FILE * f2 = NULL;

Struct tm * nt;
Time_t lt;
Char str [80];
Int n = 0;
Wordnode * p = head;

Lt = time (NULL );
Nt = localtime (& lt );
Strftime (str, sizeof (str), "% Y-% m-% d", nt );
Printf ("% s \ n", str );

F1 = fopen (str, "w ");
F2 = fopen ("dict. TXT", "r ");

Memset (nums, 0, sizeof (nums ));

Srand (time (NULL); // generates a random number
For (I = 0; I <7; I ++)
Nums [I] = rand () % num;

BubbleSort (nums, 7); // sort by bubble

I = 0;
If (head = NULL)
Return 0;

While (p! = NULL & I <= 6)
{
If (n = nums [I])
{
I ++;
Printf ("% s \ n", p-> Eword, p-> Chinese );
Fputs (p-> Eword, f1); // records the learned words to another file.
Fputs ("", f1 );
Fputs (p-> Chinese, f1 );
Fputs ("\ n", f1 );
}
N ++;
P = p-> next;
}

Fclose (f1 );
Fclose (f2 );
Return 0;
}

Int Review ()
{
Int day;
Char ch [132];
FILE * f1 = NULL;
Struct tm * nt;
Time_t lt, t1;
Char str [80];
Lt = time (NULL );
Nt = localtime (& lt );
Strftime (str, sizeof (str), "% Y-% m-% d", nt );
Printf ("today's date: % s \ n", str );

Printf ("When do you want to review the words? Enter 0 a few days ago (for more information, enter 0) \ n ");
Scanf ("% d", & day );
Nt-> tm_sec = nt-> tm_sec-86400 * day; // The current time minus the Time of the past few days, that is, the time

T1 = mktime (nt );
Nt = localtime (& t1 );
Strftime (str, sizeof (str), "% Y-% m-% d", nt );
Printf ("% s \ n", str );

Printf ("your current review date is: % s \ n", str );
F1 = fopen (str, "r ");
If (f1 = NULL)
{
Printf ("this file does not exist \ n ");
Return 0;
}
Else
{
While (fgets (ch, 132, f1 )! = NULL)
Printf ("% s", ch );
}
Printf ("\ n ");
Fclose (f1 );

Return 0;
}

Part 5

List. h file

# Ifndef _ LIST_H
# Define _ LIST_H
Typedef struct wordnode {
Char Eword [31];
Char Chinese [101];
Struct wordnode * next;
} Wordnode;
Int RegSearch (char reg [], char target []);
Wordnode * Create (struct wordnode * head, int m );
Int LookUp (wordnode * head, char word []);
Int MemWord (wordnode * head, int num );
Int Review ();
# Endif

Part 6

Tool. c file

# Include <stdio. h>
# Include "tool. h"
# Include "list. h"
# Include <string. h>
# Include <ctype. h>
# Include "game. h"
Int Seg (char ch [], char str1 [0, 101], char str2 [31])
{
Int I, len, p, flag = 0;

Len = strlen (ch );
For (I = 0; I <len; I ++)
{
If (ch [I]! = ''& Flag = 0)
{
Str1 [I] = ch [I];
P = I;
}
Else
{
Flag = 1;
Str2 [i-p-1] = ch [I + 1];
}
}

Str1 [p + 1] = '\ 0 ';
Str2 [len-p-1] = '\ 0 ';

Return 0;
}

Int Trim (char word [])
{
Int I, p = 0, q = 0, x;

X = strlen (word );

For (I = 0; I <x; I ++)
{
If (word [I] = ''| word [I] = '\ n' | word [I] =' \ t ')
P ++;
Else
Break;
}
For (I = 0; I <x; I ++)
Word [I] = word [p + I];
X = x-p;
Q = x;
For (I = X-1; I> = 0; I --)
{
If (word [I] = ''| word [I] = '\ n' | word [I] =' \ t ')
Q --;
Else
Break;
}
Word [q] = '\ 0 ';
Return 0;
}

Int BubbleSort (int nums [], int n)
{
Int I, j, flag = 0, swap;

For (I = 0; I <n; I ++)
{
For (j = 1; j <n; j ++)
{
If (nums [J-1]> nums [j])
{
Swap = nums [j];
Nums [j] = nums [J-1];
Nums [J-1] = swap;
Flag = 1;
}
}
If (flag = 0)
Break;
}

Return 0;
}

Part 7

Tool. h file

# Ifndef _ TOOL_H
# Define _ TOOL_H
Int Trim (char word [132]);
Int Seg (char word [132], char p [101], char q [31]);
Int BubbleSort (int nums [], int n );
# Endif

Part 8

Rename the txt file with 7000 or 8000 words to dict. TXT.

 

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.