提交 6ff49511 编写于 作者: L luxin

add 6 exercises

上级 48d47ca5
...@@ -6,5 +6,5 @@ ...@@ -6,5 +6,5 @@
"C语言" "C语言"
], ],
"children": [], "children": [],
"export": [] "export": ["definition.json"]
} }
\ No newline at end of file
#include <stdio.h>
void print_link();
void print_skill_tree();
int main()
{
print_skill_tree();
return 0;
}
void print_link()
{
printf("https://bbs.csdn.net/skill/c");
}
void print_skill_tree()
{
printf("欢迎访问\"C语言技能树\"网址:");
print_link();
}
\ No newline at end of file
{
"type": "code_options",
"author": "卢昕",
"source": "definition.md",
"exercise_id":"7c71b281a3c5490e95f9ab3c99d110f0"
}
\ No newline at end of file
# 打印技能树名称和网址
定义两个函数,打印C语言技能树的名称和网址。请选出正确答案。
## 答案
```c
#include <stdio.h>
void print_link();
void print_skill_tree();
int main()
{
print_skill_tree();
return 0;
}
void print_link()
{
printf("https://bbs.csdn.net/skill/c");
}
void print_skill_tree()
{
printf("欢迎访问\"C语言技能树\"网址:");
print_link();
}
```
## 选项
### A
```c
#include <stdio.h>
void print_skill_tree()
{
printf("欢迎访问\"C语言技能树\"网址:");
print_link();
}
void print_link()
{
printf("https://bbs.csdn.net/skill/c");
}
int main()
{
print_skill_tree();
return 0;
}
```
### B
```c
#include <stdio.h>
void print_link();
void print_skill_tree();
int main()
{
print_skill_tree();
return 0;
}
void print_skill_tree()
{
printf("欢迎访问\"C语言技能树\"网址:");
void print_link()
{
printf("https://bbs.csdn.net/skill/c");
}
print_link();
}
```
### C
```c
#include <stdio.h>
void print_link(char);
void print_skill_tree();
int main()
{
print_skill_tree();
return 0;
}
void print_link(char link[])
{
printf("%s", link);
}
void print_skill_tree()
{
printf("欢迎访问\"C语言技能树\"网址:");
print_link("https://bbs.csdn.net/skill/c");
}
```
...@@ -6,5 +6,5 @@ ...@@ -6,5 +6,5 @@
"C语言" "C语言"
], ],
"children": [], "children": [],
"export": [] "export": ["return.json"]
} }
\ No newline at end of file
{ {
"type": "code_options",
"author": "卢昕",
"source": "return.md",
"exercise_id":"c08cb53f455d47fbbf244d2605f5ae6d"
} }
\ No newline at end of file
...@@ -48,50 +48,8 @@ int main() ...@@ -48,50 +48,8 @@ int main()
## 选项 ## 选项
### A
```c
#include <stdio.h>
void prime(int n)
{
int is_prime = 1, i;
if (n < 0) ### A
{
printf("%d 是一个非法输入!\n", n);
return -1;
}
else if (n < 2)
{
printf("%d 不是一个素数!\n", n);
return 0;
}
for (i = 2; i < n; i++)
if (n % i == 0)
{
is_prime = 0;
break;
}
if(is_prime == 1)
printf("%d 是一个素数!\n", n);
else
printf("%d 不是一个素数!\n", n);
}
int main()
{
int num, is_prime;
printf("请输入一个整数:");
scanf("%d", &num);
prime(num);
return 0;
}
```
### B
```c ```c
#include <stdio.h> #include <stdio.h>
...@@ -132,7 +90,7 @@ int main() ...@@ -132,7 +90,7 @@ int main()
} }
``` ```
### C ### B
```c ```c
#include <stdio.h> #include <stdio.h>
...@@ -175,7 +133,7 @@ int main() ...@@ -175,7 +133,7 @@ int main()
} }
``` ```
### D ### C
```c ```c
#include <stdio.h> #include <stdio.h>
......
...@@ -6,5 +6,5 @@ ...@@ -6,5 +6,5 @@
"C语言" "C语言"
], ],
"children": [], "children": [],
"export": [] "export": ["function_call.json"]
} }
\ No newline at end of file
#include <stdio.h>
char* csdn()
{
return "CSDN-->";
}
char* yueluqu()
{
return "岳麓区-->";
}
char* changsha()
{
return "长沙-->";
}
char* hunan()
{
return "湖南-->";
}
char* china()
{
return "中国-->";
}
int main()
{
printf("%s%s%s%s%s", china(), hunan(), changsha(), yueluqu(), csdn());
printf("https://www.csdn.net/");
return 0;
}
\ No newline at end of file
{
"type": "code_options",
"author": "卢昕",
"source": "function_call.md",
"exercise_id":"41843efba027453c9f2eea82d794cdf6"
}
\ No newline at end of file
# 打印函数调用顺序
基于函数调用的顺序,正确输出 **中国-->湖南-->长沙-->岳麓区-->CSDN-->https://www.csdn.net/** 。请选出错误答案。
## 答案
```c
#include <stdio.h>
void csdn()
{
printf("CSDN-->");
}
void yueluqu()
{
printf("岳麓区-->");
csdn();
}
void changsha()
{
printf("长沙-->");
yueluqu();
}
void hunan()
{
printf("湖南-->");
changsha();
}
void china()
{
printf("中国-->");
hunan();
}
china();
printf("https://www.csdn.net/");
int main()
{
return 0;
}
```
## 选项
### A
```c
#include <stdio.h>
void csdn()
{
printf("CSDN-->");
}
void yueluqu()
{
printf("岳麓区-->");
}
void changsha()
{
printf("长沙-->");
}
void hunan()
{
printf("湖南-->");
}
void china()
{
printf("中国-->");
}
int main()
{
china();
hunan();
changsha();
yueluqu();
csdn();
printf("https://www.csdn.net/");
return 0;
}
```
### B
```c
#include <stdio.h>
void csdn()
{
printf("CSDN-->");
}
void yueluqu()
{
printf("岳麓区-->");
csdn();
}
void changsha()
{
printf("长沙-->");
yueluqu();
}
void hunan()
{
printf("湖南-->");
changsha();
}
void china()
{
printf("中国-->");
}
int main()
{
china();
printf("https://www.csdn.net/");
return 0;
}
```
### C
```c
#include <stdio.h>
char* csdn()
{
return "CSDN-->";
}
char* yueluqu()
{
return "岳麓区-->";
}
char* changsha()
{
return "长沙-->";
}
char* hunan()
{
return "湖南-->";
}
char* china()
{
return "中国-->";
}
int main()
{
printf("%s%s%s%s%s", china(), hunan(), changsha(), yueluqu(), csdn());
printf("https://www.csdn.net/");
return 0;
}
```
{
"node_id": "569d5e11c4fc5de7844053d9a733c5e8",
"keywords": [
"C语言概述",
"概述",
"C语言"
],
"children": [],
"export": ["global_n_local_var.json"]
}
\ No newline at end of file
#include <stdio.h>
int s1, s2, s3;
int vs(int a, int b, int c)
{
int v;
v = a * b * c;
s1 = a * b;
s2 = b * c;
s3 = a * c;
return v;
}
int main()
{
int v, length, width, height;
printf("请分别输入长方体的长宽高:");
scanf("%d,%d,%d", &length, &width, &height);
v = vs(length, width, height);
printf("v=%d, s1=%d, s2=%d, s3=%d\n", v, s1, s2, s3);
return 0;
}
\ No newline at end of file
{
"type": "code_options",
"author": "卢昕",
"source": "global_n_local_var.md",
"exercise_id":"8c862340818547858499a071f828bbba"
}
\ No newline at end of file
# 长方体的体积和面积
根据长方体的长宽高求它的体积以及三个面的面积。找出正确的选项。
## 答案
```c
#include <stdio.h>
int s1, s2, s3;
int vs(int a, int b, int c)
{
int v;
v = a * b * c;
s1 = a * b;
s2 = b * c;
s3 = a * c;
return v;
}
int main()
{
int v, length, width, height;
printf("请分别输入长方体的长宽高:");
scanf("%d,%d,%d", &length, &width, &height);
v = vs(length, width, height);
printf("v=%d, s1=%d, s2=%d, s3=%d\n", v, s1, s2, s3);
return 0;
}
```
## 选项
### A
```c
#include <stdio.h>
int vs(int a, int b, int c)
{
int v;
v = a * b * c;
s1 = a * b;
s2 = b * c;
s3 = a * c;
return v;
}
int s1, s2, s3;
int main()
{
int v, length, width, height;
printf("请分别输入长方体的长宽高:");
scanf("%d,%d,%d", &length, &width, &height);
v = vs(length, width, height);
printf("v=%d, s1=%d, s2=%d, s3=%d\n", v, s1, s2, s3);
return 0;
}
```
### B
```c
#include <stdio.h>
int vs(int a, int b, int c)
{
int v, s1, s2, s3;
v = a * b * c;
s1 = a * b;
s2 = b * c;
s3 = a * c;
return v;
}
int main()
{
int v, length, width, height;
printf("请分别输入长方体的长宽高:");
scanf("%d,%d,%d", &length, &width, &height);
v = vs(length, width, height);
printf("v=%d, s1=%d, s2=%d, s3=%d\n", v, s1, s2, s3);
return 0;
}
```
### C
```c
#include <stdio.h>
int s1, s2, s3;
int vs(int a, int b, int c)
{
int v, s1, s2, s3;
v = a * b * c;
s1 = a * b;
s2 = b * c;
s3 = a * c;
return v;
}
int main()
{
int v, length, width, height;
printf("请分别输入长方体的长宽高:");
scanf("%d,%d,%d", &length, &width, &height);
v = vs(length, width, height);
printf("v=%d, s1=%d, s2=%d, s3=%d\n", v, s1, s2, s3);
return 0;
}
```
...@@ -5,5 +5,5 @@ ...@@ -5,5 +5,5 @@
"C语言" "C语言"
], ],
"children": [], "children": [],
"export": [] "export": ["pointers_array.json"]
} }
\ No newline at end of file
#include <stdio.h>
#define LEN 5
int main()
{
char *fruits[LEN] = {
"apple",
"cherry",
"grape",
"peach",
"watermelon"
};
for (int i = 0; i < LEN; ++fruits, ++i)
{
printf("%s\n", *fruits);
}
return 0;
}
\ No newline at end of file
{
"type": "code_options",
"author": "卢昕",
"source": "pointers_array.md",
"exercise_id":"7c21ed88658c4513a24afbc96d043028"
}
\ No newline at end of file
# 打印字符串数组中的所有词
给定一个字符串数组,打印数组中的所有词。请选出错误答案。
## 答案
```c
#include <stdio.h>
#define LEN 5
int main()
{
char *fruits[LEN] = {
"apple",
"cherry",
"grape",
"peach",
"watermelon"
};
for (int i = 0; i < LEN; ++fruits, ++i)
{
printf("%s\n", *fruits);
}
return 0;
}
```
## 选项
### A
```c
#include <stdio.h>
#define LEN 5
int main()
{
char *fruits[LEN] = {
"apple",
"cherry",
"grape",
"peach",
"watermelon"
};
for (int i = 0; i < LEN; ++i)
{
printf("%s\n", fruits[i]);
}
return 0;
}
```
### B
```c
#include <stdio.h>
#define LEN 5
int main()
{
char *fruits[LEN] = {
"apple",
"cherry",
"grape",
"peach",
"watermelon"
};
char **ptr = fruits;
for (int i = 0; i < LEN; ++ptr, ++i)
{
printf("%s\n", *ptr);
}
return 0;
}
```
### C
```c
#include <stdio.h>
#define LEN 5
int main()
{
char *fruits[LEN] = {
"apple",
"cherry",
"grape",
"peach",
"watermelon"
};
for (int i = 0; i < LEN; ++i)
{
for (int j = 0; fruits[i][j] != '\0'; ++j)
printf("%c", *(*(fruits + i) + j));
printf("\n");
}
return 0;
}
```
{
"node_id": "569d5e11c4fc5de7844053d9a733c5e8",
"keywords": [
"指针",
"C语言"
],
"children": [],
"export": []
}
\ No newline at end of file
{
"keywords": [
"C语言概述",
"概述",
"C语言"
],
"children": [],
"export": ["string_io.json"]
}
\ No newline at end of file
#include <stdio.h>
#define STR_LEN 101
void reverse(char *str)
{
char* head = str;
char* tail = str;
char ch;
while(*tail++);
tail -= 2;
while(head < tail)
{
ch = *head;
*head++ = *tail;
*tail-- = ch;
}
}
int main()
{
char str[STR_LEN];
char *rev_str = NULL;
fputc("请输入一个字符串(length<=100):", stdout);
fgetc(str);
reverse(str);
fputc("反转后的字符串为:", stdout);
fputc(str, stdout);
return 0;
}
\ No newline at end of file
{
"type": "code_options",
"author": "卢昕",
"source": "string_io.md",
"exercise_id":"228416f6eaa44195810950a1f41de7a4"
}
\ No newline at end of file
# 字符串输入与输出
输入一个字符串,并反向输出。请选出错误答案。
## 答案
```c
#include <stdio.h>
#define STR_LEN 101
void reverse(char *str)
{
char* head = str;
char* tail = str;
char ch;
while(*tail++);
tail -= 2;
while(head < tail)
{
ch = *head;
*head++ = *tail;
*tail-- = ch;
}
}
int main()
{
char str[STR_LEN];
char *rev_str = NULL;
fputc("请输入一个字符串(length<=100):", stdout);
fgetc(str);
reverse(str);
fputc("反转后的字符串为:", stdout);
fputc(str, stdout);
return 0;
}
```
## 选项
### A
```c
#include <stdio.h>
#define STR_LEN 101
void reverse(char *str)
{
char* head = str;
char* tail = str;
char ch;
while(*tail++);
tail -= 2;
while(head < tail)
{
ch = *head;
*head++ = *tail;
*tail-- = ch;
}
}
int main()
{
char str[STR_LEN];
char *rev_str = NULL;
puts("请输入一个字符串(length<=100):");
gets(str);
reverse(str);
puts("反转后的字符串为:");
puts(str);
return 0;
}
```
### B
```c
#include <stdio.h>
#define STR_LEN 101
void reverse(char *str)
{
char* head = str;
char* tail = str;
char ch;
while(*tail++);
tail -= 2;
while(head < tail)
{
ch = *head;
*head++ = *tail;
*tail-- = ch;
}
}
int main()
{
char str[STR_LEN];
char *rev_str = NULL;
printf("请输入一个字符串(length<=100):\n");
scanf("%s", str);
reverse(str);
printf("反转后的字符串为:\n%s", str);
return 0;
}
```
### C
```c
#include <stdio.h>
#define STR_LEN 101
void reverse(char *str)
{
char* head = str;
char* tail = str;
char ch;
while(*tail++);
tail -= 2;
while(head < tail)
{
ch = *head;
*head++ = *tail;
*tail-- = ch;
}
}
int main()
{
char str[STR_LEN];
char *rev_str = NULL;
fputs("请输入一个字符串(length<=100):\n", stdout);
fgets(str, STR_LEN, stdin);
reverse(str);
fputs("反转后的字符串为:", stdout);
fputs(str, stdout);
return 0;
}
```
{
"keywords": [
"C语言概述",
"概述",
"C语言"
],
"children": [],
"export": [
"string_func.json"
]
}
\ No newline at end of file
#include <stdio.h>
#include <string.h>
#define SENT_LEN 5
#define WORDS_NUM 2
#define STR_LEN 101
int main()
{
int i, j;
char words[SENT_LEN][STR_LEN] = {"非", "淡泊", "无以", "明志", "!"};
char source[WORDS_NUM][STR_LEN] = {"淡泊", "明志"};
char target[WORDS_NUM][STR_LEN] = {"宁静", "致远"};
const char sentence_before[STR_LEN];
const char sentence_after[STR_LEN];
for (i = 0; i < SENT_LEN; ++i)
{
strcat(sentence_before, words[i]);
for (j = 0; j < WORDS_NUM; ++j)
if (strcmp(words[i], source[j]) == 0)
strcpy(words[i], target[j]);
strcat(sentence_after, words[i]);
}
puts("替换前:");
puts(sentence_before);
puts("替换后:");
puts(sentence_after);
return 0;
}
\ No newline at end of file
{
"type": "code_options",
"author": "卢昕",
"source": "string_func.md",
"exercise_id":"20a63ce313e241a49b6d7ccd2eb1d9e2"
}
\ No newline at end of file
# 字符串函数的使用
比较词序列,并替换其中的指定词,最后将所有词拼接形成一个新的句子。输出如下:
> 替换前:
>
> 非淡泊无以明志!
>
> 替换后:
>
> 非宁静无以致远!
>
请选出正确答案。
## 答案
```c
#include <stdio.h>
#include <string.h>
#define SENT_LEN 5
#define WORDS_NUM 2
#define STR_LEN 101
int main()
{
int i, j;
char words[SENT_LEN][STR_LEN] = {"非", "淡泊", "无以", "明志", "!"};
char source[WORDS_NUM][STR_LEN] = {"淡泊", "明志"};
char target[WORDS_NUM][STR_LEN] = {"宁静", "致远"};
char sentence_before[STR_LEN];
char sentence_after[STR_LEN];
for (i = 0; i < SENT_LEN; ++i)
{
strcat(sentence_before, words[i]);
for (j = 0; j < WORDS_NUM; ++j)
if (strcmp(words[i], source[j]) == 0)
strcpy(words[i], target[j]);
strcat(sentence_after, words[i]);
}
puts("替换前:");
puts(sentence_before);
puts("替换后:");
puts(sentence_after);
return 0;
}
```
## 选项
### A
```c
#include <stdio.h>
#include <string.h>
#define SENT_LEN 5
#define WORDS_NUM 2
#define STR_LEN 101
int main()
{
int i, j;
char words[SENT_LEN][STR_LEN] = {"非", "淡泊", "无以", "明志", "!"};
char source[WORDS_NUM][STR_LEN] = {"淡泊", "明志"};
char target[WORDS_NUM][STR_LEN] = {"宁静", "致远"};
char sentence_before[STR_LEN];
char sentence_after[STR_LEN];
for (i = 0; i < SENT_LEN; ++i)
{
strcat(sentence_before, words[i]);
for (j = 0; j < WORDS_NUM; ++j)
if (strcmp(words[i], source[j]) >= 0)
strcpy(words[i], target[j]);
strcat(sentence_after, words[i]);
}
puts("替换前:");
puts(sentence_before);
puts("替换后:");
puts(sentence_after);
return 0;
}
```
### B
```c
#include <stdio.h>
#include <string.h>
#define SENT_LEN 5
#define WORDS_NUM 2
#define STR_LEN 101
int main()
{
int i, j;
char* words[SENT_LEN] = {"非", "淡泊", "无以", "明志", "!"};
char* source[WORDS_NUM] = {"淡泊", "明志"};
char* target[WORDS_NUM] = {"宁静", "致远"};
char sentence_before[STR_LEN];
char sentence_after[STR_LEN];
for (i = 0; i < SENT_LEN; ++i)
{
strcat(sentence_before, words[i]);
for (j = 0; j < WORDS_NUM; ++j)
if (strcmp(words[i], source[j]) == 0)
strcpy(words[i], target[j]);
strcat(sentence_after, words[i]);
}
puts("替换前:");
puts(sentence_before);
puts("替换后:");
puts(sentence_after);
return 0;
}
```
### C
```c
#include <stdio.h>
#include <string.h>
#define SENT_LEN 5
#define WORDS_NUM 2
#define STR_LEN 101
int main()
{
int i, j;
char words[SENT_LEN][STR_LEN] = {"非", "淡泊", "无以", "明志", "!"};
char source[WORDS_NUM][STR_LEN] = {"淡泊", "明志"};
char target[WORDS_NUM][STR_LEN] = {"宁静", "致远"};
const char sentence_before[STR_LEN];
const char sentence_after[STR_LEN];
for (i = 0; i < SENT_LEN; ++i)
{
strcat(sentence_before, words[i]);
for (j = 0; j < WORDS_NUM; ++j)
if (strcmp(words[i], source[j]) == 0)
strcpy(words[i], target[j]);
strcat(sentence_after, words[i]);
}
puts("替换前:");
puts(sentence_before);
puts("替换后:");
puts(sentence_after);
return 0;
}
```
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册