堆java编程中的重写值
我已经在我的项目中添加了一个新的数据结构堆java编程中的重写值,java,Java,我已经在我的项目中添加了一个新的数据结构Heap,但唯一的问题是它只打印一次结果 如果我有意见: v=10;new(v,20);new(a,22);print(v) 行刑结束时 Heap={1->20, 2->22}, SymTable={v->1, a->2} 但它给了我什么: Heap: 1->22 SymTable: a -> 1 v -> 1 它覆盖第一个值。以下是控制器的代码,其中是主要部分: HeapA
Heap
,但唯一的问题是它只打印一次结果
如果我有意见:
v=10;new(v,20);new(a,22);print(v)
行刑结束时
Heap={1->20, 2->22},
SymTable={v->1, a->2}
但它给了我什么:
Heap: 1->22
SymTable: a -> 1
v -> 1
它覆盖第一个值。以下是控制器的代码,其中是主要部分:
HeapAllocation crtStmt1=(HeapAllocation) crtStmt;
String varI = crtStmt1.getVarname();
Exp e = crtStmt1.getExpression();
int i=0;
Id<Object,Integer> tbl = state.getDict();
IHeap<Integer,Integer> heap1 = state.getHeap();
int value= e.eval(tbl, heap1);
heap1.put(++i, value);
if (tbl.containsKey(varI))
tbl.update(varI,i);
tbl.update(varI, i);
因为对于一个新操作,它不会附加到上一个操作,只会覆盖它
编辑:
堆的实现:
public class Heap<Integer,In> implements IHeap<Integer, Integer>,Serializable{
private Map<Integer, Integer> mapp;
public Heap(){
mapp = new HashMap<Integer,Integer>();
}
public void put(Integer index, Integer value){
mapp.put(index, value);
}
public void remove(Integer index){
try{
if(isEmpty())
throw new ExceptionRepo();
else
mapp.remove(index);
}catch (ExceptionRepo ex){
System.err.println("Error: Heap is empty.");
}
}
public boolean isEmpty(){
return mapp.isEmpty();
}
public Integer get(Integer index){
return mapp.get(index);
}
public boolean containsIndex(Integer index){
return mapp.containsKey(index);
}
public void update(Integer index, Integer value){
mapp.put(index, value);
}
public String toString(){
Set<Integer> all = mapp.keySet();
Object[] keysA= all.toArray();
String res="";
for(int i=0; i<mapp.size(); i++){
Integer v = mapp.get(keysA[i]);
res += keysA[i].toString() + "->" + v.toString() + "\r\n";
}
return res;
}
}
如何解决此问题?此堆的实现是什么?看起来它的行为像一个HashMap而不是优先级队列。所以它是一个HashMap。什么是国家?您的代码只将一个值放入堆中,因此我希望堆中有一个值。好吧,状态就是我要执行的程序。从文章开头的示例中可以看出,它处于程序状态。我开始感觉到了。因此,您的语言的
new
函数将键值对存储在hashmap中。未定义simbol(即a
)时会发生什么?这仍然是太少的信息。eval函数中一定有实现错误。什么是堆
?v=10的确切含义是什么;新的(v,20);新的(a,22);打印(v)
?它不是Java代码:它是什么?
public class Heap<Integer,In> implements IHeap<Integer, Integer>,Serializable{
private Map<Integer, Integer> mapp;
public Heap(){
mapp = new HashMap<Integer,Integer>();
}
public void put(Integer index, Integer value){
mapp.put(index, value);
}
public void remove(Integer index){
try{
if(isEmpty())
throw new ExceptionRepo();
else
mapp.remove(index);
}catch (ExceptionRepo ex){
System.err.println("Error: Heap is empty.");
}
}
public boolean isEmpty(){
return mapp.isEmpty();
}
public Integer get(Integer index){
return mapp.get(index);
}
public boolean containsIndex(Integer index){
return mapp.containsKey(index);
}
public void update(Integer index, Integer value){
mapp.put(index, value);
}
public String toString(){
Set<Integer> all = mapp.keySet();
Object[] keysA= all.toArray();
String res="";
for(int i=0; i<mapp.size(); i++){
Integer v = mapp.get(keysA[i]);
res += keysA[i].toString() + "->" + v.toString() + "\r\n";
}
return res;
}
}
public class Exp{
public int eval(Id<Object,Integer> tbl)throws ExceptionRepo{
return 0;
}
public String toString(){
return "";
}
public int eval(Id<Object,Integer> tbl,IHeap<Integer,Integer> heap) throws ExceptionRepo{
return 0;
}
public class ConstExp extends Exp implements Serializable{
int number;
public int eval(Id<Object,Integer> tbl) throws ExceptionRepo{
return number;
}
public ConstExp(int n){
number = n;
}
public String toString(){
return "" + number;
}
public int eval(Id<Object,Integer> tbl,IHeap<Integer,Integer> heap) throws ExceptionRepo{
return number; //THIS IS THE NEW METHOD
}
If it's useful this is the statement evaluation rule for the heap(that I implemented in the controller up there):
Stack1={new(var,exp)| Stmt2|...}
SymTable1
Heap1
==>
Stack2={Stmt2|...}
let be v=eval(exp,SymTable1,Heap1) in
Heap2 = Heap1 U {newfreelocation ->v}
if var exists in SymTable1 then SymTable2 = update(SymTable1,
var,newfreelocation)
else SymTable2 = add(SymTable1,var, newfreelocation)