"TDD" to achieve simple arithmetic V2.0

Source: Internet
Author: User
Tags arithmetic

1.DoubleStackProcessorTest

Package Devin.wu;

Import Junit.framework.TestCase;

	public class Doublestackprocessortest extends TestCase {private doublestackprocessor DSP;
	protected void SetUp () {DSP = new doublestackprocessor ();

		public void Test_single_num () {dsp.process (' 3 ');
	Checkoperandstack (1,new int[]{3},dsp.dumpoperandstack ());
		public void Test_num_plus () {dsp.process (' 3 ');

		Dsp.process (' + ');
		Checkoperandstack (1,new int[]{3},dsp.dumpoperandstack ());
	Checkoperatorstack (1,new char[]{' + '},dsp.dumpoperatorstack ());
		public void Test_num_plus_num_plus () {dsp.process (' 3 ');
		Dsp.process (' + ');
		Dsp.process (' 2 ');
		
		Dsp.process (' + ');
		Checkoperandstack (1,new int[]{5},dsp.dumpoperandstack ());
	Checkoperatorstack (1,new char[]{' + '},dsp.dumpoperatorstack ());
		public void Test_num_plus_num_multiply () {dsp.process (' 3 ');
		Dsp.process (' + ');
		Dsp.process (' 2 ');
		
		Dsp.process (' * ');
		Checkoperandstack (2,new int[]{3,2},dsp.dumpoperandstack ()); Checkoperatorstack (2, new char[]{' + ', ' * '},dsp.dumpoperatorstack ());
		public void Test_num_multiply_num_multiply () {dsp.process (' 3 ');
		Dsp.process (' * ');
		Dsp.process (' 2 ');
		
		Dsp.process (' * ');
		Checkoperandstack (1,new int[]{6},dsp.dumpoperandstack ());
	Checkoperatorstack (1,new char[]{' * '},dsp.dumpoperatorstack ());
		public void Test_num_plus_num_multiply_num_plus () {dsp.process (' 3 ');
		Dsp.process (' + ');
		Dsp.process (' 2 ');
		Dsp.process (' * ');
		Dsp.process (' 3 ');
		
		Dsp.process (' + ');
		Checkoperandstack (1,new int[]{9},dsp.dumpoperandstack ());
	Checkoperatorstack (1,new char[]{' + '},dsp.dumpoperatorstack ());
		public void Test_num_subtract_num_subtract () {dsp.process (' 3 ');
		Dsp.process ('-');
		Dsp.process (' 2 ');
 
		Dsp.process ('-');
		Checkoperandstack (1,new int[]{1},dsp.dumpoperandstack ());
	Checkoperatorstack (1,new char[]{'-'},dsp.dumpoperatorstack ());
		public void Test_num_plus_num_subtract () {dsp.process (' 3 ');
		Dsp.process (' + ');
		Dsp.process (' 2 '); Dsp.process('-');
		Checkoperandstack (1,new int[]{5},dsp.dumpoperandstack ());
	Checkoperatorstack (1,new char[]{'-'},dsp.dumpoperatorstack ());
		public void Test_num_divide_num_divide () {dsp.process (' 6 ');
		Dsp.process ('/');
		Dsp.process (' 2 ');
 
		Dsp.process ('/');
		Checkoperandstack (1,new int[]{3},dsp.dumpoperandstack ());
	Checkoperatorstack (1,new char[]{'/'},dsp.dumpoperatorstack ()); } private void Checkoperandstack (int expectedsize, int[] expected, int[] actual) {assertequals (Expectedsize, actual
		. length);
		for (int i = 0; i < expectedsize i++) {assertequals (Expected[i], actual[i]); } private void Checkoperatorstack (int expectedsize, char[] expected, char[] actual) {Assertequals (Expectedsize, a
		Ctual.length);
		for (int i = 0; i < expectedsize i++) {assertequals (Expected[i], actual[i]);
 }
	}

}


2.DoubleStackProcessor

Package Devin.wu;
	public class Doublestackprocessor {private static final int plus_or_subtract_level = 1;

	private static final int multiply_or_divide_level = 2;
	private static final int operand_stack_max_size = 3;
	
	private static final int operator_stack_max_size = 2;
	Private int[] Operandstack = new Int[operand_stack_max_size];

	private int idxofoperandstack = 0;
	Private char[] Operatorstack = new Char[operator_stack_max_size];
	
	
	private int idxofoperatorstack = 0;
		public void process (char c) {if (IsDigit (c)) {Processoperand (c));
		else {processoperator (c);
	} private void Processoperand (char c) {Pushoperand (ToInt (c)); private void Processoperator (char c) {while (Isoperatorstacknotempty ()) {if (Islowerlevel (c)) {Calconc
			E ();
			} else {break;
	} pushoperator (c);
	public Boolean isoperatorstacknotempty () {return (Idxofoperatorstack > 0); Private Boolean Islowerlevel (char c) {return(Getoperatorlevel (Peekoperator ()) >= Getoperatorlevel (c)); private int Getoperatorlevel (char operator) {if (operator = = ' + ' | | | | operator = = ') {return plus_or_subtract
		_level;
		else {return multiply_or_divide_level;
	} private char Peekoperator () {return operatorstack[idxofoperatorstack-1];
		public void Calconce () {int roperand = Popoperand ();
		int loperand = Popoperand ();
		int result =-1;
		char operator = Popoperator ();
		if (operator = = ' + ') {result = Loperand + Roperand;
		else if (operator = = ' * ') {result = Loperand * Roperand;
		else if (operator = = ") {result = Loperand-roperand;
		else {result = Loperand/roperand;
	} pushoperand (Result);
	Public Char Popoperator () {return operatorstack[--idxofoperatorstack];
	public int Popoperand () {return operandstack[--idxofoperandstack];
	private void Pushoperator (char c) {operatorstack[idxofoperatorstack++] = C; } priVate Boolean isdigit (char c) {return (c>= ' 0 ' && c<= ' 9 ');
	public void Pushoperand (int operand) {operandstack[idxofoperandstack++] = operand;
	public int Peekoperand () {return operandstack[idxofoperandstack-1];
	private int ToInt (char c) {return (c ' 0 ');
		Public int[] Dumpoperandstack () {int[] copy = new Int[idxofoperandstack];
		for (int i=0;i<idxofoperandstack;i++) {Copy[i] = Operandstack[i];
	return copy;
		Public char[] Dumpoperatorstack () {char[] copy = new Char[idxofoperatorstack];
		for (int i=0;i<idxofoperatorstack;i++) {Copy[i] = Operatorstack[i];
	return copy;
 }

}


3.ExpressionTest

Package Devin.wu;

Import Junit.framework.TestCase;

public class Expressiontest extends TestCase
{
	private Expression exp;

	@Override
	protected void setUp ()
	{
		exp = new Expression ();
	}
	
	public void Test_single_num ()
	{
		
		assertequals (3, Exp.eval ("3"));
	}
	
	public void Test_num_plus_num ()
	{
		assertequals (5,exp.eval ("3+2"));
	}
	
	public void Test_num_plus_num_multiply_num ()
	{
		assertequals (9,exp.eval ("3+2*3"));
	}
	
	public void test_parentheses_num_parentheses ()
	{
		assertequals (3,exp.eval ("(3))");
	}
	
Public	void Test_intermediate_result_exists_multi_digit ()
//	{
//		assertequals (20, Exp.eval ("2* (2+8)"));
//	}
	
}


4.Expression


Package Devin.wu;
		public class Expression {public int eval (String Expression) {int idxofleftparentheses =-1;
			for (int i=0;i<expression.length (); i++) {if (Expression.charat (i) = = ' (') {idxofleftparentheses = i;
				else if (Expression.charat (i) = = ") {int idxofrightparentheses = i;
				int subresult = Getsubexpressionresult (expression, idxofleftparentheses, idxofrightparentheses);
				
				String newexpression = getnewexpression (expression, idxofleftparentheses, idxofrightparentheses, Subresult);
			Return eval (newexpression);
	} return evalwithoutparentheses (expression);  private int Getsubexpressionresult (String expression, int idxofleftparentheses, int idxofrightparentheses) {return
	Evalwithoutparentheses (Expression.substring (idxofleftparentheses+1, idxofrightparentheses)); 
	private string Getnewexpression (string expression, int idxofleftparentheses, int idxofrightparentheses, int subresult) {return Expression.substriNg (0, idxofleftparentheses) + Subresult + expression.substring (idxofrightparentheses+1);
		private int evalwithoutparentheses (String expression) {doublestackprocessor DSP = new Doublestackprocessor ();
		for (int i=0;i<expression.length (); i++) {dsp.process (Expression.charat (i));
		while (Dsp.isoperatorstacknotempty ()) {dsp.calconce ();
	return Dsp.peekoperand ();
 }

}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.