C语言经典例题-4

打印 上一主题 下一主题

主题 513|帖子 513|积分 1539

1.五子棋

   test.c - 测试游戏的逻辑
  game.c - 与游戏相关函数实现
  game.h - 与游戏相关函数的声明,符号声明,头文件的包含。
  1. //test.c
  2. #define _CRT_SECURE_NO_WARNINGS 1
  3. #include "game.h"
  4. void menu()  //打印菜单函数
  5. {
  6.     printf("********************************\n");
  7.     printf("*********   1.play    **********\n");
  8.     printf("*********   0.exit    **********\n");
  9.     printf("********************************\n");
  10. }
  11. void game()
  12. {
  13.     char board[ROW][COL];
  14.     char ret = 0;
  15.     srand((unsigned int)time(NULL));
  16.     InitBoard(board, ROW, COL);  //棋盘的初始化
  17.     DisplayBoard(board, ROW, COL); //打印棋盘
  18.     while (1)
  19.     {
  20.         PlayerMove(board, ROW, COL);  //玩家下棋
  21.         DisplayBoard(board, ROW, COL); //显示当前棋盘状态
  22.         ret = IsWin(board, ROW, COL);  //判断胜负
  23.         if (ret != 'C') //如果是* # Q ,那么游戏就结束了。
  24.         {
  25.             break;
  26.         }
  27.         ComputerMove(board, ROW, COL);  //电脑下棋
  28.         DisplayBoard(board, ROW, COL);   
  29.         ret = IsWin(board, ROW, COL);
  30.         if (ret != 'C')
  31.         {
  32.             break;
  33.         }
  34.     }
  35.     if (ret == '*')
  36.     {
  37.         printf("玩家获胜\n");
  38.     }
  39.     else if (ret == '#')
  40.     {
  41.         printf("电脑获胜\n");
  42.     }
  43.     else
  44.     {
  45.         printf("平局\n");
  46.     }
  47. }
  48. int main()
  49. {
  50.     int input = 0;
  51.     do
  52.     {
  53.         menu();
  54.         printf("请选择:");
  55.         scanf("%d", &input);
  56.         switch (input)
  57.         {
  58.         case 1:
  59.             game();
  60.             break;
  61.         case 0 :
  62.             printf("退出游戏\n");
  63.             break;
  64.         default:
  65.             printf("输入错误\n");
  66.             break;
  67.         }
  68.     } while (input);
  69.     return 0;
  70. }
复制代码
  1. //game.h
  2. #pragma once
  3. #define ROW 3
  4. #define COL 3
  5. #include <stdio.h>
  6. #include <stdlib.h>  //使用rand时需要调用
  7. #include <time.h>     //使用time时需要调用
  8. void InitBoard(char board[ROW][COL], int row, int col);  //初始化函数的声明
  9. void DisplayBoard(char board[ROW][COL], int row, int col);  //显示棋盘状态函数的声明
  10. void PlayerMove(char board[ROW][COL], int row, int col);  //玩家下棋函数的声明
  11. void ComputerMove(char board[ROW][COL], int row, int col);  //电脑下棋函数的声明
  12. char IsWin(char board[ROW][COL], int row, int col);  //判断胜负函数的声明
  13. //  *  -   玩家获胜
  14. //  #  -   电脑获胜
  15. //  Q  -   平局
  16. //  C  -   继续
