珠玑妙算-C语言小游戏

 

目录

一、宏常量定义及头文件引用

二、不同数的生成

三、输入校验

四、结果判断

五、结果打印

六、main函数主体


珠玑妙算是更加复杂的猜数游戏,原版是猜四个数字,每个数字均不相同,其实现如下

一、宏常量定义及头文件引用

为了使待猜数字位数可变,可以使用宏常量来定义

#define _CRT_SECURE_NO_WARNINGS //防止scanf报错
#include<time.h>
#include<ctype.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define NUMSIZE 3 //设置猜的数字个数,不超过9

二、不同数的生成

这里要使每个都不同,就需要巧妙的函数设计

//设置定义位数的各不相同的数字
void setnum(int*x)
{
	int i, j, val;
	for (i = 0; i < NUMSIZE; i++)
	{
		do {
			val = rand() % 10;//先生成一个随机数
			for (j = 0; j < i; j++)
				if (val == x[j]) //如果随机值与数组中已有的值存在相同的,则重新取值
					break;
		} while (j < i);
		x[i] = val; //将取得的值赋给数组
	}
}

三、输入校验

对用户的输入进行检验,提示相应的报错信息。

//检查输入是否有效
int check(const char *s)
{
	if (strlen(s) != NUMSIZE) //输入个数不够
		return 1;
	for (int i = 0; i < NUMSIZE; i++)
	{
		if (!isdigit(s[i])) //存在非数字字符
			return 2;
		for (int j = 0; j < i; j++) //存在相同数字
			if (s[i] == s[j])
				return 3;
	}
	return 0;
}

四、结果判断

//结果判断
void judge(const char*s, const int*n, int*x, int*y)
{
	*x = *y = 0; //x,y 分别代表位置及数字均正确的个数,数字正确但位置不正确的数字的个数
	for (int i = 0; i < NUMSIZE; i++)
	{
		for (int j = 0; j < NUMSIZE; j++)
		{
			if (s[i] == '0' + n[j]) //将数字转化为字符数字并比较
				if (i == j) //对位置进行比较
					(*x)++;
				else
					(*y)++;
		}
	}
}

五、结果打印

这里需要讨论的是,一个数,如果值对了,位置没对,则x不变,y自加,如果都对x自加,所以x,y的和代表所含有答案数字的个数。

void print(int num,int pos) //num是上一函数中提到的x,y的和,pos指x
{
	if (pos == NUMSIZE) 
		printf("回答正确!!");
	else if (num == 0) 
		printf("这些数字里没有答案。");
	else
	{
		printf("这些数字里包括%d个答案数字。\n", num);
		if (pos == 0)
			printf("但是数字的位置都不一致。");
		else
			printf("其中有%d个数字的位置是一致的。", pos);
	}
	printf("\n");
}

六、main函数主体

int main()
{
	int time_n = 0; //尝试次数
	int check_r;    //检验返回值
	int x;         //位置及数字均正确的个数
	int y;		//数字正确但位置不正确的数字的个数
	int*n = (int*)malloc(sizeof(int)*NUMSIZE); //存储要猜的数字
	char buff[10]; //存储用户输入的字符
	clock_t begin, end; //设置时间起始,用于计时

	srand((unsigned int)time(NULL)); //时间戳

	printf("# 欢迎游玩珠玑妙算。\n");
	printf("# 请猜%d个数字。\n",NUMSIZE);
	printf("# 其中不包含相同数字。\n");
	printf("# 对于3个数的,请向123这样连续输入数字。\n");
	printf("# 不能输入空格字符。\n");

	setnum(n);

	begin = clock();

	do {
		do {
			printf("请输入: ");
			scanf("%s", buff);

			check_r = check(buff);

			switch (check_r) //根据返回值做相应错误提示
			{
			case 1:printf("\a请确保输入四个数字。\n"); break;
			case 2:printf("\a请不要输入除了数字以外的字符。\n"); break;
			case 3:printf("\a请不要输入相同的数字。\n"); break;
			}
		} while (check_r != 0);
		time_n++;
		judge(buff, n, &x, &y);
		print(x+y, x);
	} while (x < NUMSIZE);

	end = clock();

	printf("用了%d次。\n用时%.lf秒。\n",
		time_n, (double)(end - begin) / CLOCKS_PER_SEC);//算出所用秒数,这是time.h中的宏定义常量,可用来转化为秒数。
	return 0;
}

看到这儿,还不支持一下博主😘

  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
Code from Programming Pearls Column 1: Programs for sorting integers bitsort.c -- Sort with bit vectors. sortints.cpp -- Sort using C++ STL sets. qsortints.c -- Sort with C library qsort. bitsortgen.c -- Generate random integers for sorting. Column 2: Test and time algorithms rotate.c -- Three ways to rotate the elements of a vector. The next two program are used in a pipeline to compute all anagrams in a dictionary sign.c -- Sign each word by its letters in sorted order. squash.c -- Put each anagram class on a single line. Column 5: Scaffolding for testing and timing search functions search.c -- Linear and binary search. Column 7: Tiny experiment on C run times timemod0.c -- Edit main to time one operation. Column 8: Compute the maximum-sum subsequence in an array maxsum.c -- Time four algs: n3, n2, n log n, n. Column 9: Code tuning programs genbins.c -- Profile this, then try a special-purpose allocator. macfun.c -- Time the cost of macros and functions. The column also uses rotate.c (Column 2), search.c (Column 5) and maxsum.c (Column 8). Column 11: Test and time sorting algorithms sort.cpp -- Mostly C, but also C++ sort function. SortAnim.java -- Animate those sort functions in Java. Column 12: Generate a sorted list of random integers sortedrand.cpp -- Several algorithms for the task. Column 13: Set representations for the problem in Column 12 sets.cpp -- Several data structures for sets. genbins.c (Column 9) implements the bin data structure in C. Column 14: Heaps priqueue.cpp -- Implement and test priority queues. The column also uses sort.c (Column 11) for heapsort. Column 15: Strings wordlist.cpp -- List words in the file, using STL set. wordfreq.cpp -- List words in the file, with counts, using STL map. wordfreq.c -- Same as above, with hash table in C. longdup.c -- Find long repeated strings in input. markov.c -- Generate random text from input. markovhash.c -- Like markov.c, but with hashing. markovlet.c -- Letter-level markov text, simple algorithm. Appendix 3: Cost Models spacemod.cpp -- Space used by various records. timemod.c -- Table of times used by various C constructs. You may use this code for any purpose, as long as you leave the copyright notice and book citation attached.
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

...404 Not Found

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值