add suanfa

This commit is contained in:
2026-06-18 10:43:27 +08:00
parent 58b1d4acff
commit d5f62e474d
3 changed files with 185 additions and 4 deletions
Vendored
+4 -4
View File
@@ -35,16 +35,16 @@ pipeline {
echo '🚀 开始部署前端文件...'
script {
// 确保目标目录存在
sh 'mkdir -p /app/www'
sh 'mkdir -p /app/suanfa/www'
// 清空旧文件
sh 'rm -rf /app/www/*'
sh 'rm -rf /app/suanfa/www/*'
// 复制新构建的 dist 文件到挂载目录
sh 'cp -r dist/* /app/www/'
sh 'cp -r dist/* /app/suanfa/www/'
echo '✅ 前端文件部署到 Web 服务目录完成!'
// 可选:列出部署的文件以便验证
sh 'ls -la /app/www/ | head -10'
sh 'ls -la /app/suanfa/www/ | head -10'
}
}
}
+102
View File
@@ -0,0 +1,102 @@
import { describe, it, expect } from 'vitest'
import { factorial, isPrime, fibonacci, gcd, lcm } from '../utils/mathUtils.js'
describe('factorial', () => {
it('should return 1 for 0!', () => {
expect(factorial(0)).toBe(1)
})
it('should return 1 for 1!', () => {
expect(factorial(1)).toBe(1)
})
it('should calculate 5! = 120', () => {
expect(factorial(5)).toBe(120)
})
it('should calculate 10! = 3628800', () => {
expect(factorial(10)).toBe(3628800)
})
it('should throw error for negative numbers', () => {
expect(() => factorial(-1)).toThrow('负数没有阶乘')
})
})
describe('isPrime', () => {
it('should return false for numbers <= 1', () => {
expect(isPrime(0)).toBe(false)
expect(isPrime(1)).toBe(false)
})
it('should return true for 2', () => {
expect(isPrime(2)).toBe(true)
})
it('should return true for prime numbers', () => {
expect(isPrime(7)).toBe(true)
expect(isPrime(13)).toBe(true)
expect(isPrime(97)).toBe(true)
})
it('should return false for composite numbers', () => {
expect(isPrime(4)).toBe(false)
expect(isPrime(15)).toBe(false)
expect(isPrime(100)).toBe(false)
})
})
describe('fibonacci', () => {
it('should return 0 for fib(0)', () => {
expect(fibonacci(0)).toBe(0)
})
it('should return 1 for fib(1)', () => {
expect(fibonacci(1)).toBe(1)
})
it('should calculate fib(10) = 55', () => {
expect(fibonacci(10)).toBe(55)
})
it('should calculate fib(20) = 6765', () => {
expect(fibonacci(20)).toBe(6765)
})
it('should throw error for negative input', () => {
expect(() => fibonacci(-5)).toThrow('下标不能为负数')
})
})
describe('gcd', () => {
it('should return 6 for gcd(18, 24)', () => {
expect(gcd(18, 24)).toBe(6)
})
it('should return 1 for coprime numbers', () => {
expect(gcd(7, 13)).toBe(1)
})
it('should handle zero', () => {
expect(gcd(0, 5)).toBe(5)
expect(gcd(5, 0)).toBe(5)
})
it('should handle negative numbers', () => {
expect(gcd(-18, 24)).toBe(6)
})
})
describe('lcm', () => {
it('should calculate lcm(4, 6) = 12', () => {
expect(lcm(4, 6)).toBe(12)
})
it('should calculate lcm(7, 13) = 91', () => {
expect(lcm(7, 13)).toBe(91)
})
it('should return 0 if either number is 0', () => {
expect(lcm(0, 5)).toBe(0)
})
})
+79
View File
@@ -0,0 +1,79 @@
/**
* 基础数学工具函数 - 算法教学辅助
*/
/**
* 计算阶乘 (n!)
* @param {number} n - 非负整数
* @returns {number} n 的阶乘
*/
export function factorial(n) {
if (n < 0) throw new Error('负数没有阶乘')
if (n === 0 || n === 1) return 1
let result = 1
for (let i = 2; i <= n; i++) {
result *= i
}
return result
}
/**
* 判断一个数是否为质数
* @param {number} n - 正整数
* @returns {boolean}
*/
export function isPrime(n) {
if (n <= 1) return false
if (n <= 3) return true
if (n % 2 === 0 || n % 3 === 0) return false
for (let i = 5; i * i <= n; i += 6) {
if (n % i === 0 || n % (i + 2) === 0) return false
}
return true
}
/**
* 计算斐波那契数列的第 n 项 (迭代法)
* @param {number} n - 非负整数
* @returns {number} 第 n 个斐波那契数
*/
export function fibonacci(n) {
if (n < 0) throw new Error('下标不能为负数')
if (n === 0) return 0
if (n === 1) return 1
let a = 0, b = 1
for (let i = 2; i <= n; i++) {
const tmp = a + b
a = b
b = tmp
}
return b
}
/**
* 求两个数的最大公约数 (辗转相除法)
* @param {number} a
* @param {number} b
* @returns {number} 最大公约数
*/
export function gcd(a, b) {
a = Math.abs(a)
b = Math.abs(b)
while (b !== 0) {
const tmp = b
b = a % b
a = tmp
}
return a
}
/**
* 求两个数的最小公倍数
* @param {number} a
* @param {number} b
* @returns {number} 最小公倍数
*/
export function lcm(a, b) {
if (a === 0 || b === 0) return 0
return Math.abs(a * b) / gcd(a, b)
}