C++的各种用法展示(二)

简介: C++的各种用法展示(二)

这段代码提供了一组用于进行3D向量操作的函数。让我们逐个解释这些函数的功能:

  1. vector_sub() 函数用于计算两个向量的差。它将一个向量从另一个向量中减去,并返回结果向量。
  2. vector_add() 函数用于计算两个向量的和。它将两个向量相加,并返回结果向量。
  3. dot_prod() 函数用于计算两个向量的点积。它将两个向量的对应分量相乘,并将结果累加得到一个标量值。
  4. vector_prod() 函数用于计算两个向量的叉积。它根据叉积的定义,将两个向量的分量进行组合计算,并返回结果向量。
  5. print_vector() 函数用于格式化输出一个向量的值。它将向量的分量格式化为字符串,并返回格式化后的字符串表示。
  6. vector_norm() 函数用于计算一个向量的范数(长度)。它使用点积的结果,并将结果开方得到向量的长度。
  7. unit_vec() 函数用于计算一个向量的单位向量。它将给定的向量除以其长度,从而获得与原向量方向相同但长度为1的单位向量。
  8. get_cross_matrix() 函数用于根据给定向量生成一个表示叉积运算的3x3矩阵。它将向量的分量按照叉积的定义填充到矩阵中。
  9. get_angle() 函数用于计算两个向量之间的夹角。它使用点积和向量长度的计算结果,通过反余弦函数来计算夹角的弧度值。

在代码的结尾,还有一个 test() 函数用于测试这些函数的正确性,并进行了一些断言来验证结果。你可以运行这段代码来执行测试,并检查断言的结果是否为真。

这些函数和操作可以应用于许多需要进行3D向量计算的场景,例如计算机图形学中的向量变换、物理模拟中的力和运动计算、机器人学中的位姿分析等。它们提供了一组基本的操作函数,方便处理和操作3D空间中的向量数据。

C++游戏

/**
 * @file tic-tac-toe.c
 * @author [vivekboss99](github.com/vivekboss99)
 * @author [Krishna Vedala](https://github.com/kvedala)
 * @brief [Tic-Tac-Toe game](https://en.wikipedia.org/wiki/Tic-tac-toe)
 * implementation in C
 * @details  Tic-Tac-Toe Game, where the user can decide to play with the
 * computer(single player mode) or with other user(double player mode), the
 * code as an array named 'game_table' which is the table and user needs to enter the
 * position inside the array(from 1-9) where he/she wants to place 'X' or 'O' on the
 * table.
 */
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
// Functions Declarations
static void singlemode();
static void doublemode();
static void placex(int);  // used for placing position of X by the 1st player
static void place();      // used by the computer to place O
static void placey(int);  // used in Double Player mode by the 2nd player to
                          // place the position of O
int checkwin();  // checks everytime when a player or computer places 'X' or 'O'
/** Tic-Tac-Toe table, so basically we are using variable 'game_table' as the table(size:3X3) and
 * updating it regularly
 */
static char game_table[9];
/**
 * Main program function.
 * @returns 0 on clean exit. 
 * @note No checks are included for program execution failures!
 */
int main()
{   
    srand( (unsigned int)time(NULL));
    int l = 0;
    do
    {
        int n = 0;
        // filling the table with multiple asterisks
        for (int i = 0; i < 9; i++) game_table[i] = '*';
        // displaying the main menu
        printf("***************************************\n");
        printf("*************TIC TAC TOE***************\n");
        printf("***************************************\n");
        printf("***********1. YOU vs COMPUTER ***********\n");
        printf("***********2. YOU vs PLAYER ***********\n");
        printf("***********3.EXIT *********************\n");
        printf("Enter your choice : ");
        scanf("%d", &n);
        switch (n)  // switch case to select between single player mode or
                    // double player mode
        {
        case 1:
            singlemode();
            break;
        case 2:
            doublemode();
            break;
        default:
            printf("THANK YOU and EXIT!");
        }
        printf("Next game ? : ");
        printf("Enter 1 – YES and 0 - NO ");
        scanf("%d", &l);
    } while (l == 1);
    return 0;
}
/**
 * @brief Implementation of game vs computer
 *
 * @returns None
 */
void singlemode()
{
    int m;
    int k = 0;
    int table_fill_count=0;
    for (int i = 0; i < 3; i++)
    {
        for (int j = 0; j < 3; j++)
        {
            printf("%c ", game_table[k]);
            k++;
        }
        printf("\n");
    }
    for (int x = 1; x < 10; x++)
    {
        k = 0;
        printf("Where would you like to place 'x' ");
        scanf("%d", &m);
        placex(m);
        if(table_fill_count<4)
        {
          place();
        }
        for (int i = 0; i < 3; i++)
        {
            for (int j = 0; j < 3; j++)
            {
                printf("%c ", game_table[k]);
                k++;
            }
            printf("\n");
        }
        table_fill_count++;
        int o = checkwin();
        if (o == -1 || o == -2)
        {
            if (o == -1)
            {
                printf("YOU WIN\n");
            }
            if (o == -2)
            {
                printf("YOU LOSE\n");
            }
            break;
        }
        if (table_fill_count==4)
        {
            printf("\nDRAW ");
            break;
        }
    }
}
/**
 * @brief Implementation of game vs another player.
 *
 * @returns None
 */
void doublemode()
{
    int m;
    int e1;
    int k = 0;
    int doublemode_table_count=0;
    for (int i = 0; i < 3; i++)
    {
        for (int j = 0; j < 3; j++)
        {
            printf("%c ", game_table[k]);
            k++;
        }
        printf("\n");
    }
    for (int x = 1; x < 10; x++)
    {
        k = 0;
        printf("PLAYER1 - where would you like to place 'x' : ");
        scanf("%d", &m);
        placex(m);
        if(doublemode_table_count<4)
        {
        printf("PLAYER2 - where would you like to place 'o' : ");
        scanf("%d", &e1);
        placey(e1);
        }
        for (int i = 0; i < 3; i++)
        {
            for (int j = 0; j < 3; j++)
            {
                printf("%c ", game_table[k]);
                k++;
            }
            printf("\n");
        }
        doublemode_table_count++;
        int o = checkwin();
        if (o == -1 || o == -2)
        {
            if (o == -1)
            {
                printf("Player 1 WIN\n");
            }
            if (o == -2)
            {
                printf("Player 2 WIN\n");
            }
            break;
        }
        if (doublemode_table_count==4)
        {
            printf("\nDRAW ");
            break;
        }
    }
}
int check_placex(){
  char input[50];
  int n1;
  while (1){
    fgets(input,49,stdin);
    if ( strlen(input) > 2 || strlen(input)  == 0){
      fprintf(stderr,"Invalid move, Enter number 1 - 9: ");
      continue;
    }
    if(sscanf(input,"%d",&n1) != 1){
      fprintf(stderr,"Invalid move, Enter number 1 - 9: ");
      continue;
    } 
    if ((game_table[n1-1] == 'x') || (game_table[n1-1]) == 'o' || (n1== 0)){
      fprintf(stderr,"Already allocated, Enter number: ");
      continue;
    }
    return n1;
  }
} 
/**
 * @brief Update table by placing an `X`
 *
 * @param m location to place `X`
 *
 * @returns None
 */
