模拟场景:
魔术师拿出一叠卡片依次给参与者看,参与者看完每一张卡片后告诉魔术师这叠卡片中有没有自己心中所想的数字。然后魔术师依据回答来猜数。现给定一个特定的数值,设置一个程序利用计算机来自动生成一些符合魔术师要求的卡片来完成这猜数小游戏。
(这篇文章写的是在完成这个任务的时候笔者所出现的一些错误的总结与反思)
问题描述:
由分析可以知道,这组卡片的分类标准是每一个整数的二进制的表示。
编写好程序后,在运行该程序的时候,被提示到错误“E2140 C++ 表达式必须具有整数或未区分范围的枚举类型”
出错代码展示:
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
int number=0,a=0,b=0,c=0,d=0,e=0,f=0,g=0,
a1 = 0, b1 = 0, c1 = 0, d1 = 0, e1 = 0, f1 = 0, g1 = 0,
a2[100]{}, b2[100]{}, c2[100]{}, d2[100]{}, e2[100]{}, f2[100]{}, g2[100]{};
//易知如果需要的数在100以内则至多需要准备7张卡片,此处7个数组指代7张卡片
cout << "请输入一个100以内的数:";
cin >> number; //输入卡片中所要求的数的最大值
for (int t = 1;t <= number;t++) //利用循环来将数字放入符合要求的卡片中
{
g = t % 2;
f = t / pow(2, 1) % 2;
e = t / pow(2, 2) % 2;
d = t / pow(2, 3) % 2;
c = t / pow(2, 4) % 2; //利用进行该运算来判断卡片中是否会出现该数。
b = t / pow(2, 5) % 2; 若值为1,则说明该数在对应的卡片中;
a = t / pow(2, 6) % 2; 若值为0,则说明不在。
if(g)
{
g2[g1] = t;
g1++;
} //该7个循环是上述判断的具体实现过程
if (f)
{
f2[f1] = t;
f1++;
}
if (e)
{
e2[e1] = t;
e1++;
}
if (d)
{
d2[d1] = t;
d1++;
}
if (c)
{
c2[c1] = t;
c1++;
}
if (b)
{
b2[b1] = t;
b1++;
}
if (a)
{
a2[a1] = t;
a1++;
}
} //至此7个循环结束,a1,b1,c1...指的是在每一张卡片中数字的数量
cout << "第一张卡片中的数有:"; //利用输出各个卡片
for (int t = 0;t < g1;t++)
cout << g2[t] << " ";
cout << endl << "第二张卡片中的数有:";
for (int t = 0;t < f1;t++)
cout << f2[t] << " ";
cout << endl << "第三张卡片中的数有:";
for (int t = 0;t < e1;t++)
cout << e2[t] << " ";
cout << endl << "第四张卡片中的数有:";
for (int t = 0;t < d1;t++)
cout << d2[t] << " ";
cout << endl << "第五张卡片中的数有:";
for (int t = 0;t < c1;t++)
cout << c2[t] << " ";
cout << endl << "第六张卡片中的数有:";
for (int t = 0;t < b1;t++)
cout << b2[t] << " ";
cout << endl << "第七张卡片中的数有:";
for (int t = 0;t < a1;t++)
cout << a2[t] << " ";
return 0;
}
原因分析:
之所以会出现该错误(表达式必须具有整数或未区分范围的枚举类型)是因为对于
a = t / pow(2, 6) % 2;
中所涉及到的取余运算对数据类型的要求:取余运算符( N % M )要求函数左右两边都是整型,即 N 和 M 都必须是整型;而在上式中 pow(X,Y)函数得到的返回值并不是整型(即使函数中我们输入的 X 和 Y 都是整型)。
通过查看帮助我们看到pow函数的使用方式:
double pow( double x, double y );
float powf( float x, float y );
long double powl( long double x, long double y );
define pow(X, Y) // Requires C11 or higher
double pow( double x, int y ); // C++ only
float pow( float x, float y ); // C++ only
float pow( float x, int y ); // C++ only
long double pow( long double x, long double y ); // C++ only
long double pow( long double x, int y ); // C++ only
通过这一组数据中我们发现pow函数无论以什么形式输入的变量值都会被强制转化为double,float或者long double的类型,同样也只会返回这三种类型的值。故含有 pow(X,Y)这一部分的返回值并不是整型,故不能用来参与取余。
解决方案:
将含有 pow 函数的那一部分的数据类型转化为整型。
利用 int 将最后的得到的非整型的部分转化为整型,此处有两种方法(两种改法皆可):
e =int( t / pow(2, 2)) % 2; //方法一
d = t / int(pow(2, 3)) % 2; //方法二
最后修改后的全部代码:
/*程序的主要功能:给定一个100以内特定的数值,自动生成一些卡片来完成猜数小游戏*/
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
int number=0,a=0,b=0,c=0,d=0,e=0,f=0,g=0,
a1 = 0, b1 = 0, c1 = 0, d1 = 0, e1 = 0, f1 = 0, g1 = 0,
a2[100]{}, b2[100]{}, c2[100]{}, d2[100]{}, e2[100]{}, f2[100]{}, g2[100]{};
cout << "请输入一个100以内的数:";
cin >> number;
for (int t = 1;t <= number;t++)
{
g = t % 2;
f =int( t / pow(2,1)) % 2;
e =int( t / pow(2, 2)) % 2;
d = t / int(pow(2, 3)) % 2;
c = t / int(pow(2, 4)) % 2;
b = t / int(pow(2, 5)) % 2;
a = t / int(pow(2, 6)) % 2;
if(g)
{
g2[g1] = t;
g1++;
}
if (f)
{
f2[f1] = t;
f1++;
}
if (e)
{
e2[e1] = t;
e1++;
}
if (d)
{
d2[d1] = t;
d1++;
}
if (c)
{
c2[c1] = t;
c1++;
}
if (b)
{
b2[b1] = t;
b1++;
}
if (a)
{
a2[a1] = t;
a1++;
}
}
cout << "第一张卡片中的数有:";
for (int t = 0;t < g1;t++)
cout<< g2[t]<< " ";
cout << endl<< "第二张卡片中的数有:";
for (int t = 0;t < f1;t++)
cout<< f2[t] << " ";
cout << endl<< "第三张卡片中的数有:";
for (int t = 0;t < e1;t++)
cout<< e2[t] << " ";
cout << endl<< "第四张卡片中的数有:";
for (int t = 0;t < d1;t++)
cout << d2[t] << " ";
cout << endl << "第五张卡片中的数有:";
for (int t = 0;t < c1;t++)
cout << c2[t] << " ";
cout << endl << "第六张卡片中的数有:";
for (int t = 0;t < b1;t++)
cout << b2[t] << " ";
cout <<endl<< "第七张卡片中的数有:";
for (int t = 0;t < a1;t++)
cout << a2[t] << " ";
return 0;
}
程序运行结果截图:
最后的总结:
在使用函数的时候,要注意函数的返回值的数据类型和所使用函数需要的数据类型,避免发生错误。
谢谢观看,如果在文章中有问题或者有描述不清楚的地方,您可以随时在评论区中与笔者交流。由于笔者的能力有限 ,对于这个场景的处理方法可能不是特别的好,如果您有其他的方法 ,也欢迎您在评论区中发表出来。