概述
四则运算算法:
Write a program that reads an expression as input and print out the result. Only integers and operators below are allowed in the expression:
+ - * / % ( )
Your program should be able to deal with multiple cases of input.
思路一:
- 从左起开始,找到最后一个左括号和与之相匹配的右括号
- 把”-“替换为”+-“
- 对无括号的运算式执行:
a. 从左开始找到第一个”/”运算符及其两边的数(split函数可以切分)
b. 执行运算并将结果替换原式
c. 重复a,b操作,如果没有”/”,依次查询”*”和”+” - 重复123,直至得出结果
import java.util.Scanner;
import java.util.Stack;
public class hello {
private String valid_chars;
private String left_braces;
private String right_braces;
private Stack<Character> brace;
private int top;
private String stCalc, x;
/*
stCalc and x are string variables to be computed recursively. See below
*/
public hello(String s)
{
valid_chars = s;
left_braces = "{[(";
right_braces = "}])";
stCalc = " ";
x = " ";
brace = new Stack<Character>();
}
public int calc(String st)
{
st = st.replaceAll("\s","");
if(st.charAt(0)== '-')
{
st = ("0").concat(x);
}
x = st.replaceAll("-","+-");
char[] carr = x.toCharArray();
int len = carr.length, i = 0;
String symbol = "";
int indicate = -5;
while(i < len)
{
int m = valid_chars.indexOf(carr[i]);
int b = left_braces.indexOf(carr[i]);
if(m == -1)
{
System.out.println("Error: "+i+"-th element is invalid");
return 0;
}
if(b != -1)
{
brace.push(carr[i]);
}
i++;
}
int left = 0, right=carr.length;
char tps =' ', right_tps= ' ';
int match_index = -5;
String [] opString = {"/", "*", "+"};
// Recursively go through string and do all bracket operations
while (brace.empty()==false) {
carr = x.toCharArray();
left= findLeft(carr);
tps = brace.peek();
match_index = left_braces.indexOf(tps);
right_tps = right_braces.charAt(match_index);
right = x.indexOf(right_tps);
stCalc = x.substring(left+1,right);
for(int j =0; j < opString.length; j++)
{
symbol = opString[j];
indicate = stCalc.indexOf(symbol);
stCalc = rep(symbol, x, indicate);
}
x = x.substring(0,left) + stCalc + x.substring(right+1);
carr = x.toCharArray();
left = findLeft(carr);
right_tps = right_braces.charAt(match_index);
right = x.indexOf(right_tps);
brace.pop();
}
stCalc = x;
for(int j = 0; j< opString.length; j++)
{
symbol = opString[j];
indicate = stCalc.indexOf(symbol);
boolean flag = true;
while(indicate != -1){
indicate = munger(indicate, symbol, flag);
}
}
int calculation = Integer.parseInt(stCalc);
return calculation;
}
public String rep(String symbol, String x, int indicate)
{
boolean flag = false;
indicate = stCalc.indexOf(symbol);
while(indicate != -1){
indicate = munger(indicate, symbol, flag);
}
if(stCalc == "N"){
return " ";
}
return stCalc;
}
public int munger(int indicate, String symbol, boolean flag)
{
int i=0;
String s1 = stCalc.substring(0,indicate);
String [] items1 = s1.split("\+|\*|\/");
float [] results1 = new float[items1.length];
for (i = 0; i < items1.length; i++) {
results1[i] = Float.valueOf(items1[i]);
}
float c1 = results1[results1.length-1];
String s2 = stCalc.substring(indicate+1);
String [] items2 = s2.split("\+|\*|\/");
float[] results2 = new float[items2.length];
for (i = 0; i < items2.length; i++) {
results2[i] = Float.parseFloat(items2[i]);
}
float c2 = results2[0];
int intc1 = (int)c1;
int intc2 = (int)c2;
String sc1 = Integer.toString(intc1);
String sc2 = Integer.toString(intc2);
int len_sc1 = sc1.length();
int len_sc2 = sc2.length();
stCalc = calcNoBrace(intc1, intc2, symbol);
if(flag == true){
String left_side = x.substring(0,indicate- len_sc1);
String right_side = x.substring(indicate + len_sc2 +1 );
x = left_side + stCalc + right_side;
stCalc = x;
}
indicate = stCalc.indexOf(symbol);
return indicate;
}
public int findLeft( char[] carr)
{
int left = -1;
for( int i = 0; i< carr.length; i++)
{
int b = left_braces.indexOf(carr[i]);
if(b != -1)
left = i;
}
return left;
}
public String calcNoBrace(int calc1, int calc2, String symbol)
{
int res = 0;
if(symbol == "/")
{
if(calc2 == 0){
System.out.println("Error: division by 0 condition");
return " ";
}
if(calc1 ==0 && calc2 == 0)
{
System.out.println("Error: undefined condition");
return " ";
}
res = calc1/calc2;
}
if(symbol == "*")
{
res = calc1*calc2;
}
if(symbol == "+")
{
res = calc1 + calc2;
}
String resSt = Integer.toString(res);
return resSt;
}
public void test() {
Scanner sc = new Scanner(System.in);
String test;
while (true) {
test = sc.nextLine();
int anc = calc(test);
System.out.println("The answer is "+anc);
}
}
public static void main(String [] args)
{
String s ="0123456789+-/*(){}[]"; // Set what entities are to be
hello ob = new hello(s);
ob.test();
}
}
最后
以上就是凶狠咖啡为你收集整理的四则运算 java的全部内容,希望文章能够帮你解决四则运算 java所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复