跳到正文

个人记录

《银山打字通》

hdkghc约 8 分钟
原文
文章信息与版本

文章信息

发布
2024年5月2日
收录
2025年12月12日
更新
2026年11月21日
篇幅
约 8 分钟

版本

  1. v1当前

版本号 Va.bb.ccccc

  • a : 大版本号
  • b : 小版本号
  • c : bug处理编号

V1.01.00004

C++
// Bug library
/*
 Bug fixed
  #00001 控制字符禁止输入 20231221 21:02
  #00002 解决制表乱显 : 替换为四个空格 20231221 21:20
  #00003 输入文件名未getline 20231221 21:30
  #00004 文件名 20231227 20:58
 Bug scaned
  #s0001 控制字符问题 未完全解决 时间靠按键刷新
*/


// Copyright (c) 2023~2123 hdkghc.
// All rights reserved.

#include<Windows.h>
#include<conio.h>
#include<iostream>
#include<fstream>
#include<sstream>
#include<ctime>
using namespace std;
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
void setcolor(WORD wAttr)
{
	SetConsoleTextAttribute(hOut, wAttr);
}
#define CORR FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY | 0x8000
#define WRON FOREGROUND_RED | FOREGROUND_INTENSITY | 0x8000
#define STND FOREGROUND_BLUE | FOREGROUND_INTENSITY
void _gotoxy(short x, short y)
{
	COORD c = {x, y};
	SetConsoleCursorPosition(hOut, c);
}
string filename;
fstream ifs;
stringstream ss;

// count
int corr, tot, backsp;
clock_t strttm;

void record()
{
	CONSOLE_SCREEN_BUFFER_INFO csbi;
	GetConsoleScreenBufferInfo(hOut, &csbi);
	COORD c = csbi.dwCursorPosition;
	_gotoxy(0, 21);
	cout << "correct :               total :               backspace :              time used :                  ";
	//       0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789 ^
	//       0000000000111111111122222222223333333333444444444455555555556666666666777777777788888888889999999999 |
	_gotoxy(10, 21);
	cout << corr;
	_gotoxy(32, 21);
	cout << tot;
	_gotoxy(58, 21);
	cout << backsp;
	_gotoxy(83, 21);
	clock_t o = strttm == 0 ? 0 : clock() - strttm;
	cout << fixed << (o / 1000.0) << " sec";
	_gotoxy(c.X, c.Y);
}

/* flush
int mgetch()
{
	while(!kbhit())
	{
		Sleep(10);
		record();
	}
	if(strttm == 0)
	{
		strttm = clock();
	}
	return getch();
}
*/
/* press*/
int mgetch()
{
	int ret = getch();
	if(strttm == 0)
	{
		strttm = clock();
	}
	record();
	return ret;
}

void nextpage()
{
	system("cls");
	string line[10] = {};
	for(int i = 0; i < 10 && ss; i++)
	{
		getline(ss, line[i]);
		string::size_type pos;
		while((pos = line[i].find('\t')) != string::npos)
		{
			line[i].replace(pos, 1, "    ");
		}
		if(line[i].size() > 100)
		{
			line[i + 1] = line[i].substr(100);
			line[i] = line[i].substr(0, 100);
			++i;
		}
	}
	for(int i = 0; i < 10; i++)
	{
		_gotoxy(0, i * 2);
		setcolor(STND);
		cout << line[i];
		setcolor(0x07);
	}
	for(int cline = 0; cline < 10; cline++)
	{
		_gotoxy(0, cline * 2 + 1);
		string linebuf = "";
		for(int col = 0; col < line[cline].size(); col++)
		{
			char hit = mgetch();
			if(hit == '\b')
			{
				if(linebuf.size() != 0)
				{
					if(linebuf.back() == line[cline][linebuf.size() - 1]) corr--;
					linebuf.pop_back();
					cout << "\b \b";
					col -= 2;
					backsp++;
					tot--;
				}
				else
				{
					col--;
				}
			}
			else
			{
				if(hit == '\t' || hit == '\r' || hit == '\n') 
				{
					col--;
				}
				else if(iscntrl(hit))
				{
//					cout << hit << "\b \b";
					col--;
				}
				else
				{
					linebuf.push_back(hit);
					setcolor(hit == line[cline][col] ? CORR : WRON);
					cout << hit;
					setcolor(0x07);
					corr += hit == line[cline][col];
					tot++;
				}
			}
		}
	}
}
int main()
{
	system("mode con cols=100 lines=22");
	_gotoxy(44, 10);
	cout << "银山打字通";
	string copyright = "hdkghc";
	copyright.push_back('!');
	_gotoxy(0, 11);
	cout << "Drag file here and press enter -> ";
	getline(cin, filename);
	if(filename[0] == '"')
	{
		filename.pop_back();
		filename.erase(0, 1);
	}
	ifs.open(filename.data());
	ss << ifs.rdbuf();
	while(ss) nextpage();
	clock_t ed = clock();
	Sleep(1000);
	system("cls");
	cout << "Report:" << endl;
	cout << "\tcorrect/total: " << corr << ' ' << tot << endl;
	cout << "\t\t" << (100.0 * corr / tot) << "%" << endl;
	cout << "\tBackspace: " << backsp << endl;
	cout << "\tTime: " << (ed - strttm) / 1000.0 << " second(s).";
	Sleep(2000);
	getch();
}
原文