java实现简单计算器算法--支持浮点数运算
来源:互联网 发布:mysql h localhost 编辑:程序博客网 时间:2024/06/05 05:56
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());
}
}
0 0
- java实现简单计算器算法--支持浮点数运算
- java浮点数运算在计算器上的应用
- 浮点数的相关运算Java实现
- java 实现简单计算器算法
- java 浮点数运算
- Java浮点数运算
- Java的简单类型进行精确的浮点数运算
- Java简单类型进行精确浮点数运算
- Java简单类型进行精确浮点数运算
- Java的简单类型进行精确的浮点数运算
- Java浮点数精确运算
- Java浮点数精确运算
- java 浮点数 运算出错
- 简单计算器实现整数运算
- Java中实现浮点数的快速简单格式化
- 两个数的运算的简单计算器
- C#实现计算器,支持括号运算
- shell中实现浮点数运算
- 斯坦福大学Andrew Ng教授主讲的《机器学习》公开课观后感
- 进击的程序员
- opengl入门系列-Bezier曲线
- 黑马程序员——高新技术——动态代理
- Ajax的技术原理
- java实现简单计算器算法--支持浮点数运算
- HTML5 canvas 可拖拽时钟
- 腾讯qq6.3官方版 v12390 体验版
- 漫谈选人与培训
- https 网站时代来临?
- 创建模式之单列模式
- objective-c 中数据类型之三 数组(NSArray) 高级操作
- 逻辑坐标和设备坐标的区别
- It's the final screen after one more month. Cheers.