本文整理汇总了C++中Expression::getError方法的典型用法代码示例。如果您正苦于以下问题:C++ Expression::getError方法的具体用法?C++ Expression::getError怎么用?C++ Expression::getError使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Expression
的用法示例。
在下文中一共展示了Expression::getError方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: runSilent
/* performs and outputs the choice expression in the output style */
void runSilent(std::string choice,std::string strExpr,std::string output) {
Expression::Notation notation = Expression::NOTATION_INFIX;
Expression* expression = NULL;
bool isValidSelection = true;
if(choice == "infix"){
notation = Expression::NOTATION_INFIX;
}
else if(choice == "postfix"){
notation = Expression::NOTATION_POSTFIX;
}
else if(choice == "prefix"){
notation = Expression::NOTATION_PREFIX;
}
else{
isValidSelection = false;
}
if(isValidSelection){
expression = new Expression(strExpr,notation);
if(!expression->hasError()){
if(output == "infix")
cout << expression->getInfix() << endl;
else if(output == "postfix")
cout << expression->getPostfix() << endl;
else if(output == "prefix")
cout << expression->getPrefix() << endl;
else if(output == "value"){
try{
cout << expression->evaluate() << endl;
}
catch (runtime_error& e)
{
cout << e.what() << endl;
}
}
else
cout << "invalid output format." << endl;
}
else{
cout << expression->getError() << endl;
}
}
else{
cout << "The selection you entered was not valid." << endl;
}
delete expression;
}
示例2: runNormal
/*
Ask the user what type of expression
Let the user enter an expression
Display the conversions and value, or an error
*/
void runNormal(){
string choice;
string expressionStr;
Expression *expression = NULL;
Expression::Notation notation = Expression::NOTATION_INFIX;
bool isValidSelection = true;
cout << "Select the notation of your expression:" << endl;
cout << "1) Infix (Traditional)." << endl;
cout << "2) Postfix (Reverse Polish)." << endl;
cout << "3) Prefix (Polish)." << endl;
cout << endl;
getline(cin,choice);
cout <<endl;
if(choice == "1" || choice == "infix" || choice == "Infix"){
notation = Expression::NOTATION_INFIX;
}
else if(choice == "2" || choice == "postfix" || choice == "Postfix"){
notation = Expression::NOTATION_POSTFIX;
}
else if(choice == "3" || choice == "prefix" || choice == "Prefix"){
notation = Expression::NOTATION_PREFIX;
}
else{
isValidSelection = false;
}
if(isValidSelection){
if(notation == Expression::NOTATION_INFIX){
cout << "Enter your expression in infix notation:" << endl;
}
else if(notation == Expression::NOTATION_POSTFIX){
cout << "Enter your expression in postfix notation:" << endl;
}
else if(notation == Expression::NOTATION_PREFIX){
cout << "Enter your expression in prefix notation:" << endl;
}
getline(cin,expressionStr);
cout << endl;
expression = new Expression(expressionStr,notation);
if(!expression->hasError()){
string outputType = "infix";
if(notation == Expression::NOTATION_INFIX)
{
cout <<
"Would you like to convert to prefix or postfix?" << endl;
getline(cin,outputType);
if(outputType != "postfix")
outputType == "prefix";
cout << endl;
}
if(outputType == "infix")
cout << "Infix: " << expression->getInfix() << endl;
else if(outputType == "postfix")
cout << "Postfix: " << expression->getPostfix() << endl;
else
cout << "Prefix: " << expression->getPrefix() << endl;
try{
cout << "Result: " << expression->evaluate() << endl;
}
catch (runtime_error& e)
{
cout << e.what() << endl;
}
}
else{
cout << "There was a problem ";
cout << "with the expression you entered:" << endl;
cout << expression->getError() << endl;
}
}
else{
cout << "The selection you entered was not valid." << endl;
}
delete expression;
}