【发布时间】:2014-12-17 22:01:07
【问题描述】:
我正在尝试编写一个简单的后缀计算器来读取行并使用堆栈执行计算。我想知道是否有人可以检查结构和推送功能是否正确实现。我非常感谢任何我需要考虑的建议或线索。 我是信息学专业的学生,也是我最想在圣诞节前完成的第一个程序之一:)
谁能告诉我如何从栈顶取出两个元素而不是遍历整个栈:/
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <malloc.h>
#include <string.h>
#include <math.h> // Definnes special Float - NAN ("Not A Number")
struct StackElement{
int data;
struct element *next;
}StackElement;
/*
Typ stack_top is pointer to the StackElement
*/
typedef struct StackElement* stack_top{
StackElement* first;
}stack_top;
/*
starc
Lay Element on Stack
If *stacktop points to NULL, there is no element in the Stack
*/
void stack_push(stack_top *stacktop, float value)
{
if(stacktop->first == NULL){
StackElement->data = value;
StackElement->next= NULL;
stack_top->first= *StackElement;
}
else{
StackElement->next = stack_top-> first;
stack_top->first= *StackElement; // New first element
StackElement->data= value;
}
/*
Take one element from the stack
If Stack is empty than set *stacktop = NULL
, give float NAN back
*/
float stack_pop(stack_top *stacktop)
{
if(&stacktop==NULL){
printf("Stack ist leer \n")
}
// Hier Code einfügen
return NAN;
}
/*
Identyfy Token. Difference few cases:
- Token is a nummber : lay it on the Stack.
- Token is an Operator (+, -, *):
1. Take the two elements from the top of the Stack.
2. Use the operator.
3. Lay the result back on the top of the stack.
Implementiere hier die Rechenoperationen (+, -, *) und lege das
Ergebnis zurück auf den Stack. Beachte, dass du mit Floatingpointwerten
arbeitest, z.B. auch negative Kommazahlen.
*/
void process(stack_top *stacktop, char* token)
{ int a, operand1, operand2;
assert(token != NULL);
StackElement* temp = malloc(sizeof(StackElement));
if ( char &token == + ||char &token== - || char &token == * ) == 0 )
{
stack_push(stacktop, token)
else{}
int a= &token;
switch(a)
{
case '+':
result = operand1 + operand2;
break;
case '-':
result = operand1 - operand2;
break;
case '*':
result = operand1 * operand2;
break;
}
return result;
}
}
const int MAX_LINE_LENGTH=1024;
/*
main() reads the Input line by line and divides it single separate Tokens (linked list).
Diese Funktion muss nicht geändert werden.
*/
int main(int argc, char** args)
{
stack_top stacktop = NULL;
char line[MAX_LINE_LENGTH]; // Input line
char* token; // Pointer the current token;
while ( fgets(line, MAX_LINE_LENGTH, stdin) ) {
token = strtok(line, " ");
while (token != NULL) {
process(&stacktop, token); // perform Stackoperationen
token = strtok(NULL, " "); // Read new Token
}
float result = stack_pop(&stacktop); // Take Last result from Stack .
if (stacktop != NULL) { // Mehr Operanden als benötigt angegeben. Fehler.
while (stacktop != NULL) {
stack_pop(&stacktop); //Clean Stack
}
printf("Error\n");
} else if (result != result) { // result ist NAN: Berechnung fehlgeschlagen (z.b. zu wenig Operanden)
printf("Error\n");
} else {
printf("%4.3f\n", result); // Gib Resultat aus
}
}
}
【问题讨论】:
-
这看起来像是一份工作:codereview.stackexchange.com
-
什么是
if ( char &token == + ||char &token== - char &token == * )?这是 C 吗? -
我已经更正了一点。我想写一个“或”条件。此行应检查 Token 是 +、- 还是 * 标记。 ' if ( char &token == + ||char &token== - || char &token == * ) == 0 )'code'
-
@StarPilot:这并不完全是代码审查的主题。它必须已经在工作(OP 不确定它是否完整),我们无法帮助添加额外的实现(第二段)。
-
if 语句确实不正确,首先你不必命名类型,这从声明中已经知道。 2、如果你想和char '+'比较,你应该做== '+',否则调用+操作符。 3.如果你想比较地址token处的字符,你应该做*token而不是&token,&token返回token指针的地址,所以是adress的地址。
标签: c list stack calculator