循环输出文字

1
#include<iostream>
using namespace std;
int main()
{
	for(int i=1;i<=100;i++)
		cout<<"字符";
	return 0;
}

求解怎么用easyX循环输出文字。效果和上面C++的一样,而不是让字符动起来

2

想要实现 cout<<"字符"; 的效果,可以自己写一个函数。例如可以这么做:

// 实现类似控制台样式的字符串输出
// 编译环境:VC2010、VC2019,EasyX_20200315(beta)
//
#include <graphics.h>
#include <conio.h>

void mycout(wchar_t* s)
{
	static int x = 0, y = 0;
	for (unsigned int i = 0; i < _tcslen(s); i++)
	{
		if (s[i] == L'\n')
			x = 0, y += textheight(L'A');
		else
		{
			int w = textwidth(s[i]);
			if (x + w > 639)
				x = 0, y += textheight(s[i]);
			outtextxy(x, y, s[i]);
			x += w;
			if (x > 635)
				x = 0, y += textheight(s[i]);
		}
	}
}

int main()
{
	// 创建图形窗口
	initgraph(640, 480);

	settextstyle(16, 0, L"宋体");
	mycout(L"123测试45");
	mycout(L"abc\n");
	mycout(L"OK");

	_getch();

	// 关闭绘图窗口
	closegraph();
	return 0;
}

补充:

关于中文的处理:

对于 MBCS 编码,这时候一个汉字会被分成两个值为 161~254 的字符,需要判断先后两个字节,来判断是否是中文,以便决定输出一个字符,还是一个长度为 2 的字符串。

对于 Unicode 编码,这就简单多了,不管中文还是英文,都是一个 wchar_t 字符,以上代码完全没问题。

ava
慢羊羊

2020-4-5

谢解答,可是,这里面的for循环变量i只是改变了坐标,使得文字位置发生变化了,而不像C++中cout那样可以循环输出字符,我想实现的是用循环让字符能像C++一样的效果 for(int i=1;i<=1000;i++) cout<<"字符" -  天上掉下来了一本自律秘籍  2020-4-5
@天上掉下来了一本自律秘籍 看我修改后的答案 -  慢羊羊  2020-4-5
@慢羊羊 行,谢大佬,如果我想让字符重复输出的话,就把他们放在while(1)里面对吧,多谢解答 -  天上掉下来了一本自律秘籍  2020-4-5
@慢羊羊 对了,大佬,能问下您上面的代码,如果输出中文应该怎么改一下吗,好像中文输出出来是乱码 -  天上掉下来了一本自律秘籍  2020-4-5
@天上掉下来了一本自律秘籍 已补充中文的处理方式。 -  慢羊羊  2020-4-5
@慢羊羊 非常感谢 -  天上掉下来了一本自律秘籍  2020-4-5
4

稍微封装了一下,目前看来没有什么bug(除了用一些稀奇古怪的字体),用微软雅黑会出现玄学bug,懒得排了,不用就行

econsole.h:懒得分开写,毕竟就这点码量

#pragma once
#include<easyx.h>
#include<windows.h>
#include<conio.h>
#include<stdio.h>
class EConsole {
private:
	RECT board;//相对于窗口的边框坐标
	int width, height;//控制台能容纳的字符行列数(建议用等宽字符,否则会出奇奇怪怪的错误,比如微软雅黑)
	POINT cursor;//控制台光标位置
	TCHAR** buffer;//缓冲区,一个滚动数组
	int start;//滚动数组的起始行

