C語言制作簡易金山打字通功能的代碼
更新時間:2018年12月14日 16:02:41 作者:Engineer-Bruce_Yang
今天小編就為大家分享一篇關于C語言制作簡易金山打字通功能的代碼,小編覺得內容挺不錯的,現在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
本小項目最終的實現如下:
輸入相應的字符,然后在最下面能夠統(tǒng)計錯誤的個數,輸入字符總個數,輸入個數以及錯誤率。
那如何來實現這個小項目呢?規(guī)劃如下,我們需要大致實現以下三個模塊:
- (1)輸入模塊
- (2)顯示模塊
- (3)統(tǒng)計模塊
實現過程:
使用getch()
函數可以獲取鍵盤輸入的字符,顯示可以使用Window自帶的API來實現,統(tǒng)計就很簡單了,就是計算輸入字符的個數等等。。。接下來就是簡單的軟件邏輯的實現。
源碼如下:
#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); //定義設置光標結構體變量 CONSOLE_CURSOR_INFO cci; //定義默認的坐標位置 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); } //關閉窗口句柄 CloseHandle(hOut); return 0 ; } //窗口初始化 void HANDLE_init(HANDLE hOut) { SetConsoleTitleA(TITLE); //獲取當前的句柄---設置為標準輸出句柄 //獲取光標信息 GetConsoleCursorInfo(hOut, &cci); //設置光標大小 pos.X = 0 ; pos.Y = 2 ; cci.dwSize = 1; //設置光標不可見 FALSE cci.bVisible = 0; //設置(應用)光標信息 SetConsoleCursorInfo(hOut, &cci); } static int __Get_User_input(HANDLE hOut,char *ch) { char tmp ; int type = Char ; //關閉回顯 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)計 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("錯誤的個數:%d", errno_Num) ; pos.X = 0; pos.Y = 16 ; SetConsoleCursorPosition(hOut,pos); printf("總個數:%d", (int)strlen(text)) ; pos.X = 0; pos.Y = 17 ; SetConsoleCursorPosition(hOut,pos); printf("輸入個數:%d", max) ; pos.X = 0; pos.Y = 18 ; SetConsoleCursorPosition(hOut,pos); if(count == 0) printf("錯誤率:0%%") ; else printf("錯誤率:%.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); }
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對腳本之家的支持。如果你想了解更多相關內容請查看下面相關鏈接
相關文章
Windows10配置VSCode C++環(huán)境(超詳細,面向小白以及大佬們)
這篇文章主要介紹了Windows10配置VSCode C++環(huán)境(超詳細,面向小白以及大佬們),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-12-12Visual Studio 2019創(chuàng)建C++ Hello World項目的方法
這篇文章主要介紹了Visual Studio 2019創(chuàng)建C++ Hello World項目的方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-03-03