【问题标题】:How to use enumeration types in C++?如何在 C++ 中使用枚举类型?
【发布时间】:2010-04-09 04:27:20
【问题描述】:

我不明白如何使用枚举类型。我明白它们是什么,但我不太明白它们的目的。

我编写了一个程序,输入三角形的三个边并输出它们是等腰、不等边还是等边。我想在某处合并枚举类型,但不知道在哪里以及如何使用它们。任何帮助将不胜感激。

#include <iostream>

using namespace std;

enum triangleType {scalene, isosceles, equilateral, noTriangle};

triangleType triangleShape(double x, double y, double z);
void printTriangleShape(triangleType shape);

int main()
{
    double x, y, z;
    triangleType scalene, isosceles, equilateral, noTriangle;

    cout << "Please enter the three sides of a triangle:" << endl;
    cout << "Enter side 1: ";
    cin >> x;
    cout << endl;
    cout << "Enter side 2: ";
    cin >> y;
    cout << endl;
    cout << "Enter side 3: ";
    cin >> z;
    cout << endl;

    triangleType t = triangleShape(x, y, z); 
printTriangleShape(t);

    return 0;
}

triangleType triangleShape(double x, double y, double z)
{
   triangleType scalene, isoceles, equilateral, noTriangle;
    if (((x+y) > z) && ((x+z) > y) && ((y+z) > x))
    {
        cout << "You have a triangle!" << endl;
            if (x == y && y == z)
               return equilateral;
            else if (x == y || x == z || y == z)
                 return isosceles;
            else
               return scalene;
    }
    else if ((x+y) <= z || ((x+z) <= y) || ((y+z) <= x))
        return noTriangle;  
} 
void printTriangleShape(triangleType shape)
{
    switch (shape)
    {
    case scalene: cout << "Your triangle is Scalene!" << endl;
        break;
    case isosceles: cout << "Your triangle is an isosceles!" << endl;
        break;
    case equilateral: cout << "Your triangle is an equilateral!" << endl;
        break;

    }
}

【问题讨论】:

  • 选择代码,然后按“代码”按钮(带有 1010 的那个)。仅此而已。
  • 请不要用您收到的反馈和答案编辑您的问题和上面的原始源代码。当我以目前的形式阅读问题和代码时,我无法弄清楚您在问什么。您可以考虑将其发布为答案之一

标签: c++ enums


【解决方案1】:

这是一个值,您可能希望从函数中返回它。

试试:

triangleType triangleShape(double x, double y, double z) {
  if (...) {
    return scalene;
  } else if (...) {
    return isosceles.
  } else if (...) {
    return equilateral
  } else {
    return noTriangle;
  } 
}

注意,您可以打印结果,但它会打印为整数: scalene = 0, isosceles = 1, ...

编辑,对于打印,您可能需要这样做:

void printTriangleShape(triangleType shape) {
   switch (shape) {
     case scalene:
       cout << "Your triangle is Scalene!" << endl;
       break;
     case isosceles:
       cout << "Your triangle is isosceles!" << endl;
       break;
     ...;
   }
}

【讨论】:

  • 我想过这样,但不会只返回0-3的返回值吗?将它们作为不等边或等边或等边返回后我该怎么办?
  • 是的,差不多。但对于编译器来说,它是类型安全的特殊标识符。
  • 那么,当它返回 scalene 时,如何显示某种消息?我想提示用户三角形的形状
  • 谢谢斯蒂芬。我想当我尝试使用案例做同样的事情时,我没有正确设置参数。
  • 啊,我不是唯一一个忘记了 switch case 结尾处的 break 的人...... ;)
【解决方案2】:

在 C 中,枚举使调试更容易,因为调试器通常会打印名称值而不是数值。它们还允许编译器强制执行它可以确定将无效值存储到枚举变量中的位置。

在 C++ 中还有另一个好处,就是可以在重载中使用枚举类型。 例如,您可以:

ostream & operator<<(ostream & ostr, triangleType t) {
     string s;
     switch (t) {
          case scalene:
             s = "scalene";
             break;
          case isosceles:
             s = "isosclese";
             break;
          case equilateral:
             s = "equilateral";
             break;
          case noTriangle:
             s = "noTriangle";
             break;
          default:
             s = "error bad triangle type";
             break;
       }
       return cout << s;
}

然后在 main 中做

cout << "Your triangle is" << t << endl;

【讨论】:

  • 恕我直言,[enum, text] 表会比switch 语句更好。
  • 许多编译器足够聪明,可以做到这一点。加上使用数组意味着编写此函数的人必须知道枚举的整数值是什么来填充该数组,并且它必须在打印函数使用的三角形代码的所有实现中保持一致。话虽这么说,如果我编写了一个转换为字符串运算符的输出运算符,那可能会更好。
【解决方案3】:

枚举可用于识别对象的“类型”,就像您的情况一样。

例如,您的三角形方法可以返回 triangleType,这样您就可以在 main 方法中执行所有 cout &lt;&lt; "..." 并将显示逻辑与三角形对象分开。

【讨论】:

    【解决方案4】:

    我们的想法是用有意义的标签(红色、绿色、蓝色...)替换无法解释其含义的数字(1、2、3...)。代码中使用的只有您理解其含义的数字称为“幻数”,应避免使用,因为它会使其他人无法理解您的代码。

    【讨论】:

      【解决方案5】:

      枚举是 c++ 中的一种新类型。使用此类型会创建额外的类型安全性,因为您只能使用为该枚举定义的值。除非您自己指定一个值,否则枚举值将自动编号,这应该很少需要。一个例子:

      enum Color { Red, Green, Blue }; // Red = 0, Green = 1, Blue = 2
      enum Shape { Circle, Square };   // Circle = 0, Square = 1
      
      int printColor(Color c)
      {
          // do something with the color here, for example print it.
          switch(c)
          {
              case Red:
                  cout << "Red";
                  break;
              case Green:
                  cout << "Green";
                  break;
              case Blue:
                  cout << "Blue";
                  break;
          }
      }
      
      int main(int argc, char* argv[])
      {
          printColor(Red); // works
          printColor(0);   // will give an error or warning in C++.
                           // However, C does less type checking and allows this.
      
          printColor(Circle);
                           // error, the type is wrong even if the values are identical.
      }
      

      您在printColor(0) 调用中获得了额外的类型安全性——c++ 在这里进行了额外的类型检查,因此您不会在调用中错误地输入无效数字。当然,您可以通过使用 #define 来获得相同的结果,甚至直接输入它们,但在这种情况下,如果您输入无效值,编译器将无法警告您。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-12-26
        • 2014-12-14
        • 2012-08-07
        • 1970-01-01
        相关资源
        最近更新 更多