void placex(int m)
{
    int n1 = 0;
    if (m >= 1 && m <= 9)
    {
        if (game_table[m - 1] != 'x' && game_table[m - 1] != 'o')
        {
            game_table[m - 1] = 'x';
        }
        else
        {
      int n = check_placex();
      placex(n);
        }
    }
    else
    {
    int n = check_placex();
    placex(n);
    }
}
/**
 * @brief Update table by placing an `O`
 *
 * @returns None
 */
void place()
{
    int e = rand() % 9;
    if (e >= 0)
    {
        if (game_table[e] != 'x' && game_table[e] != 'o')
        {
            game_table[e] = 'o';
            printf("\n Computer placed at %d position\n", e + 1);
        }
        else
        {
            place();
        }
    }
}
/**
 * @brief Update table by placing an `O`
 *
 * @param e1 location to place `O`
 *
 * @returns None
 */
void placey(int e1)
{
    int n1 = 0;
    if (e1 >= 1 && e1 <= 9)
    {
        if (game_table[e1 - 1] != 'x' && game_table[e1 - 1] != 'o')
        {
            game_table[e1 - 1] = 'o';
        }
        else
        {
      int n = check_placex();
      placex(n);
        }
    }
    else
    {
    int n = check_placex();
    placex(n);
    }
}
/**
 * @brief Implementation of win conditon checker for 'X' or 'O' whenever the table is updated
 *
 * @returns -1: if 'X' won 
 * @returns -2: if 'O' won
 * @returns 0: if there is no win condition for 'X' or 'O' 
 */
int checkwin()
{
    if (game_table[0] == game_table[1] && game_table[1] == game_table[2])
    {
        if (game_table[0] == 'x' && game_table[1] == 'x' &&
            game_table[2] == 'x')
        {
            return -1;
        }
        if (game_table[0] == 'o' && game_table[1] == 'o' &&
            game_table[2] == 'o')
        {
            return -2;
        }
    }
    else if (game_table[0] == game_table[4] && game_table[4] == game_table[8])
    {
        if (game_table[0] == 'x' && game_table[4] == 'x' &&
            game_table[8] == 'x')
        {
            return -1;
        }
        if (game_table[0] == 'o' && game_table[4] == 'o' &&
            game_table[8] == 'o')
        {
            return -2;
        }
    }
    else if (game_table[0] == game_table[3] && game_table[3] == game_table[6])
    {
        if (game_table[0] == 'x' && game_table[3] == 'x' &&
            game_table[6] == 'x')
        {
            return -1;
        }
        if (game_table[0] == 'o' && game_table[3] == 'o' &&
            game_table[6] == 'o')
        {
            return -2;
        }
    }
    else if (game_table[3] == game_table[4] && game_table[4] == game_table[5])
    {
        if (game_table[3] == 'x' && game_table[4] == 'x' &&
            game_table[5] == 'x')
        {
            return -1;
        }
        if (game_table[3] == 'o' && game_table[4] == 'o' &&
            game_table[5] == 'o')
        {
            return -2;
        }
    }
    else if (game_table[6] == game_table[7] && game_table[7] == game_table[8])
    {
        if (game_table[6] == 'x' && game_table[7] == 'x' &&
            game_table[8] == 'x')
        {
            return -1;
        }
        if (game_table[6] == 'o' && game_table[7] == 'o' &&
            game_table[8] == 'o')
        {
            return -2;
        }
    }
    else if (game_table[1] == game_table[4] && game_table[4] == game_table[7])
    {
        if (game_table[1] == 'x' && game_table[4] == 'x' &&
            game_table[7] == 'x')
        {
            return -1;
        }
        if (game_table[1] == 'o' && game_table[4] == 'o' &&
            game_table[7] == 'o')
        {
            return -2;
        }
    }
    else if (game_table[2] == game_table[5] && game_table[5] == game_table[8])
    {
        if (game_table[2] == 'x' && game_table[5] == 'x' &&
            game_table[8] == 'x')
        {
            return -1;
        }
        if (game_table[2] == 'o' && game_table[5] == 'o' &&
            game_table[8] == 'o')
        {
            return -2;
        }
    }
    else if (game_table[2] == game_table[4] && game_table[4] == game_table[6])
    {
        if (game_table[2] == 'x' && game_table[4] == 'x' &&
            game_table[6] == 'x')
        {
            return -1;
        }
        if (game_table[2] == 'o' && game_table[4] == 'o' &&
            game_table[6] == 'o')
        {
            return -2;
        }
    }
    return 0;
}

这段代码实现了井字游戏(Tic-Tac-Toe)的C语言版本。玩家可以选择与计算机进行单人模式,或与其他玩家进行双人模式。游戏使用一个3x3的数组game_table作为游戏表格,玩家需要在数组中输入位置(1-9)来放置’X’或’O’。

代码主要包含以下函数:

  1. singlemode():单人模式游戏的实现函数。玩家和计算机依次进行操作,每次操作后刷新游戏表格,并检查是否有玩家赢得比赛或平局。
  2. doublemode():双人模式游戏的实现函数。两个玩家依次进行操作,每次操作后刷新游戏表格,并检查是否有玩家赢得比赛或平局。
  3. placex(int m):根据传入的位置m,在游戏表格中放置’X’。
  4. place():计算机生成随机位置,在游戏表格中放置’O’。
  5. placey(int e1):根据传入的位置e1,在游戏表格中放置’O’。
  6. checkwin():检查游戏表格中是否存在玩家或计算机赢得比赛的情况,返回相应的结果(-1代表玩家赢,-2代表计算机赢,0代表没有赢家)。