复制代码
  1. //game.c
  2. #define _CRT_SECURE_NO_WARNINGS 1
  3. #include "game.h"
  4. void InitBoard(char board[ROW][COL], int row, int col)  //初始化函数的定义
  5. {
  6.     int i = 0;
  7.     int j = 0;
  8.     for (i = 0;i < row;i++)
  9.     {
  10.         for (j = 0;j < col;j++)
  11.         {
  12.             board[i][j] = ' ';
  13.         }
  14.     }
  15. }
  16. void DisplayBoard(char board[ROW][COL], int row, int col)  //显示棋盘状态函数的定义
  17. {
  18.     int i = 0;
  19.     int j = 0;
  20.     for (i = 0;i < row;i++)
  21.     {
  22.         for (j = 0;j < col;j++)
  23.         {
  24.             printf(" %c ", board[i][j]);
  25.             if (j < col - 1)
  26.             {
  27.                 printf("|");
  28.             }
  29.         }
  30.         printf("\n");
  31.         if (i < row - 1)
  32.         {
  33.             for (j = 0;j < col;j++)
  34.             {
  35.                 printf("---");
  36.                 if (j < col - 1)
  37.                 {
  38.                     printf("|");
  39.                 }
  40.             }
  41.             printf("\n");
  42.         }
  43.     }
  44. }
  45. void PlayerMove(char board[ROW][COL], int row, int col)  //玩家下棋函数的定义
  46. {
  47.     int x = 0;
  48.     int y = 0;
  49.     printf("玩家走:\n");
  50.     while (1)
  51.     {
  52.         printf("请选择你要走的坐标:");
  53.         scanf("%d %d", &x, &y);
  54.         if (x >= 1 && x <= row && y >= 1 && y <= col)
  55.         {
  56.             if (board[x - 1][y - 1] == ' ')
  57.             {
  58.                 board[x - 1][y - 1] = '*';
  59.                 break;
  60.             }
  61.             else
  62.             {
  63.                 printf("该位置已被占用\n");
  64.             }
  65.         }
  66.         else
  67.         {
  68.             printf("输入错误的坐标\n");
  69.         }
  70.     }
  71. }
  72. void ComputerMove(char board[ROW][COL], int row, int col)  //电脑下棋函数的定义
  73. {
  74.     printf("电脑走:\n");
  75.     while (1)
  76.     {
  77.         int x = rand() % row;
  78.         int y = rand() % col;
  79.         if (board[x][y] == ' ')
  80.         {
  81.             board[x][y] = '#';
  82.             break;
  83.         }
  84.     }
  85. }
  86. int IsFull(char board[ROW][COL], int row, int col)  //判断棋盘状态函数的定义
  87. {
  88.     int i = 0;
  89.     int j = 0;
  90.     for (i = 0;i < row;i++)
  91.     {
  92.         for (j = 0;j < col;j++)
  93.         {
  94.             if (board[i][j] == ' ')
  95.                 return 0;
  96.         }
  97.     }
  98.     return 1;
  99. }
  100. char IsWin(char board[ROW][COL], int row, int col)  //判断胜负函数的定义
  101. {
  102.     int x = 0;
  103.     int y = 0;
  104.     //三行
  105.     for (x = 0;x < row;x++)
  106.     {
  107.         if (board[x][0] == board[x][1] && board[x][1] == board[x][2] && board[x][1] != ' ')
  108.         {
  109.             return board[x][1];
  110.         }
  111.     }
  112.     //三列
  113.     for (y = 0;y < col;y++)
  114.     {
  115.         if (board[0][y] == board[1][y] && board[1][y] == board[2][y] && board[1][y] != ' ')
  116.         {
  117.             return board[1][y];
  118.         }
  119.     }
  120.     //右->左对角线
  121.     if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[1][1] != ' ')
  122.     {
  123.         return board[1][1];
  124.     }
  125.     //左->右对角线
  126.     if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[1][1] != ' ')
  127.     {
  128.         return board[1][1];
  129.     }
  130.     //判断棋盘是否满了
  131.     //如果满了返回1,没满则返回0
  132.     int ret = IsFull(board, ROW, COL);
  133.     if (ret == 1)
  134.     {
  135.         return 'Q';
  136.     }
  137.     else
  138.     {
  139.         return 'C';
  140.     }
  141. }
复制代码
2.扫雷游戏



  • test.c — 与游戏相关的逻辑测试
  • game.c — 与游戏相关的函数实现
  • game.h — 与游戏相关的函数的声明
  1. //test.c
  2. #define _CRT_SECURE_NO_WARNINGS 1
  3. #include "game.h"
  4. void menu()
  5. {
  6.     printf("******************************\n");
  7.     printf("**********1.play**************\n");
  8.     printf("**********0.exit**************\n");
  9.     printf("******************************\n");
  10. }
  11. void game()
  12. {
  13.     char mine[ROWS][COLS] = { 0 };  //存放部署好的雷的信息
  14.     char show[ROWS][COLS] = { 0 };  //存放排查出的雷的信息
  15.     InitBoard(mine, ROWS, COLS, '0');  //雷盘初始化
  16.     DisplayBoard(mine, ROW, COL);      //打印雷盘
  17.     InitBoard(show, ROWS, COLS, '*');
  18.     DisplayBoard(show, ROW, COL);
  19.     Set_Mine(mine, ROW, COL);          //部署雷
  20.     DisplayBoard(mine, ROW, COL);
  21.     Find_Mine(mine, show, ROW, COL);   //排查雷
  22. }
  23. int main()
  24. {
  25.     int input = 0;
  26.     srand((unsigned int)time(NULL));
  27.     do
  28.     {
  29.         menu(); //游戏菜单
  30.         printf("请选择:");
  31.         scanf("%d", &input);
  32.         switch (input)
  33.         {
  34.         case 1:
  35.             game(); //扫雷游戏
  36.             break;
  37.         case 0:
  38.             printf("退出游戏\n");
  39.             break;
  40.         default:
  41.             printf("输入错误,重新输入!\n");
  42.         }
  43.     } while (input);
  44. }
