D:\CurrentProjects\SearsInterviewCode\src\java\test_classes\Calculate_X_Value_2_Power_Y.java
  1 /*
  2  * To change this template, choose Tools | Templates
  3  * and open the template in the editor.
  4  */
  5 
  6 package test_classes;
  7 
  8 import constants.*;
  9 import java.math.*;
 10 
 11 /**
 12  *
 13  * @author sameldin
 14  */
 15 public class Calculate_X_Value_2_Power_Y
 16 {
 17 
 18     public Calculate_X_Value_2_Power_Y()
 19     {
 20         
 21     }
 22     /*
 23      *
 24      */
 25     public double calculateX2YRecursively(String passedStringValue)
 26     {
 27         double calculatedValue = 0;
 28         double value_X = 0;
 29         double power_Y = 0;
 30         int index   = 0;
 31         int stringLength = passedStringValue.length();  // debugging value
 32         do
 33         {
 34             char tempChar = passedStringValue.charAt(index);
 35             if(tempChar == ')')
 36                 break;
 37 
 38             if(tempChar >= '0' && tempChar <='9')
 39             {
 40                 value_X = tempChar - '0';
 41                 index++;
 42             }
 43             else
 44                 if(tempChar == '^')
 45                 {
 46                     if((index + 1) >= stringLength)
 47                         break;
 48                     char nexttChar  = passedStringValue.charAt(index + 1);
 49                     if(nexttChar == '(')
 50                     {
 51                             if((index + 2) >= stringLength)
 52                                 break;
 53                              String tempString = passedStringValue.substring(index + 2);
 54                              power_Y = calculateX2YRecursively(tempString);
 55                              break; // exist the loop
 56                     }
 57                     else
 58                         if(nexttChar >= '0' && nexttChar <='9')
 59                         {
 60                                  power_Y = nexttChar - '0';
 61                                  break;
 62                         }
 63                 }
 64                 else
 65                     index++;
 66         }
 67         while(index < (passedStringValue.length() - 1));
 68         calculatedValue = value_X;
 69         for(int loop = 1; loop < power_Y; loop++)
 70             calculatedValue = calculatedValue * value_X;
 71         return(calculatedValue);
 72 
 73     }
 74     /*
 75      *
 76      */
 77     public BigDecimal calculateX2YRecursivelyUsingBigD(String passedStringValue)
 78     {
 79         BigDecimal calculatedValue = new BigDecimal(0);
 80         BigDecimal value_X = new BigDecimal(0);
 81         BigDecimal power_Y = new BigDecimal(0);;
 82         int index   = 0;
 83         int stringLength = passedStringValue.length();  // debugging value
 84         do
 85         {
 86             char tempChar = passedStringValue.charAt(index);
 87             if(tempChar == ')')
 88                 break;
 89 
 90             if(tempChar >= '0' && tempChar <='9')
 91             {
 92                 int tempInt = tempChar - '0';
 93                 value_X = new BigDecimal(tempInt);
 94                 index++;
 95             }
 96             else
 97                 if(tempChar == '^')
 98                 {
 99                     if((index + 1) >= stringLength)
100                         break;
101                     char nexttChar  = passedStringValue.charAt(index + 1);
102                     if(nexttChar == '(')
103                     {
104                             if((index + 2) >= stringLength)
105                                 break;
106                              String tempString = passedStringValue.substring(index + 2);
107                              power_Y = calculateX2YRecursivelyUsingBigD(tempString);
108                              break; // exist the loop
109                     }
110                     else
111                         if(nexttChar >= '0' && nexttChar <='9')
112                         {
113                             int tempInt = nexttChar - '0';
114                             power_Y = new BigDecimal(tempInt);
115                                  break;
116                         }
117                 }
118                 else
119                     index++;
120         }
121         while(index < (passedStringValue.length() - 1));
122         //======================================
123         // calcuate the value_w to pwoer_Y
124         BigDecimal tempOne = new BigDecimal(1);
125         calculatedValue = value_X.multiply(tempOne);
126         BigDecimal loopIndex = new BigDecimal(1);
127         System.out.println("loopIndex ======>:\t" + loopIndex);
128         System.out.println("power_Y ======>:\t" + power_Y);
129         System.out.println("loopIndex.compareTo(power_Y) ======>:\t" + loopIndex.compareTo(power_Y));
130 
131         while(loopIndex.compareTo(power_Y) < 0)
132         {
133             calculatedValue = calculatedValue.multiply(value_X);
134             loopIndex = loopIndex.add(new BigDecimal(1));
135         }
136 //        for(int loop = 1; loop < power_Y; loop++)
137 //            calculatedValue = calculatedValue.multiply(value_X);
138         return(calculatedValue);
139 
140     }
141     /*
142      *
143      */
144     public int check4EqualParentheses(String passedValueString)
145     {
146         int numberRightParenthese   = 0;
147         int numberLeftParenthese    = 0;
148 
149         for(int count = 0; count < passedValueString.length(); count ++)
150         {
151             if(passedValueString.charAt(count) == Constants.RIGHT_PARENTHESE_CHAR)
152                 numberRightParenthese++;
153             else
154                 if(passedValueString.charAt(count) == Constants.LEFT_PARENTHESE_CHAR)
155                     numberLeftParenthese++;
156         }
157         // equal zero or not is OK
158         if(numberRightParenthese == numberLeftParenthese)
159             return(Constants.NORMAL_RUN);
160         else
161              return(Constants.ERROR_RUN);
162     }
163     /*
164      *
165      */
166     public static void main (String[] args)
167     {
168         //String localString = "2^(2^(2^(2^1)))";
169         String //localString = "2^(2^(2^(2^2))";
170         localString = "3^4";
171         System.out.println("string ======>:\t" + localString);
172         Calculate_X_Value_2_Power_Y localCalculate_X_Value_2_Power_Y = new Calculate_X_Value_2_Power_Y();
173         double myLong = localCalculate_X_Value_2_Power_Y.calculateX2YRecursively(localString);
174         System.out.println("long ======>:\t" + myLong);
175         System.out.println("====================================================");
176         System.out.println("====================================================");
177 
178         //localString = "3^4";
179         //localString = "2^(3^4)";
180         System.out.println("string ======>:\t" + localString);
181         BigDecimal vyz = localCalculate_X_Value_2_Power_Y.calculateX2YRecursivelyUsingBigD(localString);
182         System.out.println("====== vyz =================:\t" + vyz);
183         System.out.println("========  End ==================");
184     }
185 
186 }
187 
188