主程序通过循环控制实现多轮游戏的进行,玩家选择是否进行下一局游戏。

这是一个使用C语言实现的井字棋游戏(Tic-Tac-Toe)。游戏提供了两种模式:单人模式(用户与计算机对战)和双人模式(用户与其他玩家对战)。

游戏使用一个3x3的字符数组game_table作为游戏棋盘,用户需要输入位置(1-9)来在棋盘上放置’X’或’O’。游戏通过不断更新棋盘数组来进行游戏进程的展示和判断胜负。

游戏主函数main()中,利用循环让用户选择游戏模式并进行多轮游戏。在游戏模式选择后,会调用相应的函数进行游戏。

  • singlemode()函数实现了单人模式。用户和计算机轮流在棋盘上放置’X’和’O’,直到有一方获胜或棋盘填满。
  • doublemode()函数实现了双人模式。两个玩家轮流在棋盘上放置’X’和’O’,直到有一方获胜或棋盘填满。
    在放置’X’和’O’的过程中,会调用placex()place()placey()函数来更新棋盘数组。这些函数会检查用户输入的位置是否合法,并进行相应的处理。
    最后,checkwin()函数用于检查是否有一方获胜。通过判断棋盘数组中的元素是否满足获胜条件来确定胜负,如果有一方获胜则返回-1或-2,如果没有获胜则返回0。

在这个井字棋游戏中,判断胜利条件的方法是通过检查棋盘数组中的元素是否满足获胜的条件。以下是判断胜利条件的规则:

  1. 横向获胜:如果任意一行的三个元素相同且不为空字符,则判定该行的玩家获胜。
  2. 纵向获胜:如果任意一列的三个元素相同且不为空字符,则判定该列的玩家获胜。
  3. 对角线获胜:如果左上到右下或右上到左下的对角线上的三个元素相同且不为空字符,则判定该对角线上的玩家获胜。
    checkwin()函数中,按照上述规则进行判断。如果满足以上任意一种获胜条件,返回-1表示’X’获胜,返回-2表示’O’获胜。如果没有任何获胜条件满足且棋盘已满,则返回0表示平局。

在这个井字棋游戏中,判断平局的条件是当棋盘填满时,即所有位置都被玩家放置了’X’或’O’,但没有任何一方获胜。

singlemode()doublemode()函数中,每当玩家和计算机或者两个玩家放置一个’X’或’O’后,会检查棋盘是否已经填满。如果棋盘填满且没有任何一方获胜,就会判定为平局。

checkwin()函数中,也有一个条件用于判断平局。如果在检查获胜条件之前棋盘已经填满了(即执行到第四轮放置’X’或’O’),则返回0表示平局。

总结起来,当棋盘填满且没有任何一方获胜时,判定为平局。

## 游戏逻辑

要实现井字棋游戏的逻辑,可以按照以下步骤进行:

  1. 定义一个3x3的字符数组作为棋盘,用于存储玩家放置的’X’或’O’。初始时,可以将数组元素初始化为’*',表示空位置。
  2. 设计游戏的主循环,使游戏可以进行多轮。在每一轮中,轮流让玩家进行下棋操作,直到有一方获胜或棋盘填满。
  3. 实现下棋函数,根据当前玩家的回合,接受玩家输入并更新棋盘数组。可以根据玩家的选择来判断合法性,例如输入的位置是否在1-9范围内,以及该位置是否为空。
  4. 在每次玩家放置棋子后,调用函数检查是否有一方获胜。判断胜利的条件包括横向、纵向和对角线的连线。
  5. 如果没有获胜且棋盘未满,在单人模式中调用计算机放置棋子的函数,或在双人模式中切换到另一个玩家的回合。
  6. 当有一方获胜或棋盘填满时,结束当前轮游戏并显示结果。根据获胜方的不同,可以显示相应的提示信息。
  7. 提供选项让玩家选择是否开始下一轮游戏。如果选择继续,重置棋盘数组并返回到主循环;如果选择结束,退出游戏。
    通过以上步骤,可以实现井字棋游戏的逻辑,并在控制台上进行交互式的游戏体验。根据需要,你还可以添加其他功能,如错误处理、游戏界面美化等。

以下是一个示例的井字棋游戏逻辑的伪代码实现:

定义全局变量:
棋盘大小:ROWS = 3,COLS = 3
棋盘数组:game_table[ROWS][COLS]
定义函数:
主函数:
    初始化棋盘数组
    游戏循环:
        选择游戏模式
        如果选择单人模式:
            调用单人模式函数
        否则,如果选择双人模式:
            调用双人模式函数
        询问是否继续游戏,如果不继续:
            退出游戏
单人模式函数:
    初始化游戏状态
    游戏循环:
        显示当前棋盘状态
        如果轮到玩家:
            提示玩家输入位置
            如果输入合法:
                更新棋盘数组
                检查是否获胜或平局
                如果获胜或平局:
                    显示结果并结束游戏
            否则:
                提示重新输入
        否则,如果轮到计算机:
            计算机随机选择位置
            更新棋盘数组
            检查是否获胜或平局
            如果获胜或平局:
                显示结果并结束游戏
双人模式函数:
    初始化游戏状态
    游戏循环:
        显示当前棋盘状态
        提示当前玩家输入位置
        如果输入合法:
            更新棋盘数组
            检查是否获胜或平局
            如果获胜或平局:
                显示结果并结束游戏
            否则,切换到另一个玩家的回合
        否则:
            提示重新输入
更新棋盘数组函数:
    接受位置和当前玩家的参数
    根据位置更新棋盘数组
检查是否获胜函数:
    检查横向、纵向和对角线的连线是否满足获胜条件
    如果有一方获胜:
        返回-1或-2分别表示'X'或'O'获胜
    否则,如果棋盘已满:
        返回0表示平局
    否则:
        返回-3表示游戏继续
提示玩家输入位置函数:
    提示玩家输入位置
    接受输入并判断合法性,直到输入合法为止
显示当前棋盘状态函数:
    遍历棋盘数组,打印当前棋盘状态

这只是一个简单的示例,实际的游戏逻辑可能会更复杂,还可以根据需要进行扩展和优化。你可以根据这个伪代码来实现具体的游戏逻辑,并根据需要添加错误处理、界面美化等功能。

