我有一个计算机程序,读取一个字符数组,操作数和运算符用后缀表示法编写.然后程序扫描数组,通过使用堆栈计算结果,如下所示:
get next char in array until there are no more
if char is operand
push operand into stack
if char is operator
a = pop from stack
b = pop from stack
perform operation using a and b as arguments
push result
result = pop from stack
Run Code Online (Sandbox Code Playgroud)
如何通过归纳证明此程序正确评估任何后缀表达式?(摘自练习4.16 Java中的算法(Sedgewick 2003))
我正在尝试实现postfix-expression评估,这是我的代码:
#include<iostream>
#include<string.h>
using namespace std;
template < class T > class Stack {
private:
T * s;
int n;
public:
Stack(int maxn) {
s = new T[maxn];
n = 0;
}
int empth() const {
return n == 0;
}
void push(T item) {
s[n++] = item;
}
int pop() {
return s[--n];
}
};
int main()
{
string a = "598+46**7+*";
int n = a.length();
Stack < int >save(n);
for (int i = 0; i < n; i++) …Run Code Online (Sandbox Code Playgroud) 我编写了一个程序来从表达式列表中递归地评估prolog中的修复后表达式.例如,给出以下列表:
[+,1,2]
Run Code Online (Sandbox Code Playgroud)
它应该返回3.我构建谓词的方式是递归调用自身直到它到达列表的末尾,以便它向后读取值.(与从左到右阅读此列表相同:[2,1,+]).
我的问题是,当我尝试通过递归调用返回多个值时,所有值都会突然消失.
这是代码:
eval_list([Head|Tail],_,Result):-
Tail==[], % last element of list
Result=Head,
write(Head),
write(' was stored in Result!\n').
eval_list([Head|Tail],Store1,Result):-
eval_list(Tail,Store2, NewResult),
(\+integer(Store2))
->
% if no integer is bound to Store2, bind Store1 to Head
Store1=Head,
Result is NewResult,
write(Head),
write(' is stored value!\n')
; (integer(Store2)) ->
% if an integer is bound to store2, we perform operation specified by the Head with the stored number
X is Store2+NewResult,
Result is X,
write('performed operation!\n')
;
% if doesnt catch …Run Code Online (Sandbox Code Playgroud) 我一直在运行这个并输入"12+"作为表达式.当它到达while循环时,它会被卡住,好像从未遇到过这种情况.我不明白为什么这是因为在while循环之前"它"等于2所以甚至不应该使用循环.
//array based stack implementation
class Stack
{
private:
int capacity; //max size of stack
int top; //index for top element
char *listArray; //array holding stack elements
public:
Stack (int size = 50){ //constructor
capacity = size;
top = 0;
listArray = new char[size];
}
~Stack() { delete [] listArray; } //destructor
void push(char it) { //Put "it" on stack
listArray[top++] = it;
}
char pop() { //pop top element
return listArray [--top];
}
char& topValue() const { //return top …Run Code Online (Sandbox Code Playgroud) 我仍然是新的,并且不太快就用C来获取编码.对于一项任务,我必须使用堆栈从数组中评估Postfix表达式.虽然我确信我的代码有几个问题但我觉得基本结构很好.它编译没有错误,但确实有一些警告.它将在main中运行并打印出printf语句,但据我所知,它不会在评估中做任何事情.我不是在寻找作弊或完整修复,但我会赞赏一些指导.请回答假设我知之甚少.
干杯
#include <stdio.h>
#include <stdlib.h>
struct stackNode {
int data;
struct stackNode *nextPtr;
};
typedef struct stackNode StackNode;
typedef StackNode *StackNodePtr;
int evaluatePostfixExpression( char *expr );
int calculate( int op1, int op2, char operator );
void push( StackNodePtr *topPtr, int value );
int pop( StackNodePtr *topPtr );
int isEmpty( StackNodePtr topPtr );
void printStack( StackNodePtr topPtr );
char postfix[50];
int answer;
void main()
{
printf("Print an postfix expression\n");
scanf("%s", postfix);
evaluatePostfixExpression(postfix);
printf("The value of the expression is: %i",answer);
} …Run Code Online (Sandbox Code Playgroud) 我的代码:
val exitStatus = url #> outfile !
scala.sys.process:
new URL("http://www.scala-lang.org/") #> new File("scala-lang.html") !
警告:
postfix operator ! should be enabled
[warn] by making the implicit value scala.language.postfixOps visible.
[warn] This can be achieved by adding the import clause 'import scala.language.postfixOps'
[warn] or by setting the compiler option -language:postfixOps.
[warn] See the Scaladoc for value scala.language.postfixOps for a discussion
[warn] why the feature should be explicitly enabled.
[warn] val exitStatus = url #> outfile !
[warn] ^
[warn] …Run Code Online (Sandbox Code Playgroud) 以下类由另一个程序使用.访问它时,它会抛出StackOverFlowError.这是我在大学时必须做的一个后缀计算器的一部分.
非常感谢任何帮助,谢谢你提前.我是Java的新手,我不知道该怎么做.
码:
import java.util.Queue;
import java.util.Stack;
public class MyPostfixMachine implements PostfixMachineInterface {
MyMathOperations mmo = new MyMathOperations();
MyPostfixMachine mpm = new MyPostfixMachine();
public String evaluate(Queue q) {
if (q.isEmpty()) {//if the input is empty, terminate the program
System.exit(0);
}
if (q.size() == 1) {//if there is only one number in the queue, return it as the solution
if (mpm.isParsableToDouble(String.valueOf(q.remove()))) {
return String.valueOf(q.remove());
}
}
Stack<String> finalxp = new Stack<String>();//create an empty stack
if (mpm.isParsableToDouble(String.valueOf(q.remove()))) {//if first element of queue …Run Code Online (Sandbox Code Playgroud) 我正在为简单的数学表达式(常量和简单算术)编写一个推动者.
我遇到的问题是从后缀格式化表达式构建表达式树.我在大多数情况下所做的工作都很好,但维基百科没有这个例子.
如果我评估表达式3 + 4 * 2 / ( 1 - 5 ) ^ 2 ^ 3,3,0001220703125即使结果应该是,我得到结果3,001953125.这样做的原因似乎是表达式树看起来像3+((4*2)/((1-5)^(2^3)))而不是(3+((4*2)/(((1-5)^2)^3))).
原始表达式的后缀表示法看起来像 3 4 2 * 1 5 ? 2 3 ^ ^ / +
有关如何获取表达式树的任何建议,我希望它是什么?
下面是表达式树代码的后缀和一些在C#中的测试,但应该是非常明显的.
public MathExpression Parse()
{
var tokens = this.ToPostFix(_tokens);
var stack = new Stack<MathExpression>();
foreach(token in tokens)
{
if(token.IsOperand())
{
// Push the operand on the stack.
stack.Push(new ConstantExpression(token.Value));
}
else
{
Debug.Assert(token.Type == TokenType.Operator, "Expected …Run Code Online (Sandbox Code Playgroud) language-agnostic algorithm expression-trees postfix-notation
我有一个前缀表达式,它只有 4 个二元运算符 (+,-,*,/) 。评估此类表达式的直接方法是将其转换为后缀表达式,然后评估该表达式。但是我正在寻找一种直接执行此操作而不将其转换为任何其他表达式的算法?
language-agnostic algorithm infix-notation prefix postfix-notation
我被分配到编写一个使用堆栈计算后缀表达式的程序。
我编写了该程序,它似乎在大部分情况下都能正常工作,但是在确定表达式是否有效时遇到了问题。
以下是我所做的基本步骤:
因此,如果堆栈已经是空的,那么上面的方法效果很好,但是如果堆栈上有更多的操作数,结果就会简单地打印出来。这显然是不正确的,因为如果堆栈上还有多个操作数,它应该是一个无效的表达式。
我在想我应该做一个while(!stack.empty()) result = stack.top, stack.pop()但是,这仍然会有同样的问题。
有人可以告诉我应该如何正确测试它吗?
代码:
int main()
{
string expression;
char response;
int result = -1; //result of expression. Initialized to -1
Stack stack;
printMenu();
do {
cout << "Would you like to enter an expression? (y / n)" << endl;
cin >> response;
response = toupper(response);
switch(response)
{
case 'Y':
//needed due to …Run Code Online (Sandbox Code Playgroud) 如果是,那为什么会这样呢?不正确的关联性对后缀表达有效吗?