复制代码
  1. //game.c
  2. #define _CRT_SECURE_NO_WARNINGS 1
  3. #include "game.h"
  4. void InitBoard(char board[ROW][COL], int rows, int cols, char set)
  5. {
  6.     int i = 0;
  7.     int j = 0;
  8.     for (i = 0;i < rows;i++)
  9.     {
  10.         for (j = 0;j < cols;j++)
  11.         {
  12.             board[i][j] = set;
  13.         }
  14.     }
  15. }
  16. void DisplayBoard(char board[ROW][COL], int row, int col)
  17. {
  18.     int x = 0;
  19.     int y = 0;
  20.     printf("------ 扫雷游戏 -------\n");
  21.     for (x = 0;x <= row;x++)
  22.     {
  23.         printf("%d ", x);
  24.     }
  25.     printf("\n");
  26.     for (x = 1;x <= row;x++)
  27.     {
  28.         printf("%d ", x);
  29.         for (y = 1;y <= col;y++)
  30.         {
  31.             printf("%c ",board[x][y]);
  32.         }
  33.         printf("\n");
  34.     }
  35.     printf("------ 扫雷游戏 -------\n");
  36. }
  37. static int get_mine_count(char mine[ROW][COL], int x, int y)
  38. {
  39.     return mine[x-1][y] +
  40.         mine[x-1][y-1] +
  41.         mine[x][y-1] +
  42.         mine[x+1][y-1] +
  43.         mine[x+1][y] +
  44.         mine[x+1][y+1] +
  45.         mine[x][y+1] +
  46.         mine[x-1][y-1] - 8 * '0';
  47. }
  48. void Set_Mine(char mine[ROWS][COLS], int row, int col)
  49. {
  50.     int count = 10;
  51.     while (count)
  52.     {
  53.         int x = rand() % row + 1;
  54.         int y = rand() % col + 1;
  55.         if (mine[x][y] == '0')
  56.         {
  57.             mine[x][y] = '1';
  58.             count--;
  59.         }
  60.     }
  61. }
  62. void Find_Mine(char mine[ROW][COL], char show[ROW][COL], int row, int col)
  63. {
  64.     int x = 0;
  65.     int y = 0;
  66.     int win = 0;
  67.     while (win < row * col - Count)
  68.     {
  69.         printf("请输入选择的坐标:");
  70.         scanf("%d %d", &x, &y);
  71.         if (x >= 1 && x <= row && y >= 1 && y <= col)  //判断坐标的合理性
  72.         {
  73.             if (mine[x][y] == '1')
  74.             {
  75.                 printf("失败");
  76.                 break;
  77.             }
  78.             else
  79.             {
  80.                 int count = get_mine_count(mine, x, y);  //不是雷的前提下,计算x,y坐标中周围有多少个雷
  81.                 show[x][y] = count + '0';
  82.                 DisplayBoard(show, ROW, COL);            //打印排查出的信息
  83.                 win++;
  84.             }
  85.         }
  86.         else
  87.         {
  88.             printf("错误,重新输入\n");
  89.         }
  90.     }
  91.     if (win = row * col - Count)
  92.     {
  93.         printf("恭喜获胜\n");
  94.     }
  95. }
复制代码
  1. //game.h
  2. #pragma once
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <time.h>
  6. #define ROW 9
  7. #define COL 9
  8. #define ROWS ROW+2
  9. #define COLS COL+2
  10. #define Count 10
  11. //初始化函数声明
  12. void InitBoard(char board[ROW][COL], int rows, int cols, char set);
  13. //打印函数声明
  14. void DisplayBoard(char board[ROW][COL], int row, int col);
  15. //部署函数声明
  16. void Set_Mine(char mine[ROWS][COLS], int row, int col);
  17. //排查函数声明
  18. 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

  1. #include <stdio.h>
  2. int main()
  3. {
  4.     int arr[10][10] = { 0 };
  5.     int i = 0;
  6.     int j = 0;
  7.     for (i = 0; i < 10; i++)
  8.     {
  9.         for (j = 0; j <= i; j++)
  10.         {
  11.             if (j == 0)
  12.             {
  13.                 arr[i][j] = 1;
  14.             }
  15.             if (i == j)
  16.             {
  17.                 arr[i][j] = 1;
  18.             }
  19.             if (i >= 2 && j >= 1)
  20.             {
  21.                 arr[i][j] = arr[i - 1][j - 1] + arr[i - 1][j];
  22.             }
  23.         }
  24.     }
  25.     for (i = 0; i < 10; i++)
  26.     {
  27.         for (j = 0; j <= i; j++)
  28.         {
  29.             printf("%d ", arr[i][j]);
  30.         }
  31.         printf("\n");
  32.     }
  33.     return 0;
  34. }