游戏逻辑的基本结构可以分为以下几个步骤:

  1. 初始化游戏状态:包括创建游戏棋盘和设置初始的玩家回合。
  2. 游戏主循环:在一个循环中进行游戏的进行,直到有一方获胜或平局。
  3. 玩家回合:根据当前回合的玩家,接受输入并更新游戏状态。
  4. 更新游戏状态:根据玩家的输入,更新游戏棋盘和判断胜负。
  5. 判断胜负:检查游戏棋盘上是否有一方获胜或平局。
  6. 结束游戏:显示游戏结果,询问是否再次游戏。
    下面是一个更详细的游戏逻辑的基本结构示例:
初始化游戏状态
循环:
    显示游戏状态
    如果是玩家回合:
        等待玩家输入
        更新游戏状态
        检查胜负
        如果有一方获胜或平局:
            结束游戏并显示结果
        否则:
            切换到下一个回合的玩家
    否则,如果是计算机回合:
        计算机执行下棋操作
        更新游戏状态
        检查胜负
        如果有一方获胜或平局:
            结束游戏并显示结果
        否则:
            切换到下一个回合的玩家
结束游戏

这个基本结构可以根据具体的编程语言和需求进行具体实现和优化。在实现游戏逻辑时,需要考虑玩家输入的合法性、胜负的判断逻辑、游戏结果的显示等方面。

/**
 * @file
 * @brief C implementation of [Hangman Game](https://en.wikipedia.org/wiki/Hangman_(game))
 * @details
 * Simple, readable version of hangman.
 * Changed graphic to duck instead of traditional stick figure (same number of guesses).
 * @author [AtlantaEmrys2002](https://github.com/AtlantaEmrys2002)
*/
#include <ctype.h> /// for main() - tolower()
#include <stdio.h> /// for main(), new_word(), new_guess(), won() - I/O operations
#include <stdlib.h> /// for all functions - exit(), rand() and file functions
#include <string.h> /// for main() - for string operations strlen, strchr, strcpy
#include <time.h> /// for new_game() - used with srand() for declaring new game instance
/*
 * @brief game_instance structure that holds current state of game
 */
struct game_instance{
    char current_word[30]; ///< word to be guessed by player
    char hidden[30]; ///< hidden version of word that is displayed to player
    int size; ///< size of word
    int incorrect; ///< number of incorrect guesses
    char guesses[25]; ///< previous guesses
    int guesses_size; ///< size of guesses array
};
// function prototypes
struct game_instance new_game(void); // creates a new game
int new_guess(char, const char guesses[], int size); // checks if player has already played letter
int in_word(char, const char word[], unsigned int size); // checks if letter is in word
void picture(int score); // outputs image of duck (instead of hang man)
void won(const char word[], int score); // checks if player has won or lost
/**
 * @brief Main Function
 * @returns 0 on exit
 */
int main() {
    struct game_instance game = new_game(); // new game created
    char guess; // current letter guessed by player
    // main loop - asks player for guesses
    while ((strchr(game.hidden, '_') != NULL) && game.incorrect <= 12) {
        do {
            printf("\n****************************\n");
            printf("Your word: ");
            for (int i = 0; i < game.size; i++) {
                printf("%c ", game.hidden[i]);
            }
            if (game.guesses_size > 0) {
                printf("\nSo far, you have guessed: ");
                for (int i = 0; i < game.guesses_size; i++) {
                    printf("%c ", game.guesses[i]);
                }
            }
            printf("\nYou have %d guesses left.", (12 - game.incorrect));
            printf("\nPlease enter a letter: ");
            scanf(" %c", &guess);
            guess = tolower(guess);
        } while (new_guess(guess, game.guesses, game.guesses_size) != -1);
        game.guesses[game.guesses_size] = guess; // adds new letter to guesses array
        game.guesses_size++; // updates size of guesses array
        if (in_word(guess, game.current_word, game.size) == 1) {
            printf("That letter is in the word!");
            for (int i = 0; i < game.size; i++) {
                if ((game.current_word[i]) == guess) {
                    game.hidden[i] = guess;
                }
            }
        } else {
            printf("That letter is not in the word.\n");
            (game.incorrect)++;
        }
        picture(game.incorrect);
    }
    won(game.current_word, game.incorrect);
    return 0;
}
/**
 * @brief checks if letter has been guessed before
 * @param new_guess letter that has been guessed by player
 * @param guesses array of player's previous guesses
 * @param size size of guesses[] array
 * @returns 1 if letter has been guessed before
 * @returns -1 if letter has not been guessed before
 */
int new_guess(char new_guess, const char guesses[], int size) {
    for (int j = 0; j < size; j++) {
        if (guesses[j] == new_guess) {
            printf("\nYou have already guessed that letter.");
            return 1;
        }
    }
    return -1;
}
/**
 * @brief checks if letter is in current word
 * @param letter letter guessed by player
 * @param word current word
 * @param size length of word
 * @returns 1 if letter is in word
 * @returns -1 if letter is not in word
 */
int in_word(char letter, const char word[], unsigned int size) {
    for (int i = 0; i < size; i++) {
        if ((word[i]) == letter) {
            return 1;
        }
    }
    return -1;
}
/**
 * @brief creates a new game - generates a random word and stores in global variable current_word
 * @returns current_game - a new game instance containing randomly selected word, its length and hidden version of word
 */
struct game_instance new_game() {
    char word[30]; // used throughout function
    FILE *fptr;
    fptr = fopen("games/words.txt", "r");
    if (fptr == NULL){
        fprintf(stderr, "File not found.\n");
        exit(EXIT_FAILURE);
    }
    // counts number of words in file - assumes each word on new line
    int line_number = 0;
    while (fgets(word, 30, fptr) != NULL) {
        line_number++;
    }
    rewind(fptr);
    // generates random number
    int random_num;
    srand(time(NULL));
    random_num = rand() % line_number;
    // selects randomly generated word
    int s = 0;
    while (s <= random_num){
        fgets(word, 30, fptr);
        s++;
    }
    // formats string correctly
    if (strchr(word, '\n') != NULL){
        word[strlen(word) - 1] = '\0';
    }
    fclose(fptr);
    // creates new game instance
    struct game_instance current_game;
    strcpy(current_game.current_word, word);
    current_game.size = strlen(word);
    for (int i = 0; i < (strlen(word)); i++) {
        current_game.hidden[i] = '_';
    }
    current_game.incorrect = 0;
    current_game.guesses_size = 0;
    return current_game;
}
/**
 * @brief checks if player has won or lost
 * @param word the word player has attempted to guess
 * @param score how many incorrect guesses player has made
 * @returns void
 */
