D:\CurrentProjects\SearsInterviewCode\src\java\calc_value_power_using_node\CreateCalcList.java |
1
2
3
4
5
6 package calc_value_power_using_node;
7
8
9
10 @author
11
12 public class CreateCalcList
13 {
14
15 public CreateCalcList()
16 {
17
18 }
19
20 public ValuePowerNode createXYListRecursively(String passedStringValue, ValuePowerNode START)
21 {
22 if(null == passedStringValue)
23 {
24 START = null;
25 return (null);
26 }
27
28 double value_X = 0;
29 double power_Y = 1;
30 int index = 0;
31 int stringLength = passedStringValue.length();
32 START = new ValuePowerNode();
33 do
34 {
35 char tempChar = passedStringValue.charAt(index);
36 if(tempChar == ')')
37 break;
38
39 if(tempChar >= '0' && tempChar <='9')
40 {
41 value_X = tempChar - '0';
42 index++;
43 START.localValuePowerDataObject.setValue_X(value_X);
44 START.nextNode = null;
45 power_Y = 1;
46 }
47 else
48 if(tempChar == '^')
49 {
50 if((index + 1) >= stringLength)
51 break;
52 char nexttChar = passedStringValue.charAt(index + 1);
53 if(nexttChar == '(')
54 {
55 if((index + 2) >= stringLength)
56 break;
57 String tempString = passedStringValue.substring(index + 2);
58 START.nextNode = createXYListRecursively(tempString, START.nextNode);
59 if(START.nextNode != null)
60 power_Y = START.nextNode.localValuePowerDataObject.calculateX2Y();
61 else
62 power_Y = 1;
63 break;
64 }
65 else
66 if(nexttChar >= '0' && nexttChar <='9')
67 {
68 power_Y = nexttChar - '0';
69 break;
70 }
71 }
72 else
73 index++;
74 }
75 while(index < (passedStringValue.length() - 1));
76
77 if(START != null)
78 {
79 START.localValuePowerDataObject.setValue_X(value_X);
80 START.localValuePowerDataObject.setPower_Y(power_Y);
81 }
82 return(START);
83 }
84
85
86
87 public static void main (String[] args)
88 {
89
90 String localString = "2^(2^(2^(2^2))";
91 localString = "2^(2^(2^2))";
92 CreateCalcList localCreateCalcList = new CreateCalcList();
93 ValuePowerNode START = null;
94
95 START = localCreateCalcList.createXYListRecursively(localString, START);
96 if(START != null)
97 {
98 System.out.println("string ======>:\t" + localString);
99 System.out.println("string ======>:\t" + START.localValuePowerDataObject.calculateX2Y());
100 }
101 System.out.println("====================================================");
102 System.out.println("====================================================");
103 }
104
105 }
106
107