• 设为首页
  • 收藏本站
  • 积分充值
  • VIP赞助
  • 手机版
  • 微博
  • 微信
    微信公众号 添加方式:
    1:搜索微信号(888888
    2:扫描左侧二维码
  • 快捷导航
    福建二哥 门户 查看主题

    如何用 Deepseek 写的uniapp血型遗传查询工具

    发布者: 404号房间 | 发布时间: 2025-6-14 15:29| 查看数: 92| 评论数: 0|帖子模式



    引言

    在现代社会中,了解血型遗传规律对于优生优育、医疗健康等方面都有重要意义。本文将介绍如何使用Uniapp开发一个跨平台的血型遗传查询工具,帮助用户预测孩子可能的血型。

    一、血型遗传基础知识

    人类的ABO血型系统由三个等位基因决定:IA、IB和i。其中IA和IB对i是显性关系:

    • A型血基因型:IAIA或IAi
    • B型血基因型:IBIB或IBi
    • AB型血基因型:IAIB
    • O型血基因型:ii
    根据孟德尔遗传定律,孩子的血型由父母双方各提供一个等位基因组合而成。

    二、Uniapp开发优势

    选择Uniapp开发这款工具主要基于以下优势:

    • 跨平台能力:一次开发,可发布到iOS、Android、H5及各种小程序平台
    • 开发效率高:基于Vue.js框架,学习成本低,开发速度快
    • 性能优良:接近原生应用的体验
    • 生态丰富:拥有完善的插件市场和社区支持

    三、核心代码解析


    1. 血型遗传算法实现
    1. getPossibleGenotypes(parent1, parent2) {
    2.   // 血型对应的可能基因型
    3.   const typeToGenotypes = {
    4.     'A': ['AA', 'AO'],
    5.     'B': ['BB', 'BO'],
    6.     'AB': ['AB'],
    7.     'O': ['OO']
    8.   }
    9.   const parent1Genotypes = typeToGenotypes[parent1]
    10.   const parent2Genotypes = typeToGenotypes[parent2]
    11.   const possibleGenotypes = []
    12.   // 生成所有可能的基因组合
    13.   for (const g1 of parent1Genotypes) {
    14.     for (const g2 of parent2Genotypes) {
    15.       // 每个父母贡献一个等位基因
    16.       for (let i = 0; i < 2; i++) {
    17.         for (let j = 0; j < 2; j++) {
    18.           const childGenotype = g1[i] + g2[j]
    19.           possibleGenotypes.push(childGenotype)
    20.         }
    21.       }
    22.     }
    23.   }
    24.   return possibleGenotypes
    25. }
    复制代码
    这段代码实现了血型遗传的核心算法,通过遍历父母可能的基因型组合,计算出孩子所有可能的基因型。

    2. 概率计算
    1. calculateProbabilities(genotypes) {
    2.   const bloodTypeCounts = {
    3.     'A': 0,
    4.     'B': 0,
    5.     'AB': 0,
    6.     'O': 0
    7.   }
    8.   // 基因型到血型的映射
    9.   const genotypeToType = {
    10.     'AA': 'A',
    11.     'AO': 'A',
    12.     'BB': 'B',
    13.     'BO': 'B',
    14.     'AB': 'AB',
    15.     'OO': 'O'
    16.   }
    17.   // 统计每种血型的出现次数
    18.   for (const genotype of genotypes) {
    19.     const type = genotypeToType[genotype]
    20.     bloodTypeCounts[type]++
    21.   }
    22.   const total = genotypes.length
    23.   const probabilities = {}
    24.   // 计算概率
    25.   for (const type in bloodTypeCounts) {
    26.     const count = bloodTypeCounts[type]
    27.     if (count > 0) {
    28.       probabilities[type] = (count / total * 100).toFixed(1)
    29.     }
    30.   }
    31.   return probabilities
    32. }
    复制代码
    这部分代码统计各种血型出现的频率,并计算出每种血型出现的概率百分比。

    3. 界面交互实现
    1. <view class="form-item">
    2.   <text class="label">父亲血型:</text>
    3.   <picker @change="bindParent1Change" :value="parent1Index" :range="bloodTypes" range-key="name">
    4.     <view class="picker">
    5.       {{bloodTypes[parent1Index].name}}
    6.     </view>
    7.   </picker>
    8. </view>
    9. <button class="calculate-btn" @click="calculateBloodType">计算孩子可能的血型</button>
    复制代码
    使用Uniapp的picker组件实现血型选择,通过按钮触发计算逻辑,界面简洁友好。

    四、项目亮点


    • 科学准确性:严格遵循遗传学原理,计算结果准确可靠
    • 用户体验优化

      • 结果自动滚动到可视区域
      • 概率可视化展示
      • 遗传知识科普

    • 代码结构清晰

      • 业务逻辑与UI分离
      • 复用性高的工具函数
      • 良好的代码注释

    完整代码
    1. <template>
    2.         <view class="container">
    3.                 <view class="header">
    4.                         <text class="title">血型遗传查询工具</text>
    5.                 </view>
    6.                 <view class="card">
    7.                         <text class="subtitle">选择父母血型</text>
    8.                         <view class="form-item">
    9.                                 <text class="label">父亲血型:</text>
    10.                                 <picker @change="bindParent1Change" :value="parent1Index" :range="bloodTypes" range-key="name">
    11.                                         <view class="picker">
    12.                                                 {{bloodTypes[parent1Index].name}}
    13.                                         </view>
    14.                                 </picker>
    15.                         </view>
    16.                         <view class="form-item">
    17.                                 <text class="label">母亲血型:</text>
    18.                                 <picker @change="bindParent2Change" :value="parent2Index" :range="bloodTypes" range-key="name">
    19.                                         <view class="picker">
    20.                                                 {{bloodTypes[parent2Index].name}}
    21.                                         </view>
    22.                                 </picker>
    23.                         </view>
    24.                         <button class="calculate-btn" @click="calculateBloodType">计算孩子可能的血型</button>
    25.                 </view>
    26.                 <view class="card result-card" v-if="showResult">
    27.                         <text class="subtitle">结果</text>
    28.                         <text class="result-text">父母血型: {{parent1Name}} + {{parent2Name}}</text>
    29.                         <text class="result-text">孩子可能的血型:
    30.                                 <text class="blood-type">{{resultText}}</text>
    31.                         </text>
    32.                         <text class="probability" v-if="probabilityText">{{probabilityText}}</text>
    33.                 </view>
    34.                 <view class="card note-card">
    35.                         <text class="note-title">血型遗传规律说明:</text>
    36.                         <text class="note-text">• 血型由ABO基因决定,A和B是显性基因,O是隐性基因。</text>
    37.                         <text class="note-text">• A型血基因型可能是AA或AO,B型血基因型可能是BB或BO。</text>
    38.                         <text class="note-text">• AB型血基因型是AB,O型血基因型是OO。</text>
    39.                 </view>
    40.         </view>
    41. </template>
    42. <script>
    43.         export default {
    44.                 data() {
    45.                         return {
    46.                                 bloodTypes: [{
    47.                                                 name: 'A型',
    48.                                                 value: 'A'
    49.                                         },
    50.                                         {
    51.                                                 name: 'B型',
    52.                                                 value: 'B'
    53.                                         },
    54.                                         {
    55.                                                 name: 'AB型',
    56.                                                 value: 'AB'
    57.                                         },
    58.                                         {
    59.                                                 name: 'O型',
    60.                                                 value: 'O'
    61.                                         }
    62.                                 ],
    63.                                 parent1Index: 0,
    64.                                 parent2Index: 0,
    65.                                 parent1Name: 'A型',
    66.                                 parent2Name: 'A型',
    67.                                 parent1Value: 'A',
    68.                                 parent2Value: 'A',
    69.                                 showResult: false,
    70.                                 resultText: '',
    71.                                 probabilityText: ''
    72.                         }
    73.                 },
    74.                 methods: {
    75.                         bindParent1Change(e) {
    76.                                 this.parent1Index = e.detail.value
    77.                                 this.parent1Name = this.bloodTypes[this.parent1Index].name
    78.                                 this.parent1Value = this.bloodTypes[this.parent1Index].value
    79.                         },
    80.                         bindParent2Change(e) {
    81.                                 this.parent2Index = e.detail.value
    82.                                 this.parent2Name = this.bloodTypes[this.parent2Index].name
    83.                                 this.parent2Value = this.bloodTypes[this.parent2Index].value
    84.                         },
    85.                         calculateBloodType() {
    86.                                 // 计算可能的基因组合
    87.                                 const possibleGenotypes = this.getPossibleGenotypes(this.parent1Value, this.parent2Value)
    88.                                 // 计算可能的血型及其概率
    89.                                 const bloodTypeProbabilities = this.calculateProbabilities(possibleGenotypes)
    90.                                 // 生成结果文本
    91.                                 let resultText = ''
    92.                                 let probabilityText = '概率: '
    93.                                 let first = true
    94.                                 for (const type in bloodTypeProbabilities) {
    95.                                         if (!first) {
    96.                                                 resultText += '、'
    97.                                                 probabilityText += ','
    98.                                         }
    99.                                         resultText += this.getBloodTypeName(type)
    100.                                         probabilityText += `${this.getBloodTypeName(type)} ${bloodTypeProbabilities[type]}%`
    101.                                         first = false
    102.                                 }
    103.                                 this.resultText = resultText
    104.                                 this.probabilityText = probabilityText
    105.                                 this.showResult = true
    106.                                 // 滚动到结果位置
    107.                                 uni.pageScrollTo({
    108.                                         scrollTop: 300,
    109.                                         duration: 300
    110.                                 })
    111.                         },
    112.                         getBloodTypeName(type) {
    113.                                 const names = {
    114.                                         'A': 'A型',
    115.                                         'B': 'B型',
    116.                                         'AB': 'AB型',
    117.                                         'O': 'O型'
    118.                                 }
    119.                                 return names[type]
    120.                         },
    121.                         getPossibleGenotypes(parent1, parent2) {
    122.                                 // 血型对应的可能基因型
    123.                                 const typeToGenotypes = {
    124.                                         'A': ['AA', 'AO'],
    125.                                         'B': ['BB', 'BO'],
    126.                                         'AB': ['AB'],
    127.                                         'O': ['OO']
    128.                                 }
    129.                                 const parent1Genotypes = typeToGenotypes[parent1]
    130.                                 const parent2Genotypes = typeToGenotypes[parent2]
    131.                                 const possibleGenotypes = []
    132.                                 // 生成所有可能的基因组合
    133.                                 for (const g1 of parent1Genotypes) {
    134.                                         for (const g2 of parent2Genotypes) {
    135.                                                 // 每个父母贡献一个等位基因
    136.                                                 for (let i = 0; i < 2; i++) {
    137.                                                         for (let j = 0; j < 2; j++) {
    138.                                                                 const childGenotype = g1[i] + g2[j]
    139.                                                                 possibleGenotypes.push(childGenotype)
    140.                                                         }
    141.                                                 }
    142.                                         }
    143.                                 }
    144.                                 return possibleGenotypes
    145.                         },
    146.                         calculateProbabilities(genotypes) {
    147.                                 const bloodTypeCounts = {
    148.                                         'A': 0,
    149.                                         'B': 0,
    150.                                         'AB': 0,
    151.                                         'O': 0
    152.                                 }
    153.                                 // 基因型到血型的映射
    154.                                 const genotypeToType = {
    155.                                         'AA': 'A',
    156.                                         'AO': 'A',
    157.                                         'BB': 'B',
    158.                                         'BO': 'B',
    159.                                         'AB': 'AB',
    160.                                         'OO': 'O'
    161.                                 }
    162.                                 // 统计每种血型的出现次数
    163.                                 for (const genotype of genotypes) {
    164.                                         const type = genotypeToType[genotype]
    165.                                         bloodTypeCounts[type]++
    166.                                 }
    167.                                 const total = genotypes.length
    168.                                 const probabilities = {}
    169.                                 // 计算概率
    170.                                 for (const type in bloodTypeCounts) {
    171.                                         const count = bloodTypeCounts[type]
    172.                                         if (count > 0) {
    173.                                                 probabilities[type] = (count / total * 100).toFixed(1)
    174.                                         }
    175.                                 }
    176.                                 return probabilities
    177.                         }
    178.                 }
    179.         }
    180. </script>
    181. <style>
    182.         .container {
    183.                 padding: 20rpx;
    184.         }
    185.         .header {
    186.                 margin: 30rpx 0;
    187.                 text-align: center;
    188.         }
    189.         .title {
    190.                 font-size: 40rpx;
    191.                 font-weight: bold;
    192.                 color: #333;
    193.         }
    194.         .card {
    195.                 background-color: #fff;
    196.                 border-radius: 16rpx;
    197.                 padding: 30rpx;
    198.                 margin-bottom: 30rpx;
    199.                 box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.05);
    200.         }
    201.         .subtitle {
    202.                 font-size: 32rpx;
    203.                 font-weight: bold;
    204.                 margin-bottom: 30rpx;
    205.                 display: block;
    206.                 color: #333;
    207.         }
    208.         .form-item {
    209.                 margin-bottom: 30rpx;
    210.         }
    211.         .label {
    212.                 font-size: 28rpx;
    213.                 color: #666;
    214.                 margin-bottom: 10rpx;
    215.                 display: block;
    216.         }
    217.         .picker {
    218.                 height: 80rpx;
    219.                 line-height: 80rpx;
    220.                 padding: 0 20rpx;
    221.                 border: 1rpx solid #eee;
    222.                 border-radius: 8rpx;
    223.                 font-size: 28rpx;
    224.         }
    225.         .calculate-btn {
    226.                 background-color: #4CAF50;
    227.                 color: white;
    228.                 margin-top: 40rpx;
    229.                 border-radius: 8rpx;
    230.                 font-size: 30rpx;
    231.                 height: 90rpx;
    232.                 line-height: 90rpx;
    233.         }
    234.         .result-card {
    235.                 background-color: #e9f7ef;
    236.         }
    237.         .result-text {
    238.                 font-size: 28rpx;
    239.                 margin-bottom: 20rpx;
    240.                 display: block;
    241.         }
    242.         .blood-type {
    243.                 color: #e74c3c;
    244.                 font-weight: bold;
    245.         }
    246.         .probability {
    247.                 font-size: 26rpx;
    248.                 color: #666;
    249.                 display: block;
    250.                 margin-top: 10rpx;
    251.         }
    252.         .note-title {
    253.                 font-weight: bold;
    254.                 font-size: 28rpx;
    255.                 margin-bottom: 15rpx;
    256.                 display: block;
    257.                 color: #333;
    258.         }
    259.         .note-text {
    260.                 font-size: 26rpx;
    261.                 color: #666;
    262.                 display: block;
    263.                 margin-bottom: 10rpx;
    264.                 line-height: 1.6;
    265.         }
    266. </style>
    复制代码
    到此这篇关于用 Deepseek 写的uniapp血型遗传查询工具的文章就介绍到这了,更多相关Deepseek uniapp血型遗传内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

    来源:https://www.jb51.net/javascript/340023hk9.htm
    免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

    本帖子中包含更多资源

    您需要 登录 才可以下载或查看,没有账号?立即注册

    ×

    最新评论

    QQ Archiver 手机版 小黑屋 福建二哥 ( 闽ICP备2022004717号|闽公网安备35052402000345号 )

    Powered by Discuz! X3.5 © 2001-2023

    快速回复 返回顶部 返回列表