ctime 文本输出格式

CTime类通过Format可以格式化输出日期时间信息,通过控制Format的参数我们可以实现各种各样的日期时间格式输出,要想随心所欲的控制输出的格式,就需要搞清楚各个参数的意义,下面通过实例来说明各个Format参数的意义。

  假设CTime对象当前的日期时间为:2011-4-17 14:32:18 星期日 英文表示为:Sunday, April 17, 2011 14:32:18,下表中列出了各种Format参数的输出结果以及备注说明。

Format参数输出结果备注
%aSun星期的缩写(英文)
%ASunday完整的星期名(英文)
%bApr月份的缩写(英文)
%BApril完整的月份名(英文)
%c04/17/11 14:32:18 
%d17日(01-31) 支持#
%H1424小时制的小时(00-23) 支持#
%I0212小时制的小时(01-12) 支持#
%j107今年的第几天(001-366) 支持#
%m04月(01-12) 支持#
%M32分钟(00-59) 支持#
%pPM12小时制中的AM和PM
%S18秒钟(00-59) 支持#
%U16一年中的第几周,星期天作为每周的第一天(00-53) 支持#
%w0工作日,周日为0(0-6) 支持#
%W15一年中的第几周,星期一作为每周的第一天(00-53) 支持#
%x04/17/11 
%X14:32:18 
%y11两位数表示的年(00-99) 支持#
%Y2011年 支持#
%z中国标准时间 
%Z中国标准时间 

使用方法

CString str;
CTime tmNow = CTime::GetCurrentTime();
str = tmNow.Format(_T( "%Y年%m月%d日"));  // 2011年04月17日
str = tmNow.Format(_T( "%Y-%m-%d %H:%M:%S"));  // 2011-04-17 14:32:18
str = tmNow.Format(_T( "%Y-%#m-%#d %H:%M:%S"));  // 2011-4-17 14:32:18
str = tmNow.Format(_T( "%c"));  // 04/17/11 14:32:18

上面表格中,支持#号的意思是可以在输出的时候去掉前面的0,注意上面代码中倒数2、3行之间的区别,其余的格式按照表格中各参数的意思组合起来即能获取自己想要的格式。

本文转载自http://www.letuknowit.com/archives/52

好的,下面是一个简单的俄罗斯方块游戏的C++代码示例: ```c++ #include <iostream> #include <ctime> #include <conio.h> #include <windows.h> using namespace std; const int ROW = 20; // 游戏区域的行数 const int COL = 10; // 游戏区域的列数 const int BLOCK_SIZE = 4; // 方块的大小 const int SHAPE_NUM = 7; // 方块的种类数 // 方块的形状,用二维数组表示,1表示有方块,0表示无方块 int shapes[SHAPE_NUM][BLOCK_SIZE][BLOCK_SIZE] = { { // I 形 {0, 0, 0, 0}, {1, 1, 1, 1}, {0, 0, 0, 0}, {0, 0, 0, 0}, }, { // J 形 {1, 0, 0, 0}, {1, 1, 1, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, }, { // L 形 {0, 0, 1, 0}, {1, 1, 1, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, }, { // O 形 {1, 1, 0, 0}, {1, 1, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, }, { // S 形 {0, 1, 1, 0}, {1, 1, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, }, { // T 形 {0, 1, 0, 0}, {1, 1, 1, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, }, { // Z 形 {1, 1, 0, 0}, {0, 1, 1, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, } }; // 方块的颜色,用枚举类型表示 enum Color { BLACK = 0, BLUE = 1, GREEN = 2, CYAN = 3, RED = 4, MAGENTA = 5, YELLOW = 6, WHITE = 7 }; // 定义方块的结构体 struct Block { int x; // 方块的横坐标 int y; // 方块的纵坐标 int shape; // 方块的形状 Color color; // 方块的颜色 }; // 定义游戏区域的二维数组,用于记录方块的状态 int gameArea[ROW][COL] = {0}; // 定义函数:设置控制台光标位置 void setCursorPos(int x, int y) { HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); COORD pos = {x, y}; SetConsoleCursorPosition(hConsole, pos); } // 定义函数:设置控制台文本颜色 void setTextColor(Color color) { HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); SetConsoleTextAttribute(hConsole, color); } // 定义函数:在控制台输出方块 void drawBlock(int x, int y, Color color) { setCursorPos(x, y); setTextColor(color); cout << "■"; } // 定义函数:在控制台清除方块 void clearBlock(int x, int y) { setCursorPos(x, y); cout << " "; } // 定义函数:判断方块是否可以移动到指定位置 bool canMove(const Block& block, int dx, int dy) { for (int i = 0; i < BLOCK_SIZE; i++) { for (int j = 0; j < BLOCK_SIZE; j++) { if (shapes[block.shape][i][j] != 0) { int x = block.x + j + dx; int y = block.y + i + dy; if (x < 0 || x >= COL || y < 0 || y >= ROW || gameArea[y][x] != 0) { return false; } } } } return true; } // 定义函数:移动方块 void moveBlock(Block& block, int dx, int dy) { clearBlock(block.x, block.y); block.x += dx; block.y += dy; drawBlock(block.x, block.y, block.color); } // 定义函数:旋转方块 void rotateBlock(Block& block) { int oldShape = block.shape; int newShape = (block.shape + 1) % BLOCK_SIZE; if (newShape < 0) { newShape += BLOCK_SIZE; } if (canMove(block, 0, 0)) { clearBlock(block.x, block.y); block.shape = newShape; drawBlock(block.x, block.y, block.color); } else { block.shape = oldShape; } } // 定义函数:生成一个新的方块 void generateBlock(Block& block) { block.x = COL / 2 - 2; block.y = 0; block.shape = rand() % SHAPE_NUM; block.color = (Color)(rand() % 7 + 1); drawBlock(block.x, block.y, block.color); } // 定义函数:将方块固定在游戏区域中 void fixBlock(const Block& block) { for (int i = 0; i < BLOCK_SIZE; i++) { for (int j = 0; j < BLOCK_SIZE; j++) { if (shapes[block.shape][i][j] != 0) { int x = block.x + j; int y = block.y + i; gameArea[y][x] = block.color; } } } } // 定义函数:判断是否有一行已满 bool isRowFull(int row) { for (int j = 0; j < COL; j++) { if (gameArea[row][j] == 0) { return false; } } return true; } // 定义函数:消除已满的行 void clearFullRows() { int newArea[ROW][COL] = {0}; int newRow = ROW - 1; for (int i = ROW - 1; i >= 0; i--) { if (!isRowFull(i)) { for (int j = 0; j < COL; j++) { newArea[newRow][j] = gameArea[i][j]; } newRow--; } } memcpy(gameArea, newArea, sizeof(gameArea)); } // 定义函数:显示游戏区域 void showGameArea() { for (int i = 0; i < ROW; i++) { for (int j = 0; j < COL; j++) { if (gameArea[i][j] != 0) { drawBlock(j, i, (Color)gameArea[i][j]); } else { clearBlock(j, i); } } } } // 定义函数:显示分数 void showScore(int score) { setCursorPos(COL + 2, 0); setTextColor(YELLOW); cout << "Score: " << score; } int main() { srand((unsigned)time(NULL)); Block currentBlock; int score = 0; bool isGameOver = false; // 初始化游戏区域 for (int i = 0; i < ROW; i++) { for (int j = 0; j < COL; j++) { gameArea[i][j] = 0; } } // 显示游戏区域和分数 showGameArea(); showScore(score); // 生成第一个方块 generateBlock(currentBlock); // 游戏主循环 while (!isGameOver) { // 接收玩家的输入 if (_kbhit()) { int ch = _getch(); switch (ch) { case 'a': // 左移 if (canMove(currentBlock, -1, 0)) { moveBlock(currentBlock, -1, 0); } break; case 'd': // 右移 if (canMove(currentBlock, 1, 0)) { moveBlock(currentBlock, 1, 0); } break; case 's': // 下移 if (canMove(currentBlock, 0, 1)) { moveBlock(currentBlock, 0, 1); } break; case 'w': // 旋转 rotateBlock(currentBlock); break; case ' ': // 直接下落到底部 while (canMove(currentBlock, 0, 1)) { moveBlock(currentBlock, 0, 1); } break; case 'q': // 退出游戏 isGameOver = true; break; } } // 方块下落 if (canMove(currentBlock, 0, 1)) { moveBlock(currentBlock, 0, 1); } else { // 方块无法下落,固定在游戏区域中 fixBlock(currentBlock); // 消除已满的行 clearFullRows(); // 生成下一个方块 generateBlock(currentBlock); // 判断游戏是否结束 if (!canMove(currentBlock, 0, 0)) { isGameOver = true; } // 更新分数 score++; showScore(score); } // 暂停一段时间,控制方块下落速度 Sleep(50); } // 游戏结束,输出提示信息 setCursorPos(COL / 2 - 8, ROW / 2); setTextColor(RED); cout << "Game Over!"; setCursorPos(0, ROW + 1); return 0; } ``` 以上是一个简单的俄罗斯方块游戏的C++代码示例,可以在控制台中运行。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值