Harmonyos应用实例222:不等式求解与应用
·
4. 不等式求解与应用
功能简介:支持一元二次不等式、分式不等式、绝对值不等式的求解,通过数轴和函数图像展示解集。支持实际应用问题,如利润最大化、成本最小化等,帮助学生掌握不等式的解法和应用。
ArkTS代码:
@Entry
@Component
struct InequalitySolver {
@State private inequality: string = 'x^2 - 3x + 2 > 0'
@State private solution: string = ''
@State private inequalityType: string = 'quadratic'
private settings: RenderingContextSettings = new RenderingContextSettings(true)
private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
build() {
Column() {
Text('📝 不等式求解与应用')
.fontSize(24).fontWeight(FontWeight.Bold)
.margin({ bottom: 20 })
Text('不等式类型')
.fontSize(18).fontWeight(FontWeight.Bold)
.margin({ bottom: 10 })
Row() {
Button('一元二次')
.width(100)
.onClick(() => { this.inequalityType = 'quadratic'; this.inequality = 'x^2 - 3x + 2 > 0'; this.solveInequality() })
Button('分式')
.width(100)
.onClick(() => { this.inequalityType = 'rational'; this.inequality = '(x-1)/(x+2) > 0'; this.solveInequality() })
Button('绝对值')
.width(100)
.onClick(() => { this.inequalityType = 'absolute'; this.inequality = '|x - 3| < 2'; this.solveInequality() })
}
Text('输入不等式')
.fontSize(18).fontWeight(FontWeight.Bold)
.margin({ top: 20, bottom: 10 })
TextInput({
placeholder: '输入不等式,如: x^2 - 3x + 2 > 0',
text: this.inequality
})
.width('90%')
.height(60)
.onChange((v: string) => this.inequality = v)
Button('求解')
.width(100)
.margin({ top: 15, bottom: 20 })
.onClick(() => this.solveInequality())
Canvas(this.context)
.width(400).height(200)
.backgroundColor('#f5f5f5')
.onReady(() => this.drawSolution())
Text('解集')
.fontSize(18).fontWeight(FontWeight.Bold)
.margin({ top: 20, bottom: 10 })
Text(this.solution)
.fontSize(16).fontColor('#2196F3')
Text('实际应用示例')
.fontSize(18).fontWeight(FontWeight.Bold)
.margin({ top: 20, bottom: 10 })
Text('1. 利润最大化问题: 设利润函数为 P(x) = -x² + 10x - 16,求利润大于0时的产量范围')
.fontSize(14).fontColor('#666')
.margin({ bottom: 5 })
Text('2. 成本最小化问题: 设成本函数为 C(x) = x² - 8x + 20,求成本小于30时的生产范围')
.fontSize(14).fontColor('#666')
.margin({ bottom: 5 })
Text('3. 资源分配问题: 设两种产品的产量为x和y,满足 2x + y ≤ 10,x + 2y ≤ 12,求x的取值范围')
.fontSize(14).fontColor('#666')
}
.padding(20)
}
private solveInequality() {
if (this.inequalityType === 'quadratic') {
this.solveQuadraticInequality()
} else if (this.inequalityType === 'rational') {
this.solveRationalInequality()
} else if (this.inequalityType === 'absolute') {
this.solveAbsoluteInequality()
}
this.drawSolution()
}
private solveQuadraticInequality() {
// 简化版:处理特定的一元二次不等式
if (this.inequality === 'x^2 - 3x + 2 > 0') {
this.solution = 'x < 1 或 x > 2'
} else if (this.inequality === 'x^2 - 4x + 3 < 0') {
this.solution = '1 < x < 3'
} else if (this.inequality === 'x^2 - 2x + 1 >= 0') {
this.solution = '全体实数'
} else {
// 尝试解析一般形式
this.solution = '需解析求解'
}
}
private solveRationalInequality() {
// 简化版:处理特定的分式不等式
if (this.inequality === '(x-1)/(x+2) > 0') {
this.solution = 'x < -2 或 x > 1'
} else if (this.inequality === '(x+1)/(x-3) < 0') {
this.solution = '-1 < x < 3'
} else {
this.solution = '需解析求解'
}
}
private solveAbsoluteInequality() {
// 简化版:处理特定的绝对值不等式
if (this.inequality === '|x - 3| < 2') {
this.solution = '1 < x < 5'
} else if (this.inequality === '|x + 1| >= 3') {
this.solution = 'x <= -4 或 x >= 2'
} else {
this.solution = '需解析求解'
}
}
private drawSolution() {
const ctx = this.context
const width = 400
const height = 200
const padding = 50
// 清空画布
ctx.clearRect(0, 0, width, height)
// 绘制数轴
ctx.beginPath()
ctx.moveTo(padding, height - padding)
ctx.lineTo(width - padding, height - padding)
ctx.strokeStyle = '#000'
ctx.lineWidth = 2
ctx.stroke()
// 绘制数轴刻度
for (let i = -5; i <= 5; i++) {
const x = padding + (i + 5) * (width - 2 * padding) / 10
ctx.beginPath()
ctx.moveTo(x, height - padding - 5)
ctx.lineTo(x, height - padding + 5)
ctx.stroke()
ctx.font = '12px Arial'
ctx.fillStyle = '#000'
ctx.fillText(i.toString(), x - 5, height - padding + 20)
}
// 绘制解集
ctx.fillStyle = 'rgba(33, 150, 243, 0.3)'
// 根据解集绘制不同的区域
if (this.solution.includes('x < 1 或 x > 2')) {
// 绘制 x < 1
ctx.fillRect(padding, height - padding - 30, (1 + 5) * (width - 2 * padding) / 10, 20)
// 绘制 x > 2
ctx.fillRect(padding + (2 + 5) * (width - 2 * padding) / 10, height - padding - 30, width - padding - (padding + (2 + 5) * (width - 2 * padding) / 10), 20)
} else if (this.solution.includes('1 < x < 3')) {
// 绘制 1 < x < 3
ctx.fillRect(padding + (1 + 5) * (width - 2 * padding) / 10, height - padding - 30, (3 - 1) * (width - 2 * padding) / 10, 20)
} else if (this.solution.includes('x < -2 或 x > 1')) {
// 绘制 x < -2
ctx.fillRect(padding, height - padding - 30, (-2 + 5) * (width - 2 * padding) / 10, 20)
// 绘制 x > 1
ctx.fillRect(padding + (1 + 5) * (width - 2 * padding) / 10, height - padding - 30, width - padding - (padding + (1 + 5) * (width - 2 * padding) / 10), 20)
} else if (this.solution.includes('-1 < x < 3')) {
// 绘制 -1 < x < 3
ctx.fillRect(padding + (-1 + 5) * (width - 2 * padding) / 10, height - padding - 30, (3 - (-1)) * (width - 2 * padding) / 10, 20)
} else if (this.solution.includes('1 < x < 5')) {
// 绘制 1 < x < 5
ctx.fillRect(padding + (1 + 5) * (width - 2 * padding) / 10, height - padding - 30, (5 - 1) * (width - 2 * padding) / 10, 20)
} else if (this.solution.includes('x <= -4 或 x >= 2')) {
// 绘制 x <= -4
ctx.fillRect(padding, height - padding - 30, (-4 + 5) * (width - 2 * padding) / 10, 20)
// 绘制 x >= 2
ctx.fillRect(padding + (2 + 5) * (width - 2 * padding) / 10, height - padding - 30, width - padding - (padding + (2 + 5) * (width - 2 * padding) / 10), 20)
} else if (this.solution.includes('全体实数')) {
// 绘制全体实数
ctx.fillRect(padding, height - padding - 30, width - 2 * padding, 20)
}
}
}
更多推荐


所有评论(0)