gpt4 book ai didi

c++ - 如何对堆栈中的所有项目求和?后缀计算器

转载 作者:行者123 更新时间:2023-11-30 03:13:34 24 4
gpt4 key购买 nike

我正在制作一个后缀计算器,我想为其添加一个新功能。如何将堆栈中的所有项目相加,然后从堆栈中删除用于求和的项目,然后将总和推回堆栈?所有这一切都应该通过按下命令“s”来实现。

我试过这样做,但它总是只打印 0。

    case 's':
if (numbers.top().first == underflow)
cout << "Stack empty" << endl;
else {
numbers.pop();
if (numbers.top().first == underflow) {
cout << "Stack has just one entry" << endl;
numbers.push(p);
}

else {
while (!numbers.empty()) {
sum += numbers.top().second;

}
numbers.push(sum);
cout << sum << endl;

}
}
break;

这是我的 main.cpp 的一些代码。

#include <iostream>
#include "Stack.h"
#include <cmath>
using namespace std;

void help()
{
cout
<< "[?]push to stack [=]print top [x]exchange [s]sum [a]average" << endl
<< "[+] [-] [*] [/] [%] [^] [v] are arithmetic operations" << endl
<< "[Q]uit." << endl;
}

char get_command()
{
char command;
bool waiting = true;
cout
<< "Select command and press <Enter>:" << endl;

while (waiting) {
cin >> command;
command = tolower(command);
if (command == '?' || command == '=' || command == '+' ||
command == '-' || command == '*' || command == '/' ||
command == 'q' || command == 'x' || command == 's' ||
command == 'a' || command == '%' || command == '^' ||
command == 'v') waiting = false;


else {
cout << "Please enter a valid command:" << endl;
}
}
return command;
}


bool do_command(char command, Stack& numbers)

{
double p, q, g, r;
double sum = 0;
switch (command) {
case '?':
cout << "Enter a real number: " << flush;
cin >> p;
if (numbers.push(p) == overflow)
cout << "Warning: Stack full, lost number" << endl;
break;

case '=':
if (numbers.top().first == underflow)
cout << "Stack empty" << endl;
else
cout << p << endl;
break;

case '+':
if (numbers.top().first == underflow)
cout << "Stack empty" << endl;
else {
numbers.pop();
if (numbers.top().first == underflow) {
cout << "Stack has just one entry" << endl;
numbers.push(p);
}

else {
numbers.pop();
if (numbers.push(q + p) == overflow)
cout << "Warning: Stack full, lost result" << endl;
}
}
break;

case '-':
if (numbers.top().first == underflow)
cout << "Stack empty" << endl;
else {
numbers.pop();
if (numbers.top().first == underflow) {
cout << "Stack has just one entry" << endl;
numbers.push(p);
}

else {
numbers.pop();
if (numbers.push(q - p) == overflow)
cout << "Warning: Stack full, lost result" << endl;
}
}
break;

case '*':
if (numbers.top().first == underflow)
cout << "Stack empty" << endl;
else {
numbers.pop();
if (numbers.top().first == underflow) {
cout << "Stack has just one entry" << endl;
numbers.push(p);
}

else {
numbers.pop();
if (numbers.push(q * p) == overflow)
cout << "Warning: Stack full, lost result" << endl;
}
}
break;

case '/':
if (numbers.top().first == underflow)
cout << "Stack empty" << endl;
else {
numbers.pop();
if (numbers.top().first == underflow) {
cout << "Stack has just one entry" << endl;
numbers.push(p);
}

else {
numbers.pop();
if (numbers.push(q / p) == overflow)
cout << "Warning: Stack full, lost result" << endl;
}
}
break;

case 'x':
if (numbers.top().first == underflow)
cout << "Stack empty" << endl;
else {
numbers.pop();
if (numbers.top().first == underflow) {
cout << "Stack has just one entry" << endl;
numbers.push(p);
}

else {
numbers.pop();
numbers.pop();
numbers.push(p);
numbers.push(q);
}
}
break;

case 's':
if (numbers.top().first == underflow)
cout << "Stack empty" << endl;
else {
numbers.pop();
if (numbers.top().first == underflow) {
cout << "Stack has just one entry" << endl;
numbers.push(p);
}

else {
while (!numbers.empty()) {
sum += numbers.top().second;

}
numbers.push(sum);
cout << sum << endl;

}
}
break;

这是我的 Stack.cpp。

Error_code Stack::push(const Stack_entry& item)
/*
Pre: None.
Post: If the Stack is not full, item is added to the top
of the Stack. If the Stack is full,
an Error_code of overflow is returned and the Stack is left unchanged.
*/

{
Error_code outcome = success;
if (count >= maxstack)
outcome = overflow;
else
entry[count++] = item;
return outcome;
}


Error_code Stack::pop()
/*
Pre: None.
Post: If the Stack is not empty, the top of
the Stack is removed. If the Stack
is empty, an Error_code of underflow is returned.
*/

{
Error_code outcome = success;
if (count == 0)
outcome = underflow;
else --count;
return outcome;
}


pair<Error_code, int> Stack::top() const
{
pair<Error_code, int> p;
Error_code outcome = success;
if (count == 0)
p.first = underflow;
p.second = 0;
return p;

p.first = outcome;
p.second = entry[count - 1];
return p;
}

这是我的 Stack.h

#include "Utility.h"

typedef double Stack_entry;

const int maxstack = 10;

class Stack {
public:
Stack();
bool empty() const;
Error_code pop();
//Error_code top(Stack_entry& item) const;
Error_code push(const Stack_entry& item);
//Error_code pop_top(Stack& s, Stack_entry& item);
double size() const;
std::pair<Error_code, int> top() const;

private:
int count;
Stack_entry entry[maxstack];
};

我希望它将堆栈中的所有数字加在一起,然后删除它们,然后将其推回堆栈。我真的很感激任何帮助。提前致谢。

最佳答案

pop 不像 C++ 中的其他语言那样返回值。您需要使用 top 函数来返回堆栈中的顶部元素。您可以将该元素添加到 sum,然后使用 pop 将其删除。您可以按如下方式实现它:

while (!numbers.empty()) 
{
sum += numbers.top();
numbers.pop()
}

对于 Op 的另一个请求:

top 的实现更改为:

std::pair<Error_code,int> top() const
{
std::pair<Error_code,int> p;
Error_code outcome = success;
if (count == 0)
p.first = underflow;
p.second = 0;
return p;

p.first = outcome;
p.second = entry[count -1];
return p;
}

你的第一个问题的解决方案变成了:

while (!numbers.empty()) 
{
sum += numbers.top().second;
numbers.pop()
}

如果你想要结果,你可以做numbers.top().first。就这样。

关于c++ - 如何对堆栈中的所有项目求和?后缀计算器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58579395/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com