/*********************************************************************************************************************
                                              strCompare2
This function takes two strings and compares them using recursion similarly to the strCompare function (see above). 
Unlike strCompare, this function ignores any non-letter characters and ignores the case of the characters.
*********************************************************************************************************************/
int strCompare2(char* str1, char* str2) 
{
	// The character is not a letter, skip it. 
	if (whatLetter(str1[0]) == -1 && str1[0] != 0)
	{
		return strCompare2(str1 + 1, str2);
	}
	if (whatLetter(str2[0]) == -1 && str2[0] != 0)
	{
		return strCompare2(str1, str2 + 1);
	}

	// Both characters are null terminators, so the strings must be equal.
	if ((str1[0] == 0) && (str2[0] == 0))	
	{
		return 0;
	}

	if (whatLetter(str1[0]) < whatLetter(str2[0]))		// str1 is "less than" str2
	{
		return -1;
	}
	else if (whatLetter(str1[0]) > whatLetter(str2[0]))	// str1 is "greater than" str2
	{
		return 1;
	}

	// The first letter of the two strings are equal, check rest
	if (whatLetter(str1[0]) == whatLetter(str2[0]))		
	{
		strCompare2(str1 + 1, str2 + 1);
	}
}
/*
 * same as before, only this time 
 * ignore anything that is not a letter
 * ignore the case (upper case, lower case)
 * So, strCompare2("Hello", "hello") should return 0 (they're the same)
 * strCompare2("The plane!", "theater") should return 1 since "theplane" is larger than "theater"
 * once again, you can only use recursion, no loops
 */
int strCompare2(char* str1, char* str2) {
	if (str1[0] == str2[0]) {
		if (str1[0] == 0) {
			return 0;
		}
	}
	if (whatLetter(*str1) == -1) {
		str1++;
		int t = strCompare2(str1, str2);
		return t;
	}
	if (whatLetter(*str2) == -1) {
		str2++;
		int x = strCompare2(str1, str2);
		return x;
	}
	if (whatLetter(*str1) > whatLetter(*str2)) {
		return 1;
	}
	if (whatLetter(*str2) > whatLetter(*str1)) {
		return -1;
	}
	else {
		str1++;
		str2++;
		int z = strCompare2(str1, str2);
		return z;
	}
}
示例#3
0
/*
 * same as before, only this time 
 * ignore anything that is not a letter
 * ignore the case (upper case, lower case)
 * So, strCompare2("Hello", "hello") should return 0 (they're the same)
 * strCompare2("The plane!", "theater") should return 1 since "theplane" is larger than "theater"
 * once again, you can only use recursion, no loops
 */
