lab10.c - #include #include #include...

Info icon This preview shows pages 1–6. Sign up to view the full content.

View Full Document Right Arrow Icon
#include <stdio.h> #include <stdlib.h> #include <math.h> #include <draw.h> // FORWARD DECLARATIONS int exercise1(void); void exercise2(void); void exercise3(void); int stringCompare(char * pStr1, char * pStr2); char * getToken(char * pSource, char delim, char * pDest, int destLen); void processDrawCommand(char source[]); int main(int argc, char * argv[]) { if (exercise1() != 0) { return 1; } if (exercise2() != 0) { return 1; } exercise3(); return 0; }
Image of page 1

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
int exercise1(void) { int i; int ret = 0; // assume success; changes if any test fails. int compare; char * testA[] = { "a", "a", "b", "abc", "ab", "abc", "" }; char * testB[] = { "a", "b", "a", "ab", "abc", "abc", "" }; int testVal[] = { 0, -1, 1, 99, -99, 0, 0 }; printf("BEGIN EXERCISE 1\n"); // Run each of the tests, comparing an element from the testA array // against the same-index element from the testB array. The expected // result from stringCompare() is in the same-index element of the // testVal array. for (i = 0; i < sizeof(testA) / sizeof(char *); i++) { // Call the stringCompare function with this test's values compare = stringCompare(testA[i], testB[i]); // Did we get the expected result? if (compare != testVal[i]) { // Nope. Show 'em which test failed. printf("Bad comparison %d: expected %d, got %d\n", i, testVal[i], compare); ret = 1; } }
Image of page 2
// Exercise complete. printf("END EXERCISE 1\n\n"); // Let the caller know if it succeeded return ret; } /** * Compare both strings. Return a value less than, equal to, or greater than * zero depending on whether pStr1 is lexicographically * less than, equal to, or greater than pStr2. * * The strings are compared character-by-character, until there is a difference, * or until the characters being compared are both the null terminator, '\0'. * At that point, the difference of the ascii values between the two characters * being compared is returned (character from second string subtracted from * character from first string). * * @param pStr1 * The first string to be compared * * @param pStr2 * The second string to be compared * * @return * 0 if the two strings are identical in length and contents * <0 if the first string is lexicographically less than the second string
Image of page 3

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
* >0 if the first string is lexicographically greater than the second string */ int stringCompare(char * pStr1, char * pStr2) { // COMPLETE THIS FUNCTION // (WRITE THE CODE HERE) int i=0; int testVal; while (pStr1[i]!='\0'&& pStr2[i]!='\0') { if(pStr1[i]!=pStr2[i]) { return pStr1[i]-pStr2[i]; } i++; } if(pStr1[i]=='\0') { return -(pStr2[i]); } if (pStr2[i]=='\0') { return pStr1[i]; } return 0; } /** * Read from a file and split each line of text into its constituent tokens
Image of page 4
*/ void exercise2(void) { FILE * inputFile; char line[64]; char * pNextSource; char token[32]; printf("BEGIN EXERCISE 2\n"); // Open the file /public/lab10ex2.txt for reading. Store the handle in
Image of page 5

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern