To Integer Stack
by Isai Damier, Android Engineer @ Google
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | /******************************************************************* * Author: Isai Damier * Title: LargeInteger.java * Project: geekviewpoint * Package: datastructure * * Description: Normally modern computers cannot handle very large * numbers. So the task here is to create a datastructure that * can handle numbers of perhaps infinite size. And to prove that * the datastructure works, included are arithmetic operators. * * Naturally the numbers are represented as Strings. And to * perform addition and subtraction, stacks are used as the media. * Finally, as any trusty calculator, this datastructure returns * erroneous values for erroneous inputs and correct values for * correct inputs. This is to say, if an input string does not * represent a valid decimal integer, then the output string * will be bogus. ******************************************************************/ import java.util.Stack; public class LargeInteger { private String number; public LargeInteger(String number) { this .number = number; } //constructor /*************************************************************** * Function: toIntegerStack * Inputs: @param n * Output: Stack<Integer> * * Description: This auxiliary function converts a String into a * stack of integers. The function makes no checks for erroneous * values. The user will get what the user asks for. * **************************************************************/ private Stack<Integer> toIntegerStack(String n) { Stack<Integer> stack = new Stack<Integer>(); for ( char c : n.toCharArray()) { //stack.push(c - 48);//ASCII stack.push(Integer.parseInt(c+ "" )); } return stack; } } |