Summary Files Commits Branches @commit @diff @stats
path: root / Year 1 / Programming introduction / Homeworks / homework2.cpp
diff options
context:
space:
mode:
1 files changed, 97 insertions, 50 deletions
-rw-r--r--Year 1/Programming introduction/Homeworks/homework2.cpp147
diff --git a/Year 1/Programming introduction/Homeworks/homework2.cpp b/Year 1/Programming introduction/Homeworks/homework2.cpp
index 98a132a..fa0464c 100644
--- a/Year 1/Programming introduction/Homeworks/homework2.cpp
+++ b/Year 1/Programming introduction/Homeworks/homework2.cpp
@@ -197,56 +197,93 @@ void ex1() {
const int MAX_STR = 256;
-void substr(const char *src, char *dstLeft, char *dstRight, int skipStart, int skipEnd) {
+void subStr(char *str, char *dst, int startInd, int endInd) {
+ for (int i = startInd, j = 0; i <= endInd; i++)
+ dst[j++] = str[i];
+}
+
+void splitStr(char *src, char *dstLeft, char *dstRight, int skipStart, int skipEnd) {
+ subStr(src, dstLeft, 0, skipStart - 1);
int len = strlen(src);
- for (int i = 0, j = 0, k = 0; i < len; i++) {
- if (i < skipStart) {
- dstLeft[j++] = src[i];
- dstLeft[j] = '\0';
- }
- else if (i > skipEnd) {
- dstRight[k++] = src[i];
- dstRight[k] = '\0';
- }
+ subStr(src, dstRight, skipEnd + 1, len - 1);
+}
+
+void symSplitStr(char *src, char *dstLeft, char *dstRight, char *dstLeftover, int skipStart, int skipEnd) {
+ int len = strlen(src);
+
+ if (len - skipEnd > skipStart) {
+ subStr(src, dstLeft, 0, skipStart - 1);
+ subStr(src, dstRight, skipEnd + 1, skipEnd + skipStart);
+ subStr(src, dstLeftover, skipEnd + skipStart + 1, len - 1);
+ }
+ else {
+ subStr(src, dstLeftover, 0, skipStart - 1 - (len - 1 - skipEnd));
+ subStr(src, dstLeft, skipStart - (len - 1 - skipEnd), skipStart - 1);
+ subStr(src, dstRight, skipEnd + 1, len - 1);
}
}
bool isWord(char *str);
-bool checkForD(char *str, int ind) {
- char tmpLeft[MAX_STR] = { '\0' }, tmpRight[MAX_STR] = { '\0' }, tmpLeftover[MAX_STR] = { '\0' };
-
- substr(str, tmpLeft, tmpRight, ind, ind);
+int checkForD(char *str) {
+ int len = strlen(str);
+ int status = -1;
- // TODO: Fix cases like CAABDAAB (this should be true)
- int lenL = strlen(tmpLeft), lenR = strlen(tmpRight);
- if (lenL != lenR && lenL > 0 && lenR > 0) {
- char tmptmp[MAX_STR];
- if (lenL > lenR) {
- substr(tmpLeft, tmpLeftover, tmptmp, lenL - lenR - 1, lenL - lenR - 1);
+ for (int i = 0; i < len; i++) {
+ if (str[i] == 'D') {
+ if (status == -1) status = 0;
+ char tmpLeft[MAX_STR] = { '\0' }, tmpRight[MAX_STR] = { '\0' }, tmpLeftover[MAX_STR] = { '\0' };
+
+ int len = strlen(str);
+ if (i != len - i - 1 && i > 0 && i < len - 1)
+ symSplitStr(str, tmpLeft, tmpRight, tmpLeftover, i, i);
+ else
+ splitStr(str, tmpLeft, tmpRight, i, i);
+
+ if (strcmp(tmpLeft, tmpRight) == 0)
+ status |= isWord(tmpLeft) && isWord(tmpLeftover);
}
}
- if (strcmp(tmpLeft, tmpRight) == 0)
- return isWord(tmpLeft);
- return false;
+ return status;
}
-bool checkForC(char *str, int ind) {
- char tmpLeft[MAX_STR] = { '\0' }, tmpRight[MAX_STR] = { '\0' };
+int checkForC(char *str) {
+ int len = strlen(str);
- substr(str, tmpLeft, tmpRight, ind, ind);
+ for (int i = 0; i < len; i++) {
+ if (str[i] == 'C') {
+ char tmpLeft[MAX_STR] = { '\0' }, tmpRight[MAX_STR] = { '\0' };
- return isWord(tmpLeft) && isWord(tmpRight);
+ splitStr(str, tmpLeft, tmpRight, i, i);
+
+ return isWord(tmpLeft) && isWord(tmpRight);
+ }
+ }
+ return -1;
}
-bool checkForAAB(char *str, int ind) {
- char tmpLeft[MAX_STR] = { '\0' }, tmpRight[MAX_STR] = { '\0' };
+int checkForAAB(char *str) {
+ int len = strlen(str), indA = -1, indB = -1;
+ for (int i = 0; i < len; i++) {
+ if ((i < len - 1) && indA < 0 && (str[i] == 'A') && (str[i + 1] == 'A'))
+ indA = ++i;
+ else if (indA >= 0 && str[i] == 'B')
+ indB = i;
+ }
+
+ if (indA >= 0 && indB > 0) {
+ if (indA > 1 || indB < len - 1)
+ return false;
- substr(str, tmpLeft, tmpRight, ind, ind + 2);
+ char tmpMiddle[MAX_STR] = { '\0' };
- return isWord(tmpLeft) && isWord(tmpRight);
+ subStr(str, tmpMiddle, indA + 1, indB - 1);
+
+ return isWord(tmpMiddle);
+ }
+ return -1;
}
bool isWord(char *str) {
@@ -256,35 +293,45 @@ bool isWord(char *str) {
else if (len > 64)
return false;
- std::cout << str << std::endl;
+ int tmp;
- bool result = false; int encounter = 0;
-
- for (int i = 0; i < len; i++) {
- if (str[i] == 'D') {
- encounter++;
- result |= checkForD(str, i);
+ tmp = checkForD(str);
+ if (tmp < 0) {
+ tmp = checkForC(str);
+ if (tmp < 0) {
+ tmp = checkForAAB(str);
}
}
+ if (tmp > 0) return tmp;
- if (encounter) return result;
-
- encounter = 0;
- for (int i = 0; i < len; i++) {
- if (str[i] == 'C') {
- encounter++;
- result |= checkForC(str, i);
+ tmp = checkForC(str);
+ if (tmp < 0) {
+ tmp = checkForD(str);
+ if (tmp < 0) {
+ tmp = checkForAAB(str);
}
}
+ if (tmp > 0) return tmp;
- if (encounter) return result;
+ tmp = checkForAAB(str);
+ if (tmp < 0) {
+ tmp = checkForD(str);
+ if (tmp < 0) {
+ tmp = checkForC(str);
+ }
+ }
+ if (tmp > 0) return tmp;
- for (int i = 0; i < len; i++) {
- if ((i < len - 2) && (str[i] == 'A') && (str[i + 1] == 'A') && (str[i + 2] == 'B'))
- result |= checkForAAB(str, i);
+ tmp = checkForAAB(str);
+ if (tmp < 0) {
+ tmp = checkForC(str);
+ if (tmp < 0) {
+ tmp = checkForD(str);
+ }
}
+ if (tmp > 0) return tmp;
- return result;
+ return false;
}
void ex2() {