复制代码
4.通讯录



  • test.c — 与通讯录相关的逻辑测试
  • game.c — 与通讯录相关的函数实现
  • game.h — 与通讯录相关的函数的声明
test.c:
  1. #define _CRT_SECURE_NO_WARNINGS 1
  2. //通讯录
  3. //1.能存放1000个人的信息
  4. //每个人信息:姓名+年龄+性别+电话+地址
  5. //2.增加人的信息
  6. //3.删除指定的人的信息
  7. //4.修改指定的人的信息
  8. //5.查找指定的人的信息
  9. //6.排序通讯录的信息
  10. #include "Contact.h"
  11. enum Option
  12. {
  13.     EXIT,
  14.     ADD,
  15.     DEL,
  16.     MODIFY,
  17.     SEARCH,
  18.     SORT,
  19.     PRINT
  20. };
  21. void menu()
  22. {
  23.     printf("********************************************************\n");
  24.     printf("********** 1.add        2.del   ****************\n");
  25.     printf("********** 3.modify        4.search****************\n");
  26.     printf("********** 5.sort        6.print ****************\n");
  27.     printf("********** 0.exit               *************\n");
  28.     printf("********************************************************\n");
  29. }
  30. int main()
  31. {
  32.     int input = 0;
  33.     Contact con;
  34.     Init_Contact(&con);
  35.     do
  36.     {
  37.         menu();
  38.         printf("请选择:>");
  39.         scanf("%d", &input);
  40.         switch (input)
  41.         {
  42.         case ADD:
  43.             Add_Contact(&con);
  44.             break;
  45.         case DEL:
  46.             Del_Contact(&con);
  47.             break;
  48.         case MODIFY:
  49.             Modify_Contact(&con);
  50.             break;
  51.         case SEARCH:
  52.             Search_Contact(&con);
  53.             break;
  54.         case SORT:
  55.             break;
  56.         case PRINT:
  57.             Print_Contact(&con);
  58.             break;
  59.         case EXIT:
  60.             return;
  61.             break;
  62.         default:
  63.             printf("选择错误,请重新选择:>");
  64.             break;
  65.         }
  66.     } while (input);
  67. }
