我目前正在學習數據結構,我試圖用C++編寫一個程序,它使用一個數組作爲模擬堆棧。代碼應該像這樣工作:C++中的後綴計算結果只在一些時間正確的答案
用戶輸入一個Postfix表達式。表達式從左到右讀取。每次讀取數字時,它都會「彈出」到模擬堆棧上。當讀取操作數時,模擬堆棧中的前兩個數字將被「彈出」,操作數將使用這些數字執行計算,並將得到的答案「推回」到模擬堆棧上。該過程繼續,直到模擬堆棧中只剩下一個數字。這個數字是Postfix表達式的答案,它在屏幕上向用戶讀出。
我的代碼大部分工作在下面。但是,如果我模擬堆棧中的其中一個值大於或等於128,那麼該數字似乎存儲不正確(例如,+128將變成-128)。
我在做什麼錯?它與calc
函數中的答案有什麼關係?具體來說,這行代碼:return answer;
任何幫助或提示將不勝感激。
#include <string>
#include <iostream>
using namespace std;
// Define the size of the stack:
#define SIZE 50
// Global variable declarations:
char stack[SIZE];
int top = -1; // The top of the array
// Function for PUSHING operands onto the stack:
void push(char elem)
{
top++;
stack[top] = elem;
// cout << elem << " TESTING PUSH OUTPUT\n"; // TESTING
}
// Function for POPPING operands from the stack:
char pop()
{
char answer;
answer = stack[top];
top--;
// cout << top << " TESTING WHAT'S POPPED\n"; // TESTING
return (answer);
}
// Function for CALCULATING two popped numbers:
double calc(char op)
{
int num1, num2, answer;
// Pop two numbers from the stack:
num1 = pop();
num2 = pop(); // put the second popped number into 'num2'
// Calculate the two popped numbers:
switch (op)
{
case '*':
answer = num2 * num1;
break;
case '/':
answer = num2/num1;
break;
case '+':
answer = num2 + num1;
break;
case '-':
answer = num2 - num1;
break;
default:
cout << "Invalid expression!";
}
// Push the result of the calculation back onto the stack:
push(answer);
return answer;
}
// The main program, which takes in the expression:
int main()
{
string postfix;
int solution;
cout << "Please enter a Postfix expression, with operators and operands separated by spaces and/or commas (e.g: 3 7 5 + 2 - * 16 4 + 10//): \n";
getline(cin,postfix);
// If the User's Postfix expression begins with an operator, then throw up an error message:
if (postfix.find('/') == 0 || postfix.find('*') == 0 || postfix.find('+') == 0 || postfix.find('-') == 0)
{
cout << "Sorry, that's an invalid Postfix expression, and it can't be evaluated.";
return 0;
}
// FOR loop to read the Postfix expression:
for(unsigned int i = 0; i < postfix.length(); i++)
{
// If the value is a space or a comma, move on to the next value:
if(postfix[i] == ' ' || postfix[i] == ',')
{
continue;
} // End IF
// If the value is a number, extract it, and continue reading and extracting the next value until it is NOT a number:
else if(isalnum(postfix[i]))
{
int operand = 0;
while(i < postfix.length() && isalnum(postfix[i]))
{
// For multi-digit numbers, multiply the initial extracted number by 10 to move it into the 'tens' column, then add the next number to the initial number.
// Continue doing this until the next value is NOT a number
operand = (operand * 10) + (postfix[i] - '0');
i++;
} // End WHILE
// When WHILE exits, 'i' will be non-numeric, of the 'end of the string'. Decrement 'i', because it will be incremented in the FOR loop as longs as the FOR condition is true
// (Stops 'i' from being incremented twice)
i--;
// Push the operand onto the stack:
push(operand);
} // End ELSE IF
// If the value is an operator, run the calculation function:
else
{
calc(postfix[i]);
} // End ELSE
} // End FOR
solution = pop(); // put the solution of the equation into 'answer'
cout << "The solution to the Postfix expression is: " << solution;
}
你的堆棧元素是'char',因爲你的默認'char'類型必須是'signed'。因此無法存儲除-128..127之外的任何內容。 – usr2564301