1. 实例一:计算阶乘
1.1 实例描述
编写一个C语言程序,计算给定正整数的阶乘。
1.2 代码示例
#include <stdio.h>
long factorial(int n) {
if (n == 0)
return 1;
else
return n * factorial(n - 1);
}
int main() {
int num;
printf("Enter a positive integer: ");
scanf("%d", &num);
printf("Factorial of %d is %ld\n", num, factorial(num));
return 0;
}
1.3 技巧分享
- 使用递归函数计算阶乘。
- 注意递归的终止条件。
2. 实例二:冒泡排序
2.1 实例描述
编写一个C语言程序,使用冒泡排序算法对一组整数进行排序。
2.2 代码示例
#include <stdio.h>
void bubbleSort(int arr[], int n) {
int i, j, temp;
for (i = 0; i < n-1; i++) {
for (j = 0; j < n-i-1; j++) {
if (arr[j] > arr[j+1]) {
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
}
int main() {
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr)/sizeof(arr[0]);
bubbleSort(arr, n);
printf("Sorted array: \n");
for (int i=0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
return 0;
}
2.3 技巧分享
- 冒泡排序是一种简单的排序算法,但效率较低。
- 注意冒泡排序中交换元素的位置。
3. 实例三:字符串比较
3.1 实例描述
编写一个C语言程序,比较两个字符串是否相等。
3.2 代码示例
#include <stdio.h>
#include <string.h>
int stringCompare(const char *str1, const char *str2) {
return strcmp(str1, str2);
}
int main() {
char str1[100], str2[100];
printf("Enter first string: ");
scanf("%s", str1);
printf("Enter second string: ");
scanf("%s", str2);
if (stringCompare(str1, str2) == 0)
printf("Both strings are equal.\n");
else
printf("Strings are not equal.\n");
return 0;
}
3.3 技巧分享
- 使用
strcmp函数比较两个字符串。 - 注意字符串比较的结束条件。
4. 实例四:斐波那契数列
4.1 实例描述
编写一个C语言程序,计算斐波那契数列的前n项。
4.2 代码示例
#include <stdio.h>
void fibonacci(int n) {
int a = 0, b = 1, c;
if (n < 0)
return;
for (int i = 0; i < n; i++) {
if (i <= 1)
c = i;
else {
c = a + b;
a = b;
b = c;
}
printf("%d ", c);
}
printf("\n");
}
int main() {
int n;
printf("Enter the number of terms: ");
scanf("%d", &n);
printf("Fibonacci Series: \n");
fibonacci(n);
return 0;
}
4.3 技巧分享
- 使用循环和变量计算斐波那契数列。
- 注意数列的递推关系。
5. 实例五:查找最大值
5.1 实例描述
编写一个C语言程序,查找一组整数中的最大值。
5.2 代码示例
#include <stdio.h>
int findMax(int arr[], int n) {
int max = arr[0];
for (int i = 1; i < n; i++) {
if (arr[i] > max)
max = arr[i];
}
return max;
}
int main() {
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr)/sizeof(arr[0]);
printf("Maximum element in the array is %d\n", findMax(arr, n));
return 0;
}
5.3 技巧分享
- 使用循环遍历数组,找出最大值。
- 注意初始化最大值为数组的第一个元素。
6. 实例六:计算平均值
6.1 实例描述
编写一个C语言程序,计算一组整数的平均值。
6.2 代码示例
#include <stdio.h>
float calculateAverage(int arr[], int n) {
int sum = 0;
for (int i = 0; i < n; i++)
sum += arr[i];
return (float)sum / n;
}
int main() {
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr)/sizeof(arr[0]);
printf("Average value of the array is %.2f\n", calculateAverage(arr, n));
return 0;
}
6.3 技巧分享
- 使用循环计算数组的总和。
- 将总和转换为浮点数,计算平均值。
7. 实例七:判断素数
7.1 实例描述
编写一个C语言程序,判断一个整数是否为素数。
7.2 代码示例
#include <stdio.h>
#include <stdbool.h>
bool isPrime(int num) {
if (num <= 1)
return false;
for (int i = 2; i * i <= num; i++) {
if (num % i == 0)
return false;
}
return true;
}
int main() {
int num;
printf("Enter a positive integer: ");
scanf("%d", &num);
if (isPrime(num))
printf("%d is a prime number.\n", num);
else
printf("%d is not a prime number.\n", num);
return 0;
}
7.3 技巧分享
- 使用循环判断一个数是否为素数。
- 注意素数的定义和判断条件。
8. 实例八:计算幂
8.1 实例描述
编写一个C语言程序,计算一个数的幂。
8.2 代码示例
#include <stdio.h>
long long power(int base, int exp) {
long long result = 1;
for (int i = 0; i < exp; i++)
result *= base;
return result;
}
int main() {
int base, exp;
printf("Enter base: ");
scanf("%d", &base);
printf("Enter exponent: ");
scanf("%d", &exp);
printf("%d^%d = %lld\n", base, exp, power(base, exp));
return 0;
}
8.3 技巧分享
- 使用循环计算幂。
- 注意幂运算的结果可能很大。
9. 实例九:逆序输出
9.1 实例描述
编写一个C语言程序,逆序输出一个字符串。
9.2 代码示例
#include <stdio.h>
#include <string.h>
void reverseString(char str[]) {
int len = strlen(str);
for (int i = len - 1; i >= 0; i--)
printf("%c", str[i]);
}
int main() {
char str[100];
printf("Enter a string: ");
scanf("%s", str);
printf("Reversed string: ");
reverseString(str);
printf("\n");
return 0;
}
9.3 技巧分享
- 使用字符串的长度和循环逆序输出字符。
- 注意字符串结束符。
10. 实例十:计算最大公约数
10.1 实例描述
编写一个C语言程序,计算两个整数的最大公约数。
10.2 代码示例
#include <stdio.h>
int gcd(int a, int b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
int main() {
int num1, num2;
printf("Enter two positive integers: ");
scanf("%d %d", &num1, &num2);
printf("GCD of %d and %d is %d\n", num1, num2, gcd(num1, num2));
return 0;
}
10.3 技巧分享
- 使用递归计算最大公约数。
- 注意递归的终止条件。
11. 实例十一:判断闰年
11.1 实例描述
编写一个C语言程序,判断一个年份是否为闰年。
11.2 代码示例
#include <stdio.h>
int isLeapYear(int year) {
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
return 1;
return 0;
}
int main() {
int year;
printf("Enter a year: ");
scanf("%d", &year);
if (isLeapYear(year))
printf("%d is a leap year.\n", year);
else
printf("%d is not a leap year.\n", year);
return 0;
}
11.3 技巧分享
- 使用条件判断判断闰年。
- 注意闰年的定义。
12. 实例十二:计算三角形的面积
12.1 实例描述
编写一个C语言程序,计算一个三角形的面积。
12.2 代码示例
#include <stdio.h>
#include <math.h>
double calculateTriangleArea(double a, double b, double c) {
double s = (a + b + c) / 2;
return sqrt(s * (s - a) * (s - b) * (s - c));
}
int main() {
double a, b, c;
printf("Enter the lengths of the three sides of a triangle: ");
scanf("%lf %lf %lf", &a, &b, &c);
printf("Area of the triangle is %.2f\n", calculateTriangleArea(a, b, c));
return 0;
}
12.3 技巧分享
- 使用海伦公式计算三角形的面积。
- 注意三角形的定义和海伦公式。
13. 实例十三:计算圆柱体体积
13.1 实例描述
编写一个C语言程序,计算一个圆柱体的体积。
13.2 代码示例
#include <stdio.h>
#include <math.h>
double calculateCylinderVolume(double radius, double height) {
return 3.14159265358979323846 * radius * radius * height;
}
int main() {
double radius, height;
printf("Enter the radius and height of a cylinder: ");
scanf("%lf %lf", &radius, &height);
printf("Volume of the cylinder is %.2f\n", calculateCylinderVolume(radius, height));
return 0;
}
13.3 技巧分享
- 使用圆柱体体积公式计算体积。
- 注意圆周率的定义。
14. 实例十四:计算球体表面积
14.1 实例描述
编写一个C语言程序,计算一个球体的表面积。
14.2 代码示例
#include <stdio.h>
#include <math.h>
double calculateSphereSurfaceArea(double radius) {
return 4 * 3.14159265358979323846 * radius * radius;
}
int main() {
double radius;
printf("Enter the radius of a sphere: ");
scanf("%lf", &radius);
printf("Surface area of the sphere is %.2f\n", calculateSphereSurfaceArea(radius));
return 0;
}
14.3 技巧分享
- 使用球体表面积公式计算表面积。
- 注意圆周率的定义。
15. 实例十五:计算长方体体积
15.1 实例描述
编写一个C语言程序,计算一个长方体的体积。
15.2 代码示例
#include <stdio.h>
double calculateCubeVolume(double side) {
return side * side * side;
}
int main() {
double side;
printf("Enter the length of the side of a cube: ");
scanf("%lf", &side);
printf("Volume of the cube is %.2f\n", calculateCubeVolume(side));
return 0;
}
15.3 技巧分享
- 使用长方体体积公式计算体积。
- 注意长方体的定义。
16. 实例十六:计算圆的周长
16.1 实例描述
编写一个C语言程序,计算一个圆的周长。
16.2 代码示例
#include <stdio.h>
#include <math.h>
double calculateCircleCircumference(double radius) {
return 2 * 3.14159265358979323846 * radius;
}
int main() {
double radius;
printf("Enter the radius of a circle: ");
scanf("%lf", &radius);
printf("Circumference of the circle is %.2f\n", calculateCircleCircumference(radius));
return 0;
}
16.3 技巧分享
- 使用圆周长公式计算周长。
- 注意圆周率的定义。
17. 实例十七:计算矩形面积
17.1 实例描述
编写一个C语言程序,计算一个矩形的面积。
17.2 代码示例
#include <stdio.h>
double calculateRectangleArea(double length, double width) {
return length * width;
}
int main() {
double length, width;
printf("Enter the length and width of a rectangle: ");
scanf("%lf %lf", &length, &width);
printf("Area of the rectangle is %.2f\n", calculateRectangleArea(length, width));
return 0;
}
17.3 技巧分享
- 使用矩形面积公式计算面积。
- 注意矩形的定义。
18. 实例十八:计算正方体表面积
18.1 实例描述
编写一个C语言程序,计算一个正方体的表面积。
18.2 代码示例
#include <stdio.h>
double calculateCubeSurfaceArea(double side) {
return 6 * side * side;
}
int main() {
double side;
printf("Enter the length of the side of a cube: ");
scanf("%lf", &side);
printf("Surface area of the cube is %.2f\n", calculateCubeSurfaceArea(side));
return 0;
}
18.3 技巧分享
- 使用正方体表面积公式计算表面积。
- 注意正方体的定义。
19. 实例十九:计算等腰三角形面积
19.1 实例描述
编写一个C语言程序,计算一个等腰三角形的面积。
19.2 代码示例
#include <stdio.h>
#include <math.h>
double calculateIsoscelesTriangleArea(double base, double height) {
return (base * height) / 2;
}
int main() {
double base, height;
printf("Enter the base and height of an isosceles triangle: ");
scanf("%lf %lf", &base, &height);
printf("Area of the isosceles triangle is %.2f\n", calculateIsoscelesTriangleArea(base, height));
return 0;
}
19.3 技巧分享
- 使用等腰三角形面积公式计算面积。
- 注意等腰三角形的定义。
20. 实例二十:计算梯形面积
20.1 实例描述
编写一个C语言程序,计算一个梯形的面积。
20.2 代码示例
#include <stdio.h>
double calculateTrapezoidArea(double a, double b, double h) {
return (a + b) * h / 2;
}
int main() {
double a, b, h;
printf("Enter the lengths of the parallel sides and height of a trapezoid: ");
scanf("%lf %lf %lf", &a, &b, &h);
printf("Area of the trapezoid is %.2f\n", calculateTrapezoidArea(a, b, h));
return 0;
}
20.3 技巧分享
- 使用梯形面积公式计算面积。
- 注意梯形的定义。
21. 实例二十一:计算圆锥体积
21.1 实例描述
编写一个C语言程序,计算一个圆锥的体积。
21.2 代码示例
#include <stdio.h>
#include <math.h>
double calculateConeVolume(double radius, double height) {
return (1.0 / 3) * 3.14159265358979323846 * radius * radius * height;
}
int main() {
double radius, height;
printf("Enter the radius and height of a cone: ");
scanf("%lf %lf", &radius, &height);
printf("Volume of the cone is %.2f\n", calculateConeVolume(radius, height));
return 0;
}
21.3 技巧分享
- 使用圆锥体积公式计算体积。
- 注意圆周率的定义。
##
