1. 习题概述
C语言设计(第五版)的第六章主要涉及了函数的定义、调用、参数传递以及递归等内容。本章习题涵盖了这些知识点的应用,包括简单的函数设计、递归函数的编写以及函数调用中的错误处理等。以下是对本章习题的解答汇总。
2. 习题一:编写一个函数,计算两个整数的和
#include <stdio.h>
int sum(int a, int b) {
return a + b;
}
int main() {
int x = 10, y = 20;
printf("The sum of %d and %d is %d\n", x, y, sum(x, y));
return 0;
}
3. 习题二:编写一个函数,计算两个浮点数的平均值
#include <stdio.h>
float average(float a, float b) {
return (a + b) / 2;
}
int main() {
float a = 3.5, b = 4.5;
printf("The average of %.2f and %.2f is %.2f\n", a, b, average(a, b));
return 0;
}
4. 习题三:编写一个递归函数,计算阶乘
#include <stdio.h>
int factorial(int n) {
if (n == 0) {
return 1;
} else {
return n * factorial(n - 1);
}
}
int main() {
int num = 5;
printf("Factorial of %d is %d\n", num, factorial(num));
return 0;
}
5. 习题四:编写一个函数,判断一个整数是否为素数
#include <stdio.h>
#include <stdbool.h>
bool is_prime(int n) {
if (n <= 1) {
return false;
}
for (int i = 2; i * i <= n; i++) {
if (n % i == 0) {
return false;
}
}
return true;
}
int main() {
int num = 29;
if (is_prime(num)) {
printf("%d is a prime number\n", num);
} else {
printf("%d is not a prime number\n", num);
}
return 0;
}
6. 习题五:编写一个函数,实现字符串的逆序
#include <stdio.h>
#include <string.h>
void reverse_string(char *str) {
int len = strlen(str);
for (int i = 0; i < len / 2; i++) {
char temp = str[i];
str[i] = str[len - i - 1];
str[len - i - 1] = temp;
}
}
int main() {
char str[100] = "Hello, World!";
printf("Original string: %s\n", str);
reverse_string(str);
printf("Reversed string: %s\n", str);
return 0;
}
7. 习题六:编写一个函数,计算一个整数的阶乘(使用循环)
#include <stdio.h>
int factorial(int n) {
int result = 1;
for (int i = 1; i <= n; i++) {
result *= i;
}
return result;
}
int main() {
int num = 5;
printf("Factorial of %d is %d\n", num, factorial(num));
return 0;
}
8. 习题七:编写一个函数,实现字符串的复制
#include <stdio.h>
#include <string.h>
void copy_string(char *dest, const char *src) {
while (*src) {
*dest++ = *src++;
}
*dest = '\0';
}
int main() {
char src[100] = "Hello, World!";
char dest[100];
copy_string(dest, src);
printf("Copied string: %s\n", dest);
return 0;
}
9. 习题八:编写一个函数,计算一个整数的平方根(使用牛顿迭代法)
#include <stdio.h>
#include <math.h>
double sqrt_newton(double n) {
double x = n;
double last_x;
while (fabs(x - last_x) >= 0.000001) {
last_x = x;
x = (x + n / x) / 2;
}
return x;
}
int main() {
double num = 25;
printf("The square root of %.2f is %.2f\n", num, sqrt_newton(num));
return 0;
}
10. 习题九:编写一个函数,实现两个整数的最大公约数(使用辗转相除法)
#include <stdio.h>
int gcd(int a, int b) {
if (b == 0) {
return a;
} else {
return gcd(b, a % b);
}
}
int main() {
int num1 = 24, num2 = 36;
printf("The GCD of %d and %d is %d\n", num1, num2, gcd(num1, num2));
return 0;
}
11. 习题十:编写一个函数,实现字符串的查找(使用KMP算法)
#include <stdio.h>
#include <string.h>
void kmp_search(const char *str, const char *sub) {
int n = strlen(str);
int m = strlen(sub);
int lps[m];
int i = 0, j = 0;
// Preprocess the pattern to create the longest prefix suffix (LPS) array
lps[0] = 0;
int len = 0;
for (int i = 1; i < m; i++) {
if (sub[i] == sub[len]) {
len++;
lps[i] = len;
} else {
if (len != 0) {
len = lps[len - 1];
} else {
lps[i] = 0;
}
}
}
// Perform the KMP search
i = 0;
j = 0;
while (i < n) {
if (sub[j] == str[i]) {
i++;
j++;
}
if (j == m) {
printf("Pattern found at index %d\n", i - j);
j = lps[j - 1];
} else if (i < n && sub[j] != str[i]) {
if (j != 0) {
j = lps[j - 1];
} else {
i++;
}
}
}
}
int main() {
const char *str = "ABABDABACDABABCABAB";
const char *sub = "ABABCABAB";
kmp_search(str, sub);
return 0;
}
以上是对C语言设计第五版第六章习题的解答汇总。希望这些解答能够帮助你更好地理解和掌握C语言函数和递归的相关知识。
