C語言制作簡易金山打字通功能的代碼
本小項(xiàng)目最終的實(shí)現(xiàn)如下:
輸入相應(yīng)的字符,然后在最下面能夠統(tǒng)計(jì)錯(cuò)誤的個(gè)數(shù),輸入字符總個(gè)數(shù),輸入個(gè)數(shù)以及錯(cuò)誤率。
那如何來實(shí)現(xiàn)這個(gè)小項(xiàng)目呢?規(guī)劃如下,我們需要大致實(shí)現(xiàn)以下三個(gè)模塊:
- (1)輸入模塊
- (2)顯示模塊
- (3)統(tǒng)計(jì)模塊
實(shí)現(xiàn)過程:
使用getch()函數(shù)可以獲取鍵盤輸入的字符,顯示可以使用Window自帶的API來實(shí)現(xiàn),統(tǒng)計(jì)就很簡單了,就是計(jì)算輸入字符的個(gè)數(shù)等等。。。接下來就是簡單的軟件邏輯的實(shí)現(xiàn)。
源碼如下:
#include <stdio.h>
#include <string.h>
#include <Windows.h>
#include <unistd.h>
#include <conio.h>
#define NR(x) sizeof(x)/sizeof(x[0])
//清屏
#define ClearScreen() \
system("cls");
#define TITLE "金山打字通"
enum
{
LEFT = 1 ,
RIGHT ,
BACKSPACE ,
ESC ,
Char,
};
enum KEYBOARD
{
ESC_KEY = 27,
BACKSPACE_KEY = 8 ,
LEFT_KEY = 75 ,
RIGHT_KEY = 77
};
int iindex = 0 ;
int max = 0 ;
static int count = 0 ;
char buffer[1024] = {0} ;
int Get_User_input(HANDLE hOut,char *ch) ;
void Show_string(HANDLE hOut,const char *text) ;
//窗口初始化
void HANDLE_init(HANDLE hOut);
//定義設(shè)置光標(biāo)結(jié)構(gòu)體變量
CONSOLE_CURSOR_INFO cci;
//定義默認(rèn)的坐標(biāo)位置
COORD pos = {0,0};
int main(void)
{
char *text = "WelCome to School ... Good Good Work ,Day Day Up !" ;
char ch ;
int ret ;
HANDLE hOut;
hOut = GetStdHandle(STD_OUTPUT_HANDLE);
HANDLE_init(hOut);
printf("\n%s\n",text);
Show_string(hOut,text);
while(1)
{
if(max >= strlen(text))
break ;
ret = Get_User_input(hOut,&ch) ;
if(ret == ESC)
break ;
Show_string(hOut,text);
}
//關(guān)閉窗口句柄
CloseHandle(hOut);
return 0 ;
}
//窗口初始化
void HANDLE_init(HANDLE hOut)
{
SetConsoleTitleA(TITLE);
//獲取當(dāng)前的句柄---設(shè)置為標(biāo)準(zhǔn)輸出句柄
//獲取光標(biāo)信息
GetConsoleCursorInfo(hOut, &cci);
//設(shè)置光標(biāo)大小
pos.X = 0 ;
pos.Y = 2 ;
cci.dwSize = 1;
//設(shè)置光標(biāo)不可見 FALSE
cci.bVisible = 0;
//設(shè)置(應(yīng)用)光標(biāo)信息
SetConsoleCursorInfo(hOut, &cci);
}
static int __Get_User_input(HANDLE hOut,char *ch)
{
char tmp ;
int type = Char ;
//關(guān)閉回顯
pos.X = 0 ;
pos.Y = 2 ;
GetConsoleCursorInfo(hOut, &cci);
cci.dwSize = 100;
cci.bVisible = 0;
SetConsoleCursorInfo(hOut, &cci);
tmp = getch() ;
switch(tmp)
{
case ESC_KEY : type = ESC ; break ;
case BACKSPACE_KEY : type = BACKSPACE ; break ;
case LEFT_KEY : type = LEFT ; break ;
case RIGHT_KEY : type = RIGHT; break ;
}
*ch = tmp ;
//打開回顯
pos.X = 0 ;
pos.Y = 2 ;
GetConsoleCursorInfo(hOut, &cci);
cci.dwSize = 100;
cci.bVisible = 1;
SetConsoleCursorInfo(hOut, &cci);
return type ;
}
//獲取用于輸入
int Get_User_input(HANDLE hOut,char *ch)
{
int type ;
type = __Get_User_input(hOut,ch);
switch(type)
{
case Char :
if(buffer[iindex] == '\0' )
buffer[iindex] = *ch ;
else
{
memmove(buffer+iindex+1 , buffer+iindex , max-iindex) ;
buffer[iindex] = *ch ;
}
iindex ++ ; max ++ ; break ;
//case LEFT : if(iindex > 0) iindex -- ; break ;
//case RIGHT : if(iindex < max) iindex ++ ; break ;
case BACKSPACE :
if(iindex > 0){
memmove(buffer+iindex-1 , buffer+iindex , max-iindex) ;
iindex -- ;
max -- ;
}
if(iindex == 0)
{
count = 0 ;
}
break ;
case ESC : return ESC ;
}
return 0 ;
}
//顯示和統(tǒng)計(jì)
void Show_string(HANDLE hOut,const char *text)
{
system("cls") ;
printf("\n%s\n",text) ;
int i ;
int errno_Num = 0 ;
for(i = 0 ; i < max ; i++)
{
if(buffer[i] == text[i])
{
SetConsoleTextAttribute(hOut, FOREGROUND_GREEN | 0x8);
printf("%c",buffer[i]);
}
else
{
SetConsoleTextAttribute(hOut, FOREGROUND_RED | 0x8);
printf("%c",buffer[i]);
errno_Num++ ;
}
}
pos.X = 0 ;
pos.Y = 2 ;
cci.dwSize = 100;
cci.bVisible = 1 ;
SetConsoleCursorPosition(hOut,pos);
SetConsoleCursorInfo(hOut, &cci);
SetConsoleTextAttribute(hOut,FOREGROUND_GREEN | 0x8);
pos.X = 0;
pos.Y = 15 ;
SetConsoleCursorPosition(hOut,pos);
printf("錯(cuò)誤的個(gè)數(shù):%d", errno_Num) ;
pos.X = 0;
pos.Y = 16 ;
SetConsoleCursorPosition(hOut,pos);
printf("總個(gè)數(shù):%d", (int)strlen(text)) ;
pos.X = 0;
pos.Y = 17 ;
SetConsoleCursorPosition(hOut,pos);
printf("輸入個(gè)數(shù):%d", max) ;
pos.X = 0;
pos.Y = 18 ;
SetConsoleCursorPosition(hOut,pos);
if(count == 0)
printf("錯(cuò)誤率:0%%") ;
else
printf("錯(cuò)誤率:%.2f%%",((float)errno_Num)/((float)max)*100) ;
pos.X = iindex + 1 ;
pos.Y = 2 ;
cci.dwSize = 100;
cci.bVisible = 1 ;
count = 1 ;
SetConsoleCursorPosition(hOut,pos);
SetConsoleCursorInfo(hOut, &cci);
fflush(stdout);
}
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對我們的支持。如果你想了解更多相關(guān)內(nèi)容請查看下面相關(guān)鏈接
上一篇:C語言中g(shù)etchar()的返回類型為什么是int詳解
欄 目:C語言
本文標(biāo)題:C語言制作簡易金山打字通功能的代碼
本文地址:http://m.jygsgssxh.com/a1/Cyuyan/649.html
您可能感興趣的文章
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用函數(shù)刪除字符
- 04-02c語言的正則匹配函數(shù) c語言正則表達(dá)式函數(shù)庫
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言中對數(shù)函數(shù)的表達(dá)式 c語言中對數(shù)怎么表達(dá)
- 04-02c語言用函數(shù)寫分段 用c語言表示分段函數(shù)
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排序法函數(shù)
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段函數(shù)
- 04-02C語言中怎么打出三角函數(shù) c語言中怎么打出三角函數(shù)的值
- 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求階乘


閱讀排行
本欄相關(guān)
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言的正則匹配函數(shù) c語言正則表達(dá)
- 04-02c語言用函數(shù)寫分段 用c語言表示分段
- 04-02c語言中對數(shù)函數(shù)的表達(dá)式 c語言中對
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段
- 04-02C語言中怎么打出三角函數(shù) c語言中怎
- 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求
隨機(jī)閱讀
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-10C#中split用法實(shí)例總結(jié)
- 01-10使用C語言求解撲克牌的順子及n個(gè)骰子
- 04-02jquery與jsp,用jquery
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-10delphi制作wav文件的方法
- 01-11ajax實(shí)現(xiàn)頁面的局部加載
- 08-05織夢dedecms什么時(shí)候用欄目交叉功能?
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改


