对于这个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 |