【问题标题】:Is it possible to convert this if/else if to switch statements? [duplicate]是否可以将此 if/else if 转换为 switch 语句? [复制]
【发布时间】:2014-06-26 15:14:58
【问题描述】:

我根据here 找到的练习编写了一个简短的程序 (评分程序),我想知道是否可以(出于好奇)用 switch/case 语句替换 .cpp 中的 if 语句。此外,欢迎提供额外反馈,因为这里实施的一些东西对我来说是新的。

标题:

// include guard
#ifndef __MAINLAUNCH_H_INCLUDED__
#define __MAINLAUNCH_H_INCLUDED__

using namespace std;

class MainLaunch
{
    int *grade;
public:
    MainLaunch();
    MainLaunch(int&);
    ~MainLaunch();
    string getLetterGrade ();
    int getGrade() {return *grade;};
};

#endif //__MAINLAUNCH_H_INCLUDED__

Cpp:

#include <iostream>
#include <string>
#include "MainLaunch.h"

using namespace std;

MainLaunch::MainLaunch()
{
    grade=new int;
    *grade=75;
}

MainLaunch::MainLaunch(int& x)
{
    grade=new int;
    *grade=x;
}

MainLaunch::~MainLaunch()
{
    delete grade;
}

string MainLaunch::getLetterGrade()
{
    int y = MainLaunch::getGrade();
    if(y==100)
        return "Perfect score!";
    else if(y>90)
        return "A";
    else if(y>80)
        return "B";
    else if(y>70)
        return "C";
    else if(y>60)
        return "D";
    else
        return "F";
}

void main ()
{
    int input;
    MainLaunch ml1;

    cout << "Hello. Please enter your grade:" << endl;
    cin >> input;

    MainLaunch ml2(input);

    cout << "Default Constructor Object Grade is " << ml1.getGrade() << "(" << ml1.    getLetterGrade() << ")." << endl;
    cout << "Declared Constructor Object Grade is " << ml2.getGrade() << "(" << ml2.getLetterGrade() << ")." << endl << endl;
    system("pause");
}

【问题讨论】:

    标签: c++


    【解决方案1】:

    与 C++ 中的许多事情一样,它绝对可以通过多种方式完成。 但有时这并不是一个好主意。

    这就是 switch 语句的样子:

    switch(y)
    {
        case 100:
            return "Perfect Score!";
        break;
        case 99:
        case 98:
        case 97:
        case 96:
        case 95:
        case 94:
        case 93:
        case 92:
        case 91:
            return "A";
        break;
        case 90:
        case 89:
        case 88:
        case 87:
        case 86:
        case 85:
        case 84:
        case 83:
        case 82:
        case 81:
            return "B";
        break;
        case 80:
        case 79:
        case 78:
        case 77:
        case 76:
        case 75:
        case 74:
        case 73:
        case 72:
        case 71:
            return "C";
        break;
        case 70:
        case 69:
        case 68:
        case 67:
        case 66:
        case 65:
        case 64:
        case 63:
        case 62:
        case 61:
            return "C";
        break;
        default:
            return "F";
        break;
    
    }
    

    它还会限制你只使用整数,因为 switch 语句只适用于它们

    【讨论】:

    • 在这里查看我的答案以获取简单的方法stackoverflow.com/a/23330215/642626
    • 我并没有真正计算不可移植的 GCC 扩展(特别是因为我主要为 MSVC 编程)。不过那还是很酷的。
    【解决方案2】:
    switch ((y - 1) / 10)
    {
        case 9:  return y == 100 ? "Perfect score!" : "A";
        case 8:  return "B";
        case 7:  return "C";
        case 6:  return "D";
        default: return "F";
    }
    

    如果比较是&gt;=,它会不那么难看 - 你可以:

    switch (y / 10)
    {
      case 10: return "Perfect score!";
      case 9:  return "A";
      case 8:  return "B";
      ...etc...
    

    你确定他们不应该那样吗?在我的学校里,90 的“A”比必须达到 91 更常见....

    更新:查看您的链接(有一个较早的步骤 100 =“完美分数!” - 如果您也想在下一步中停留,我很好)

    ★★ 修改程序,让它通知用户他们的字母等级

    0-59 F 60-69 D 70-79 C 80-89 B 90-100 A

    所以 y / 10 确实合适....

    【讨论】:

    • 啊,可以传表达式来切换!我的印象是您只能指定变量。
    猜你喜欢
    • 2019-07-04
    • 2019-07-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多