标签:
团队:
郭志豪:http://www.cnblogs.com/gzh13692021053/
杨子健:http://www.cnblogs.com/yzj666/
刘森松:http://www.cnblogs.com/lssh/
谭宇森:http://www.cnblogs.com/dalaotan/
GITHUB地址:https://github.com/Sub-key/1/tree/master/app
app功能实现
主要类代码:
CaculateEx.class源代码以及说明如下:
package com.lsf.cal.calc;
import java.util.Stack;
public class CaculateEx {
public String suffix_expression(String expression)//中缀表达式转换后缀表达式(逆波兰式)
{
//Stack<Double> s2=new Stack<Double>();//存放数字栈
Stack<Object> s3=new Stack<Object>();//存放结果栈
Stack<Character> s4=new Stack<Character>();//存放操作符栈
int len=expression.length();//
char c1;
double number;
int m,n=-1;
for(int i=0;i<len;i++)
{
c1=expression.charAt(i);
if(isOprator(c1)||(i==len-1))//如果是运算符,将前面的数数字入s3栈,操作符入s4栈
{
if(i==len-1&&(!isOprator(c1)))//当最后一位且不是操作符时,将前面的数压栈
m=i+1;
else
m=i;
//操作数入栈,向前遍历到下一个运算符,将中间的字符串转化为double
for(int j=i-1;j>=0;j--)
{
if(isOprator(expression.charAt(j)))
{
n=j;
break;
}
n=j-1;
}
if(m!=n+1)//只有当这两个值不等时中间才会有操作数
{
number=Double.parseDouble(expression.substring(n+1,m));
s3.push(number);
}
//运算符入栈
if(i==0&&(c1!=‘(‘))//当表达式第一个字符就为运算符且不是左括号时,返回表达式错误
{
return "表达式错误!";
}
else if(isOprator(c1))//且是操作符时
{
while(true)
{
if(s4.isEmpty()||s4.peek()==‘(‘||c1==‘(‘)//如果栈为空或者栈顶元素为(或者c1为(时,则直接将运算符压入栈内
{
s4.push(c1);
break;
}
else if(c1==‘)‘)//当c1为)时,依次弹出s4中的运算符并压入s3,直到(,舍弃这一对括号
{
while(s4.peek()!=‘(‘)
{
s3.push(s4.pop());
if(s4.isEmpty())//弹出所有不为左括号之后堆栈为空,则表达式不合法
{
return "缺少左括号";
}
}
s4.pop();//弹出(
break;
}
else
{
if(priorityCompare(c1,s4.peek())==1)//判断优先级,优先级高入栈,优先级低将栈顶运算符压入s3
{
s4.push(c1);
break;
}
else
{
s3.push(s4.pop());
}
}
}
}
}
else
continue;
}
while(!s4.isEmpty())//表达式结束后,依次将s4剩下的运算符压入s3
{
if((char)s4.peek()==‘(‘)
return "缺少右括号";
s3.push(s4.pop());
}
return count_result(s3);
}
private int priorityCompare(char c1,char c2)
{
switch(c1)
{
case ‘+‘:
case ‘-‘:
return (c2 == ‘*‘ || c2 == ‘/‘ ? -1 : 0);
case ‘*‘:
case ‘/‘:
return (c2 == ‘+‘ || c2 == ‘-‘ ? 1 : 0);
}
return 1;
}
//判断字符是否为运算符,是为真,不是为假
private boolean isOprator(Object c) {
// TODO Auto-generated method stub
try
{
char c1=(char)c;
if(c1==‘+‘||c1==‘-‘||c1==‘*‘||c1==‘/‘||c1==‘(‘||c1==‘)‘)
return true;
}
catch (Exception e) {
// TODO: handle exception
return false;
}
return false;
}
private String count_result(Stack<Object> ob) {
// TODO Auto-generated method stub
Stack<Object> s1=new Stack<Object>();//后缀表达式栈
Stack<Double> s2=new Stack<Double>();//操作数栈
//char c1;
// Stack<Character> s3=new Stack<Character>();//操作符栈
while(!ob.isEmpty())//将传入的栈逆序压入
{
s1.push(ob.pop());
}
while(!s1.isEmpty())
{
if(!isOprator(s1.peek()))//遇到非操作符,压入s2栈
{
s2.push((Double)s1.pop());
}
else
{
s2.push(cout(s2.pop(),s2.pop(),(char)s1.pop()));
}
}
return Double.toString(s2.peek());
}
private Double cout(double s1,double s2,char s3)
{
double result=0;
switch(s3)
{
case ‘+‘:
result=s1+s2;
break;
case ‘-‘:
result=s1-s2;
break;
case ‘*‘:
result=s1*s2;
break;
case ‘/‘:
result=s1/s2;
break;
}
return result;
}
}
MainActivity.class源代码如下:
4、运package com.lsf.cal.calc;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends ActionBarActivity implements View.OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button caculateButton=(Button)findViewById(R.id.button);
caculateButton.setOnClickListener(this);
Button clearButton=(Button)findViewById(R.id.clear);
clearButton.setOnClickListener(this);
}
@Override
public void onClick(View v) {
EditText editText = (EditText) findViewById(R.id.text);
if(v.getId()==R.id.button) {
CaculateEx caculateEx = new CaculateEx();
String expreString = editText.getText().toString();
expreString = caculateEx.suffix_expression(expreString);
editText.setText(expreString);
}else{
editText.setText("");
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
标签:
原文地址:http://www.cnblogs.com/gzh13692021053/p/5586093.html