void won(const char word[], int score) {
    if (score > 12) {
        printf("\nYou lost! The word was: %s.\n", word);
    }
    else {
        printf("\nYou won! You had %d guesses left.\n", (12 - score));
    }
}
/*
 * @brief gradually draws duck as player gets letters incorrect
 * @param score how many incorrect guesses player has made
 * @returns void
 */
void picture(int score) {
    switch(score) {
        case 12:
            printf("\n      _\n"
                   "  __( ' )> \n"
                   " \\_ < _ ) ");
            break;
        case 11:
            printf("\n      _\n"
                   "  __( ' )\n"
                   " \\_ < _ ) ");
            break;
        case 10:
            printf("\n      _\n"
                   "  __(   )\n"
                   " \\_ < _ ) ");
            break;
        case 9:
            printf("\n        \n"
                   "  __(   )\n"
                   " \\_ < _ ) ");
            break;
        case 8:
            printf("\n        \n"
                   "  __(    \n"
                   " \\_ < _ ) ");
            break;
        case 7:
            printf("\n        \n"
                   "  __     \n"
                   " \\_ < _ ) ");
            break;
        case 6:
            printf("\n        \n"
                   "  _      \n"
                   " \\_ < _ ) ");
            break;
        case 5:
            printf("\n        \n"
                   "  _      \n"
                   "   _ < _ ) ");
            break;
        case 4:
            printf("\n        \n"
                   "         \n"
                   "   _ < _ ) ");
            break;
        case 3:
            printf("\n        \n"
                   "         \n"
                   "     < _ ) ");
            break;
        case 2:
            printf("\n        \n"
                   "         \n"
                   "       _ ) ");
            break;
        case 1:
            printf("\n        \n"
                   "         \n"
                   "         ) ");
            break;
        case 0:
            break;
        default:
            printf("\n      _\n"
                   "  __( ' )> QUACK!\n"
                   " \\_ < _ ) ");
            break;
    }
}

这是一个用C语言实现的简单的Hangman(猜词游戏)的代码示例。该游戏使用了一个结构体game_instance来保存游戏的当前状态。

  1. new_game()函数用于创建一个新的游戏实例,它从文件中随机选择一个单词作为要猜的单词,并初始化游戏状态。
  2. new_guess()函数用于检查玩家是否已经猜过某个字母。
  3. in_word()函数用于检查猜测的字母是否在要猜的单词中。
  4. picture()函数根据玩家的错误猜测次数输出对应的图形。
  5. won()函数用于判断玩家是否胜利或失败。
  6. main()函数是游戏的主函数,包括主循环。在主循环中,玩家输入字母进行猜测,根据猜测结果更新游戏状态,并判断是否获胜或失败。

游戏逻辑

实现游戏逻辑主要涉及以下几个方面:

  1. 初始化游戏状态:创建一个包含游戏所需信息的数据结构,例如当前要猜的单词、已猜过的字母、错误猜测次数等。
  2. 接收玩家输入:使用合适的输入方法(如键盘输入)接收玩家的猜测。
  3. 更新游戏状态:根据玩家的猜测,更新游戏状态,例如更新已猜过的字母列表、检查猜测的字母是否在单词中等。
  4. 判断胜负条件:检查游戏状态,判断玩家是否已经获胜或失败,例如判断已猜出的字母是否完全揭示了单词,或者判断错误猜测次数是否达到了一定阈值。
  5. 显示游戏状态:根据当前游戏状态,以合适的方式将游戏信息呈现给玩家,例如显示当前猜测的单词状态、已猜过的字母列表、剩余的错误猜测次数等。
  6. 循环游戏流程:通过循环不断接收玩家输入、更新游戏状态和判断胜负条件,直至游戏结束。

具体实现游戏逻辑的方式会根据编程语言和游戏设计的要求而有所不同。你可以使用条件语句、循环、函数等编程结构来实现上述步骤。在实现过程中,可以利用数据结构、字符串处理、随机数生成等功能来简化和优化游戏逻辑的实现。

以下是一种基本的游戏逻辑实现步骤:

  1. 初始化游戏状态:
  • 创建一个包含游戏所需信息的数据结构,例如当前要猜的单词、已猜过的字母、错误猜测次数等。
  • 从单词列表中选择一个单词作为要猜的单词,并将其存储在游戏状态数据结构中。
  • 初始化已猜过的字母列表和错误猜测次数为初始值。
  1. 循环游戏流程:
  • 在一个循环中执行以下步骤,直到游戏结束:
  • 显示当前游戏状态,例如显示已猜过的字母、单词的部分揭示情况、错误猜测次数等。
  • 接收玩家的输入,例如接收玩家猜测的字母。
  • 更新游戏状态:
  • 检查玩家的输入是否合法,例如检查是否为有效的字母。
  • 检查玩家的输入是否已经猜过,避免重复猜测。
  • 检查玩家的输入是否在要猜的单词中,更新已猜过的字母列表和单词的部分揭示情况。
  • 如果玩家的输入不在要猜的单词中,增加错误猜测次数。
  • 判断胜负条件:
  • 检查单词的部分揭示情况,判断是否已经猜出了整个单词。
  • 检查错误猜测次数是否达到了允许的最大次数。
  • 如果游戏结束,跳出循环。
  1. 显示游戏结果:
  • 根据游戏的胜负情况,显示相应的游戏结果,例如显示玩家获胜或失败的消息,显示正确的单词等。
    这些步骤是一个基本的游戏逻辑实现流程,你可以根据具体的编程语言和游戏设计要求进行调整和扩展。在实现过程中,可以使用条件语句、循环、函数等编程结构来实现不同的步骤。同时,根据需要,你可能还需要实现其他辅助函数和处理用户输入的逻辑。

