简单的字符串运算
来源:互联网 发布:mac word of the day 编辑:程序博客网 时间:2024/05/21 10:09
package com.feiyang.arithmetic;
import java.util.Stack;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @author Qingtao.Xie
* @date Feb 23, 2010
*/
public class PostFixExpression
{
private String str;
PostFixExpression(String str)
{
this.str = str;
}
public float result()
{
Stack<String> numbers = new Stack<String>();
numbers.push(null);
this.str = covertPostEx();
String[] splitStr = this.str.split(" ");
for (int i = 0; i < splitStr.length; i++)
{
if (isNumber(splitStr[i]))
{
numbers.push(splitStr[i]);
}
else
{
float f1 = Float.parseFloat(numbers.pop());
float f2 = Float.parseFloat(numbers.pop());
numbers.push(String.valueOf(calculate(f2, f1, splitStr[i].charAt(0))));
}
}
return Float.parseFloat(numbers.pop());
}
public String covertPostEx()
{
String postEx = "";
Stack<String> ops = new Stack<String>();
ops.push(null);
int i = 0;
while (i < this.str.length())
{
StringBuffer tmp = new StringBuffer();
if (isNumber(String.valueOf(str.charAt(i))))
{
do
{
tmp.append(str.charAt(i));
i++;
}
while (i < this.str.length() && isNumber(String.valueOf(str.charAt(i))));
postEx += tmp.toString();
postEx += " ";
}
else
{
String top = ops.peek();
if (top != null)
{
if (isHigh(top.charAt(0), str.charAt(i)))
{
postEx += ops.pop();
postEx += " ";
}
else
{
ops.push(String.valueOf(str.charAt(i)));
i++;
}
}
else
{
ops.push(String.valueOf(str.charAt(i)));
i++;
}
}
}
while (null != ops.peek())
{
postEx += ops.pop();
postEx += " ";
}
System.out.println(this.str + " postFix=" + postEx);
return postEx;
}
boolean isHigh(int first, int second)
{
boolean ret = true;
if ((Operators.MINUS.getOp() == first || Operators.PLUS.getOp() == first)
&& (Operators.MUTIPLY.getOp() == second || Operators.DIV.getOp() == second))
{
ret = false;
}
return ret;
}
boolean isNumber(String num)
{
Pattern p = Pattern.compile("//d*");
Matcher m = p.matcher(num);
return m.matches();
}
float calculate(float a, float b, char op)
{
switch (op)
{
case '+':
return a + b;
case '-':
return a - b;
case '*':
return a * b;
case '/':
return a / b;
}
return -1;
}
/**
* @param args
*/
public static void main(String[] args)
{
String str = "645-90/4+89/25*3-8";
PostFixExpression post = new PostFixExpression(str);
System.out.println(str + "=" + post.result());
}
}
package com.feiyang.arithmetic;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @author Qingtao.Xie
* @date Feb 23, 2010
*/
public class StackExpression
{
private String str;
private Stack<String> operators = new Stack<String>();
private Stack<String> numerics = new Stack<String>();
public StackExpression(String str)
{
this.str = str;
}
public float result()
{
Object[] nums = splitStr("[+|//-|*|/]");
Object[] ops = splitStr("//d");
String[] dividedStr = new String[nums.length + ops.length];
//compsite the string to an array
for (int i = 0, j = 0; j < nums.length; i++, j++)
{
dividedStr[i] = (String) nums[j];
if (j < ops.length)
{
i = i + 1;
dividedStr[i] = (String) ops[j];
}
}
operators.push(null);
for (int k = 0; k < dividedStr.length;)
{
if (isNumber(dividedStr[k]))
{
numerics.push(dividedStr[k]);
k++;
System.out.println("------number:" + numerics.toString());
}
else
{
String top = operators.peek();
if (null != top
&& isHigh(operators.peek().charAt(0), dividedStr[k].charAt(0)))
{
float f2 = Float.parseFloat(numerics.pop());
float f1 = Float.parseFloat(numerics.pop());
char op = operators.pop().charAt(0);
numerics.push(String.valueOf(calculate(f1, f2, op)));
}
else
{
operators.push(dividedStr[k]);
k++;
}
System.out.println("------op:" + operators.toString());
}
}
while (operators.peek() != null)
{
float f2 = Float.parseFloat(numerics.pop());
float f1 = Float.parseFloat(numerics.pop());
char op = operators.pop().charAt(0);
numerics.push(String.valueOf(calculate(f1, f2, op)));
System.out.println("------number:" + numerics.toString());
System.out.println("------op:" + operators.toString());
}
return Float.parseFloat(numerics.peek());
}
private Object[] splitStr(String pattern)
{
Pattern p = Pattern.compile(pattern);
String[] strsWithEmpty = p.split(this.str);
List<String> list = new ArrayList<String>();
for (int i = 0; i < strsWithEmpty.length; i++)
{
if (strsWithEmpty[i].length() != 0)
{
list.add(strsWithEmpty[i]);
}
}
return list.toArray();
}
boolean isNumber(String num)
{
Pattern p = Pattern.compile("//d*");
Matcher m = p.matcher(num);
return m.matches();
}
boolean isHigh(int first, int second)
{
boolean ret = true;
if ((Operators.MINUS.getOp() == first || Operators.PLUS.getOp() == first)
&& (Operators.MUTIPLY.getOp() == second || Operators.DIV.getOp() == second))
{
ret = false;
}
return ret;
}
float calculate(float a, float b, char op)
{
switch (op)
{
case '+':
return a + b;
case '-':
return a - b;
case '*':
return a * b;
case '/':
return a / b;
}
return -1;
}
/**
* @param args
*/
public static void main(String[] args)
{
// TODO Auto-generated method stub
String str = "645-90/4+89/25*3-8";
StackExpression c = new StackExpression(str);
System.out.println(str + "=" + c.result());
}
}
- 简单的字符串运算
- 异或运算实现简单的字符串加密
- .net上机第一题 - 简单的字符串运算
- 异或运算实现简单的字符串加密和解密
- 基础类型的简单运算,三目运算和字符串的加法
- 简单的阶乘运算。
- 简单的级数运算
- 复数的简单运算
- 简单的加减乘除运算
- 十六进制的简单运算
- 简单的位运算
- 简单的位运算
- js 运算的字符串 判断运算是否合法
- 【2014】【辛星】【php】【秋季】【4】字符串和注释以及简单的变量运算
- [绍棠_swift] swift的简单学习(字符串、4元组、类型转换及运算)
- 字面量常量运算符,switch case 字符串的简单举例,constexpr使用详解
- 字符串的运算符重载
- 字符串的运算符重载
- saas
- HDU2059龟兔赛跑-题解动态规划DP
- 从追MM谈Java的23种设计模式
- sigpending显示当前进程有哪个信号被pending
- 模式对话框的相关知识
- 简单的字符串运算
- LINQ体验(8)--LINQ to SQL语句之Union All/Union/Intersect和Top/Bottom和P...
- How to attach behavior in code behind (Silverlight 3)
- Asp.net
- linux写一个监控日志,发邮件的shell
- poj1226
- oracle删除表空间中一用户及其对应的所有数据
- LINQ体验(9)--LINQ to SQL语句之Insert/Update/Delete操作
- 如何使用HIVE-based Registry