当前位置:网站首页>C语言项目实战:24点游戏计算器(基于结构体、指针、函数、数组、循环等知识点)
C语言项目实战:24点游戏计算器(基于结构体、指针、函数、数组、循环等知识点)
2022-07-23 09:00:00 【编程爱好者-阿新】
文章目录
C语言项目实战:24点游戏计算器(基于结构体、指针、函数、数组、循环等知识点)
前言:24点游戏计算器的规则如下
24点是一种益智游戏,24点是把4个整数(一般是正整数)通过加减乘除以及括号运算,使最后的计算结果是24的一个数学游戏,24点可以考验人的智力和数学敏感性,它能在游戏中提高人们的心算能力。
24点通常是使用扑克牌来进行游戏的,一副牌中抽去大小王后还剩下52张(如果初练也可只用1~10这40张牌),任意抽取4张牌(称为牌组),用加、减、乘、除(可加括号)把牌面上的数算成24。每张牌必须只能用一次,如抽出的牌是3、8、8、9,那么算式为(9-8)×8×3或3×8÷(9-8)或(9-8÷8)×3等。
一、项目的创建标
1、选择项目
打开VS2019,点击创建新项目-》然后选择如下如图所示的选项创建C语言项目

2、输入项目名称和路径

3、创建头文件Main.h、PointGame.h和源文件Main.c、PointGame.c如下所示

二、项目的编写
1、Main.h头文件的编写
#pragma once
#include <stdio.h>
#include <stdlib.h>
2、PointGame.h头文件的编写
#pragma once
#include <stdbool.h>
#include <math.h>
#define PRECISION 1E-6
#define TARGET 24
#define SIZE 4
/* * 最终表达式由四个数、括号、运算符组成,其大小不会超过 * 5 * (SIZE - 1) + SIZE * 2 + 1 */
typedef struct card
{
double value;
char expression[5 * (SIZE - 1) + SIZE * 2 + 1];
} Card, *pCard;
bool game24(pCard card, int size);
3、PointGame.c源文件的编写
#define _CRT_SECURE_NO_WARNINGS
#include "Main.h"
#include "PointGame.h"
/* * 牌堆中计算24点 * card 牌堆 * size 个数 */
bool game24(pCard card, int size)
{
Card stack[3];
/* * 如果牌堆里只有一张牌,判断是否就是需要的目标 */
if (size == 1)
{
if (fabs(card[0].value - TARGET) < PRECISION)
{
printf("%d = %s\n", TARGET, card[0].expression);
return true;
}
else
{
return false;
}
}
/* *从牌堆中任选两张牌 */
int m, n, i, t;
for (m = 0; m < size - 1; m++)
{
for (n = m + 1; n < size; n++)
{
/* * 剩余的牌放入新牌堆 */
t = 0;
for (i = 0; i < size; i++)
{
if (i != m && i != n)
{
stack[t].value = card[i].value;
sprintf(stack[t].expression, "%s", card[i].expression);
t++;
}
}
/* * 选中两张牌的和放入新牌堆 */
stack[size - 2].value = card[m].value + card[n].value;
sprintf(stack[size - 2].expression, "(%s + %s)", card[m].expression, card[n].expression);
if (game24(stack, size - 1))
{
return true;
}
/* * 选中两张牌的差放入新牌堆 */
if (card[m].value > card[n].value)
{
stack[size - 2].value = card[m].value - card[n].value;
sprintf(stack[size - 2].expression, "(%s - %s)", card[m].expression, card[n].expression);
}
else
{
stack[size - 2].value = card[n].value - card[m].value;
sprintf(stack[size - 2].expression, "(%s - %s)", card[n].expression, card[m].expression);
}
if (game24(stack, size - 1))
{
return true;
}
/* * 选中两张牌的积放入新牌堆 */
stack[size - 2].value = card[m].value * card[n].value;
sprintf(stack[size - 2].expression, "%s * %s", card[m].expression, card[n].expression);
if (game24(stack, size - 1))
{
return true;
}
/* * 选中两张牌的商放入新牌堆 */
if (card[m].value != 0)
{
stack[size - 2].value = card[n].value / card[m].value;
sprintf(stack[size - 2].expression, "%s / %s", card[n].expression, card[m].expression);
if (game24(stack, size - 1))
{
return true;
}
}
if (card[n].value != 0)
{
stack[size - 2].value = card[m].value / card[n].value;
sprintf(stack[size - 2].expression, "%s / %s", card[m].expression, card[n].expression);
if (game24(stack, size - 1))
{
return true;
}
}
}
}
return false;
}
3、Main.c源文件的编写
#define _CRT_SECURE_NO_WARNINGS
#include "Main.h"
#include "PointGame.h"
int main()
{
system("color 3E");
int i, poke;
Card card[SIZE];
printf("请输入4个数字 以判断解法:");
for (i = 0; i < SIZE; i++)
{
scanf("%d", &poke);
card[i].value = (double)poke;
sprintf(card[i].expression, "%d", poke);
}
if (game24(card, SIZE))
{
printf("Done\n");
}
else
{
printf("No Answer\n");
}
system("pause");
return 0;
}
三、项目的调试结果
选择生成----》生成解决方案。
结果如下
按F5调试结果如下所示
边栏推荐
- 工作小记:一次抓包
- Changing the historical length of chart during LabVIEW operation
- Flat style feedback form page
- 【FLink】FLink Hash collision on user-specified ID “opt“. Most likely cause is a non-unique ID
- Experience in developing large crawlers in requests Library
- 【数组&&字符串&&宏练习题】
- Looking for peak [Abstract dichotomy exercise]
- 云呐|公司固定资产如何管理?公司固定资产如何管理比较好?
- Drag and drop----
- Antd form - reset method does not work - Basic accumulation - importance of prop
猜你喜欢

Find the maximum area of the island -- depth first search (staining method)

Aruba学习笔记05-配置架构- WLAN配置架构

右键新建txt,新建文本文件不见了,通过添加注册表就可以解决,找来找去办法解决不了的终极办法

【附下载】值得收藏的几款渗透测试常用的脚本

10 years of software testing engineer experience, very confused

Authing supports Zadig! Unified authentication and rapid docking of cloud native users

【C语言】猜数字小游戏+关机小程序

Towhee 每周模型
![Pychart reads excel file with error: raise xlrderror (file_format_descriptions[file_format]+; not supported)](/img/f0/9491ccc2a86d95bb30066397fb9fb6.png)
Pychart reads excel file with error: raise xlrderror (file_format_descriptions[file_format]+; not supported)

JS texture style pie chart plug-in
随机推荐
CPU,内存,磁盘速度比较
Pychart reads excel file with error: raise xlrderror (file_format_descriptions[file_format]+; not supported)
ValidationError: Invalid options object. Dev Server has been initialized using an options object th
(重链剖分)魔法树
回文相关题目
shell跑的時候需要的需要了解命令
ValidationError: Invalid options object. Dev Server has been initialized using an options object th
C语言实现memcpy、memmove
C language implements StrCmp, strstr, strcat, strcpy
可以进行2D或3D三角剖分的一些库
Dynamic programming -- knapsack problem
【附下载】值得收藏的几款渗透测试常用的脚本
152. 乘积最大子数组
Cool code rain dynamic background registration page
Sword finger offer 46. translate numbers into strings
股票炒股开户风险性大吗,安全吗?
Is it risky and safe to open an account for stock speculation?
First acquaintance and search set
【FLink】FLink Hash collision on user-specified ID “opt“. Most likely cause is a non-unique ID
Towhee weekly model