游戏逻辑的实现步骤可以根据具体的游戏设计和编程语言而有所不同,但是下面是一个通用的游戏逻辑实现的基本步骤:

  1. 初始化游戏状态:
  • 创建一个包含游戏所需信息的数据结构,例如当前游戏的状态、玩家的得分、关卡等。
  • 设置游戏的初始状态,例如设置玩家的初始位置、初始得分等。
  1. 游戏主循环:
  • 在一个循环中进行游戏的进行,直到达到游戏结束的条件。
  • 在每次循环中更新游戏状态、接收玩家的输入并处理。
  1. 更新游戏状态:
  • 根据玩家的输入和当前的游戏状态,更新游戏中的角色位置、得分、生命值等。
  • 检查碰撞、判断游戏结束的条件等。
  1. 处理玩家输入:
  • 根据游戏设计,接收玩家的输入,例如按键、鼠标点击等。
  • 根据玩家的输入,执行相应的操作,例如移动角色、发射子弹等。
  1. 判断游戏结束:
  • 在游戏的每个循环中,检查游戏是否达到结束的条件,例如玩家达到目标、时间耗尽、生命值耗尽等。
  1. 游戏结束处理:
  • 根据游戏的结束条件,执行相应的游戏结束处理操作,例如显示得分、显示游戏结束画面、询问玩家是否重新开始等。
    这些步骤是一个通用的游戏逻辑实现流程,具体的实现会根据游戏的类型和需求有所不同。在实现过程中,你可能需要使用条件判断、循环、事件处理等编程技巧来实现游戏逻辑。同时,你还可以根据需要添加其他功能,例如音效、动画效果、关卡设计等,以增强游戏的乐趣和体验。

这是一个C语言实现的简单的“Hangman”游戏。游戏初始化的过程在new_game()函数中完成,具体步骤如下:

  1. 打开包含单词的文件:
  • 使用fopen()函数打开文件,文件名为games/words.txt
  • 检查文件是否成功打开。如果打开失败,则输出错误消息并使用exit()函数退出程序。
  1. 统计文件中的单词数:
  • 使用循环和fgets()函数逐行读取文件,计算文件中的单词行数。
  1. 生成随机数:
  • 使用srand()函数结合time(NULL)函数生成随机数的种子。
  • 使用rand()函数生成一个随机数,范围为0到单词行数之间的整数。
  1. 选择随机单词:
  • 使用循环和fgets()函数跳过文件中的前几行,直到达到随机数所指定的行。
  • 从文件中读取选定的单词,并将其存储在word数组中。
  • 对读取的单词进行格式化处理,去除末尾的换行符。
  1. 创建游戏实例:
  • 定义一个game_instance结构,并将选定的单词、单词长度和隐藏版本的单词存储在相应的成员变量中。
  • 将隐藏版本的单词初始化为下划线。
  • 设置初始的错误猜测次数和猜测记录数组的大小。
  1. 返回游戏实例:
  • 返回创建的游戏实例供主函数使用。

这个初始化过程确保了游戏开始时随机选择一个单词,并将游戏状态初始化为初始值。游戏的主循环在主函数中执行,玩家可以进行猜测,直到猜测正确或错误次数超过限制。在游戏结束后,调用won()函数根据猜测结果输出相应的信息。new_guess()函数用于检查玩家是否已经猜过某个字母,in_word()函数用于检查猜测的字母是否在单词中。picture()函数用于根据错误猜测的次数显示相应的图形。

对于这个Hangman游戏的C实现,你可以使用以下测试样例:

测试样例1:

输入:D

输入:O

输入:G

输入:C

输入:A

输入:T

输入:S

输入:R

输入:N

输入:E

输入:Y

输入:H

输入:J

输入:L

输入:B

输入:M

输入:U

输入:F

输入:I

输入:K

输入:Q

输入:P

输入:W

输入:V

输入:X

输入:Z

预期输出:


Your word: _ _ _ _

You have 12 guesses left.

Please enter a letter: D

That letter is not in the word.

_

__( )

_ < _ )


Your word: _ _ _ _

So far, you have guessed: D

You have 11 guesses left.

Please enter a letter: O

That letter is in the word!

_

__( ’ )

_ < _ )


Your word: _ _ _ _

So far, you have guessed: D O

You have 10 guesses left.

Please enter a letter: G

That letter is not in the word.

_

__( ’ )

_ < _ )


Your word: _ _ _ _

So far, you have guessed: D O G

You have 9 guesses left.

Please enter a letter: C

That letter is not in the word.

_

__( ’ )

_ < _ )


Your word: _ _ _ _

So far, you have guessed: D O G C

You have 8 guesses left.

Please enter a letter: A

That letter is in the word!

_

__( ’ )

_ < _ )

预期输出应该是根据你的猜测和游戏规则逐步更新的,直到你赢得游戏或者猜测次数用尽。你可以根据需要进行进一步的测试,尝试猜测不同的字母,并观察游戏的输出结果。

C++t图形化界面

/**
 * @file
 * @author [Krishna Vedala](https://github.com/kvedala)
 * @brief Implementation of
 * [Spirograph](https://en.wikipedia.org/wiki/Spirograph)
 *
 * @details
 * Implementation of the program is based on the geometry shown in the figure
 * below:
 *
 * <a
 * href="https://commons.wikimedia.org/wiki/File:Resonance_Cascade.svg"><img
 * src="https://upload.wikimedia.org/wikipedia/commons/3/39/Resonance_Cascade.svg"
 * alt="Spirograph geometry from Wikipedia" style="width: 250px"/></a>
 */
#define _USE_MATH_DEFINES /**< required for MSVC compiler */
#include <math.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
/** Generate spirograph curve into arrays `x` and `y` such that the i^th point
 * in 2D is represented by `(x[i],y[i])`. The generating function is given by:
 * \f{eqnarray*}{
 * x &=& R\left[ (1-k) \cos (t) + l\cdot k\cdot\cos \left(\frac{1-k}{k}t\right)
 * \right]\\
 * y &=& R\left[ (1-k) \sin (t) - l\cdot k\cdot\sin \left(\frac{1-k}{k}t\right)
 * \right] \f}
 * where
 * * \f$R\f$ is the scaling parameter that we will consider \f$=1\f$
 * * \f$l=\frac{\rho}{r}\f$ is the relative distance of marker from the centre
 * of inner circle and \f$0\le l\le1\f$
 * * \f$\rho\f$ is physical distance of marker from centre of inner circle
 * * \f$r\f$ is the radius of inner circle
 * * \f$k=\frac{r}{R}\f$ is the ratio of radius of inner circle to outer circle
 * and \f$0<k<1\f$
 * * \f$R\f$ is the radius of outer circle
 * * \f$t\f$ is the angle of rotation of the point i.e., represents the time
 * parameter
 *
 * Since we are considering ratios, the actual values of \f$r\f$ and
 * \f$R\f$ are immaterial.
 *
 * @param [out] x output array containing absicca of points (must be
 * pre-allocated)
 * @param [out] y output array containing ordinates of points (must be
 * pre-allocated)
 * @param l the relative distance of marker from the centre of
 * inner circle and \f$0\le l\le1\f$
 * @param k the ratio of radius of inner circle to outer circle and
 * \f$0<k<1\f$
 * @param N number of sample points along the trajectory (higher = better
 * resolution but consumes more time and memory)
 * @param num_rot the number of rotations to perform (can be fractional value)
 */
void spirograph(double *x, double *y, double l, double k, size_t N, double rot)
{
    double dt = rot * 2.f * M_PI / N;
    double t = 0.f, R = 1.f;
    const double k1 = 1.f - k;
    for (size_t dk = 0; dk < N; dk++, t += dt)
    {
        x[dk] = R * (k1 * cos(t) + l * k * cos(k1 * t / k));
        y[dk] = R * (k1 * sin(t) - l * k * sin(k1 * t / k));
    }
}
/**
 * @brief Test function to save resulting points to a CSV file.
 *
 */
void test(void)
{
    size_t N = 500;
    double l = 0.3, k = 0.75, rot = 10.;
    char fname[50];
    snprintf(fname, 50, "spirograph_%.2f_%.2f_%.2f.csv", l, k, rot);
    FILE *fp = fopen(fname, "wt");
    if (!fp)
    {
        perror(fname);
        exit(EXIT_FAILURE);
    }
    double *x = (double *)malloc(N * sizeof(double));
    double *y = (double *)malloc(N * sizeof(double));
    spirograph(x, y, l, k, N, rot);
    for (size_t i = 0; i < N; i++)
    {
        fprintf(fp, "%.5g, %.5g", x[i], y[i]);
        if (i < N - 1)
        {
            fputc('\n', fp);
        }
    }
    fclose(fp);
    free(x);
    free(y);
}
#ifdef USE_GLUT  // this is set by CMAKE automatically, if available
#ifdef __APPLE__
#include <GLUT/glut.h>  // include path on Macs is different
#else
#include <GL/glut.h>
#endif
static bool paused = 0; /**< flag to set pause/unpause animation */
static const int animation_speed = 25; /**< animation delate in ms */
static const double step = 0.01;   /**< animation step size */
static double l_ratio = 0.1;       /**< the l-ratio defined in docs */
static double k_ratio = 0.1;       /**< the k-ratio defined in docs */
static const double num_rot = 20.; /**< number of rotations to simulate */
/** A wrapper that is not available in all GLUT implementations.
 */
static inline void glutBitmapString(void *font, char *string)
{
    for (char *ch = string; *ch != '\0'; ch++) glutBitmapCharacter(font, *ch);
}
/**
 * @brief Function to graph (x,y) points on the OpenGL graphics window.
 *
 * @param x array containing absicca of points (must be pre-allocated)
 * @param y array containing ordinates of points (must be pre-allocated)
 * @param N number of points in the arrays
 */
void display_graph(const double *x, const double *y, size_t N, double l,
                   double k)
{
    glClearColor(1.0f, 1.0f, 1.0f,
                 0.0f);            // Set background color to white and opaque
    glClear(GL_COLOR_BUFFER_BIT);  // Clear the color buffer (background)
    if (x && y)
    {
        glBegin(GL_LINES);         // draw line segments
        glColor3f(0.f, 0.f, 1.f);  // blue
        glPointSize(2.f);          // point size in pixels
        for (size_t i = 1; i < N; i++)
        {
            glVertex2f(x[i - 1], y[i - 1]);  // line from
            glVertex2f(x[i], y[i]);          // line to
        }
        glEnd();
    }
    glColor3f(0.f, 0.f, 0.f);
    char buffer[20];
    snprintf(buffer, 20, "l = %.3f", l);
    glRasterPos2f(-.85, .85);
    glutBitmapString(GLUT_BITMAP_HELVETICA_18, buffer);
    snprintf(buffer, 20, "k = %.3f", k);
    glRasterPos2f(-.85, .75);
    glutBitmapString(GLUT_BITMAP_HELVETICA_18, buffer);
    glutSwapBuffers();
}
/**
 * @brief Test function with animation
 *
 */
void test2(void)
{
    const size_t N = 1000;  // number of samples
    static bool direction1 = true;  // increment if true, otherwise decrement
    static bool direction2 = true;  // increment if true, otherwise decrement
    double *x = (double *)malloc(N * sizeof(double));
    double *y = (double *)malloc(N * sizeof(double));
    spirograph(x, y, l_ratio, k_ratio, N, num_rot);
    display_graph(x, y, N, l_ratio, k_ratio);
    free(x);  // free dynamic memories
    free(y);
    if (paused)
        // if paused, do not update l_ratio and k_ratio
        return;
    if (direction1)  // increment k_ratio
    {
        if (k_ratio >= (1.f - step))  // maximum limit
            direction1 = false;       // reverse direction of k_ratio
        else
            k_ratio += step;
    }
    else  // decrement k_ratio
    {
        if (k_ratio <= step)  // minimum limit
        {
            direction1 = true;  // reverse direction of k_ratio
            if (direction2)  // increment l_ratio
            {
                if (l_ratio >= (1.f - step))  // max limit of l_ratio
                    direction2 = false;       // reverse direction of l_ratio
                else
                    l_ratio += step;
            }
            else  // decrement l_ratio
            {
                if (l_ratio <= step)    // minimum limit of l_ratio
                    direction2 = true;  // reverse direction of l_ratio
                else
                    l_ratio -= step;
            }
        }
        else  // no min limit of k_ratio
            k_ratio -= step;
    }
}
/**
 * @brief GLUT timer callback function to add animation delay.
 */
void timer_cb(int id)
{
    glutPostRedisplay();
    glutTimerFunc(animation_speed, timer_cb, 0);
}
/**
 * @brief Keypress event call back function.
 *
 * @param key ID of the key pressed
 * @param x mouse pointer position at event
 * @param y mouse pointer position at event
 */
void keyboard_cb(unsigned char key, int x, int y)
{
    switch (key)
    {
    case ' ':              // spacebar toggles pause
        paused = !paused;  // toggle
        break;
    case '+':  // up arrow key
        k_ratio += step;
        display_graph(NULL, NULL, 1, l_ratio, k_ratio);
        break;
    case '_':  // down arrow key
        k_ratio -= step;
        display_graph(NULL, NULL, 1, l_ratio, k_ratio);
        break;
    case '=':  // left arrow key
        l_ratio += step;
        display_graph(NULL, NULL, 1, l_ratio, k_ratio);
        break;
    case '-':  // right arrow key
        l_ratio -= step;
        display_graph(NULL, NULL, 1, l_ratio, k_ratio);
        break;
    case 0x1B:  // escape key exits
        exit(EXIT_SUCCESS);
    }
}
#endif
/** Main function */
int main(int argc, char **argv)
{
    test();
#ifdef USE_GLUT
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
    glutCreateWindow("Spirograph");
    glutInitWindowSize(400, 400);
    // glutIdleFunc(glutPostRedisplay);
    glutTimerFunc(animation_speed, timer_cb, 0);
    glutKeyboardFunc(keyboard_cb);
    glutDisplayFunc(test2);
    glutMainLoop();
#endif
    return 0;
}

这段代码实现了Spirograph(旋转曲线)的绘制功能。Spirograph是一种绘图工具,通过在内园上放置一个小块(marker),然后让它随时间旋转,可以绘制出各种华丽的旋转曲线。在这个程序中,根据给定的参数l和k,生成了一条Spirograph曲线,并将结果保存到CSV文件中。

代码中主要的函数是spirographtest

  • spirograph函数根据给定的l和k参数,以及采样点数N和旋转次数num_rot,生成Spirograph曲线的(x, y)坐标数据。
  • test函数是一个测试函数,它调用spirograph函数生成Spirograph曲线,并将结果保存到一个CSV文件中。

如果编译时定义了USE_GLUT宏,并且已经安装了GLUT库,那么代码中还包含了一些用于在OpenGL图形窗口中显示Spirograph曲线和实现动画效果的函数。如果不需要OpenGL图形界面,可以直接运行test函数进行曲线生成和保存,不需要使用GLUT部分的代码。

总而言之,这段代码提供了一个生成Spirograph曲线并可视化的功能,可以根据需要选择是否使用OpenGL图形界面。

除了主要的函数以外,代码还包含了一些辅助函数和全局变量。

辅助函数:

  • snprintf函数用于生成保存CSV文件的文件名,格式为spirograph_%.2f_%.2f_%.2f.csv,其中%.2f表示保留两位小数的浮点数。
  • display_graph函数用于在OpenGL图形窗口中绘制Spirograph曲线。它使用OpenGL的绘图函数来绘制线段,并使用glRasterPos2fglutBitmapString函数在窗口中显示l和k参数的值。
  • glutBitmapString是一个不在所有GLUT实现中都可用的包装函数,用于绘制字符串。

全局变量:

  • paused是一个布尔变量,用于指示动画是否处于暂停状态。
  • animation_speed是动画的延迟时间,单位为毫秒。
  • step是动画中l_ratio和k_ratio的步长。
  • l_ratiok_ratio是用于绘制Spirograph曲线的相对距离参数。
  • num_rot是旋转的次数。

如果定义了USE_GLUT宏并成功编译并运行了程序,那么它将创建一个名为"Spirograph"的OpenGL图形窗口,并执行以下操作:

  • 使用glClearColor函数设置背景颜色为白色。
  • 使用glClear函数清除颜色缓冲区。
  • 调用spirograph函数生成Spirograph曲线的坐标数据。
  • 使用glBeginglVertex2f函数绘制线段。
  • 使用glColor3f函数设置颜色为蓝色,使用glPointSize函数设置点的大小为2像素。
  • 使用glRasterPos2fglutBitmapString函数在窗口中显示l和k参数的值。
  • 使用glutSwapBuffers函数交换前后缓冲区。

程序还定义了一些其他函数,包括:

  • timer_cb是一个GLUT定时器回调函数,用于控制动画的刷新频率。
  • keyboard_cb是一个键盘事件回调函数,用于监听按键操作。

这就是代码的主要内容和功能。它提供了一个完整的Spirograph曲线生成和可视化的功能,并且可以通过键盘操作来控制动画的暂停、调节l和k参数的值等。如果没有OpenGL支持或者不需要图形界面,可以直接调用test函数进行曲线生成和保存。

目录
相关文章
|
2月前
|
存储 算法 编译器
【C++ TypeName用法 】掌握C++中的TypeName:模板编程的瑞士军刀
【C++ TypeName用法 】掌握C++中的TypeName:模板编程的瑞士军刀
238 0
|
2月前
|
存储 JSON 算法
C++ JSON库 nlohmann::basic_json::boolean_t 的用法
C++ JSON库 nlohmann::basic_json::boolean_t 的用法
35 0
|
23天前
|
人工智能 机器人 测试技术
【C++】static_cast基本用法(详细讲解)
【C++】static_cast基本用法(详细讲解)
|
2月前
|
存储 缓存 安全
【C/C++ 关键字 存储类说明符 】 线程局部变量的魔法:C++ 中 thread_local的用法
【C/C++ 关键字 存储类说明符 】 线程局部变量的魔法:C++ 中 thread_local的用法
33 0
|
15天前
|
C++
C++ 默认参数与引用传递:语法、用法及示例
C++ 允许函数参数具有默认值,简化调用。例如,`void myFunction(string country = &quot;Norway&quot;)` 中`country`默认为&quot;Norway&quot;。默认参数仅适用于函数参数,不包括返回值。引用传递是另一种传递方式,函数直接访问变量内存,允许修改原值,提高效率。`void swapNums(int &x, int &y)` 中`x`和`y`为引用参数。了解这些特性可提升代码可读性和性能。
44 0
|
23天前
|
人工智能 安全 机器人
【C++】dynamic_cast基本用法(详细讲解)
【C++】dynamic_cast基本用法(详细讲解)
|
23天前
|
人工智能 安全 机器人
【C++】const_cast基本用法(详细讲解)
【C++】const_cast基本用法(详细讲解)
|
28天前
|
人工智能 机器人 中间件
【C++】C++回调函数基本用法(详细讲解)
【C++】C++回调函数基本用法(详细讲解)
|
2月前
|
算法 安全 编译器
【C++ 17 新特性 折叠表达式 fold expressions】理解学习 C++ 17 折叠表达式 的用法
【C++ 17 新特性 折叠表达式 fold expressions】理解学习 C++ 17 折叠表达式 的用法
25 1
|
2月前
|
算法 安全 Unix
【C++ 20 信号量 】C++ 线程同步新特性 C++ 20 std::counting_semaphore 信号量的用法 控制对共享资源的并发访问
【C++ 20 信号量 】C++ 线程同步新特性 C++ 20 std::counting_semaphore 信号量的用法 控制对共享资源的并发访问
32 0