예제 #1
0
파일: main2.cpp 프로젝트: cliftbar/Comp-15
void test_findmax()
{
#if FINDMAX
    printf("Running test_findmax\n");
    SortedList l1, l2;
    int	m1, m2;

    readFile(l1, file1);
    readFile(l2, file2);
    m1 = l1.findMax();
    m2 = l2.findMax();
    cout << "list is ";
    l1.print();
    printf("Your max is %d, should be 78\n", m1);
    cout << "list is ";
    l2.print();
    printf("Your max is %d, should be 42\n", m2);
    SortedList eric;
    eric.insert(-100);
    eric.insert(-101);
    eric.insert(-102);
    m1 = eric.findMax();
    cout << "list is ";
    eric.print();
    printf("Your max is %d, should be -100\n", m1);
#endif
}
예제 #2
0
파일: main2.cpp 프로젝트: cliftbar/Comp-15
void readFile(SortedList &l, const char filename[])
{
    int temp;
    ifstream fin;

    fin.open(filename);
    while ( fin >> temp ) {
        l.insert(temp);
    }
    fin.close();
}
int main()
{
    SortedList* sl = new SortedList();

    sl->insert(1);
    sl->insert(50);

    sl->displayList();

    sl->insert(3);
    sl->insert(20);
    sl->insert(7);

    sl->displayList();
    
    while(sl->removeFirst())
	sl->displayList();
    
    delete sl;
    
    return 0;
} // end main()
예제 #4
0
int main()
{
	
	#define HELP "Usage: $ palindrome [parameter]\nWhere: parameter is one of:\n\t\t-h (help)\n\t\t-k (read from keyboard)\n\t\t-f file (read from file)\n\t\t-d letter (remove all palindromes which start by this letter)\n\t\t-s (show data)\n\t\t-q (exit)\n";
	
	string input ("\0");
	string original ("\0");
	string command;

	SortedList list;

	//always print the help at the begin
	cout << HELP;	

	do
	{
		//print a new prompt until -q
		cout << "$ palindrome ";

		//read command
		getline(cin, command, '\n');

		//create our own argv
		vector<string> myArgv;
		istringstream iss(command);
		string token;
		while(getline(iss, token, ' '))
		{
			myArgv.push_back(token);
		} 

		//create our own argc
		int myArgc = myArgv.size();

		//main method
		if (myArgc == 1)
		{
			if (myArgv[0] == "-h")
			{
				cout << HELP;
			}
			else if (myArgv[0] == "-s")
			{
				//show in the screen the number of queues in the list, the total number of palindromes in each queue and their starting letter
				cout << list.toString() << endl;
			}
			else if (myArgv[0] == "-k")
			{
				int i = 0;

				cout << "(-q to exit)" << endl;

				do
				{
					cout << "(" << i << ") "; i++;
					getline(cin, original, '\n');
					input = rmSpace(original);

					if (isPalindrome(input))
					{
						//insert original in the list
						list.insert(original);
						cout << "Palindrome inserted!" << endl;
					}
					else
						if (original!="-q")
							cout << "This is not palindrome, then not inserted!" << endl;
				} 
				while(original!="-q");
			}
			else
			{
				if (myArgv[0] != "-q")
				{
					cout << "Paremeter unknown " << myArgv[0] << endl;
				}
			}
		}
		else if (myArgc == 2)
		{
			if (myArgv[0] == "-f")
			{
				int cont0 = 0, cont1 = 0;

				ifstream inputFile (myArgv[1]);
				if(inputFile.is_open())
				{
					string tmp;
					while(getline(inputFile, tmp))
					{
						input = rmSpace(tmp);
						if (isPalindrome(input))
						{
							//insert tmp in the list
							list.insert(tmp);
							cont1++;
						}
						else
							cont0++;
					}
					cout << cont1 << " palindromes inserted." << endl;
					cout << cont0 << " sentences that are not palindrome, not inserted." << endl;
				}	
				else
				{
					//filename does not exists
					cout << "The file couldn't be opened, maybe doesn't exists :(\n";
				}
			}
			else if (myArgv[0] == "-d")
			{
				//delete the node of the leter passed in parameter
				if (myArgv[1].length() == 1)
					if(list.remove(myArgv[1].front()))
						cout << "Removed!" << endl;
					else
						cout<< "Node does not exists!" << endl;
				else
					cout << "just one char pleaaaaaase :)" << endl;
			}
			else
			{
				cout << "Paremeter unknown " << myArgv[0] << endl;
			}
		}
		else if (myArgc > 2)
		{
			cout << "There's way too much arguments :')\n";
		}
	}
	while(command != "-q");
}