标题:问一个关于枚举
只看楼主
树上月
Rank: 3Rank: 3
等 级:论坛游侠
威 望:1
帖 子:114
专家分:154
注 册:2010-1-6
结帖率:87.5%
已结贴  问题点数:15 回复次数:4 
问一个关于枚举
最近看到这样一个关于水果拼盘的另解:
  现有苹果,橘子,香蕉,菠萝,梨5种水果用来做水果拼盘,每个水果拼盘一定有三个水果,且这3个水果的种类不同,问可以制作多少个水果拼盘:
   #include<stdio.h>
void main()
{
    enum fruit {apple,orange,banana,pineapple,pear};   //定义枚举结构
    enum fruit x,y,z,pri;                             //定义枚举变量
    int n=0,loop;
    for(x=apple;x<=pear;x++)
        for(y=apple;y<=pear;x++)
            if(x!=y){
                for(z=apple;z<=pear;z++)
                    if((z!=x)&&(z!=y)){
                        n=n+1;
                        printf("\n%-4d",n);
                        for(loop=1;loop<=3;loop++)
                        {
                            switch(loop){
                            case 1:
                                pri=x;
                                break;
                            case 2:
                                pri=y;
                                break;
                            case 3:
                                pri=z;
                                break;
                            default :break;
                            }
                            switch(pri){
                            case apple:
                                printf("%-9s","apple");
                                break;
                            case orange:
                                printf("%-9s","orange");
                                break;
                            case pineapple:
                                printf("%-9s","pineapple");
                                break;
                            case pear:
                                printf("%-9s","pear");
                                break;
                            default:break;
                            }
                        }
                    }
            }
}
   
复杂了点》但在Vc下运行有错误?该怎么改呢?
搜索更多相关主题的帖子: 枚举 
2010-03-18 22:47
ldg628
Rank: 12Rank: 12Rank: 12
等 级:火箭侠
威 望:3
帖 子:526
专家分:3036
注 册:2009-6-23
得分:5 
    for(x=apple;x<=pear;x++)
        for(y=apple;y<=pear;y++)
            if(x!=y){
                for(z=apple;z<=pear;z++)
2010-03-18 23:22
mikelr
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:67
专家分:161
注 册:2010-3-17
得分:8 
#include <stdio.h>

int main(void)
{
    enum frui {apple,orange,banana,pineapple,pear};
    enum frui x,y,z;
    int count = 0;

    for(x = apple;x <= pear;x++)
    {
        for(y = apple;y <= pear;y++)
        {
            for(z = apple;z <= pear;z++)
            {
                if(x != y && y != z && x != z)
                    count++;
            }
        }
    }
    printf("%d",count);

    return 0;
}
2010-03-18 23:22
树上月
Rank: 3Rank: 3
等 级:论坛游侠
威 望:1
帖 子:114
专家分:154
注 册:2010-1-6
得分:0 
回复 3楼 mikelr
在VC下和我出现了一样的问题啊:
\users\lenovo\desktop\新建文件夹\fhjuyt.cpp(9) : error C2676: binary '++' : 'enum main::frui' does not define this operator or a conversion to a type acceptable to the predefined operator
c:\users\lenovo\desktop\新建文件夹\fhjuyt.cpp(11) : error C2676: binary '++' : 'enum main::frui' does not define this operator or a conversion to a type acceptable to the predefined operator
c:\users\lenovo\desktop\新建文件夹\fhjuyt.cpp(13) : error C2676: binary '++' : 'enum main::frui' does not define this operator or a conversion to a type acceptable to the predefined operato

每一个不曾起舞的日子,都是对未来的一种辜负......
2010-03-18 23:27
mikelr
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:67
专家分:161
注 册:2010-3-17
得分:2 
这可能是编译器的问题,VC不支持enum 变量 自加.

我用的是codeblocks ,可以通过
2010-03-18 23:47



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-299848-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.090593 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved