This commit is contained in:
jiale.yao 2025-08-18 22:42:15 +08:00
commit a654775296
6 changed files with 63 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
*.o
*.a

BIN
app Executable file

Binary file not shown.

21
app.c Normal file
View File

@ -0,0 +1,21 @@
#include <stdio.h>
#include "math/base.h"
int main() {
// 测试加法
printf("5 + 3 = %d\n", add(5, 3));
// 测试减法
printf("10 - 7 = %d\n", subtract(10, 7));
// 测试乘法
printf("4 * 6 = %d\n", multiply(4, 6));
// 测试正常除法
printf("20 / 4 = %d\n", divide(20, 4));
// 测试除零保护
printf("8 / 0 = %d\n", divide(8, 0));
return 0;
}

24
math/base.c Normal file
View File

@ -0,0 +1,24 @@
#include "base.h"
// 加法实现
int add(int a, int b) {
return a + b;
}
// 减法实现
int subtract(int a, int b) {
return a - b;
}
// 乘法实现
int multiply(int a, int b) {
return a * b;
}
// 除法实现(含除零保护)
int divide(int a, int b) {
if (b == 0) {
return 0; // 除数为0时返回0
}
return a / b;
}

14
math/base.h Normal file
View File

@ -0,0 +1,14 @@
#pragma once
// 加法
int add(int a, int b);
// 减法
int subtract(int a, int b);
// 乘法
int multiply(int a, int b);
// 除法整数除法除数为0时返回0
int divide(int a, int b);

2
readme.md Normal file
View File

@ -0,0 +1,2 @@
#### 编译
gcc -c math/base.c -o math/base.o && ar rcs math/libmath.a math/base.o