一.项目简介
二.功能架构图
三.前期调查及参考代码
四.核心代码
五.项目运行图片
六.git提交记录
一.项目简介:
设计一个计算器,其能够:
1)由用户输入一个简单的四则运算表达式,求出其计算结果后显示。
2)特殊数学函数,如:绝对值、取整、三角函数、倒数、平方根、平方、立方等。
3)对一定范围内的数字将其转换成中文货币。
二.功能架构图:
三.前期调查及参考代码:
我们参考了他们监听器功能的编写设计和计算表达式程序功能的编写设计以及界面设计。
计算数学表达式的程序设计_设计数学运算程序_陈圆媛的博客-CSDN博客
四.核心代码:
计算四则表达式
package model;
import java.util.ArrayList;
import java.util.Stack;
public class Calculate {
public static String calc(String expression){
int index= 0,i=0,flag=0;
Stack<String> op = new Stack<String>();
String[] Operator = {"+","-"," ","*","/"," ","(",")"};
ArrayList<String> digit = new ArrayList<String>();
for(i = 0;i<expression.length();i++)
{
if(expression.charAt(i)=='+'||expression.charAt(i)=='-'||expression.charAt(i)=='*'||expression.charAt(i)=='/')
{
if(index<i)
digit.add(expression.substring(index, i));
index = i+1;
if(op.isEmpty())
{
op.push(expression.valueOf(expression.charAt(i))); //字符串形式
}
else { //判断优先集
int index1 = -1,index2 = -1;
for(int j = 0; j<Operator.length;j++)
{
if(op.peek().equals(Operator[j]))
{
if(j==6)
index1 = -2;
else
index1 = j;
}
if(expression.valueOf(expression.charAt(i)).equals(Operator[j]))
index2 = j;
}
if(index2-index1<2&&flag==0) //同优先集
{
digit.add(op.pop());
op.push(expression.valueOf(expression.charAt(i)));
}
else if(index2-index1>=2&&flag==0){ //优先集大
op.push(expression.valueOf(expression.charAt(i)));
}
else if(index2-index1<2&&flag==1)
{
digit.add(op.pop());
op.push(expression.valueOf(expression.charAt(i)));
}
else if(index2-index1>=2&&flag==1)
{
op.push(expression.valueOf(expression.charAt(i)));
}
}
}
else if(expression.charAt(i)=='(')
{
flag=1;
index = i+1;
op.push("(");
}
else if(expression.charAt(i)==')')
{
flag = 0;
digit.add(expression.substring(index, i));
index = i+1;
while(op.peek()!="(")
{
digit.add(op.pop());
}
op.pop();
}
}
if(index<i)
digit.add(expression.substring(index, i));
while(!op.isEmpty())
{
digit.add(op.pop());
}
Stack<String> operation = new Stack<String>();
System.out.println(digit.toString());
System.out.println(operation);
for( i = 0;i<digit.size();i++)
{
double num1,num2;
String judge = digit.get(i);
if(digit.get(i).equals("+"))
{
num1 = Double.valueOf(operation.pop());
num2 = Double.valueOf(operation.pop());
operation.push(String.valueOf(num1+num2));
}
else if(digit.get(i).equals("-"))
{
num1 = Double.valueOf(operation.pop());
num2 = Double.valueOf(operation.pop());
operation.push(String.valueOf(num2-num1));
}
else if(digit.get(i).equals("*"))
{
num1 = Double.valueOf(operation.pop());
num2 = Double.valueOf(operation.pop());
operation.push(String.valueOf(num1*num2));
}
else if(digit.get(i).equals("/"))
{
num1 = Double.valueOf(operation.pop());
num2 = Double.valueOf(operation.pop());
operation.push(String.valueOf(num2/num1));
}
else {
operation.push(digit.get(i));
}
}
System.out.println(operation.peek());
return operation.pop();
}
}
中文转换
package model;
public class Transform {
public static String transNumToCN(String text){
String [] cnNum = {"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"};
String [] cnCount = {"万","仟", "佰","拾"};
String cnNumber = null;
double n = 0.00;
try{
n = Double.parseDouble(text);
}catch(NumberFormatException ee){cnNumber = "非法数字!"; return cnNumber;}
if( n>=0.00 && n<100000.00){
double m = n;
try{
m = Double.parseDouble(text);
}catch(NumberFormatException ee){cnNumber = "系统错误!"; return cnNumber;}
int m1 = (int) ((m*100)%100);
int m2 = (int) Math.floor(m);
int num [] = new int [7];
num[0] = m2/10000;
num[1] = (m2%10000)/1000;
num[2] = (m2%1000)/100;
num[3] = (m2%100)/10;
num[4] = m2%10;
num[5] = m1/10;
num[6] = m1%10;
int index = 0;
for(int i = 0;i<7;i++) {
if(num[i]!=0) {
index = i;
break;
}
}
System.out.println(index);
if(index == 0)
cnNumber = (cnNum[num[0]]+cnCount[0]+cnNum[num[1]]+cnCount[1]
+cnNum[num[2]]+cnCount[2]+cnNum[num[3]]+cnCount[3]+cnNum[num
[4]]+"元"+cnNum[num[5]]+"角"+cnNum[num[6]]+"分");
if(index == 1)
cnNumber = (cnNum[num[1]]+cnCount[1]+cnNum[num[2]]+cnCount[2]+cnNum[
num[3]]+cnCount[3]+cnNum[num[4]]+"元"+cnNum[num[5]]+"角"+cnNum
[num[6]]+"分");
if(index == 2)
cnNumber = (cnNum[num[2]]+cnCount[2]+cnNum[num[3]]+cnCount[3]+cnNum[
num[4]]+"元"+cnNum[num[5]]+"角"+cnNum[num[6]]+"分");
if(index == 3)
cnNumber = (cnNum[num[3]]+cnCount[3]+cnNum[num[4]]+"元"+cnNum[num[5]]
+"角"+cnNum[num[6]]+"分");
if(index == 4)
cnNumber = (cnNum[num[4]]+"元"+cnNum[num[5]]+"角"+cnNum[num[6]]+"分");
if(index == 5)
cnNumber = (cnNum[num[5]]+"角"+cnNum[num[6]]+"分");
if(index == 6)
cnNumber = (cnNum[num[6]]+"分");
}
else if(n == 100000.00){
cnNumber = "拾万元";
}
else {
cnNumber = "超出范围!";
}
return cnNumber;
}
}
五.项目运行图片:
1、特殊函数计算:
2、四则表达式运算:
3、中文转换:
六.git提交记录: