Example #1
0
void Display(ReversibleStack& rs)
{
	// Implement this function (use only Push, Pop and IsEmpty member functions of stack)
	// After the function completes, the stack should be unmodified (see assignment instructions)
	ReversibleStack buffer;
	int num;
	if (rs.isEmpty())
	{
		cout << "Stack is empty.\n" << endl;
		return;
	}
	while (!rs.isEmpty())
	{
		num = rs.Pop();
		cout << num << " ";
		buffer.Push(num);
	}
	cout << "\n" << endl;
	while (!buffer.isEmpty()) // Puts stack rs back to original state
	{
		num = buffer.Pop();
		rs.Push(num);
	}
}
Example #2
0
File: main.cpp Project: Mishkuh/wsu
void Display(ReversibleStack& rs)
{
	int temp;
	ReversibleStack *tempArray = new ReversibleStack;

	if (!rs.isEmpty())
	{
		temp = rs.pop();
		printf("%d ", temp);
		tempArray->push(temp);
		Display(rs);
		rs.push(tempArray->pop());
	}

	// Implement this function (use only Push, Pop and IsEmpty member functions of stack)
	// After the function completes, the stack should be unmodified (see assignment instructions)
}
Example #3
0
int main(int argc, char* argv[])
{
	if (argc < 2)
	{
		cout << "Missing required argument for input file" << endl;
		return 0;
	}

	ReversibleStack rs;
	ifstream inFile;
	int num;
	inFile.open(argv[1], ifstream::in);
	if (!inFile.is_open())
	{
		cout << "Could not open: " << argv[1] << endl;
		return 0;
	}

	while (!inFile.eof())
	{
		string line;
		getline(inFile, line);

		// Process the command here
		if (0 == line.compare(0, 6, "header"))
		{
			rs.Header();
		}
		else if (0 == line.compare(0, 4, "push"))
		{
			char *str = &line[5];
			num = atoi(str);
			rs.Push(num);
			cout << num << " has been pushed.\n" << endl;
		}
		else if (0 == line.compare(0, 3, "pop"))
		{
			num = rs.Pop();
			cout << num << " has been popped.\n" << endl;
		}
		else if (0 == line.compare(0, 7, "reverse"))
		{
			rs.Reverse();
		}
		else if (0 == line.compare(0, 7, "isempty"))
		{
			bool ans = rs.isEmpty();
			if (ans)
			{
				cout << "Stack is empty.\n" << endl;
			}
			else
			{
				cout << "Stack is not empty.\n" << endl;
			}
		}
		else if (0 == line.compare(0, 7, "display"))
		{
			Display(rs);
		}
		// You'll need to implement several other commands in here
		else
		{
			cout << "Unknown command: " << line;
		}
	}

	// Complete
	inFile.close();
	cout << endl << "Done" << endl;
	cin >> num;
	return 1;
}
Example #4
0
File: main.cpp Project: Mishkuh/wsu
int main(int argc, char* argv[])
{
	ReversibleStack rs;
	ifstream inFile;
	inFile.open(argv[2], ifstream::in);
	if (!inFile.is_open())
	{
		cout << "Could not open: " << argv[2] << endl;
		return 0;
	}

	while (!inFile.eof())
	{
		string line;
		getline(inFile, line);

		// Process the command here
		if (0 == line.compare(0, 6, "header"))
		{
			// Display the header line here, as the instructions describe
			cout << "Michael Wiens - 11383291\n" << endl;
		}
		else if (0 == line.compare(0, 7, "display"))
		{
			Display(rs);
			cout << endl;
		}
		else if (0 == line.compare(0, 4, "push"))
		{
			int pushValue = 0;
			string tempString = line.substr(4);
			pushValue = stoi(tempString);
			rs.push(pushValue);
		}
		else if (0 == line.compare(0, 3, "pop"))
		{
			rs.pop();
		}
		else if (0 == line.compare(0, 7, "reverse"))
		{
			rs.reverse();
		}
		else if (0 == line.compare(0, 7, "isempty"))
		{
			if (rs.isEmpty())
			{
				cout << "true" << endl;
			}
			else
			{
				cout << "false" << endl;
			}
		}
		// You'll need to implement several other commands in here
		else
		{
			cout << "Unknown command: " << line;
		}
	}

	// Complete
	inFile.close();
	cout << endl << "Done" << endl;
	system("pause");
	return 1;
}