Question Details

[solution]: What do you mean by Built-to-change organizations? Discuss in


Answer download
More Details:

Need help creating a calculator program using stacks with infix and postfix expressions.


/*

 

* The shell of the class, to be completed as part of CSC115 Assignment 3 :

 

Calculator.

 

*/

 

import java.util.regex.Pattern;

 

import java.util.regex.Matcher;

 

/*

 

* NOTE TO STUDENT:

 

* Fill in any of the parts that have the comment:

 

/******COMPLETE *****

 

* Do not change method headers or code that has been supplied.

 

* All methods must be properly commented.

 

* Please delete all messages to you, incuding this one, before submitting.

 

*/

 

public class ArithExpression {

 

private TokenList postfixTokens;

 

private TokenList infixTokens;

 

/**

 

* Sets up a legal standard Arithmetic expression.

 

* The only parentheses accepted are "(" and ")".

 

* @param word An arithmetic expression in standard infix order.

 

*

 

An invalid expression is not expressly checked for, but will

 

not be

 

*

 

successfully evaluated, when the <b>evaluate</b> method is

 

called.

 

* @throws InvalidExpressionException if the expression cannot be properly

 

parsed,

 

*

 

or converted to a postfix expression.

 

*/

 

public ArithExpression(String word) {

 

if (Tools.isBalancedBy("()",word)) {

 

tokenizeInfix(word);

 

infixToPostfix();

 

} else {

 

throw new InvalidExpressionException("Parentheses

 

unbalanced");

 

}

 

}

 


 

to

 


 

/*

 

*

 

*

 

*

 

*

 


 

A private helper method that tokenizes a string by separating out

 

any arithmetic operators or parens from the rest of the string.

 

It does no error checking.

 

The method makes use of Java Pattern matching and Regular expressions

 


 

* isolate the operators and parentheses.

 

* The operands are assumed to be the substrings delimited by the

 

operators and parentheses.

 

* The result is captured in the infixToken list, where each token is

 

* an operator, a paren or a operand.

 

* @param express The string that is assumed to be an arithmetic

 

expression.

 

*/

 

private void tokenizeInfix(String express) {

 

infixTokens = new TokenList(express.length());

 

// regular expression that looks for any operators or parentheses.

 

Pattern opParenPattern = Pattern.compile("[-+*/^()]");

 

Matcher opMatcher = opParenPattern.matcher(express);

 


 

String matchedBit, nonMatchedBit;

 

int lastNonMatchIndex = 0;

 

String lastMatch = "";

 

// find all occurrences of a matched substring

 

while (opMatcher.find()) {

 

matchedBit = opMatcher.group();

 

// get the substring between matches

 

nonMatchedBit = express.substring(lastNonMatchIndex,

 

opMatcher.start());

 

nonMatchedBit = nonMatchedBit.trim(); //removes outside

 

whitespace

 

// The very first '-' or a '-' that follows another operator

 

is considered a negative sign

 

if (matchedBit.charAt(0) == '-') {

 

if (opMatcher.start() == 0 ||

 

!lastMatch.equals(")") &&

 

nonMatchedBit.equals("")) {

 

continue; // ignore this match

 

}

 

}

 

// nonMatchedBit can be empty when an operator follows a ')'

 

if (nonMatchedBit.length() != 0) {

 

infixTokens.append(nonMatchedBit);

 

}

 

lastNonMatchIndex = opMatcher.end();

 

infixTokens.append(matchedBit);

 

lastMatch = matchedBit;

 

}

 

// parse the final substring after the last operator or paren:

 

if (lastNonMatchIndex < express.length()) {

 

nonMatchedBit =

 

express.substring(lastNonMatchIndex,express.length());

 

nonMatchedBit = nonMatchedBit.trim();

 

infixTokens.append(nonMatchedBit);

 

}

 

}

 

/**

 

* Determines whether a single character string is an operator.

 

* The allowable operators are {+,-,*,/,^}.

 

* @param op The string in question.

 

* @return True if it is recognized as a an operator.

 

*/

 

public static boolean isOperator(String op) {

 

switch(op) {

 

case "+":

 

case "-":

 

case "/":

 

case "*":

 

case "^":

 

return true;

 

default:

 

return false;

 

}

 

}

 

/**

 

* NOTE TO STUDENT: These requirements don't show up in the

 

* java documentation because it is a private method.

 

* It is private because it directly accesses the data fields.

 

*

 

* A private method that initializes the postfixTokens data field.

 


 

* It takes the information from the infixTokens data field.

 

* If, during the process, it is determined that the expression is

 

invalid,

 

* an InvalidExpressionException is thrown.

 

* Note that since the only method that calls this method is the

 

constructor,

 

* the Exception is propogated through the constructor.

 

*/

 

private void infixToPostfix() {

 

/******COMPLETE *****/

 

}

 

public String getInfixExpression() {

 

/******COMPLETE *****/

 

}

 

public String getPostfixExpression() {

 

/******COMPLETE *****/

 

}

 

public double evaluate() {

 

/******COMPLETE *****/

 

}

 

public static void main(String args) {

 

/******COMPLETE *****/

 

}

 

}

 


 

 


Solution details:
STATUS
Answered
QUALITY
Approved
ANSWER RATING

This question was answered on: Dec 18, 2020

PRICE: $18.50

Solution~00031148033837.zip (25.37 KB)

Buy this answer for only: $18.50

This attachment is locked

We have a ready expert answer for this paper which you can use for in-depth understanding, research editing or paraphrasing. You can buy it or order for a fresh, original and plagiarism-free copy (Deadline assured. Flexible pricing. TurnItIn Report provided)

Pay using PayPal (No PayPal account Required) or your credit card . All your purchases are securely protected by .
SiteLock

About this Question

STATUS

Answered

QUALITY

Approved

DATE ANSWERED

Dec 18, 2020

EXPERT

Tutor

ANSWER RATING

GET INSTANT HELP/h4>

We have top-notch tutors who can do your essay/homework for you at a reasonable cost and then you can simply use that essay as a template to build your own arguments.

You can also use these solutions:

  • As a reference for in-depth understanding of the subject.
  • As a source of ideas / reasoning for your own research (if properly referenced)
  • For editing and paraphrasing (check your institution's definition of plagiarism and recommended paraphrase).
This we believe is a better way of understanding a problem and makes use of the efficiency of time of the student.

NEW ASSIGNMENT HELP?

Order New Solution. Quick Turnaround

Click on the button below in order to Order for a New, Original and High-Quality Essay Solutions. New orders are original solutions and precise to your writing instruction requirements. Place a New Order using the button below.

WE GUARANTEE, THAT YOUR PAPER WILL BE WRITTEN FROM SCRATCH AND WITHIN YOUR SET DEADLINE.

Order Now