Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // main.cpp
- // Project 5
- //
- // Created by Andy Yu on 11/17/14.
- // Copyright (c) 2014 Hongseok (Andy) Yu. All rights reserved.
- //
- #define CRT_SECURE_NO_WARNINGS
- #include <cstring>
- #include <iostream>
- #include <cassert>
- using namespace std;
- const int MAX_WORD_LENGTH = 20;
- void delChar(int pos, int max, char array[][MAX_WORD_LENGTH+1])
- {
- char temp[20];
- for (int i = pos; i < max-1; i++)
- {
- strcpy(temp, array[i]);
- strcpy(array[i], array[i+1]);
- strcpy(array[i+1], temp);
- }
- }
- void delInt(int pos, int max, int array[])
- {
- int temp = -1;
- for (int i = pos; i < max-1; i++)
- {
- temp = array[i];
- array[i] = array[i+1];
- array[i+1] = temp;
- }
- }
- void delRule(int pos, int max, int array[], char array2[][MAX_WORD_LENGTH+1], char array3[][MAX_WORD_LENGTH+1])
- {
- delInt(pos, max, array);
- delChar(pos, max, array2);
- delChar(pos, max, array3);
- }
- int cleanRule(int length, char array2[], char array3[])
- {
- if (strlen(array2) == 0)
- return 1;
- int j = 0;
- while (array2[j] != '\0')
- {
- if (!(isalpha(array2[j])))
- return 1;
- if (isupper(array2[j]))
- array2[j] = tolower(array2[j]);
- j++;
- }
- if (strlen(array3) == 0)
- return 1;
- j = 0;
- while (array3[j] != '\0')
- {
- if (!(isalpha(array3[j])))
- return 1;
- if (isupper(array3[j]))
- array3[j] = tolower(array3[j]);
- j++;
- }
- return 2;
- }
- int standardizeRules(int distance[],
- char word1[][MAX_WORD_LENGTH+1],
- char word2[][MAX_WORD_LENGTH+1],
- int nRules)
- {
- int counter = 0;
- int cleancounter = nRules;
- for (int i = 0; i < cleancounter; i++)
- {
- if (distance[i] < 0) // check that distance is positive
- {
- distance[i] = -1; // if not mark to remove respective values from the arrays
- }
- if (cleanRule(nRules, word1[i], word2[i]) == 1)
- {
- // delete match rule if it contains a nonalpha character
- distance[i] = -1;
- }
- for (int k = 0; k < nRules; k++) // check for any repeated w1 / w2 values and remove the smaller distanced one
- {
- if (k != i)
- {
- if (strcmp(word1[k],word2[k]) == 0)
- {
- if (strcmp(word1[i],word2[i])== 0)
- {
- if (!(distance[k] == -1|| distance [i] == -1))
- {
- if (distance[k] > distance[i])
- {
- distance[i] = -1;
- }
- else
- {
- distance[k] = -1;
- }
- }
- }
- }
- if (strcmp(word2[i],word1[k]) == 0)
- {
- if (strcmp(word1[i], word2[k]) == 0)
- {
- if (!(distance[k] == -1|| distance [i] == -1))
- {
- if (distance[k] > distance[i])
- {
- distance[i] = -1;
- }
- else
- {
- distance[k] = -1;
- }
- }
- }
- }
- }
- }
- }
- for (int i = 0; i < nRules; i++)
- {
- if (distance[i] == -1)
- {
- delRule(i, nRules, distance, word1, word2);
- counter++;
- }
- }
- return nRules - counter;
- }
- int determineQuality(const int distance[],
- const char word1[][MAX_WORD_LENGTH+1],
- const char word2[][MAX_WORD_LENGTH+1],
- int nRules,
- const char document[])
- {
- int counter = 0;
- char doccopy[200] = {}; // copy of document
- char arraycopy[][20] = {};
- int i = 0;
- int k = 0;
- int words = 0;
- char temp[20];
- int templ = 0;
- while (document[i]!= '\0') // copy all alphabetical characters to document
- {
- if (document[i] == ' ')
- {
- doccopy[k] = ' ';
- k++;
- }
- if (isalpha(document[i]))
- {
- doccopy[k] = document[i];
- toupper(doccopy[k]); // change case
- k++;
- }
- i++;
- }
- while (doccopy[i]!= '\0') // write document to char 2d array
- {
- int j = 0;
- if (!(doccopy[i] == ' '))
- {
- temp[templ] = doccopy[i];
- templ++;
- }
- else
- {
- strcpy(arraycopy[j],temp);
- j++;
- }
- words = j;
- }
- for (int m = 0; m < nRules; m++)
- {
- for (int n = 0; n < words; n++)
- {
- if (strcmp(word1[m], arraycopy[n]) == 0)
- {
- if (n >= distance[m])
- {
- for (int i = 1; i < distance[m]+1; i++)
- {
- if (strcmp(arraycopy[n-i], word2[m]))
- {
- counter++;
- }
- }
- }
- if (n <= words - distance[m] - 1)
- {
- for (int i = 1; i < distance[m]; i++)
- {
- if (strcmp(arraycopy[n+i], word2[m]))
- {
- counter++;
- }
- }
- }
- }
- }
- }
- return counter;
- }
- int main(int argc, const char * argv[])
- {
- auto does_test1_pass = [](int distance[], char word1[][MAX_WORD_LENGTH + 1], char word2[][MAX_WORD_LENGTH + 1])
- {
- const int returnValue = 4;
- int match = 0;
- int y = 0;
- while (y < returnValue)
- {
- if (distance[y] == 2 && (strcmp(word1[y], "mad") == 0) && (strcmp(word2[y], "scientist") == 0))
- match++;
- y++;
- }
- if (match != 1)
- return false;
- y = 0;
- match = 0;
- while (y < returnValue)
- {
- if (distance[y] == 4 && (strcmp(word1[y], "deranged") == 0) && (strcmp(word2[y], "robot") == 0))
- match++;
- y++;
- }
- if (match != 1)
- return false;
- y = 0;
- match = 0;
- while (y < returnValue)
- {
- if (distance[y] == 1 && (strcmp(word1[y], "nefarious") == 0) && (strcmp(word2[y], "plot") == 0))
- match++;
- else if (distance[y] == 1 && (strcmp(word1[y], "plot") == 0) && (strcmp(word2[y], "nefarious") == 0))
- match++;
- y++;
- }
- if (match != 1)
- return false;
- y = 0;
- match = 0;
- while (y < returnValue)
- {
- if (distance[y] == 13 && (strcmp(word1[y], "have") == 0) && (strcmp(word2[y], "mad") == 0))
- match++;
- y++;
- }
- if (match != 1)
- return false;
- return true;
- };
- auto does_test3_pass = [](int distance[], char word1[][MAX_WORD_LENGTH + 1], char word2[][MAX_WORD_LENGTH + 1])
- {
- const int returnValue = 2;
- int match = 0;
- int y = 0;
- while (y < returnValue)
- {
- if (distance[y] == 5 && (strcmp(word1[y], "sad") == 0) && (strcmp(word2[y], "happy") == 0))
- match++;
- y++;
- }
- if (match != 1)
- return false;
- y = 0;
- match = 0;
- while (y < returnValue)
- {
- if (distance[y] == 13 && (strcmp(word1[y], "net") == 0) && (strcmp(word2[y], "ten") == 0))
- match++;
- y++;
- }
- if (match != 1)
- return false;
- return true;
- };
- int distance1[7] = { 2, 4, 1, 3, 2, 1, 13 };
- char word1_1[7][MAX_WORD_LENGTH + 1] = { "mad", "deranged", "NEFARIOUS", "half-witted", "robot", "plot", "have" };
- char word2_1[7][MAX_WORD_LENGTH + 1] = { "scientist", "robot", "plot", "assistant", "deranged", "Nefarious", "mad" };
- int a = standardizeRules(distance1, word1_1, word2_1, 7);
- cout << a << endl;
- assert(does_test1_pass(distance1, word1_1, word2_1));
- int distance2[3] = { -1, 0, 1 };
- char word1_2[3][MAX_WORD_LENGTH + 1] = { "mad", "deranged", "nefarious" };
- char word2_2[3][MAX_WORD_LENGTH + 1] = { "scientist", "robot", "" };
- int b = standardizeRules(distance2, word1_2, word2_2, 3);
- assert(b == 0);
- int distance3[5] = { 5, 1, 10, 5, 13 };
- char word1_3[5][MAX_WORD_LENGTH + 1] = { "sad", "happy", "ten", "net", "net" };
- char word2_3[5][MAX_WORD_LENGTH + 1] = { "happy", "sad", "net", "ten", "ten" };
- int c = standardizeRules(distance3, word1_3, word2_3, 5);
- assert(does_test3_pass(distance3, word1_3, word2_3));
- int distance4[1] = { 1 };
- char word1_4[1][MAX_WORD_LENGTH + 1] = { "mad" };
- char word2_4[1][MAX_WORD_LENGTH + 1] = { "mad" };
- int d = standardizeRules(distance4, word1_4, word2_4, 1);
- assert(d == 1);
- const int TEST1_NCRITERIA = 4;
- int test1dist[TEST1_NCRITERIA] = {
- 2, 4, 1, 13
- };
- char test1w1[TEST1_NCRITERIA][MAX_WORD_LENGTH + 1] = {
- "mad", "deranged", "nefarious", "have"
- };
- char test1w2[TEST1_NCRITERIA][MAX_WORD_LENGTH + 1] = {
- "scientist", "robot", "plot", "mad"
- };
- assert(determineQuality(test1dist, test1w1, test1w2, TEST1_NCRITERIA,
- "The mad UCLA scientist unleashed a deranged evil giant robot.") == 2);
- assert(determineQuality(test1dist, test1w1, test1w2, TEST1_NCRITERIA,
- "The mad UCLA scientist unleashed a deranged robot.") == 2);
- assert(determineQuality(test1dist, test1w1, test1w2, TEST1_NCRITERIA,
- "**** 2014 ****") == 0);
- assert(determineQuality(test1dist, test1w1, test1w2, TEST1_NCRITERIA,
- " That plot: NEFARIOUS!") == 1);
- assert(determineQuality(test1dist, test1w1, test1w2, TEST1_NCRITERIA,
- "deranged deranged robot deranged robot robot") == 1);
- assert(determineQuality(test1dist, test1w1, test1w2, TEST1_NCRITERIA,
- "Two mad scientists suffer from deranged-robot fever.") == 0);
- const int TEST2_NCRITERIA = 3;
- int test2dist[TEST2_NCRITERIA] = {
- 4, 1, 2
- };
- char test2w1[TEST2_NCRITERIA][MAX_WORD_LENGTH + 1] = {
- "mad", "micheal", "presents"
- };
- char test2w2[TEST2_NCRITERIA][MAX_WORD_LENGTH + 1] = {
- "mad", "bay", "explosions"
- };
- assert(determineQuality(test2dist, test2w1, test2w2, TEST2_NCRITERIA,
- "Mad Micheal Bay presents mad explosions!") == 3);
- assert(determineQuality(test2dist, test2w1, test2w2, TEST2_NCRITERIA,
- "Mad Micheal Bay presents explosions!") == 2);
- assert(determineQuality(test2dist, test2w1, test2w2, TEST2_NCRITERIA,
- "Mad mad") == 1);
- assert(determineQuality(test2dist, test2w1, test2w2, TEST2_NCRITERIA,
- "Mad") == 0);
- assert(determineQuality(test2dist, test2w1, test2w2, TEST2_NCRITERIA,
- "Micheal presents explosions at the bay!") == 1);
- assert(determineQuality(test2dist, test2w1, test2w2, TEST2_NCRITERIA,
- "explosions presents bay micheal bay bay micheal mad") == 2);
- cout << "All tests succeed";
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement