Bootstrap

C语言经典例题-4

1.五子棋

test.c - 测试游戏的逻辑

game.c - 与游戏相关函数实现

game.h - 与游戏相关函数的声明,符号声明,头文件的包含。

//test.c
#define _CRT_SECURE_NO_WARNINGS 1

#include "game.h"

void menu()  //打印菜单函数
{
    printf("********************************\n");
    printf("*********   1.play    **********\n");
    printf("*********   0.exit    **********\n");
    printf("********************************\n");
}


void game()
{
    char board[ROW][COL];
    char ret = 0;
    srand((unsigned int)time(NULL));
    InitBoard(board, ROW, COL);  //棋盘的初始化
    DisplayBoard(board, ROW, COL); //打印棋盘
    while (1)
    {
        PlayerMove(board, ROW, COL);  //玩家下棋
        DisplayBoard(board, ROW, COL); //显示当前棋盘状态
        ret = IsWin(board, ROW, COL);  //判断胜负
        if (ret != 'C') //如果是* # Q ,那么游戏就结束了。
        {
            break;
        }

        ComputerMove(board, ROW, COL);  //电脑下棋
        DisplayBoard(board, ROW, COL);    
        ret = IsWin(board, ROW, COL);
        if (ret != 'C')
        {
            break;
        }
    }
    if (ret == '*')
    {
        printf("玩家获胜\n");
    }
    else if (ret == '#')
    {
        printf("电脑获胜\n");
    }
    else
    {
        printf("平局\n");
    }

}

int main()
{
    int input = 0;
    do
    {
        menu();
        printf("请选择:");
        scanf("%d", &input);
        switch (input)
        {
        case 1:
            game();
            break;
        case 0 :
            printf("退出游戏\n");
            break;
        default:
            printf("输入错误\n");
            break;
        }
    } while (input);
    return 0;
}
//game.h
#pragma once

#define ROW 3
#define COL 3

#include <stdio.h>
#include <stdlib.h>  //使用rand时需要调用
#include <time.h>     //使用time时需要调用

void InitBoard(char board[ROW][COL], int row, int col);  //初始化函数的声明

void DisplayBoard(char board[ROW][COL], int row, int col);  //显示棋盘状态函数的声明

void PlayerMove(char board[ROW][COL], int row, int col);  //玩家下棋函数的声明

void ComputerMove(char board[ROW][COL], int row, int col);  //电脑下棋函数的声明

char IsWin(char board[ROW][COL], int row, int col);  //判断胜负函数的声明

//  *  -   玩家获胜
//  #  -   电脑获胜
//  Q  -   平局
//  C  -   继续
//game.c
#define _CRT_SECURE_NO_WARNINGS 1

#include "game.h"

void InitBoard(char board[ROW][COL], int row, int col)  //初始化函数的定义
{
    int i = 0;
    int j = 0;
    for (i = 0;i < row;i++)
    {
        for (j = 0;j < col;j++)
        {
            board[i][j] = ' ';
        }
    }
}

void DisplayBoard(char board[ROW][COL], int row, int col)  //显示棋盘状态函数的定义
{
    int i = 0;
    int j = 0;
    for (i = 0;i < row;i++)
    {
        for (j = 0;j < col;j++)
        {
            printf(" %c ", board[i][j]);
            if (j < col - 1)
            {
                printf("|");
            }
        }
        printf("\n");
        if (i < row - 1)
        {
            for (j = 0;j < col;j++)
            {
                printf("---");
                if (j < col - 1)
                {
                    printf("|");
                }
            }
            printf("\n");
        }
    }
}

void PlayerMove(char board[ROW][COL], int row, int col)  //玩家下棋函数的定义
{
    int x = 0;
    int y = 0;
    printf("玩家走:\n");
    while (1)
    {
        printf("请选择你要走的坐标:");
        scanf("%d %d", &x, &y);
        if (x >= 1 && x <= row && y >= 1 && y <= col)
        {
            if (board[x - 1][y - 1] == ' ')
            {
                board[x - 1][y - 1] = '*';
                break;
            }
            else
            {
                printf("该位置已被占用\n");
            }
        }
        else
        {
            printf("输入错误的坐标\n");
        }
    }

}

void ComputerMove(char board[ROW][COL], int row, int col)  //电脑下棋函数的定义
{
    printf("电脑走:\n");
    while (1)
    {
        int x = rand() % row;
        int y = rand() % col;
        if (board[x][y] == ' ')
        {
            board[x][y] = '#';
            break;
        }
    }
}

int IsFull(char board[ROW][COL], int row, int col)  //判断棋盘状态函数的定义
{
    int i = 0;
    int j = 0;
    for (i = 0;i < row;i++)
    {
        for (j = 0;j < col;j++)
        {
            if (board[i][j] == ' ')
                return 0;
        }
    }
    return 1;
}

char IsWin(char board[ROW][COL], int row, int col)  //判断胜负函数的定义
{
    int x = 0;
    int y = 0;

    //三行
    for (x = 0;x < row;x++)
    {
        if (board[x][0] == board[x][1] && board[x][1] == board[x][2] && board[x][1] != ' ')
        {
            return board[x][1];
        }
    }

    //三列
    for (y = 0;y < col;y++)
    {
        if (board[0][y] == board[1][y] && board[1][y] == board[2][y] && board[1][y] != ' ')
        {
            return board[1][y];
        }
    }

    //右->左对角线
    if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[1][1] != ' ')
    {
        return board[1][1];
    }

    //左->右对角线
    if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[1][1] != ' ')
    {
        return board[1][1];
    }

    //判断棋盘是否满了
    //如果满了返回1,没满则返回0
    int ret = IsFull(board, ROW, COL);
    if (ret == 1)
    {
        return 'Q';
    }
    else
    {
        return 'C';
    }
}
2.扫雷游戏
  • test.c — 与游戏相关的逻辑测试

  • game.c — 与游戏相关的函数实现

  • game.h — 与游戏相关的函数的声明

//test.c
#define _CRT_SECURE_NO_WARNINGS 1

#include "game.h"

void menu()
{
    printf("******************************\n");
    printf("**********1.play**************\n");
    printf("**********0.exit**************\n");
    printf("******************************\n");
}

void game()
{
    char mine[ROWS][COLS] = { 0 };  //存放部署好的雷的信息
    char show[ROWS][COLS] = { 0 };  //存放排查出的雷的信息

    InitBoard(mine, ROWS, COLS, '0');  //雷盘初始化
    DisplayBoard(mine, ROW, COL);      //打印雷盘

    InitBoard(show, ROWS, COLS, '*');
    DisplayBoard(show, ROW, COL);

    Set_Mine(mine, ROW, COL);          //部署雷
    DisplayBoard(mine, ROW, COL);

    Find_Mine(mine, show, ROW, COL);   //排查雷
}

int main()
{
    int input = 0;
    srand((unsigned int)time(NULL));
    do
    {
        menu(); //游戏菜单
        printf("请选择:");
        scanf("%d", &input);
        switch (input)
        {
        case 1:
            game(); //扫雷游戏
            break;
        case 0:
            printf("退出游戏\n");
            break;
        default:
            printf("输入错误,重新输入!\n");
        }
    } while (input);
}
//game.c
#define _CRT_SECURE_NO_WARNINGS 1

#include "game.h"

void InitBoard(char board[ROW][COL], int rows, int cols, char set)
{
    int i = 0;
    int j = 0;

    for (i = 0;i < rows;i++)
    {
        for (j = 0;j < cols;j++)
        {
            board[i][j] = set;
        }
    }
}

void DisplayBoard(char board[ROW][COL], int row, int col)
{
    int x = 0;
    int y = 0;
    printf("------ 扫雷游戏 -------\n");
    for (x = 0;x <= row;x++)
    {
        printf("%d ", x);
    }
    printf("\n");
    for (x = 1;x <= row;x++)
    {
        printf("%d ", x);
        for (y = 1;y <= col;y++)
        {
            printf("%c ",board[x][y]);
        }
        printf("\n");
    }
    printf("------ 扫雷游戏 -------\n");
}
static int get_mine_count(char mine[ROW][COL], int x, int y)
{
    return mine[x-1][y] +
        mine[x-1][y-1] +
        mine[x][y-1] +
        mine[x+1][y-1] +
        mine[x+1][y] +
        mine[x+1][y+1] +
        mine[x][y+1] +
        mine[x-1][y-1] - 8 * '0';
}

void Set_Mine(char mine[ROWS][COLS], int row, int col)
{
    int count = 10;

    while (count)
    {
        int x = rand() % row + 1;
        int y = rand() % col + 1;
        if (mine[x][y] == '0')
        {
            mine[x][y] = '1';
            count--;
        }
    }
}