int strCompare2(char* str1, char* str2) {
	//End scenario
	if (str1[0] == 0 && str2[0] == 0) { return 0; }

	//Result variable 
	int result = 0;

	//We jump one for the non letters
	if (whatLetter(str1[0]) == -1) {result = strCompare2(str1 + 1, str2);}
	else if (whatLetter(str2[0]) == -1) { result = strCompare2(str1, str2+1); }

	//We do recursion until we reach the end of both strings 
	else if (str1[0] != 0 && str2[0] != 0) { result = strCompare2(str1 + 1, str2 + 1); }
	else if (str1[0] == 0) { result = strCompare2(str1, str2 + 1); }
	else if (str2[0] == 0) { result = strCompare2(str1 + 1, str2); }

	//We return previous result if current is not a letter 
	if (whatLetter(str1[0]) == -1 || whatLetter(str2[0]) == -1) { return result; }

	//If past match was equal, we check if stll equal or return less or more 
	if (result == 0) {
		if (whatLetter(str1[0]) == whatLetter(str2[0])) { return 0; } //Still equal
		else if (whatLetter(str1[0]) < whatLetter(str2[0])) { return -1; } //It is less
		else { return 1; } //It is more 
	}

	//If currently equal, but the previous was not, we return the result 
	else if (whatLetter(str1[0]) == whatLetter(str2[0])) { return result;}

	//Last case, if not equal, then check if for less or more 
	else {
		if (whatLetter(str1[0]) < whatLetter(str2[0])) { return -1; } //It is less
		else { return 1; } //It is more
	}
}
int main(void) {
	const int magic_number = 13017;

	/* test min */
	int a[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
	printf("the smallest of the first 10 natural numbers is: %d\n", minIt(a, 10));
	printf("the smallest of the first 10 natural numbers is: %d\n", minRec1(a, 10));
	printf("the smallest of the first 10 natural numbers is: %d\n", minRec2(a, 10));
	a[3] = -1;
	printf("now the smallest is %d\n", minIt(a, 10));
	printf("now the smallest is %d\n", minRec1(a, 10));
	printf("now the smallest is %d\n", minRec2(a, 10));
	
	/* test sqrt */
	printf("the sqrt of 25 is %g\n", sqrtIt(25.0, 0, 25.0));
	printf("the sqrt of 26 is %g\n", sqrtIt(26.0, 0, 26.0));
	printf("the sqrt of 2 is %g\n", sqrtIt(2.0, 0, 2.0));
	printf("the sqrt of 25 is %g\n", sqrtRec(25.0, 0, 25.0));
	printf("the sqrt of 26 is %g\n", sqrtRec(26.0, 0, 26.0));
	printf("the sqrt of 2 is %g\n", sqrtRec(2.0, 0, 2.0));

	/* test strCompare */
	char* s1; char* s2;
	s1 = "apple"; s2 = "apricot";
	if (strCompare(s1, s2) < 0) { 
		printf("\"%s\" is less than \"%s\", very good\n", s1, s2);
	} else {
		printf("oops, \"%s\" should be less than \"%s\"\n", s1, s2);
	}
			
	s1 = "Apple"; s2 = "apple";
	if (strCompare(s1, s2) < 0) { 
		printf("\"%s\" is less than \"%s\", very good\n", s1, s2);
	} else {
		printf("oops, \"%s\" should be less than \"%s\"\n", s1, s2);
	}

	s1 = "baby boy"; s2 = "banana";
	if (strCompare(s1, s2) < 0) { 
		printf("\"%s\" is less than \"%s\", very good\n", s1, s2);
	} else {
		printf("oops, \"%s\" should be less than \"%s\"\n", s1, s2);
	}

	s1 = "a rather silly string"; s2 = "a rather silly string";
	if (strCompare(s1, s2) == 0) { 
		printf("\"%s\" is equal to \"%s\", very good\n", s1, s2);
	} else {
		printf("oops, \"%s\" should be equal to \"%s\"\n", s1, s2);
	}

	s1 = "12345"; s2 = "12345";
	if (strCompare(s1, s2) == 0) { 
		printf("\"%s\" is equal to \"%s\", very good\n", s1, s2);
	} else {
		printf("oops, \"%s\" should be equal to \"%s\"\n", s1, s2);
	}

	s1 = "Numbers: 12345"; s2 = "12345";
	if (strCompare(s1, s2) > 0) { 
		printf("\"%s\" is greater than \"%s\", very good\n", s1, s2);
	} else {
		printf("oops, \"%s\" should be greater than \"%s\"\n", s1, s2);
	}

	s1 = "Texas"; s2 = "California";
	if (strCompare(s1, s2) > 0) { 
		printf("\"%s\" is greater than \"%s\", very good\n", s1, s2);
	} else {
		printf("oops, \"%s\" should be greater than \"%s\"\n", s1, s2);
	}

	/* test strCompare2 */
	s1 = "apple"; s2 = "Apricot";
	if (strCompare2(s1, s2) < 0) { 
		printf("\"%s\" is less than \"%s\", very good\n", s1, s2);
	} else {
		printf("oops, \"%s\" should be less than \"%s\"\n", s1, s2);
	}

	s1 = "Batman!"; s2 = "bat man";
	if (strCompare2(s1, s2) == 0) { 
		printf("\"%s\" is equal to \"%s\", very good\n", s1, s2);
	} else {
		printf("oops, \"%s\" should be equal to \"%s\"\n", s1, s2);
	}

	s1 = "OMG, WTF?"; s2 = "oh my goodness, what the heck?";
	if (strCompare2(s1, s2) > 0) { 
		printf("\"%s\" is greater than \"%s\", very good\n", s1, s2);
	} else {
		printf("oops, \"%s\" should be greater than \"%s\"\n", s1, s2);
	}

	/* test maze */		
	srand(magic_number);
	makeMaze();
	recodeMaze();
	printf("recursive solution to the maze\n");
	printMaze();
	solveMazeRec(0, start_col);	
	printMaze();
	printf("\n\n\n");
	
	printf("iterative solution to the maze\n");
	srand(magic_number);
	makeMaze();
	recodeMaze();
	printMaze();
	solveMazeIt(0, start_col);
	printMaze();


	/* test Martian */
	Martian change1 = changeIt(15);
	printf("change 1 should be 0d, 3n, 0p and is: %dd %dn %dp\n", change1.dodeks, change1.nicks, change1.pennies);

	Martian change2 = changeIt(0);
	printf("change 2 should be 0d, 0n, 0p and is: %dd %dn %dp\n", change2.dodeks, change2.nicks, change2.pennies);

	Martian change3 = changeIt(17);
	printf("change 3 should be 1d, 1n, 0p and is: %dd %dn %dp\n", change3.dodeks, change3.nicks, change3.pennies);

	Martian change4 = changeIt(25);
	printf("change 4 should be 2d, 0n, 1p and is: %dd %dn %dp\n", change4.dodeks, change4.nicks, change4.pennies);

	/* A very simple and obvious test of the general form of Martian
	 * be sure and test your solution more thoroughly!!!! */
	change4 = changeIt(25, 5, 12);
	printf("change 4 should be 2d, 0n, 1p and is: %dd %dn %dp\n", change4.dodeks, change4.nicks, change4.pennies);
}
示例#5
0
int main(void) {
	const int magic_number = time(NULL);

	/* test min */
	int a[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
//	printf("the smallest of the first 10 natural numbers is: %d\n", minIt(a, 10));
	printf("the smallest of the first 10 natural numbers is: %d\n", minRec1(a, 10));
	printf("the smallest of the first 10 natural numbers is: %d\n", minRec2(a, 10));
	a[3] = -1;
//	printf("now the smallest is %d\n", minIt(a, 10));
	printf("now the smallest is %d\n", minRec1(a, 10));
	printf("now the smallest is %d\n", minRec2(a, 10));
	
	/* test sqrt */
//	printf("the sqrt of 25 is %g\n", sqrtIt(25.0, 0, 25.0));
//	printf("the sqrt of 26 is %g\n", sqrtIt(26.0, 0, 26.0));
//	printf("the sqrt of 2 is %g\n", sqrtIt(2.0, 0, 2.0));
	// printf("the sqrt of 25 is %g\n", sqrtRec(25.0, 0, 25.0));
	// printf("the sqrt of 26 is %g\n", sqrtRec(26.0, 0, 26.0));
	// printf("the sqrt of 2 is %g\n", sqrtRec(2.0, 0, 2.0));
	//printf("sqrt of 2e60 is %.17g\n", sqrtRec(2.0 * pow10(60), 1.4142135623731, 1.5 * pow10(30)));

	/* test strCompare */
	char* s1; char* s2;
	s1 = "apple"; s2 = "apricot";
	if (strCompare(s1, s2) < 0) { 
		printf("\"%s\" is less than \"%s\", very good\n", s1, s2);
	} else {
		printf("oops, \"%s\" should be less than \"%s\"\n", s1, s2);
	}
			
	s1 = "Apple"; s2 = "apple";
	if (strCompare(s1, s2) < 0) { 
		printf("\"%s\" is less than \"%s\", very good\n", s1, s2);
	} else {
		printf("oops, \"%s\" should be less than \"%s\"\n", s1, s2);
	}

	s1 = "baby boy"; s2 = "banana";
	if (strCompare(s1, s2) < 0) { 
		printf("\"%s\" is less than \"%s\", very good\n", s1, s2);
	} else {
		printf("oops, \"%s\" should be less than \"%s\"\n", s1, s2);
	}

	s1 = "a rather silly string"; s2 = "a rather silly string";
	if (strCompare(s1, s2) == 0) { 
		printf("\"%s\" is equal to \"%s\", very good\n", s1, s2);
	} else {
		printf("oops, \"%s\" should be equal to \"%s\"\n", s1, s2);
	}

	s1 = "12345"; s2 = "12345";
	if (strCompare(s1, s2) == 0) { 
		printf("\"%s\" is equal to \"%s\", very good\n", s1, s2);
	} else {
		printf("oops, \"%s\" should be equal to \"%s\"\n", s1, s2);
	}

	s1 = "Numbers: 12345"; s2 = "12345";
	if (strCompare(s1, s2) > 0) { 
		printf("\"%s\" is greater than \"%s\", very good\n", s1, s2);
	} else {
		printf("oops, \"%s\" should be greater than \"%s\"\n", s1, s2);
	}

	s1 = "Texas"; s2 = "California";
	if (strCompare(s1, s2) > 0) { 
		printf("\"%s\" is greater than \"%s\", very good\n", s1, s2);
	} else {
		printf("oops, \"%s\" should be greater than \"%s\"\n", s1, s2);
	}

	/* test strCompare2 */
	s1 = "apple"; s2 = "Apricot";
	if (strCompare2(s1, s2) < 0) { 
		printf("\"%s\" is less than \"%s\", very good\n", s1, s2);
	} else {
		printf("oops, \"%s\" should be less than \"%s\"\n", s1, s2);
	}

	s1 = "Batman!"; s2 = "bat man";
	if (strCompare2(s1, s2) == 0) { 
		printf("\"%s\" is equal to \"%s\", very good\n", s1, s2);
	} else {
		printf("oops, \"%s\" should be equal to \"%s\"\n", s1, s2);
	}

	s1 = "OMG, WTF?"; s2 = "oh my goodness, what the heck?";
	if (strCompare2(s1, s2) > 0) { 
		printf("\"%s\" is greater than \"%s\", very good\n", s1, s2);
	} else {
		printf("oops, \"%s\" should be greater than \"%s\"\n", s1, s2);
	}

	printf("********************************************\n");


	s1 = "!79857#*#*b*&atm#@@an 989^";
	s2 = "     b   a   t   m   a   ! n";

	if (strCompare2(s1, s2) == 0) {
		printf("Good\n");
	}
	else {
		printf("Bad\n");
	}

	s1 = "CAPITAL LETTERS DON'T MATTER FOR STRCOMPARE2";
	s2 = "capital letters don't matter for strcompare2";

	if (strCompare2(s1, s2) == 0) {
		printf("Good\n");
	}
	else {
		printf("Bad\n");
	}

	s1 = "asdf ASDF jkl JKL a   *&@#^&^#";
	s2 = "asdf ASDF jkl JKL b   *&@#^&^#";
	if (strCompare2(s1, s2) == -1) {
		printf("Good\n");
	}
	else {
		printf("Bad\n");
	}

	printf("Strong test case:\n");
	s1 = "strongg";
	s2 = "strong";
	printf("%d\n", strCompare(s1, s2));
	printf("%d\n", strCompare2(s1, s2));

	/* test maze */		
	srand(magic_number);
	makeMaze();
	recodeMaze();
	printf("recursive solution to the maze\n");
	fflush(stdout);
	solveMazeRec(0, start_col);
	printMaze();
	printf("\n\n\n");
	
//	printf("iterative solution to the maze\n");
//	srand(magic_number);
//	makeMaze();
//	recodeMaze();
//	solveMazeIt(0, start_col);
//	printMaze();
/*
	printf("\n");
	printf("\n");
	printf("\n");
	printf("\n");
	printf("\n");

	// for (double j = 3; j < 2e3; j+= 2) {
	// 	printf("the sqrt of %g is %g expected: %g\n", j, sqrtRec(j, 0, 9000), sqrt(j));
	// 	double_equals(sqrtRec(j, 0, 9000), sqrt(j));
	// }


	printf("\n");
	printf("\n");
	printf("\n");
	printf("\n");
	printf("\n");

	//printf("%.17g\n", sqrtRec(27.0, 0.0, 50.0));
*/
	/* test Martian */
/*	Martian change1 = change(15);
	printf("change 1 should be 0d, 3n, 0p and is: %dd %dn %dp\n", change1.dodeks, change1.nicks, change1.pennies);

	int c = 0;
int d = 0;
int n = 0;
int p = 0;
int i = 0;
int iterate = 20;
FILE *fp;
fp = fopen("output.txt", "r");

while (!feof(fp) && i < iterate) {
  change1 = change(i);
  fscanf(fp, "%d", &c);
  fscanf(fp, "%d", &d);
  fscanf(fp, "%d", &n);
  fscanf(fp, "%d", &p);
  printf("change: %d %d %d %d\n", i ,change1.dodeks, change1.nicks, change1.pennies);
  printf("python: %d %d %d %d\n", c, d, n, p);
  assert(change1.pennies == p);
  assert(change1.nicks == n);
  assert(change1.dodeks == d);
  i++;
}
fclose(fp);

	Martian change2 = change(0);
	printf("change 2 should be 0d, 0n, 0p and is: %dd %dn %dp\n", change2.dodeks, change2.nicks, change2.pennies);

	Martian change3 = change(17);
	printf("change 3 should be 1d, 1n, 0p and is: %dd %dn %dp\n", change3.dodeks, change3.nicks, change3.pennies);

	Martian change4 = change(25);
	printf("change 4 should be 2d, 0n, 1p and is: %dd %dn %dp\n", change4.dodeks, change4.nicks, change4.pennies);
*/
    //************************************************* PERSONAL TESTS *****************************************************//
    Martian change5 = change(16);
    printf("change 5 should be 0d, 3n, 1p and is: %dd %dn %dp\n", change5.dodeks, change5.nicks, change5.pennies);

	/* A very simple and obvious test of the general form of Martian
	 * be sure and test your solution more thoroughly!!!! */
	Martian change6 = change(25, 5, 12);
	printf("change 6 should be 2d, 0n, 1p and is: %dd %dn %dp\n", change6.dodeks, change6.nicks, change6.pennies);

    Martian change7 = change(16, 5, 12);
    printf("change 7 should be 0d, 3n, 1p and is: %dd %dn %dp\n", change7.dodeks, change7.nicks, change7.pennies);

    Martian change8 = change(20, 5, 10);
    printf("change 8 should be 2d, 0n, 0p and is: %dd %dn %dp\n", change8.dodeks, change8.nicks, change8.pennies);

    Martian change9 = change(21, 5, 10);
    printf("change 9 should be 2d, 0n, 1p and is: %dd %dn %dp\n", change9.dodeks, change9.nicks, change9.pennies);

/*
    printf("\n");
	printf("\n");
	printf("\n");
	printf("\n");
	printf("\n");
	printf("\n");
	printf("\n");
	printf("\n");
	printf("\n");
	printf("\n");
	printf("\n");
	printf("\n");
	printf("\n");
	n = 5;
	int z[] = {0, 0, 0, 0, 0};
	printf("Min of z: %d\n", minRec1(z, n));
	printf("Min of z: %d\n", minRec2(z, n));
	int oi[] = {-5, 0, 0, 0, -4};
	printf("Min of oi: %d\n", minRec1(oi, n));
	printf("Min of oi: %d\n", minRec2(oi, n));
	int koi[] = {-5, -234, -2525, 2, 4};
	printf("Min of koi: %d\n", minRec1(koi, n));
	printf("Min of koi: %d\n", minRec2(koi, n));
	char* str1 = "Genji";
	char* str2 = "GENJI";
	printf("%d\n", strCompare(str1, str2));
	printf("%d\n", strCompare2(str1, str2));
	str1 = " hehe xD";
	str2 = "hehe xD";
	printf("%d\n", strCompare(str1, str2));
	printf("%d\n", strCompare2(str1, str2));
	printf("\n");
	printf("\n");
	printf("\n");
	printf("\n");
	printf("\n");
	printf("\n");
	printf("\n");
	printf("\n");
	printf("\n");
	printf("\n");
	printf("\n");
	printf("\n");
	printf("\n");

	Martian change45 = change(28);
    printf("%dd %dn %dp\n", change45.dodeks, change45.nicks, change45.pennies);

    */
}