java实现简单计算器算法--支持浮点数运算



import java.util.Vector;


public class Test {


static String str1 = "147*258.2+369/3+6.4";
static double value = 0;
static Vector dataVector = null;
static Vector opVector = null;

public static void main(String args[])
{
splitStr();
printVector();
value = doCalc();
System.out.println("");
System.out.println("value = " + value);
}

public static void splitStr()
{
dataVector = new Vector();
opVector = new Vector();

int strLen = str1.length();
int beginIdx = 0;
int endIdx = 0;
int length = 1;
char c1 = ';';
while (beginIdx < strLen)
{
length = 1;
endIdx = beginIdx + 1;
c1 = str1.charAt(beginIdx);
if (c1 == '+'
|| c1 == '-'
|| c1 == '*'
|| c1 == '/')
{
opVector.addElement(str1.substring(beginIdx, endIdx));
beginIdx += length;
}
else 
{
if (endIdx <= (strLen - 1))
{
while (str1.charAt(endIdx) != '+'
&& str1.charAt(endIdx) != '-'
&& str1.charAt(endIdx) != '*'
&& str1.charAt(endIdx) != '/')
{
endIdx++;
length++;
if (endIdx >= strLen)
break;
}
dataVector.addElement(str1.substring(beginIdx, endIdx));
beginIdx += length;
}
else
{
dataVector.addElement(str1.substring(beginIdx, endIdx));
beginIdx += length;
}
}
}
}

public static void printVector()
{
int len = dataVector.size();
int i = 0;
for (; i< len; ++i)
{
if (0 == i)
System.out.print(" " + dataVector.get(i));
else
System.out.print(", " + dataVector.get(i));
}

System.out.println("");
len = opVector.size();
i = 0;
for (; i< len; ++i)
{
if (0 == i)
System.out.print(" " + opVector.get(i));
else
System.out.print(", " + opVector.get(i));
}
}

public static double doCalc()
{
int multiDivOpCnt = 0;
for (int i = 0; i < opVector.size(); ++i)
{
char op = opVector.get(i).toString().charAt(0);
if (op == '*' || op == '/')
multiDivOpCnt++;
}

int opIdx = 0;
char op = ';';
double tmpData = 0;
int opSize = opVector.size();
while (opSize > 0)
{
op = opVector.get(opIdx).toString().charAt(0);
if (multiDivOpCnt > 0)
{
if (op == '*')
{
tmpData = Double.parseDouble(dataVector.get(opIdx).toString()) * Double.parseDouble(dataVector.get(opIdx + 1).toString());
opVector.remove(opIdx);
multiDivOpCnt--;
dataVector.remove(opIdx + 1);
dataVector.remove(opIdx);
dataVector.insertElementAt(Double.toString(tmpData), opIdx);
opSize = opVector.size();
}
else if (op == '/')
{
tmpData = Double.parseDouble(dataVector.get(opIdx).toString()) / Double.parseDouble(dataVector.get(opIdx + 1).toString()); 
opVector.remove(opIdx);
multiDivOpCnt--;
dataVector.remove(opIdx + 1);
dataVector.remove(opIdx);
dataVector.insertElementAt(Double.toString(tmpData), opIdx);
opSize = opVector.size();
}
else
{
opIdx++;
}
}
else 
{
opIdx = 0;
if (op == '+')
{
tmpData = Double.parseDouble(dataVector.get(opIdx).toString()) + Double.parseDouble(dataVector.get(opIdx + 1).toString());
opVector.remove(opIdx);
dataVector.remove(opIdx + 1);
dataVector.remove(opIdx);
dataVector.insertElementAt(Double.toString(tmpData), opIdx);
opSize = opVector.size();
}
else if (op == '-')
{
tmpData = Double.parseDouble(dataVector.get(opIdx).toString()) - Double.parseDouble(dataVector.get(opIdx + 1).toString()); 
opVector.remove(opIdx);
dataVector.remove(opIdx + 1);
dataVector.remove(opIdx);
dataVector.insertElementAt(Double.toString(tmpData), opIdx);
opSize = opVector.size();
}
}

if (0 == multiDivOpCnt)
opIdx = 0;
}

return Double.parseDouble(dataVector.get(0).toString());
}
}

你可能感兴趣的:(JAVA)