void Find_Mine(char mine[ROW][COL], char show[ROW][COL], int row, int col)
{
    int x = 0;
    int y = 0;
    int win = 0;
    while (win < row * col - Count)
    {
        printf("请输入选择的坐标:");
        scanf("%d %d", &x, &y);
        if (x >= 1 && x <= row && y >= 1 && y <= col)  //判断坐标的合理性
        {
            if (mine[x][y] == '1')
            {
                printf("失败");
                break;
            }
            else
            {
                int count = get_mine_count(mine, x, y);  //不是雷的前提下,计算x,y坐标中周围有多少个雷
                show[x][y] = count + '0';
                DisplayBoard(show, ROW, COL);            //打印排查出的信息
                win++;
            }
        }
        else
        {
            printf("错误,重新输入\n");
        }
    }
    if (win = row * col - Count)
    {
        printf("恭喜获胜\n");
    }
} 
//game.h
#pragma once

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define ROW 9
#define COL 9

#define ROWS ROW+2
#define COLS COL+2

#define Count 10

//初始化函数声明
void InitBoard(char board[ROW][COL], int rows, int cols, char set);

//打印函数声明
void DisplayBoard(char board[ROW][COL], int row, int col);

//部署函数声明
void Set_Mine(char mine[ROWS][COLS], int row, int col);

//排查函数声明
void Find_Mine(char mine[ROW][COL], char show[ROW][COL], int row, int col);
3.杨辉三角

在屏幕上打印杨辉三角。

1

1 1

1 2 1

1 3 3 1

#include <stdio.h>

int main()
{
    int arr[10][10] = { 0 };
    int i = 0;
    int j = 0;
    for (i = 0; i < 10; i++)
    {
        for (j = 0; j <= i; j++)
        {
            if (j == 0)
            {
                arr[i][j] = 1;
            }
            if (i == j)
            {
                arr[i][j] = 1;
            }
            if (i >= 2 && j >= 1)
            {
                arr[i][j] = arr[i - 1][j - 1] + arr[i - 1][j];
            }
        }
    }
    for (i = 0; i < 10; i++)
    {
        for (j = 0; j <= i; j++)
        {
            printf("%d ", arr[i][j]);
        }
        printf("\n");
    }
    return 0;
}
4.通讯录
  • test.c — 与通讯录相关的逻辑测试

  • game.c — 与通讯录相关的函数实现

  • game.h — 与通讯录相关的函数的声明

test.c:

#define _CRT_SECURE_NO_WARNINGS 1

//通讯录
//1.能存放1000个人的信息
//每个人信息:姓名+年龄+性别+电话+地址
//2.增加人的信息
//3.删除指定的人的信息
//4.修改指定的人的信息
//5.查找指定的人的信息
//6.排序通讯录的信息

#include "Contact.h"

enum Option
{
    EXIT,
    ADD,
    DEL,
    MODIFY,
    SEARCH,
    SORT,
    PRINT
};

void menu()
{
    printf("********************************************************\n");
    printf("********** 1.add        2.del   ****************\n");
    printf("********** 3.modify        4.search****************\n");
    printf("********** 5.sort        6.print ****************\n");
    printf("********** 0.exit               *************\n");
    printf("********************************************************\n");
}

int main()
{
    int input = 0;
    Contact con;
    Init_Contact(&con);
    do
    {

        menu();
        printf("请选择:>");
        scanf("%d", &input);
        switch (input)
        {
        case ADD:
            Add_Contact(&con);
            break;
        case DEL:
            Del_Contact(&con);
            break;
        case MODIFY:
            Modify_Contact(&con);
            break;
        case SEARCH:
            Search_Contact(&con);
            break;
        case SORT:
            break;
        case PRINT:
            Print_Contact(&con);
            break;
        case EXIT:
            return;
            break;
        default:
            printf("选择错误,请重新选择:>");
            break;
        }
    } while (input);

}

Contact.c:

#include "Contact.h"

static int Find_ByName(Contact* c,char name[])
{
    int i = 0;
    for (i = 0; i < c->sz; i++)
    {
        if (strcmp(c->data[i].name ,name) == 0)
        {
            return i;
        }
    }
    return -1;
}

void Init_Contact(Contact* c)
{
    c->sz = 0;
    memset(c->data, 0, sizeof(c->data));
}

void Add_Contact(Contact* c)
{
    if (c->sz == 1000)
    {
            printf("通讯录已满,无法增加\n");
            return;
    }

    printf("请输入名字:>");
    scanf("%s",c->data[c->sz].name );
    printf("请输如年龄:>");
    scanf("%d", &(c->data[c->sz].age));
    printf("请输入性别:>");
    scanf("%s", c->data[c->sz].sex);
    printf("请输入电话:>");
    scanf("%s", c->data[c->sz].tele);
    printf("请输入地址:>");
    scanf("%s", c->data[c->sz].addr);

    c->sz++;
    printf("增加成功\n");
}

