JAVA程序。一个利用自行设计的堆栈实现四则运算(不考虑小括号),帮忙看下错在哪里了?)
packagemyutil.calc;importjava.io.BufferedReader;importjava.io.IOException;importjava....
package myutil.calc;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Calc
{
public static void main(String[] args)
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("请输入运算表达式:");
try
{
String expression = br.readLine();
Computting com = new Computting(expression);
int result = com.rule();
System.out.println("运算结果为:"+result);
} catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
package myutil.calc;
import java.lang.Character;
public class Computting
{
//private static final Object (String) = null;
//运算表达式
private String expression;
//运算符栈
public MyStack symbol;
//运算数栈
public MyStack num;
int t;
public Computting(String expression)
{
super();
this.expression = expression;
}
//运算规则
public int rule()
{
String s = "";
int result;
//解析运算表达式 并计算
for(int i=0;i<expression.length();i++)
{
char ge = expression.charAt(i);
switch (ge)
{
case '+':
{
System.out.println('+');
symbol.push(Oparator.ADD );
break;
}
case '-':
{
System.out.println('-');
symbol.push(Oparator.DEC );
break;
}
case '*':
{
System.out.println('*');
symbol.push(Oparator.RIDE);
break;
}
case '/':
{
System.out.println('/');
symbol.push(Oparator.DIV);
break;
}
}
if (ge!='+'&&ge!='-'&&ge!='*'&&ge!='/')
{
s = s+ge;
}
else
{
num.push(s);
System.out.println(s);
s = "";
}
if (!symbol.isEmpty())
{
String s1 = (String) num.pop();
int oprands1 = Integer.parseInt(s1);
int resul = (int)((Oparator) symbol.pop()).comput(oprands1,getOprands2(i));
if(((Oparator) symbol.pop()).getPro()==1)
{
symbol.pop();
}
num.push(resul);
i = i + t;
}
}
result = Integer.parseInt((String) num.peek());
return result;
}
public int getOprands2(int i)
{
String s="";
int Oprands2;
t = i;
if(((Oparator) symbol.pop()).getPro()==1)
{
t = t+1;
while(expression.charAt(t)!='+'&&expression.charAt(t)!='-'&&expression.charAt(t)!='*'&&expression.charAt(t)!='/')
{
s = s+expression.charAt(i+1);
}
Oprands2 = Integer.parseInt(s);
}
else
{
Oprands2 = Integer.parseInt((String) num.pop());
}
return Oprands2;
}
}
import java.util.ArrayList;
import java.util.LinkedList;
public class MyStack{}
package myutil.calc;
public class Oparator{}
报错是Computting的 if (!symbol.isEmpty()) 空指针异常
Exception in thread "main" java.lang.NullPointerException
at myutil.calc.Computting.rule(Computting.java:71)
at myutil.calc.Calc.main(Calc.java:17) 展开
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Calc
{
public static void main(String[] args)
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("请输入运算表达式:");
try
{
String expression = br.readLine();
Computting com = new Computting(expression);
int result = com.rule();
System.out.println("运算结果为:"+result);
} catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
package myutil.calc;
import java.lang.Character;
public class Computting
{
//private static final Object (String) = null;
//运算表达式
private String expression;
//运算符栈
public MyStack symbol;
//运算数栈
public MyStack num;
int t;
public Computting(String expression)
{
super();
this.expression = expression;
}
//运算规则
public int rule()
{
String s = "";
int result;
//解析运算表达式 并计算
for(int i=0;i<expression.length();i++)
{
char ge = expression.charAt(i);
switch (ge)
{
case '+':
{
System.out.println('+');
symbol.push(Oparator.ADD );
break;
}
case '-':
{
System.out.println('-');
symbol.push(Oparator.DEC );
break;
}
case '*':
{
System.out.println('*');
symbol.push(Oparator.RIDE);
break;
}
case '/':
{
System.out.println('/');
symbol.push(Oparator.DIV);
break;
}
}
if (ge!='+'&&ge!='-'&&ge!='*'&&ge!='/')
{
s = s+ge;
}
else
{
num.push(s);
System.out.println(s);
s = "";
}
if (!symbol.isEmpty())
{
String s1 = (String) num.pop();
int oprands1 = Integer.parseInt(s1);
int resul = (int)((Oparator) symbol.pop()).comput(oprands1,getOprands2(i));
if(((Oparator) symbol.pop()).getPro()==1)
{
symbol.pop();
}
num.push(resul);
i = i + t;
}
}
result = Integer.parseInt((String) num.peek());
return result;
}
public int getOprands2(int i)
{
String s="";
int Oprands2;
t = i;
if(((Oparator) symbol.pop()).getPro()==1)
{
t = t+1;
while(expression.charAt(t)!='+'&&expression.charAt(t)!='-'&&expression.charAt(t)!='*'&&expression.charAt(t)!='/')
{
s = s+expression.charAt(i+1);
}
Oprands2 = Integer.parseInt(s);
}
else
{
Oprands2 = Integer.parseInt((String) num.pop());
}
return Oprands2;
}
}
import java.util.ArrayList;
import java.util.LinkedList;
public class MyStack{}
package myutil.calc;
public class Oparator{}
报错是Computting的 if (!symbol.isEmpty()) 空指针异常
Exception in thread "main" java.lang.NullPointerException
at myutil.calc.Computting.rule(Computting.java:71)
at myutil.calc.Calc.main(Calc.java:17) 展开
展开全部
public class Box {
private int length;
private int width;
private int height;
private int area;
private int volume;
//构造方法,通过长、宽、高计算出面积和体积
public Box (int length, int width, int height) {
this.length = length;
this.width = width;
this.height = height;
area = 2 * (length * width
+ length * height
+ width * height);
volume = length * width * height;
}
//显示面积
public void printArea () {
System.out.println("Area is: " + area);
}
//显示体积
public void printVolume () {
System.out.println("Volume is: " + volume);
}
}
private int length;
private int width;
private int height;
private int area;
private int volume;
//构造方法,通过长、宽、高计算出面积和体积
public Box (int length, int width, int height) {
this.length = length;
this.width = width;
this.height = height;
area = 2 * (length * width
+ length * height
+ width * height);
volume = length * width * height;
}
//显示面积
public void printArea () {
System.out.println("Area is: " + area);
}
//显示体积
public void printVolume () {
System.out.println("Volume is: " + volume);
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
对象symbol没有初始化,你应该在构造方法 public Computting(String expression)
{
super();
this.expression = expression;
this.symbol=new MyStack();//初始化对象
}
{
super();
this.expression = expression;
this.symbol=new MyStack();//初始化对象
}
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
rule()这个方法 对null进行操作
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询