	//滚动
	void Scroll() {
		if (++cursor.x == height)cursor.x = 0;
		memset(buffer[cursor.x], 0, sizeof(TCHAR) * width);
		if (cursor.x == start && ++start == height)start = 0;
	}
public:
	bool autoflush = true;//用于批量打印
	EConsole(RECT _board) {
		board = _board;

		LOGFONT font;
		ZeroMemory((void*)&font, sizeof font);
		font.lfCharSet = GB2312_CHARSET;//如果遇到想要的字体显示不出来可以试试注释掉这句,原理未知
		font.lfHeight = 24;
		font.lfWeight = FW_NORMAL;
		const TCHAR* fontname = L"Microsoft YaHei Mono";
		wcscpy_s(font.lfFaceName, fontname);
		settextstyle(&font);

		width = (board.right - board.left) / textwidth(L'A');
		height = (board.bottom - board.top) / textheight(L'A');
		cursor = POINT{ 0,0 };

		buffer = new TCHAR * [height];
		for (int i = 0; i < height; i++) {
			buffer[i] = new TCHAR[width];
			memset(buffer[i], 0, sizeof(TCHAR) * width);
		}
		start = 0;
	}
	//清空缓冲区
	void Clear() {
		for (int i = 0; i < height; i++)memset(buffer[i], 0, sizeof(TCHAR) * width);
		cursor = { 0,0 };
		start = 0;
	}
	//刷新缓冲区
	void Flush() {
		BeginBatchDraw();
		cleardevice();//如果有别的需求可以把这行删掉
		int x = board.left, y = board.top;
		for (int i = 0; i < height; i++) {
			int j = (i + start) % height;
			x = board.left;
			TCHAR* p = buffer[j];
			for (; p < buffer[j] + width; p++) {
				outtextxy(x, y, *p);
				x += textwidth(*p);
			}
			y += textheight(L'A');
		}
		EndBatchDraw();
	}
	//重载运算符,懒得写其他的
	EConsole& operator<<(const TCHAR* wstr) {
		for (unsigned int i = 0; i < wcslen(wstr); i++) {
			buffer[cursor.x][cursor.y++] = wstr[i];
			if (textwidth(wstr[i]) > textwidth(L'a')) {
				if (cursor.y == width) {
					//处理最后一个是宽字符可能会卡出去的问题
					buffer[cursor.x][cursor.y - 1] = 0;
					buffer[cursor.x + 1][0] = wstr[i];
					Scroll();
					cursor.y = 2;
				}
				else cursor.y++;
			}
			if (cursor.y == width) {
				cursor.y = 0;
				Scroll();
			}
		}
		if (autoflush)Flush();
		return *this;
	}
};

test.cpp:一个伪打字机,不能识别回车,不能打中文,但是可以粘贴(玄学)

#include<graphics.h>
#include<conio.h>
#include<stdio.h>
#include"econsole.h"
int main() {
	initgraph(640, 480);
	EConsole cout({40,40,600,440});
	while (1) {
		while (!_kbhit())Sleep(1);
		char c = _getch();
		TCHAR* wc=new TCHAR;
		memset(wc, 0, sizeof(wc));
		swprintf_s(wc,sizeof wc, L"%wc", c);
		cout << wc;
	}
	_getch();
	return 0;
}
ava
无名氏

2020-4-5

#include<iostream> using namespace std; int main() { for(int i=1;i<=100;i++) cout<<"字符"; return 0; }您好,让easyX的for循环和outtexty实现r如上代码的效果,这样的满屏输出 -  天上掉下来了一本自律秘籍  2020-4-5
@天上掉下来了一本自律秘籍 看一下能不能用 -  无名氏  2020-4-5
@无名氏 谢谢啊,辛苦了,感谢! -  天上掉下来了一本自律秘籍  2020-4-5
@无名氏 我用VC6试了一下,是不是编译器问题,在public: bool autoflush = true;这一块,编译器说cpp(21) : error C2258: illegal pure syntax, must be '= 0' (是说初始要为false吧)cpp(21) : error C2252: 'autoflush' : pure specifier can only be specified for functionsError executing cl.exe. -  天上掉下来了一本自律秘籍  2020-4-5
@天上掉下来了一本自律秘籍 你可以把初始化放到构造函数里,我这只是图方便和易读性把它和定义写一起了 -  无名氏  2020-4-6
@无名氏 嗯,好了,多谢啊 -  天上掉下来了一本自律秘籍  2020-4-6
技术讨论社区
相关提问