예제 #1
0
파일: main.cpp 프로젝트: kylienic/CTP-250
void copyConstructorTester()
{
	LinkedQueue<string> queue;
   string items[] = {"zero", "one", "two", "three", "four", "five"};
	for (int i = 0; i < 6; i++)
   {
		cout << "Adding " << items[i] << endl;
      bool success = queue.enqueue(items[i]);
      if (!success)
         cout << "Failed to add " << items[i] << " to the queue." << endl;
	}
   cout << "Queue contains, from front to back, zero one two three four five." << endl;
   
   LinkedQueue<string> copyOfQueue(queue);
   cout << "Copy of queue contains, from front to back, ";
	for (int i = 0; i < 6; i++)
   {
      cout << " " << copyOfQueue.peekFront();
      copyOfQueue.dequeue();
   }
   cout << "." << endl;
   
   cout << "Original queue contains, from front to back,";
	for (int i = 0; i < 6; i++)
   {
      cout << " " << queue.peekFront();
      queue.dequeue();
   }
   cout << "." << endl << endl;
}  // end copyConstructorTester
예제 #2
0
int main()
{
	ifstream inputFile;
	string fileName;  // Holds the user-inputted file name
	string fileLine;  // Holds the line extracted from the file
	string expression; // Holds the expression to be tested
	bool ableToOpen;  // Flag to see if file exists
	bool isBalanced;  // Flag for balanced expression

	cout << "Create queue object lq\n\n";
	LinkedQueue<string> lq;

	cout << "Get the fle input\n\n";
	do
	{	// Ask user for file name and attempt to open
		cout << "Enter input file name: ";
		cin >> fileName;
		ableToOpen = openFileIn(inputFile, fileName);
		if (!ableToOpen)
			cout << fileName << " cannot be opened." << endl;
	} while (!ableToOpen);

	// Read lines from the file and put into queue object
	getline(inputFile, fileLine);
	while (inputFile)
	{
		lq.enqueue(fileLine);
		getline(inputFile, fileLine);
	} // end while
	inputFile.close();

	cout << "\n\n\nAfter getting the file input, test other functions*******\n";
	cout << "Create lq1, a copy of the queue\n\n";
	LinkedQueue<string> lq1(lq);
	cout << "Display the contents of the copy queue lq1:\n";
	cout << lq1 << endl;

	cout << "\nAttempt to peek the now empty queue lq1:\n";
	try
	{
		lq1.peekFront();
	}
	catch (PrecondViolatedExcep e)
	{
		cout << e.what();
	}

	cout << "\nAssign lq to lq1 and then display lq1:\n";
	lq1 = lq;
	cout << lq1;

	cout << "\nPut the first string in lq into a stack of chars, ls1\n\n";
	LinkedStack<char> ls1;
	string firstLine = lq.peekFront();
	for (unsigned int i = 0; i < firstLine.length(); i++)
	{
		ls1.push(firstLine[i]);
	} // end for

	cout << "Create a copy, ls2, of the stack and display the copy:\n";
	LinkedStack<char> ls2(ls1);
	cout << ls2;

	cout << "\n\nAssign the first stack, ls1, to the second stack, ls2, and then display the second stack:\n";
	ls2 = ls1;
	cout << ls2;

	cout << "\n\n\n\nDo the expression checking:\n\n\n";
	while (!lq.isEmpty())
	{
		expression = lq.peekFront();
		lq.dequeue();
		cout << "The next string is:\t" << expression;
		isBalanced = checkExpression(expression);
		if (isBalanced)
			cout << "\tis a correct expression\n\n\n";
		else
			cout << "\tis NOT a correct expression\n\n\n";
	} // end while

	// Program Over
	cout << "Program Over\n\n";
	cin.ignore();
	cout << "Press Enter to end --> ";
	cin.ignore();
	cout << endl;
	return 0;
} // end main