我想知道如何正确使用开关/外壳

时间:2010-08-24 00:16:11

标签: c++ case switch-statement

我只是想知道是否有人可以给我一个如何使用开关/案例的例子。我有点喜欢它,但我很难弄清楚我能用什么以及如何使用它。提前谢谢。

5 个答案:

答案 0 :(得分:2)

关于switch case语句,有几点需要记住:

a)条件应该是integeral / enum / user defined type,它支持转换为int或enum

b)case lables是编译时常量

c)没有两个案例标签表达式可以具有相同的值

d)$ 6.4.2 / 5-“当执行switch语句时,它的条件被计算并与每个case常量进行比较。如果其中一个case常量等于条件的值,则控制权被传递给匹配的case标签后面的语句。如果没有case常量匹配条件,并且如果有一个默认标签,则控制传递给默认标签标记的语句。如果没有大小写匹配,如果没有默认,则没有任何语句开关被执行。“

e)$ 6.4.2 / 6-“案例和默认标签本身不会改变控制流程,这些标签在这些标签上继续畅通无阻。要退出交换机,请参阅”

enum direction {north, south, east, west};
char x;
class UD{
    operator int(){return 0;}
};

direction f1(){
    return north;
}

char f2(){
    return 'A';
}

int main(){
    direction d = f();
    string country;

    // switch condition of type enum
    switch(d){
    case north:
        country = "P";
        break;
    case south:
        country = "Q";
        break;
    case east:
        country = "R";
        break;
    case west:
        country = "S";
        break;
    default:
        country = "";
        break;
    }

    // switch condition of integral type
    switch(c){
    case 'A':
    case 'E':
    case 'I':
    case 'O':
    case 'U':
        cout << "Vowel";
        break;
    default:
        cout << "Not a Vowel";
        break;
    }

    UD u;

    // switch condition of user defined type (conversion to integral type)
    switch(u){
    case 0:
    case 1:
        cout << "Good";
        break;
    default:
        cout << "Not so good";
        break;
    }
}

答案 1 :(得分:1)

这是一个相当典型的用例。您有一个值列表(枚举)和一个检查输入以确定您正在处理的开关。这当然假设您将采取的行动取决于枚举的基础价值。

enum ImageFormat
{
    FormatRGB888,
    FormatRGB8888,
    FormatRGB101010,
    FormatRGB161616,
    FormatRGB16161616
};

void SomeFunc(ImageFormat format)
{
    switch(format)
    {
        case FormatRGB888:
            // do stuff
            break;
        case FormatRGB8888:
            // do stuff
            break;
        case FormatRGB101010,
            // do stuff
            break;
        case FormatRGB161616:
            // do stuff
            break;
        case FormatRGB16161616:
            // do stuff
            break;
        default:
            // bad value for 'format'
    }
}

答案 2 :(得分:0)

假设你有一个枚举

enum expr_type {
    EXPR_TYPE_ADD,
    EXPR_TYPE_SUBTRACT,
    EXPR_TYPE_GET_VALUE
};

我们可以对此进行切换:

enum expr_type t = /* get input somehow and find the type */;

switch(t) {
    case EXPR_TYPE_ADD:
         cout << "Operator Add";
         /* fall through */
    case EXPR_TYPE_SUBTRACT:
         cout << "Operator (Add or Subtract)";
         break;
    case EXPR_TYPE_GET_VALUE;
         cout << "Getting some value";
         break;
}

你必须放下休息;所以它没有落实 - 目前,EXPR_TYPE_ADD将会为EXPR_TYPE_SUBTRACT的所有代码提供支持。请务必正确使用break

答案 3 :(得分:0)

切换语句是执行大量ifs和elses的更有效方法。

答案 4 :(得分:-2)

import java.util.Scanner;

class Date{
public static void main(String[] args) {

    String dow;
    String wowby;
    String yowby;
    
    Double n1,n2,res;
    Scanner scan = new Scanner (System.in);
    System.out.print("Enter Date (dd/mm/yy): ");
    
    String date = scan.nextLine();
    String dd = date.substring(0,2);
    String mm = date.substring(3,5);
    String yy = date.substring(6,8);
    
    int d = Integer.valueOf(dd);
    int m = Integer.valueOf(mm);
    int y = Integer.valueOf(yy);
    
    boolean valid = (d>=1) && (d<31)||(m>=1) && (m<12);//||((y>=00) && (y<99));
    
    if(!valid)
        System.out.print("Invalid date");
    
    else {
    switch (dd)
    { 
        case "01":  
            System.out.print("First of  ");
            switch (mm)  {
                case "01":
                System.out.print("January,2020");
                break;
                case "02":
                System.out.print("February,2020");
                break;
                case "03":
                System.out.print("March,2020");
                break;
                case "04":
                System.out.print("April,2020");
                break;
                case "05":
                System.out.print("May,2020");
                break;
                case "06":
                System.out.print("June,2020");
                break;
                case "07":
                System.out.print("July,2020");
                break;
                case "08":
                System.out.print("August,2020");
                break;
                case "09":
                System.out.print("September,2020");
                break;
                case "10":
                System.out.print("October,2020");
                break;
                case "11":
                System.out.print("November,2020");
                break;
                case "12":
                System.out.print("December,2020");
                break;
        
        default:
        System.out.print(" Invalid date ");
            }
            break;
              
                
        case "02":  
            System.out.print("Second of  ");    
            switch (mm)  
            {
                case "01":
                System.out.print("January,2020");
                break;
                case "02":
                System.out.print("February,2020");
                break;
                case "03":
                System.out.print("March,2020");
                break;
                case "04":
                System.out.print("April,2020");
                break;
                case "05":
                System.out.print("May,2020");
                break;
                case "06":
                System.out.print("June,2020");
                break;
                case "07":
                System.out.print("July,2020");
                break;
                case "08":
                System.out.print("August,2020");
                break;
                case "09":
                System.out.print("September,2020");
                break;
                case "10":
                System.out.print("October,2020");
                break;
                case "11":
                System.out.print("November,2020");
                break;
                case "12":
                System.out.print("December,2020");
                break;
        
        default:
        System.out.print(" Invalid month ");
            }
            break;
        case "03":  
            System.out.print("Third of  "); 
            switch (mm)  
            {
                case "01":
                System.out.print("January,2020");
                break;
                case "02":
                System.out.print("February,2020");
                break;
                case "03":
                System.out.print("March,2020");
                break;
                case "04":
                System.out.print("April,2020");
                break;
                case "05":
                System.out.print("May,2020");
                break;
                case "06":
                System.out.print("June,2020");
                break;
                case "07":
                System.out.print("July,2020");
                break;
                case "08":
                System.out.print("August,2020");
                break;
                case "09":
                System.out.print("September,2020");
                break;
                case "10":
                System.out.print("October,2020");
                break;
                case "11":
                System.out.print("November,2020");
                break;
                case "12":
                System.out.print("December,2020");
                break;
        
        default:
        System.out.print(" Invalid month ");
            }
            return;
    }       
        }
        
    
    }
}