【问题标题】:Display numbers from A to B with a modifier使用修饰符显示从 A 到 B 的数字
【发布时间】:2018-10-01 11:24:27
【问题描述】:

在C++中,是否可以使用变量输入运算符(如“-、+、*、/”),然后在计算中使用?

例子:

cin>>(input operator here)
cout<< i (inputted operator here) b;

目前我已经通过使用“开关”来完成它,它很长而且对眼睛非常不友好 - 我坚信有一种输入运算符的方法,但我不知道如何。

这是我编写的代码 - 如果可能,请按照我询问的方式修复它(并说明你是如何完成的!)。 非常感谢您的帮助:)。

#include <iostream>
#include <string>

using namespace std;

bool wantedModifyed=0;
string choice;
char operato;

template<typename T>
T get(const string &prompt)
{
    std::cout<<prompt;
    T ret;
    std::cin>>ret;
    return ret;
}

void modifier()
{
    if (!(choice=="Y" || choice=="y" || choice=="N" || choice=="n"))
    {
        cout<<"Y or N: ";
        cin>>choice;
        modifier();
    }
    if (choice=="Y" || choice=="y")
    {
        wantedModifyed=1;
    }
}
void operatorChecker()
{
    if (!(operato=='+' || operato=='-' || operato=='*' || operato=='/'))
    {
        operato=get<char>("\nEnter your modifier (+,-,*,/): ");
        operatorChecker();
    }
}

int OperatorPlus(int a, int c){return a+c;}

int OperatorMinus(int a, int c){return a-c;}

int OperatorMultiply(int a, int c){return a*c;}

int OperatorDivide(int a, int c){return a/c;}

int main()
{
    int a,b;
    a=get<int>("\nEnter number A: ");
    b=get<int>("\nEnter number B: ");
    if(a>b) swap(a,b);
    modifier();
    if(wantedModifyed==0)
        for(int i=a; i<=b; i++)
            if(i==b) cout<<i<<". ";
            else cout<<i<<", ";
    else
    {
        get<char>("\nEnter your operator (+,-,*,/): ");
        operatorChecker();
        int modifier=get<int>("\nEnter your modifier: ");
        for(int i=a; i<=b; i++)
        {
            switch(operato)
            {
            case '+':
                cout<<OperatorPlus(i,modifier);
                if(i==b) cout<<".";
                else cout<<", "; break;
            case '-':
                cout<<OperatorMinus(i,modifier);
                if(i==b) cout<<".";
                else cout<<", "; break;
            case '*':
                cout<<OperatorMultiply(i,modifier);
                if(i==b) cout<<".";
                else cout<<", "; break;
            case '/':
                cout<<OperatorDivide(i,modifier);
                if(i==b) cout<<".";
                else cout<<", "; break;
            }
        }
    }

    return 0;
}

【问题讨论】:

  • 您可能想在互联网上搜索“c++ 计算器”,看看是否可行。请记住,您需要考虑优先级。
  • 顺便说一句,如果在比较之前将字符响应转换为全大写或全小写,则可以将比较减少 50%!见toupper 和tolower。
  • 您可以通过删除template 来简化您的程序。 C++ I/O 流将为您处理匹配的数据类型(它是内置的)。
  • 不建议对提示或菜单使用递归。使用while 或do-while。
  • @ThomasMatthews 你是说不可能以任何方式输入运算符吗?我熟悉超基本的开关计算器,但我非常怀疑没有一种方法可以使用 cin 输入运算符...也许是一些库?

标签: c++ performance io sequence


【解决方案1】:

如果您想输入 运算符 而不是字符类型,则需要创建运算符类型并重载 operator&gt;&gt;。

class Operator
{
  public:
    Operator(const char symbol)
    : operator_char(symbol)
    { ; }
    virtual int evaluate(int a, int b) = 0;
  private:
    const char operator_char;
};

class Multiply_Operator : public Operator
{
  public:
    Multiply_Operator() : Operator('*')
    { ; }
};

// ...
Operator * Create_Operator()
{
  static std::map<char, Operator *> dictionary;
  if (dictionary.empty())
  {
     dictionary['*'] = new Multiply_Operator;
     //...
  }
  char operator_char;
  std::cout << "Enter operator (*, /, +, -): ";
  std::cin >> operator_char;
  Operator * p_operator = dictionary[operator_char];
  return p_operator;
}

上面类似于 Factory Design 模式,但比使用简单的switch 语句有点矫枉过正。

编辑 1:简化
让我们有一个通用运算符:

class Operator
{
  public:
    friend std::istream operator>>(std::istream& input, Operator& opr);
  private:
    enum Operation_Type
    {
       OPR_MULTIPLE, OPR_DIVIDE, OPR_ADD, OPR_SUBTRACT
    };
    Operation_type ot;
};
std::istream operator>>(std::istream& input, Operator& opr)
{
  char opr_char;
  input >> opr_char;
  switch (opr_char)
  {
    case '*' : opr.ot = OPR_MULTIPLY; break;
    //...
  }
  return input;
}

总结
基本问题是您需要将一个字符关联或映射到一个运算符。关联的方法有很多,switch 是其中一种,std::map 是另一种。

【讨论】:

  • 所以有 no 更简单的方法可以做到这一点,例如一个简单的变量,让您可以输入运算符(+、-、*、/、%)然后使用它在一个 cout 中,即添加它就像添加一个数字一样?这似乎确实有缺陷,并且浪费了很多时间来完成应该在 2 行中完成的事情。
  • 最简单的方法是将操作符作为字符读入。您仍然必须将角色与功能相关联:开关或地图或表格查找。您可以使用 if-else 阶梯,但这些阶梯比 switch 更难看。
猜你喜欢
  • 2013-02-13
  • 2022-11-29
  • 2013-06-04
  • 2012-06-09
  • 2012-04-09
  • 1970-01-01
  • 1970-01-01
  • 2021-11-10
  • 2023-03-04
相关资源
最近更新 更多