[英]C++ if() into switch and case statement
I have if() function for ternary operator (':', '?') 我有用于三元运算符(':','?')的if()函数
if(operation == ':')
....
....
if(operation == '?')
....
return new ternaryOperator(first, second, third)
These give a perfect operation value. 这些提供了完美的操作价值。 But I need to make them Switch() and Case statement.
但是我需要使它们具有Switch()和Case语句。 I have used Switch() and nested Switch() statement as well, but it doesn't return correct value.
我也使用过Switch()和嵌套的Switch()语句,但是它没有返回正确的值。 How do I put those if functions into Switch and Case statement ?
如何将这些if函数放入Switch and Case语句中?
#include <iostream>
using namespace std;
#include "expression.h"
#include "subexpression.h"
#include "operand.h"
#include "plus.h"
#include "minus.h"
#include "times.h"
#include "divide.h"
#include "greaterThan.h"
#include "lessThan.h"
#include "equal.h"
#include "and.h"
#include "or.h"
#include "notEqual.h"
#include "ternaryOperator.h"
SubExpression::SubExpression(Expression* left, Expression* right){
this->left = left;
this->right = right;
}
SubExpression::SubExpression(Expression* first, Expression* second, Expression* third)
{
this->first = first;
this->second = second;
this->third = third;
}
SubExpression::SubExpression(Expression* left)
{
this->left = left;
}
Expression* SubExpression::parse()
{
Expression* left;
Expression* right;
Expression* first;
Expression* second;
Expression* third;
char operation, paren;
bool isTernary = false;
left = Operand::parse();
cin >> operation;
right = Operand::parse();
**if (operation == ':')
{
first = left;
second = right;
left = Operand::parse();
cin >> operation;
right = Operand::parse();
if (operation == '?')
{
third = right;
isTernary = true;
}
}
cin >> paren;
if (isTernary == true)
{
return new ternaryOperator(first, second, third);
}**
switch (operation)
{
case '+':
return new Plus(left, right);
case '-':
return new Minus(left, right);
case '*':
return new Times(left, right);
case '/':
return new Divide(left, right);
case '>':
return new greaterThan(left, right);
case '<':
return new lessThan(left, right);
case '=':
return new Equal(left, right);
case '&':
return new And(left, right);
case '|':
return new Or(left, right);
case '!':
return new notEqual(left);
}
return 0;
}
class ternaryOperator: public SubExpression
{
public:
ternaryOperator(Expression* first, Expression* second, Expression* third):
SubExpression(first, second, third)
{
}
double evaluate()
{
return third->evaluate() ? first->evaluate() : second->evaluate();
}
};
Ok. 好。 I was able to break the nested if(operation == '?') into the below Switch and case statements and it worked.
我能够将嵌套的if(operation =='?')分解为下面的Switch和case语句,并且可以正常工作。 But I still need put the main if(operation ==':') into switch and case statements
但是我仍然需要将主要的if(operation ==':')放入switch和case语句中
if (operation == ':')
{
first = left;
second = right;
left = Operand::parse();
cin >> operation;
right = Operand::parse();}
..
...
....
case '?':
third = right;
return new ternaryOperator(first, second, third);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.