通过一个switch语句发送不同的变量

时间:2014-02-12 05:18:11

标签: c++ arrays enums switch-statement

我必须为我的第一个c ++类创建一个老虎机,我必须使用枚举数据类型和数组用于转轴。我的程序目前选择所有三个卷轴随机帧,但对于我的生活,我想不出一种方法,使其更紧凑。我必须保留所有卷轴值,以便我可以使用if语句创建赢或输的结果。有没有办法通过同一个开关循环每个变量?

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

enum frameType {PETROLEUM=1, COAL, NATURAL_GAS, URANIUM, GLOBAL_WARMING,NUCLEAR_DISASTER, DIRT};
enum outcomeType {PETROLEUM_WIN, COAL_WIN, NATURAL_GAS_WIN, URANIUM_WIN, LOSS, DRAW};

int main()
{
  srand(time(NULL));

  frameType frm1;


  int frameReel1[] =  {1,1,1,1,1,1,2,2,2,2,2,3,3,3,3,4,4,5,5,6,7,7,7,7,7};
  int frameReel2[] =  {1,1,1,1,1,1,2,2,2,2,2,3,3,3,3,4,4,5,5,6,7,7,7,7,7};
  int frameReel3[] =  {1,1,1,1,1,1,2,2,2,2,2,3,3,3,3,4,4,5,5,6,7,7,7,7,7};

  int RandIndex1 = rand() % 25;
  int RandIndex2 = rand() % 25;
  int RandIndex3 = rand() % 25;

  int a = frameReel1[RandIndex1];
  int b = frameReel2[RandIndex2];
  int c = frameReel3[RandIndex3];

  switch (a)
  {
  case PETROLEUM:
    cout << "[petroleum]n";
    break;
  case COAL:
    cout << "[coal]";
    break;
  case NATURAL_GAS:
    cout << "[natural gas]";
    break;
  case URANIUM:
    cout << "[uranium]";
    break;
  case GLOBAL_WARMING:
    cout << "[global warming]";
    break;
  case NUCLEAR_DISASTER:
    cout << "[nuclear disaster]";
    break;
  case DIRT:
    cout << "[dirt]";
    break;
 }

switch (b)
{
case PETROLEUM:
  cout << "[petroleum]";
  break;
case COAL:
  cout << "[coal]";
  break;
case NATURAL_GAS:
  cout << "[natural gas]";
  break;
case URANIUM:
  cout << "[uranium]";
  break;
case GLOBAL_WARMING:
  cout << "[global warming]";
  break;
case NUCLEAR_DISASTER:
  cout << "[nuclear disaster]";
  break;
case DIRT:
  cout << "[dirt]";
  break;
}

switch (c)
{
case PETROLEUM:
  cout << "[petroleum]";
  break;
case COAL:
  cout << "[coal]\n";
  break;
case NATURAL_GAS:
  cout << "[natural gas]";
  break;
case URANIUM:
  cout << "[uranium]";
  break;
case GLOBAL_WARMING:
  cout << "[global warming]";
  break;
case NUCLEAR_DISASTER:
  cout << "[nuclear disaster]";
  break;
case DIRT:
  cout << "[dirt]";
  break;
}
} // end main

3 个答案:

答案 0 :(得分:1)

试试这段代码:

int outcomes[3] = {frameReel1[RandIndex1], frameReel2[RandIndex2], frameReel3[RandIndex3]};
for(int i=0; i<3; i++)
  switch(outcomes[i])
    // Cases...

答案 1 :(得分:1)

这是函数非常有用的一件事 - 避免重复代码。这是您的代码,在函数中使用switch语句:

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

enum frameType {PETROLEUM=1, COAL, NATURAL_GAS, URANIUM, GLOBAL_WARMING,NUCLEAR_DISASTER, DIRT};
enum outcomeType {PETROLEUM_WIN, COAL_WIN, NATURAL_GAS_WIN, URANIUM_WIN, LOSS, DRAW};


void printFrameType(int a)
{
    switch (a)
    {
    case PETROLEUM:
        cout << "[petroleum]n";
        break;
    case COAL:
        cout << "[coal]";
        break;
    case NATURAL_GAS:
        cout << "[natural gas]";
        break;
    case URANIUM:
        cout << "[uranium]";
        break;
    case GLOBAL_WARMING:
        cout << "[global warming]";
        break;
    case NUCLEAR_DISASTER:
        cout << "[nuclear disaster]";
        break;
    case DIRT:
        cout << "[dirt]";
        break;
    }
}


int main()
{
    srand(time(NULL));

    frameType frm1;

    int frameReel1[] =  {1,1,1,1,1,1,2,2,2,2,2,3,3,3,3,4,4,5,5,6,7,7,7,7,7};
    int frameReel2[] =  {1,1,1,1,1,1,2,2,2,2,2,3,3,3,3,4,4,5,5,6,7,7,7,7,7};
    int frameReel3[] =  {1,1,1,1,1,1,2,2,2,2,2,3,3,3,3,4,4,5,5,6,7,7,7,7,7};

    int RandIndex1 = rand() % 25;
    int RandIndex2 = rand() % 25;
    int RandIndex3 = rand() % 25;

    int a = frameReel1[RandIndex1];
    int b = frameReel2[RandIndex2];
    int c = frameReel3[RandIndex3];

    printFrameType(a);
    printFrameType(b);
    printFrameType(c);
} // end main

还有其他方法可以改进这段代码,但是你说这是你的第一个C ++类,所以我不会用新信息来淹没你。由于frameReel1frameReel2frameReel3相同,为什么不只是拥有一个并将其称为frameReel

您可以使用这样的循环代替使用缩短代码的函数:

for(int i = 0; i < 3; i++)
{
    any code you put in here will run three times, so
    if you put the random choice and the switch statement
    in here, it will print 3 random frame types
}

您将了解for(int i = 0; i < 3; i++)之后的含义,但您现在可以将其视为魔术。如果您希望它运行不同的次数,请将3更改为其他数字。

Tony D's和LeonardBlunderbuss的答案更好,但涉及更高级的概念。

答案 2 :(得分:0)

您可以编写可重复使用的支持功能:

std::ostream& operator<<(std::ostream& os, frameType x)
{
    switch (a)
    {
      case PETROLEUM:        return os << "[petroleum]n";
      case COAL:             return os << "[coal]";
      case NATURAL_GAS:      return os << "[natural gas]";
      case URANIUM:          return os << "[uranium]";
      case GLOBAL_WARMING:   return os << "[global warming]";
      case NUCLEAR_DISASTER: return os << "[nuclear disaster]";
      case DIRT:             return os << "[dirt]";
      default:               return os << "<invalid>"; // or throw?
    }
}

然后:

frameType a = static_cast<frameType>(frameReel1[RandIndex1]);
...

std::cout << a << b << c;

(您应该在frameType / frameReel1 / 2中存储3