But there is a problem with the software:
1, run the program, enter a string of Chinese in the process of practice, when you want to delete an input han
Words, when the cursor on the screen to remove the half of the Chinese characters, but actually delete a character,
Consult experts, how to solve this problem?
First thank you,
-- -- -- -- -- --
EasyType. C program
# include & lt; Stdio. H>
# include & lt; String. H>
# include & lt; Time. H>
# include & lt; Stdlib. H>
20 # define N
int type;//used to judge character is half Angle or the Angle of
/* the auther 2013-12-3 shencz2000
Email: [email protected]
English. TXT file is in English, there are half Angle of English letters and characters,
Chinese. TXT file is the text in the Chinese characters and the whole Angle of punctuation,
Mydrill. TXT file is the text in the Chinese characters and the whole Angle of punctuation,
These files each line should not exceed 40 or 80 half-width characters all Angle character,
Will be one of the file read text according to the line array, the most can only read the N rows, */
Int main (void)
{
Int select_file (char filename []);
Int type_drilling (char array [] [150]).
The FILE * fp.
Char array [N] [150], fname [20].
Int I=0, j;
System (" the clear ");//clear screen
Select_file (fname);
If ((fp=fopen (fname, "r"))==NULL)
{
Printf (" can 't open the file! \n");
The exit (0);
}
While (i
Type_drilling (array);
The fclose (fp);
return 0;
}
Int type_drilling (char array [] [150])
{
Int right_words (char * p, char * string);
Int I=0, j=0, k=0, m, n, s=0, t;
Time_t start and stop;
Float time_used, speed, rightw=0;
Char * p, string [150]={" start drilling "};
While (I & lt; N & amp; & Strlen (array [I])!=0)
{
P=array [I];
j=0;
While (j<3 & amp; & Rightw<90 & amp; & STRCMP (string, "quit")!=0)
{
Printf (" % s ", p);
S++;
Time (& amp; Start);
//the following two statements is to save the input to the 1 d character array
For (k=0; (string [k]=getchar ())!='\ n'; K++);
String [k]='\ 0';
Time (& amp; Stop);
Time_used=difftime (stop, start);
M=strlen (p) - 1;//the string including line breaks, so the length of the string to minus 1
If (type & gt; 0)
{k=k/3; M=m/3; }//a utf8 encoding of Chinese characters is 3 bytes
N=right_words (p, string);//statistics of the correct word function called
Speed=60 * k/(time_used + 0.1);//+ 0.1 is in order to avoid the denominator
the condition of the small//printf (" the words: % d, the right words: % d \ n ", m, n);
If (m & gt; 0) rightw=n * 100/m;
The else rightw=100;
Speed: printf (" % 5.0 f, right thewire: % 5.0 f \ n \ n ", Speed, rightw);
j++;
S +=3;
}
If (s & gt; 17)
{
S=0;
System (" the clear ");//clear screen instructions executed
Speed: printf (" % 5.0 f, right thewire: % 5.0 f \ n \ n ", speed, rightw);
}
Rightw=0;
If (i
return 0;
}
Int select_file (char filename [])
{
Int I, k;
Char array [3] [20]={" English. TXT ", "Chinese. TXT", "mydrill. TXT"};
Printf (" do input 1, 2 or 3 to select a file for drilling. \ n ");
for (i=0; i<3; I++)
Printf (" % d file name is: % s \ n ", (I + 1), array [I]);
Printf (" ");
The scanf (" % d ", & amp; K);
Getchar ();//why do you want to add this statement?
System (" the clear ");//clear screen
If (k<1 | | k> 3)
{
Printf (" \ n The number is wrong, so The first file is 2 \ n ");
k=1;
}
Strncpy (filename, array [] k - 1, 20).
Type=k - 1;//0 (English half Angle character, & gt; On behalf of the Chinese whole Angle 0 characters.
Printf (" \ n ");
return 0;
}
Int right_words (char * p, char * string)
{
Int I, k=0, r, s, t=0;
R=strlen (p);
S=strlen (string);
If (s> R) s=r;
If (type==0)//if it is English half-width characters
{
for (i=0; i
}
Angle of the else//if it is all in Chinese characters
{
for (i=0; i
If t++ string++ p++==(* *);
If (t==3) k++;
If ((I + 1) % 3==0) t=0;
}
}
The return of k;
}
CodePudding user response:
A Chinese utf8 encoding is 3 bytes; Should be two characters, so you delete one character at a time, half is Chinese ah, you're actually delete a Chinese character, but your application is actually delete a byte character, that is half of the Chinese characters, so a delete half of the Chinese characters, just happened to be your handling of the program, so it will make your program running in the right way; If you want to be a delete a Chinese character, also need to change your program, I have no idea about this, but you can try the readline library, this should be supportedCodePudding user response:
Utf8, Chinese are the three bytes, you can test,CodePudding user response: