//optional field, but good to have public PrgState(StackInterface<IStmt> stack, MapInterface<String, int> dictionary, HeapInterface<int> heap, ListInterface<int> list, IStmt prg) { id = generator++; exeStack = stack; symTable = dictionary; heapTable = heap; output = list; originalProgram = prg; exeStack.Push (originalProgram); }
//optional field, but good to have public PrgState(StackInterface <IStmt> stack, MapInterface <String, int> dictionary, HeapInterface <int> heap, ListInterface <int> list, IStmt prg) { id = generator++; exeStack = stack; symTable = dictionary; heapTable = heap; output = list; originalProgram = prg; exeStack.Push(originalProgram); }
/// <summary> /// Calculating exression written in postfix /// </summary> /// <param name="stack"></param> /// <param name="line"></param> /// <returns></returns> public static int Calculator(StackInterface stack, string line) { int mainResult = 0; for (int i = 0; i != line.Length; i++) { char symbol = line[i]; if (symbol != '-' && symbol != '*' && symbol != '/' && symbol != '+' && symbol != ' ') { stack.Push(Convert.ToInt16(symbol) - 48); } else { switch (symbol) { case '+': { int result = 0; result += stack.Pop(); result += stack.Pop(); stack.Push(result); break; } case '-': { int result = 0; result -= stack.Pop(); result += stack.Pop(); stack.Push(result); break; } case '*': { int result = 1; result *= stack.Pop(); result *= stack.Pop(); stack.Push(result); break; } case '/': { int tempor = stack.Pop(); if (tempor != 0) { int result = 0; result = stack.Pop() / tempor; stack.Push(result); break; } else { throw new DivideByZeroException(); } } default: { continue; } } } } if (mainResult != -99999) { mainResult = stack.Pop(); return(mainResult); } else { return(mainResult); } }
/// <summary> /// Calculating exression written in postfix /// </summary> /// <param name="stack"></param> /// <param name="line"></param> /// <returns></returns> public static int Calculator(StackInterface stack, string line) { int mainResult = 0; for (int i = 0; i != line.Length; i++) { char symbol = line[i]; if (symbol != '-' && symbol != '*' && symbol != '/' && symbol != '+' && symbol != ' ') { stack.Push(Convert.ToInt16(symbol) - 48); } else switch (symbol) { case '+': { int result = 0; result += stack.Pop(); result += stack.Pop(); stack.Push(result); break; } case '-': { int result = 0; result -= stack.Pop(); result += stack.Pop(); stack.Push(result); break; } case '*': { int result = 1; result *= stack.Pop(); result *= stack.Pop(); stack.Push(result); break; } case '/': { int tempor = stack.Pop(); if (tempor != 0) { int result = 0; result = stack.Pop() / tempor; stack.Push(result); break; } else { throw new DivideByZeroException(); } } default: { continue; } } } if (mainResult != -99999) { mainResult = stack.Pop(); return mainResult; } else { return mainResult; } }