2017-02-25 109 views
0

我如果三元运算符()函数( ':' '?',)C++如果()为开关和case语句

if(operation == ':') 
.... 
.... 
    if(operation == '?') 
.... 
    return new ternaryOperator(first, second, third) 

这些给一个完美的操作价值。但我需要让他们 Switch()和Case语句。我也使用了Switch()和嵌套的Switch()语句,但它没有返回正确的值。 如何将这些if函数放入Switch和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(); 
     } 
}; 
+0

'开关()'不能处理字符串文字。 –

+0

但它可以处理字符文字(注意单引号,而不是双引号)。 @ JC315,你可以在这些“操作”值的开关语句中显示你的尝试吗? – Squirrel

+0

case':' first = left; second = right; switch(operation){ case'?': third = right; isTernary = true; } if(isTernary == true) \t { \t \t return new ternaryOperator(first,second,third); \t} – JC315

回答

0

好的。我能够将嵌套的if(operation =='?')分解到下面的Switch和case语句中并且它工作正常。 但我仍然需要把主如果(操作==“:”)为开关和case语句

if (operation == ':') 
    { 
     first = left; 
     second = right; 
     left = Operand::parse(); 
     cin >> operation; 
     right = Operand::parse();} 
.. 
... 
.... 
case '?': 
    third = right; 
    return new ternaryOperator(first, second, third);