如何用 Deepseek 寫的uniapp血型遺傳查詢工具

引言
在現(xiàn)代社會中,了解血型遺傳規(guī)律對于優(yōu)生優(yōu)育、醫(yī)療健康等方面都有重要意義。本文將介紹如何使用Uniapp開發(fā)一個跨平臺的血型遺傳查詢工具,幫助用戶預(yù)測孩子可能的血型。
一、血型遺傳基礎(chǔ)知識
人類的ABO血型系統(tǒng)由三個等位基因決定:IA、IB和i。其中IA和IB對i是顯性關(guān)系:
- A型血基因型:IAIA或IAi
- B型血基因型:IBIB或IBi
- AB型血基因型:IAIB
- O型血基因型:ii
根據(jù)孟德爾遺傳定律,孩子的血型由父母雙方各提供一個等位基因組合而成。
二、Uniapp開發(fā)優(yōu)勢
選擇Uniapp開發(fā)這款工具主要基于以下優(yōu)勢:
- 跨平臺能力:一次開發(fā),可發(fā)布到iOS、Android、H5及各種小程序平臺
- 開發(fā)效率高:基于Vue.js框架,學(xué)習(xí)成本低,開發(fā)速度快
- 性能優(yōu)良:接近原生應(yīng)用的體驗
- 生態(tài)豐富:擁有完善的插件市場和社區(qū)支持
三、核心代碼解析
1. 血型遺傳算法實現(xiàn)
getPossibleGenotypes(parent1, parent2) {
// 血型對應(yīng)的可能基因型
const typeToGenotypes = {
'A': ['AA', 'AO'],
'B': ['BB', 'BO'],
'AB': ['AB'],
'O': ['OO']
}
const parent1Genotypes = typeToGenotypes[parent1]
const parent2Genotypes = typeToGenotypes[parent2]
const possibleGenotypes = []
// 生成所有可能的基因組合
for (const g1 of parent1Genotypes) {
for (const g2 of parent2Genotypes) {
// 每個父母貢獻(xiàn)一個等位基因
for (let i = 0; i < 2; i++) {
for (let j = 0; j < 2; j++) {
const childGenotype = g1[i] + g2[j]
possibleGenotypes.push(childGenotype)
}
}
}
}
return possibleGenotypes
}這段代碼實現(xiàn)了血型遺傳的核心算法,通過遍歷父母可能的基因型組合,計算出孩子所有可能的基因型。
2. 概率計算
calculateProbabilities(genotypes) {
const bloodTypeCounts = {
'A': 0,
'B': 0,
'AB': 0,
'O': 0
}
// 基因型到血型的映射
const genotypeToType = {
'AA': 'A',
'AO': 'A',
'BB': 'B',
'BO': 'B',
'AB': 'AB',
'OO': 'O'
}
// 統(tǒng)計每種血型的出現(xiàn)次數(shù)
for (const genotype of genotypes) {
const type = genotypeToType[genotype]
bloodTypeCounts[type]++
}
const total = genotypes.length
const probabilities = {}
// 計算概率
for (const type in bloodTypeCounts) {
const count = bloodTypeCounts[type]
if (count > 0) {
probabilities[type] = (count / total * 100).toFixed(1)
}
}
return probabilities
}這部分代碼統(tǒng)計各種血型出現(xiàn)的頻率,并計算出每種血型出現(xiàn)的概率百分比。
3. 界面交互實現(xiàn)
<view class="form-item">
<text class="label">父親血型:</text>
<picker @change="bindParent1Change" :value="parent1Index" :range="bloodTypes" range-key="name">
<view class="picker">
{{bloodTypes[parent1Index].name}}
</view>
</picker>
</view>
<button class="calculate-btn" @click="calculateBloodType">計算孩子可能的血型</button>使用Uniapp的picker組件實現(xiàn)血型選擇,通過按鈕觸發(fā)計算邏輯,界面簡潔友好。
四、項目亮點
- 科學(xué)準(zhǔn)確性:嚴(yán)格遵循遺傳學(xué)原理,計算結(jié)果準(zhǔn)確可靠
- 用戶體驗優(yōu)化:
- 結(jié)果自動滾動到可視區(qū)域
- 概率可視化展示
- 遺傳知識科普
- 代碼結(jié)構(gòu)清晰:
- 業(yè)務(wù)邏輯與UI分離
- 復(fù)用性高的工具函數(shù)
- 良好的代碼注釋
完整代碼
<template>
<view class="container">
<view class="header">
<text class="title">血型遺傳查詢工具</text>
</view>
<view class="card">
<text class="subtitle">選擇父母血型</text>
<view class="form-item">
<text class="label">父親血型:</text>
<picker @change="bindParent1Change" :value="parent1Index" :range="bloodTypes" range-key="name">
<view class="picker">
{{bloodTypes[parent1Index].name}}
</view>
</picker>
</view>
<view class="form-item">
<text class="label">母親血型:</text>
<picker @change="bindParent2Change" :value="parent2Index" :range="bloodTypes" range-key="name">
<view class="picker">
{{bloodTypes[parent2Index].name}}
</view>
</picker>
</view>
<button class="calculate-btn" @click="calculateBloodType">計算孩子可能的血型</button>
</view>
<view class="card result-card" v-if="showResult">
<text class="subtitle">結(jié)果</text>
<text class="result-text">父母血型: {{parent1Name}} + {{parent2Name}}</text>
<text class="result-text">孩子可能的血型:
<text class="blood-type">{{resultText}}</text>
</text>
<text class="probability" v-if="probabilityText">{{probabilityText}}</text>
</view>
<view class="card note-card">
<text class="note-title">血型遺傳規(guī)律說明:</text>
<text class="note-text">? 血型由ABO基因決定,A和B是顯性基因,O是隱性基因。</text>
<text class="note-text">? A型血基因型可能是AA或AO,B型血基因型可能是BB或BO。</text>
<text class="note-text">? AB型血基因型是AB,O型血基因型是OO。</text>
</view>
</view>
</template>
<script>
export default {
data() {
return {
bloodTypes: [{
name: 'A型',
value: 'A'
},
{
name: 'B型',
value: 'B'
},
{
name: 'AB型',
value: 'AB'
},
{
name: 'O型',
value: 'O'
}
],
parent1Index: 0,
parent2Index: 0,
parent1Name: 'A型',
parent2Name: 'A型',
parent1Value: 'A',
parent2Value: 'A',
showResult: false,
resultText: '',
probabilityText: ''
}
},
methods: {
bindParent1Change(e) {
this.parent1Index = e.detail.value
this.parent1Name = this.bloodTypes[this.parent1Index].name
this.parent1Value = this.bloodTypes[this.parent1Index].value
},
bindParent2Change(e) {
this.parent2Index = e.detail.value
this.parent2Name = this.bloodTypes[this.parent2Index].name
this.parent2Value = this.bloodTypes[this.parent2Index].value
},
calculateBloodType() {
// 計算可能的基因組合
const possibleGenotypes = this.getPossibleGenotypes(this.parent1Value, this.parent2Value)
// 計算可能的血型及其概率
const bloodTypeProbabilities = this.calculateProbabilities(possibleGenotypes)
// 生成結(jié)果文本
let resultText = ''
let probabilityText = '概率: '
let first = true
for (const type in bloodTypeProbabilities) {
if (!first) {
resultText += '、'
probabilityText += ','
}
resultText += this.getBloodTypeName(type)
probabilityText += `${this.getBloodTypeName(type)} ${bloodTypeProbabilities[type]}%`
first = false
}
this.resultText = resultText
this.probabilityText = probabilityText
this.showResult = true
// 滾動到結(jié)果位置
uni.pageScrollTo({
scrollTop: 300,
duration: 300
})
},
getBloodTypeName(type) {
const names = {
'A': 'A型',
'B': 'B型',
'AB': 'AB型',
'O': 'O型'
}
return names[type]
},
getPossibleGenotypes(parent1, parent2) {
// 血型對應(yīng)的可能基因型
const typeToGenotypes = {
'A': ['AA', 'AO'],
'B': ['BB', 'BO'],
'AB': ['AB'],
'O': ['OO']
}
const parent1Genotypes = typeToGenotypes[parent1]
const parent2Genotypes = typeToGenotypes[parent2]
const possibleGenotypes = []
// 生成所有可能的基因組合
for (const g1 of parent1Genotypes) {
for (const g2 of parent2Genotypes) {
// 每個父母貢獻(xiàn)一個等位基因
for (let i = 0; i < 2; i++) {
for (let j = 0; j < 2; j++) {
const childGenotype = g1[i] + g2[j]
possibleGenotypes.push(childGenotype)
}
}
}
}
return possibleGenotypes
},
calculateProbabilities(genotypes) {
const bloodTypeCounts = {
'A': 0,
'B': 0,
'AB': 0,
'O': 0
}
// 基因型到血型的映射
const genotypeToType = {
'AA': 'A',
'AO': 'A',
'BB': 'B',
'BO': 'B',
'AB': 'AB',
'OO': 'O'
}
// 統(tǒng)計每種血型的出現(xiàn)次數(shù)
for (const genotype of genotypes) {
const type = genotypeToType[genotype]
bloodTypeCounts[type]++
}
const total = genotypes.length
const probabilities = {}
// 計算概率
for (const type in bloodTypeCounts) {
const count = bloodTypeCounts[type]
if (count > 0) {
probabilities[type] = (count / total * 100).toFixed(1)
}
}
return probabilities
}
}
}
</script>
<style>
.container {
padding: 20rpx;
}
.header {
margin: 30rpx 0;
text-align: center;
}
.title {
font-size: 40rpx;
font-weight: bold;
color: #333;
}
.card {
background-color: #fff;
border-radius: 16rpx;
padding: 30rpx;
margin-bottom: 30rpx;
box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.05);
}
.subtitle {
font-size: 32rpx;
font-weight: bold;
margin-bottom: 30rpx;
display: block;
color: #333;
}
.form-item {
margin-bottom: 30rpx;
}
.label {
font-size: 28rpx;
color: #666;
margin-bottom: 10rpx;
display: block;
}
.picker {
height: 80rpx;
line-height: 80rpx;
padding: 0 20rpx;
border: 1rpx solid #eee;
border-radius: 8rpx;
font-size: 28rpx;
}
.calculate-btn {
background-color: #4CAF50;
color: white;
margin-top: 40rpx;
border-radius: 8rpx;
font-size: 30rpx;
height: 90rpx;
line-height: 90rpx;
}
.result-card {
background-color: #e9f7ef;
}
.result-text {
font-size: 28rpx;
margin-bottom: 20rpx;
display: block;
}
.blood-type {
color: #e74c3c;
font-weight: bold;
}
.probability {
font-size: 26rpx;
color: #666;
display: block;
margin-top: 10rpx;
}
.note-title {
font-weight: bold;
font-size: 28rpx;
margin-bottom: 15rpx;
display: block;
color: #333;
}
.note-text {
font-size: 26rpx;
color: #666;
display: block;
margin-bottom: 10rpx;
line-height: 1.6;
}
</style>到此這篇關(guān)于用 Deepseek 寫的uniapp血型遺傳查詢工具的文章就介紹到這了,更多相關(guān)Deepseek uniapp血型遺傳內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue?設(shè)置圖片不轉(zhuǎn)為base64的方式
這篇文章主要介紹了Vue實現(xiàn)設(shè)置圖片不轉(zhuǎn)為base64的方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-02-02
如何解決sass-loader和node-sass版本沖突的問題
這篇文章主要介紹了如何解決sass-loader和node-sass版本沖突的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-04-04

