2016-03-24 85 views
0

我目前正在上化學課,並認爲這將是一個有趣的程序,可以計算強酸/弱酸溶液的pH值。代碼在遊樂場中工作,我想最終將其實施到iOS項目中。該程序需要3個參數:酸的初始濃度,酸的化學式以及酸的平衡常數(如果需要)。該程序首先考慮酸的初始濃度,然後考慮酸的化學分子式並確定酸是強還是弱。接下來,我建立了一個if語句來根據酸的強度計算pH值。如果酸較弱,則需要一個平衡常數。以下是我迄今爲止:該代碼有效,但不知道如何實現它到iOS

import UIKit 

func acidConcentration(acidMolarity: Double) -> Double { 
    return acidMolarity 
} 

let initialConcentration = acidConcentration(0.50) 

var hydroniumConcentration = 0.00 

var pH = 0.00 

func determineAcidStrength(acidName: String) -> String { 

    var acidStrength = "" 

    if acidName=="HBr" || acidName=="HI" || acidName=="HClO4" || acidName=="HCl" || acidName=="HClO3" || acidName=="H2SO4" || acidName=="HNO3" { 
     acidStrength = "strong" 
    } else { 
     acidStrength = "weak" 
    } 

    return acidStrength 

} 

let strength = determineAcidStrength("HBr") 

if strength == "strong" { 

    hydroniumConcentration = initialConcentration 

    pH = -log10(hydroniumConcentration) 

    print(String(format: "%.2f", pH)) 

} else { 

    func equilibriumConstant(ka: Double) -> Double { 
     return ka 
    } 

    let eqConstant = equilibriumConstant 

    var weakAcidConcentration = sqrt(eqConstant(1.8e-5) * initialConcentration) 

    pH = -log10(weakAcidConcentration) 

    print(String(format: "%.2f", pH)) 

} 

由於我很新的節目,這是我在寫任何代碼的第一次嘗試,我想知道是否有收拾什麼,我有什麼辦法或任何提示更好地組織代碼。任何幫助表示讚賞。謝謝。

回答

2

您發佈的代碼對於可以在單個函數中完成的事情過於複雜。

下面的函數接受2個參數,酸值名稱爲String,初始濃度爲Double,並返回濃度值作爲Double值。

func calculateConcentration(acidName: String, initialConcentration: Double) -> Double { 
    var acidIsStrong = false; 

    if acidName=="HBr" || acidName=="HI" || acidName=="HClO4" || acidName=="HCl" || acidName=="HClO3" || acidName=="H2SO4" || acidName=="HNO3" { 
     acidIsStrong = true; 
    } 
    if acidIsStrong { 
     return -log10(initialConcentration); 
    } else { 
     return -log10((1.8e-5) * initialConcentration); 
    } 
} 

下面是一個例子使用的功能:

//Just pass in your acid name and initial concentration 
let concentration = calculateConcentration("HBr", initialConcentration: 0.50); 
print(String(format: "%.2f", concentration)); 

下面是更有效地寫入一個位的函數的較小版本(它的工作原理是相同的):

func calculateConcentration(acidName: String, initialConcentration: Double) -> Double { 
    if acidName=="HBr" || acidName=="HI" || acidName=="HClO4" || acidName=="HCl" || acidName=="HClO3" || acidName=="H2SO4" || acidName=="HNO3" { 
     return -log10(initialConcentration); 
    } 
    return -log10((1.8e-5) * initialConcentration); 
} 

還有一個更小的版本,它不是非常易讀:

func calculateConcentration(acidName: String, initialConcentration: Double) -> Double { 
    return -log10(initialConcentration * ((acidName=="HBr" || acidName=="HI" || acidName=="HClO4" || acidName=="HCl" || acidName=="HClO3" || acidName=="H2SO4" || acidName=="HNO3") ? (1.8e-5) : 1.0)); 
} 
0

我沒有太大的化學家的話,我不知道有關的公式,但這個怎麼樣:

class Acid: CustomStringConvertible { 

    static let strongAcids = [ "HBr", "HI", "HClO4", "HCl", "HClO3", "H2SO4", "HNO3" ] 

    var name: String 
    var concentration: Double 
    var equilibriumConstant: Double? 

    var pH: Double { 
     get { 
      var adjustedConcentration = concentration 
      if !Acid.strongAcids.contains(name) { 
       guard let ka = equilibriumConstant else { 
        return Double.NaN; 
       } 
       adjustedConcentration = sqrt(ka * concentration) 
      } 
      return -log10(adjustedConcentration) 
     } 
    } 

    init(name: String, concentration: Double, equilibriumConstant ka: Double? = nil) { 
     self.name = name 
     self.concentration = concentration 
     self.equilibriumConstant = ka 
    } 

    var description: String { 
     get { 
      if let ka = equilibriumConstant { 
       return "\(name) (concentration: \(concentration)/EqK: \(ka) pH: \(String(format: "%.2f", pH)))" 

      } else { 
       return "\(name) (concentration: \(concentration)/pH: \(String(format: "%.2f", pH)))" 
      } 
     } 
    } 
} 

let acid = Acid(name: "HBr", concentration: 0.5); 
print("\(acid)") // prints "HBr (concentration: 0.5/pH: 0.30)" 
print(String(format: "%.2f", acid.pH)) // prints "0.30" 
let weakAcid = Acid(name: "Weak", concentration: 0.2, equilibriumConstant: 1.8e-5) 
print("\(weakAcid)") // prints "Weak (concentration: 0.2/EqK: 1.8e-05 pH: 2.72)" 

作爲創建一個應用程序......對於一個簡單的應用程序這樣,你可以可能只是創建一個包含3個文本字段(名稱,濃度和常量)和一個按鈕「計算」的「單一視圖應用程序」。我希望這是有道理的。

相關問題