好的,所以我要做的是拿一个等式并将每个字符放到堆栈中以检查错误。每当我尝试这样做时,我得到了奇怪的结果,几乎就像它只是添加到之前的那些但是将它放在下一个元素中。
部首:
#ifndef STACK_H
#define STACK_H
#include <iostream>
using namespace std;
// Stack template
template <class T>
class Stack
{
private:
T *stackArray;
int stackSize;
int top;
public:
//Constructor
Stack(int);
// Copy constructor
Stack(const Stack&);
// Destructor
~Stack();
// Stack operations
void push(T);
void pop(T &);
bool isFull();
bool isEmpty();
};
//***************************************************
// Constructor *
//***************************************************
template <class T>
Stack<T>::Stack(int size)
{
stackArray = new T[size];
stackSize = size;
top = -1;
}
//***************************************************
// Copy constructor *
//***************************************************
template <class T>
Stack<T>::Stack(const Stack &obj)
{
// Create the stack array.
if (obj.stackSize > 0)
stackArray = new T[obj.stackSize];
else
stackArray = NULL;
// Copy the stackSize attribute.
stackSize = obj.stackSize;
// Copy the stack contents.
for (int count = 0; count < stackSize; count++)
stackArray[count] = obj.stackArray[count];
// Set the top of the stack.
top = obj.top;
}
//***************************************************
// Destructor *
//***************************************************
template <class T>
Stack<T>::~Stack()
{
if (stackSize > 0)
delete[] stackArray;
}
//*************************************************************
// Member function push pushes the argument onto *
// the stack. *
//*************************************************************
template <class T>
void Stack<T>::push(T item)
{
if (isFull())
{
cout << "The stack is full.\n";
}
else
{
top++;
stackArray[top] = item;
}
}
//*************************************************************
// Member function pop pops the value at the top *
// of the stack off, and copies it into the variable *
// passed as an argument. *
//*************************************************************
template <class T>
void Stack<T>::pop(T &item)
{
if (isEmpty())
{
cout << "The stack is empty.\n";
}
else
{
item = stackArray[top];
top--;
}
}
//*************************************************************
// Member function isFull returns true if the stack *
// is full, or false otherwise. *
//*************************************************************
template <class T>
bool Stack<T>::isFull()
{
bool status;
if (top == stackSize - 1)
status = true;
else
status = false;
return status;
}
//*************************************************************
// Member function isEmpty returns true if the stack *
// is empty, or false otherwise. *
//*************************************************************
template <class T>
bool Stack<T>::isEmpty()
{
bool status;
if (top == -1)
status = true;
else
status = false;
return status;
}
#endif
驱动:
// This program demonstrates the Stack template.
#include <iostream>
#include <string>
#include "Stack.h"
using namespace std;
// Constants for the menu choices
const int PUSH_CHOICE = 1,
POP_CHOICE = 2,
QUIT_CHOICE = 3;
// Function prototypes
void menu(int &);
void getStackSize(int &);
void pushItem(Stack<string>&);
void popItem(Stack<string>&);
int main()
{
int stackSize; // The stack size
string equ;
//int choice; // To hold a menu choice
// Get the stack size.
//getStackSize(stackSize);
cout << "Enter the equation: ";
cin >> equ;
// Create the stack.
Stack<string> stack(equ.length());
for(int i=0;i<equ.length(); i++){
stack.push(equ.substr(i, i+1));
}
for(int i=0; i<equ.length(); i++){
popItem(stack);
}
/*do
{
// Get the user's menu choice.
menu(choice);
// Perform the user's choice.
if (choice != QUIT_CHOICE)
{
switch (choice)
{
case PUSH_CHOICE:
pushItem(stack);
break;
case POP_CHOICE:
popItem(stack);
}
}
} while (choice != QUIT_CHOICE);
*/
system("Pause");
return 0;
}
//************************************************
// The getStackSize function gets the desired *
// stack size, which is assigned to the *
// reference parameter. *
//************************************************
void getStackSize(int &size)
{
// Get the desired stack size.
cout << "How big should I make the stack? ";
cin >> size;
// Validate the size.
while (size < 1)
{
cout << "Enter 1 or greater: ";
cin >> size;
}
}
//************************************************
// The menu function displays the menu and gets *
// the user's choice, which is assigned to the *
// reference parameter. *
//************************************************
void menu(int &choice)
{
// Display the menu and get the user's choice.
cout << "\nWhat do you want to do?\n"
<< PUSH_CHOICE
<< " - Push an item onto the stack\n"
<< POP_CHOICE
<< " - Pop an item off the stack\n"
<< QUIT_CHOICE
<< " - Quit the program\n"
<< "Enter your choice: ";
cin >> choice;
// Validate the choice
while (choice < PUSH_CHOICE || choice > QUIT_CHOICE)
{
cout << "Enter a valid choice: ";
cin >> choice;
}
}
//************************************************
// The pushItem function gets an item from the *
// user and pushes it onto the stack. *
//************************************************
void pushItem(Stack<string> &stack)
{
string item;
// Get an item to push onto the stack.
cin.ignore();
cout << "\nEnter an equation: ";
//cin >> item;
getline(cin, item);
stack.push(item);
}
//***************************************************
// The popItem function pops an item from the stack *
//***************************************************
void popItem(Stack<string> &stack)
{
string item = "";
// Pop the item.
stack.pop(item);
// Display the item.
if (item != "")
cout << item << " was popped.\n";
}
答案 0 :(得分:0)
std::string::substr
采用索引和长度,而不是两个索引。你需要更长和更长的子串。只需使用1
作为第二个参数。