diff --git a/pset13/1-1-GetScore.c b/pset13/1-1-GetScore.c new file mode 100644 index 0000000..dad3fbf --- /dev/null +++ b/pset13/1-1-GetScore.c @@ -0,0 +1,30 @@ +#include + +#define MAX 48 + +int getScore(char *s); + +int main() { + char input[MAX]; + int score; + + scanf("%s", input); + score = getScore(input); + printf("%d\n", score); + + return 0; +} + +/* 请在这里填写答案 */ +int getScore(char *s) { + int score = 0; + while (*s != '\0') { + if (*s == 'W') { // Win + score += 3; + } else if (*s == 'D') { // Draw + score += 1; + } // If lost, the score is unchanged + s++; + } + return score; +} diff --git a/pset13/1-2-GetScore.c b/pset13/1-2-GetScore.c new file mode 100644 index 0000000..8c2b801 --- /dev/null +++ b/pset13/1-2-GetScore.c @@ -0,0 +1,47 @@ +#include +#include + +int *create(int n); +void cal(int *array, int size); + +int main() { + int *array, n, i; + + scanf("%d", &n); + array = create(n); + cal(array, n); + for (i = 0; i < n; i++) { + printf("%d", *(array + i)); + if (i == n - 1) + printf("\n"); + else + printf(" "); + } + + free(array); + + return 0; +} + +/* 请在这里填写答案 */ +int *create(int n) { return malloc(n * sizeof(int)); } + +void cal(int *array, int size) { + for (int i = 0; i < size; i++) { + scanf("%d", &array[i]); + } + + int sum = 0, max = array[0], min = array[0]; + for (int i = 0; i < size; i++) { + sum += array[i]; + if (array[i] > max) + max = array[i]; + else if (array[i] < min) + min = array[i]; + } + + printf("%d\n", sum); + printf("%.2f\n", sum / (float)size); + printf("%d\n", max); + printf("%d\n", min); +} diff --git a/pset13/1-3-DynamicMemoryAllocation.c b/pset13/1-3-DynamicMemoryAllocation.c new file mode 100644 index 0000000..91863d7 --- /dev/null +++ b/pset13/1-3-DynamicMemoryAllocation.c @@ -0,0 +1,87 @@ +#include +#include + +char **create1(int n); +void create2(char **strPtr, int n); +void fill(char **strPtr, int n); + +int main() { + int n, i, j; + char **strPtr; + + scanf("%d", &n); + strPtr = create1(n * 2 + 1); + create2(strPtr, n * 2 + 1); + fill(strPtr, n); + + for (i = 0; i < 2 * n + 1; i++) { + printf("%s\n", strPtr[i]); + } + + for (i = 0; i < n * 2 + 1; i++) + free(strPtr[i]); + free(strPtr); + + return 0; +} + +/* 请在这里填写答案 */ +char **create1(int n) { + char **strPtr = + malloc(sizeof(char *) * n); // This char * is soooooo important + return strPtr; +} + +void create2(char **strPtr, int n) { + for (int i = 0; i < n; i++) { + strPtr[i] = malloc(sizeof(char) * (n + 1)); + } +} + +void fill(char **strPtr, int n) { + int loc; + if (n == 0) { // Take care of special case + strPtr[0][0] = 'X'; // When n == 0, it is only X + return; + } + for (int i = 0; i <= n; i++) { + loc = 0; + for (int j = 0; j < n - i; j++) { + strPtr[i][loc++] = ' '; + } + if (i % n == 0) { + strPtr[i][loc++] = 'X'; + } else { + strPtr[i][loc++] = '/'; + } + for (int j = 0; j < 2 * i - 1; j++) { + strPtr[i][loc++] = ' '; + } + if (i != 0) { + if (i == n) { + strPtr[i][loc++] = 'X'; + } else { + strPtr[i][loc++] = '\\'; + } + } + strPtr[i][loc] = '\0'; // EOL + } + for (int i = n + 1; i < 2 * n + 1; i++) { + loc = 0; + for (int j = 0; j < i - n; j++) { + strPtr[i][loc++] = ' '; + } + if (i != n * 2) { + strPtr[i][loc++] = '\\'; + } else { + strPtr[i][loc++] = 'X'; + } + for (int j = 0; j < -2 * (i - n) + 2 * n - 1; j++) { + strPtr[i][loc++] = ' '; + } + if (i != 2 * n) { + strPtr[i][loc++] = '/'; + } + strPtr[i][loc] = '\0'; // EOL + } +} diff --git a/pset13/1-4-LocateSubStr.c b/pset13/1-4-LocateSubStr.c new file mode 100644 index 0000000..2fea32f --- /dev/null +++ b/pset13/1-4-LocateSubStr.c @@ -0,0 +1,41 @@ +#include + +char *locatesubstr(char *str1, char *str2); +int main() { + char str1[505], str2[505]; + char *p; + // Was gets in the example, but it is unsafe. + fgets(str1, 505, stdin); + fgets(str2, 505, stdin); + p = locatesubstr(str1, str2); + + if (p == NULL) + printf("NULL!\n"); + else + puts(p); + + return 0; +} + +/* 请在这里填写答案 */ +char *locatesubstr(char *str1, char *str2) { + char *loc = NULL; + int i; + while (*str1 != '\0' && loc == NULL) { + if (*str1 == *str2) { // Head matched + loc = str1; + i = 1; + while (*(str2 + i) != '\0') { + if (*(str1 + i) != *(str2 + i) && + *(str2 + i) != '\n' && // Because gets also inputs the \n + *(str1 + i) != '\n') { // So we need to take care of that. + loc = NULL; + break; + } + i++; + } + } + str1++; + } + return loc; +} diff --git a/pset13/1-5-GetString.c b/pset13/1-5-GetString.c new file mode 100644 index 0000000..0787716 --- /dev/null +++ b/pset13/1-5-GetString.c @@ -0,0 +1,47 @@ +#include + +int getString(char *source, char *strPtr[]); + +int main() { + char str[100005]; + char *strPtr[1005] = {0}; + int i, num; + + /* gets(str); */ + fgets(str, 100005, stdin); + num = getString(str, strPtr); + for (i = 0; i < num; i++) + puts(strPtr[i]); + + return 0; +} + +/* 请在这里填写答案 */ +int getString(char *source, char *strPtr[]) { + int count = 0; + int loc; + int j; + // Find the first ones. + for (int i = 0; source[i] != '\0'; i++) { + if (source[i] != ' ') { + strPtr[count++] = &source[i]; + loc = i; + break; + } + } + // Start populating the array by finding the spaces. + for (int i = loc; source[i] != '\0'; i++) { + if (source[i] == ' ') { + // Find the next alpha. + for (j = i; j < 1005 + i && source[j] != '\0'; j++) { + if (source[j] != ' ' && source[j] != '\n') { + strPtr[count++] = &source[j]; + break; + } + } + source[i] = '\0'; + i = j; + } + } + return count; +} diff --git a/pset13/1-6-DelChar.c b/pset13/1-6-DelChar.c new file mode 100644 index 0000000..33094ab --- /dev/null +++ b/pset13/1-6-DelChar.c @@ -0,0 +1,33 @@ +#include + +void delcharfun(char *str, char ch); + +int main() { + char ch, str[110]; + + scanf("%s", str); //读入字符串 + getchar(); //读取回车符号 + scanf("%c", &ch); //读入字符 + delcharfun(str, ch); //删除 + printf("%s\n", str); //输出删除后结果 + return 0; +} + +/* 请在这里填写答案 */ +void delcharfun(char *str, char ch) { + int i; + while (*str != '\0' && *str != '\n') { + if (*str == ch) { + // Shift this array + i = 0; + while (*(str + i + 1) != '\0') { + *(str + i) = *(str + i + 1); + i++; + } + // End this string to avoid duplicates + *(str + i) = '\0'; + str--; // Because the string is shifted + } + str++; + } +}