你知道吗?~
#include<stdio.h>int main()
{
enum color {red=0,yellow=1,blue=2,white=3,black=4};
enum color i,j,k,pi;
int l=0,h;
for(i=red;i<=black;i++)
{
for(j=red;j<=black;j++)
{
if(i!=j)
{
for(k=red;k<=black;k++)
{
if(k!=i&&k!=j)
{
l=l+1;
printf("%-4d",l);
for(h=0;h<3;h++)
{
switch(h)
{
case(0):pi=i;break;
case(1):pi=j;break;
case(2):pi=k;break;
default:break;
}
switch(pi)
{
case(red):printf("%-10s","red");break;
case(yellow):printf("%-10s","yellow");break;
case(blue):printf("%-10s","blue");break;
case(white):printf("%-10s","white");break;
case(black):printf("%-10s","black");break;
default:break;
}
}
printf("\n");
}
}
}
}
}
printf("\ntotal:%-4d",l);
}
口袋中有红 黄 蓝白黑五种颜色的区域若干个,每次从口袋中先后去除三个球,问得到三种不同颜色的球的可能取法,输出每种排列的情况。。。。
编译时出现
E:\CCCCCCCCC\cao.cpp In function 'int main()':
7 22 E:\CCCCCCCCC\cao.cpp no 'operator++(int)' declared for postfix '++' [-fpermissive]
9 23 E:\CCCCCCCCC\cao.cpp no 'operator++(int)' declared for postfix '++' [-fpermissive]
13 25 E:\CCCCCCCCC\cao.cpp no 'operator++(int)' declared for postfix '++' [-fpermissive]
哪个大神知道什么原因?
++不支持这样的类型递增 只支持int DWORD WORD long 啥的
CPP的运算符重载能解决这个问题,或者用结构体 只需将i++,j++,k++都改为i=Color(i+1),j=Color(j+1),k=Color(k+1)就行
口袋中有红、黄、蓝、白、黑5种颜色球若干个。每次从口袋先后取出3个球,问得到3种颜色不同的球的可能 ,输出每种排列的情况。
#include<stdio.h>
enum Color {red,yellow,blue,white,black};
int main()
{
enum Color i,j,k,pri;
int n,loop;
n=0;
for(i=red;i<=black;i=Color(i+1))//不能用i++,会报错
for(j=red;j<=black;j=Color(j+1))
if(i!=j)
{
for(k=red;k<=black;k=Color(k+1))
if((k!=i)&&(k!=j)){
n=n+1;
printf("%-4d",n);
for(loop=1;loop<=3;loop++)
{
switch(loop)
{
case 1:pri=i;break;
case 2:pri=j;break;
case 3:pri=k;break;
default :break;
}
switch(pri)
{
case red:printf("%-10s","red");break;
case yellow:printf("%-10s","yellow");break;
case blue:printf("%-10s","blue");break;
case white:printf("%-10s","white");break;
case black:printf("%-10s","black");break;
default :break;
}
}
printf("\n");
}
}
printf("\ntotal:%5d\n",n);
return 0;
}
对于这个color类型的++来说,enum本身不支持++运算符,如果说非要支持,那么要么把他转成int类型,或者重载++运算符,转成int应该不用说了,下面提供了一种重载++运算符的做法
#include<stdio.h>
enum color
{
red = 0, yellow = 1, blue = 2, white = 3, black = 4
};
color operator++(color c, int)
{
return (color)(c + 1);
}
int main()
{
enum color i, j, k, pi;
int l = 0, h;
for (i = red; i <= black; i++)
{
for (j = red; j <= black; j++)
{
if (i != j)
{
for (k = red; k <= black; k++)
{
if (k != i&&k != j)
{
l = l + 1;
printf("%-4d", l);
for (h = 0; h < 3; h++)
{
switch (h)
{
case(0) : pi = i; break;
case(1) : pi = j; break;
case(2) : pi = k; break;
default:break;
}
switch (pi)
{
case(red) : printf("%-10s", "red"); break;
case(yellow) : printf("%-10s", "yellow"); break;
case(blue) : printf("%-10s", "blue"); break;
case(white) : printf("%-10s", "white"); break;
case(black) : printf("%-10s", "black"); break;
default:break;
}
}
printf("\n");
}
}
}
}
}
printf("\ntotal:%-4d", l);
return 0;
}
另外说一点,这个int main(),虽然没返回值也能编译通过,这是因为编译器对main函数特殊处理了,但是最好养成好习惯,加上return
页:
[1]