复制代码
Contact.c:
  1. #include "Contact.h"
  2. static int Find_ByName(Contact* c,char name[])
  3. {
  4.     int i = 0;
  5.     for (i = 0; i < c->sz; i++)
  6.     {
  7.         if (strcmp(c->data[i].name ,name) == 0)
  8.         {
  9.             return i;
  10.         }
  11.     }
  12.     return -1;
  13. }
  14. void Init_Contact(Contact* c)
  15. {
  16.     c->sz = 0;
  17.     memset(c->data, 0, sizeof(c->data));
  18. }
  19. void Add_Contact(Contact* c)
  20. {
  21.     if (c->sz == 1000)
  22.     {
  23.             printf("通讯录已满,无法增加\n");
  24.             return;
  25.     }
  26.     printf("请输入名字:>");
  27.     scanf("%s",c->data[c->sz].name );
  28.     printf("请输如年龄:>");
  29.     scanf("%d", &(c->data[c->sz].age));
  30.     printf("请输入性别:>");
  31.     scanf("%s", c->data[c->sz].sex);
  32.     printf("请输入电话:>");
  33.     scanf("%s", c->data[c->sz].tele);
  34.     printf("请输入地址:>");
  35.     scanf("%s", c->data[c->sz].addr);
  36.     c->sz++;
  37.     printf("增加成功\n");
  38. }
  39. void Print_Contact(const Contact* c)
  40. {
  41.     int i = 0;
  42.     printf("%-10s\t%-5s\t%-8s\t%-15s\t%-20s\n", "姓名","年龄","性别","电话","地址");
  43.     for (i = 0; i < c->sz; i++)
  44.     {
  45.         printf("%-10s\t%-5d\t%-8s\t%-15s\t%-20s\n",
  46.             c->data[i].name,
  47.             c->data[i].age,
  48.             c->data[i].sex,
  49.             c->data[i].tele,
  50.             c->data[i].addr);
  51.     }
  52. }
  53. void Del_Contact(Contact* c)
  54. {
  55.     char name[MAX_NAME] = { 0 };
  56.     if (c->sz == 0)
  57.     {
  58.         printf("通讯录为空,无法删除\n");
  59.     }
  60.     printf("请输入你要删除的信息:>");
  61.     scanf("%s", name);
  62.     int pos = Find_ByName(c, name);
  63.     if (pos == -1)
  64.     {
  65.         printf("你要删除的信息不存在\n");
  66.     }
  67.     for (int i = 0; i < c->sz - 1; i++)
  68.     {
  69.         c->data[i] = c->data[i + 1];
  70.     }
  71.     c->sz--;
  72.     printf("删除成功\n");
  73. }
  74. void Modify_Contact(Contact* c)
  75. {
  76.     char name[MAX_NAME] = { 0 };
  77.     printf("请输入你要修改的信息:>");
  78.     scanf("%s", name);
  79.     int pos = Find_ByName(c, name);
  80.     if (pos == -1)
  81.     {
  82.         printf("你要修改的信息不存在\n");
  83.     }
  84.     else
  85.     {
  86.         printf("请输入名字:>");
  87.         scanf("%s", c->data[pos].name);
  88.         printf("请输如年龄:>");
  89.         scanf("%d", &(c->data[pos].age));
  90.         printf("请输入性别:>");
  91.         scanf("%s", c->data[pos].sex);
  92.         printf("请输入电话:>");
  93.         scanf("%s", c->data[pos].tele);
  94.         printf("请输入地址:>");
  95.         scanf("%s", c->data[pos].addr);
  96.         printf("修改成功\n");
  97.     }
  98. }
  99. void Search_Contact(Contact* c)
  100. {
  101.     char name[MAX_NAME] = { 0 };
  102.     printf("请输入你要查找的信息:>");
  103.     scanf("%s", name);
  104.     int pos = Find_ByName(c, name);
  105.     if (pos == -1)
  106.     {
  107.         printf("你要查找的信息不存在\n");
  108.     }
  109.     else
  110.     {
  111.         printf("%-10s\t%-5s\t%-8s\t%-15s\t%-20s\n", "姓名", "年龄", "性别", "电话", "地址");
  112.         printf("%-10s\t%-5d\t%-8s\t%-15s\t%-20s\n",
  113.             c->data[pos].name,
  114.             c->data[pos].age,
  115.             c->data[pos].sex,
  116.             c->data[pos].tele,
  117.             c->data[pos].addr);
  118.     }
  119. }
复制代码
Contact.h:
  1. #define _CRT_SECURE_NO_WARNINGS 1
  2. #include <stdio.h>
  3. #define MAX_NAME 20
  4. #define MAX_SEX 8
  5. #define MAX_TELE 15
  6. #define MAX_ADDR 30
  7. #define MAX 1000
  8. typedef struct PeoInfo
  9. {
  10.     char name[MAX_NAME];
  11.     int age;
  12.     char sex[MAX_SEX];
  13.     char tele[MAX_TELE];
  14.     char addr[MAX_ADDR];
  15. }PeoInfo;
  16. typedef struct Contact
  17. {
  18.     PeoInfo data[MAX];
  19.     int sz;
  20. }Contact;
  21. void Init_Contact(Contact* c);
  22. void Add_Contact(Contact* c);
  23. void Print_Contact(const Contact* c);
  24. void Del_Contact(Contact* c);
  25. void Modify_Contact(Contact* c);
  26. void Search_Contact(Contact* c);
复制代码
5.杨氏矩阵
题目内容:有一个数字矩阵,矩阵的每行从左到右是递增的,矩阵从上到下是递增的,请编写步伐在这样的矩阵中查找某个数字是否存在。
要求:时间复杂度小于o(N);
  1. #include <stdio.h>
  2. int find_num(int arr[][3], int row, int col, int key)
  3. {
  4.     int i = 0;
  5.     int j = col-1;
  6.     while (i<row && j>=0)
  7.     {
  8.         if (arr[i][j] > key)
  9.         {
  10.             j--;
  11.         }
  12.         else if (arr[i][j] < key)
  13.         {
  14.             i++;
  15.         }
  16.         else
  17.         {
  18.             return 1;
  19.         }
  20.     }
  21.     return 0;
  22. }
  23. int main()
  24. {
  25.     int arr[3][3] = { 1,2,3,4,5,6,7,8,9 };
  26.     int k = 12;
  27.     int ret = find_num(arr,3,3,k);
  28.     if (ret == 1)
  29.     {
  30.         printf("找到了\n");
  31.     }
  32.     else
  33.     {
  34.         printf("没找到\n");
  35.     }
  36.     return 0;
  37. }
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

何小豆儿在此

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表