add 1-* (Function problems)

This commit is contained in:
juan 2021-12-09 17:26:31 +08:00
parent 06f1dc419c
commit 5e6d6c63ee
No known key found for this signature in database
GPG key ID: 5C1E5093C74F1DC7
6 changed files with 285 additions and 0 deletions

30
pset13/1-1-GetScore.c Normal file
View file

@ -0,0 +1,30 @@
#include <stdio.h>
#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;
}

47
pset13/1-2-GetScore.c Normal file
View file

@ -0,0 +1,47 @@
#include <stdio.h>
#include <stdlib.h>
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);
}

View file

@ -0,0 +1,87 @@
#include <stdio.h>
#include <stdlib.h>
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
}
}

41
pset13/1-4-LocateSubStr.c Normal file
View file

@ -0,0 +1,41 @@
#include <stdio.h>
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;
}

47
pset13/1-5-GetString.c Normal file
View file

@ -0,0 +1,47 @@
#include <stdio.h>
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;
}

33
pset13/1-6-DelChar.c Normal file
View file

@ -0,0 +1,33 @@
#include <stdio.h>
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++;
}
}