void Print_Contact(const Contact* c)
{
    int i = 0;
    printf("%-10s\t%-5s\t%-8s\t%-15s\t%-20s\n", "姓名","年龄","性别","电话","地址");
    for (i = 0; i < c->sz; i++)
    {
        printf("%-10s\t%-5d\t%-8s\t%-15s\t%-20s\n", 
            c->data[i].name,
            c->data[i].age, 
            c->data[i].sex, 
            c->data[i].tele, 
            c->data[i].addr);
    }
}

void Del_Contact(Contact* c)
{
    char name[MAX_NAME] = { 0 };
    if (c->sz == 0)
    {
        printf("通讯录为空,无法删除\n");
    }
    printf("请输入你要删除的信息:>");
    scanf("%s", name);
    int pos = Find_ByName(c, name);
    if (pos == -1)
    {
        printf("你要删除的信息不存在\n");
    }

    for (int i = 0; i < c->sz - 1; i++)
    {
        c->data[i] = c->data[i + 1];
    }

    c->sz--;
    printf("删除成功\n");
}

void Modify_Contact(Contact* c)
{
    char name[MAX_NAME] = { 0 };
    printf("请输入你要修改的信息:>");
    scanf("%s", name);
    int pos = Find_ByName(c, name);
    if (pos == -1)
    {
        printf("你要修改的信息不存在\n");
    }
    else
    {
        printf("请输入名字:>");
        scanf("%s", c->data[pos].name);
        printf("请输如年龄:>");
        scanf("%d", &(c->data[pos].age));
        printf("请输入性别:>");
        scanf("%s", c->data[pos].sex);
        printf("请输入电话:>");
        scanf("%s", c->data[pos].tele);
        printf("请输入地址:>");
        scanf("%s", c->data[pos].addr);
        printf("修改成功\n");
    }

}

void Search_Contact(Contact* c)
{
    char name[MAX_NAME] = { 0 };
    printf("请输入你要查找的信息:>");
    scanf("%s", name);
    int pos = Find_ByName(c, name);
    if (pos == -1)
    {
        printf("你要查找的信息不存在\n");
    }
    else
    {
        printf("%-10s\t%-5s\t%-8s\t%-15s\t%-20s\n", "姓名", "年龄", "性别", "电话", "地址");

        printf("%-10s\t%-5d\t%-8s\t%-15s\t%-20s\n",
            c->data[pos].name,
            c->data[pos].age,
            c->data[pos].sex,
            c->data[pos].tele,
            c->data[pos].addr);
    }
}

Contact.h:

#define _CRT_SECURE_NO_WARNINGS 1

#include <stdio.h>

#define MAX_NAME 20
#define MAX_SEX 8
#define MAX_TELE 15
#define MAX_ADDR 30
#define MAX 1000

typedef struct PeoInfo
{
    char name[MAX_NAME];
    int age;
    char sex[MAX_SEX];
    char tele[MAX_TELE];
    char addr[MAX_ADDR];
}PeoInfo;

typedef struct Contact
{
    PeoInfo data[MAX];
    int sz;
}Contact;

void Init_Contact(Contact* c);
void Add_Contact(Contact* c);
void Print_Contact(const Contact* c);
void Del_Contact(Contact* c);
void Modify_Contact(Contact* c);
void Search_Contact(Contact* c);

5.杨氏矩阵

题目内容:有一个数字矩阵,矩阵的每行从左到右是递增的,矩阵从上到下是递增的,请编写程序在这样的矩阵中查找某个数字是否存在。

要求:时间复杂度小于o(N);

#include <stdio.h>

int find_num(int arr[][3], int row, int col, int key)
{
    int i = 0;
    int j = col-1;
    while (i<row && j>=0)
    {
        if (arr[i][j] > key)
        {
            j--;
        }
        else if (arr[i][j] < key)
        {
            i++;
        }
        else
        {
            return 1;
        }
    }
    return 0;
}

int main()
{
    int arr[3][3] = { 1,2,3,4,5,6,7,8,9 };
    int k = 12;
    int ret = find_num(arr,3,3,k);
    if (ret == 1)
    {
        printf("找到了\n");
    }
    else
    {
        printf("没找到\n");
    }
    return 0;
}

悦读

道可道,非常道;名可名,非常名。 无名,天地之始,有名,万物之母。 故常无欲,以观其妙,常有欲,以观其徼。 此两者,同出而异名,同谓之玄,玄之又玄,众妙之门。

;