返回字符串的函数 - Swift

时间:2014-10-18 16:02:36

标签: ios xcode swift

我正在做一个分数计算器,我正在尝试添加2个分数,当我放入我的分数时它只添加我的整数而没有别的如果我的分数是2 3/4 + 2 3/5它添加整数和输出4

    var firstStep = firstDenomInTextField! * firstWholeInTextField! / firstDenomInTextField!

    var secondStep = firstStep + firstNumInTextField! / firstDenomInTextField!

    var thirdStep = secondDenomInTextField! * secondWholeInTextField! / secondDenomInTextField!
    var fourthStep = thirdStep + secondNumInTextField! / secondDenomInTextField!

    var calculatedAnswer = (secondStep + fourthStep)
    var numerator = Int(calculatedAnswer * 10 * 10)
    println(numerator)
    answerLabel.hidden = false
    answerLabel.text = printSimplifiedFraction(Numerator: numerator)

printSimplifiedFraction Function

func printSimplifiedFraction(Numerator numerator: Int, Denominator denominator: Int = 100) -> String
{
    var finalNumerator = numerator;
    var finalDenominator = denominator;

    var wholeNumbers:Int = numerator / denominator;
    var remainder:Int = numerator % denominator;

    //println("wholeNumbers = \(wholeNumbers), remainder = \(remainder)");
    //println("\(denominator) % \(remainder) = \(denominator % remainder)");

    if(remainder > 0)
    {
        // see if we can simply the fraction part as well
        if(denominator % remainder == 0) // no remainder means remainder can be simplified further
        {
            finalDenominator = denominator / remainder;
            finalNumerator = remainder / remainder;
        }
        else
        {
            finalNumerator = remainder;
            finalDenominator = denominator;
        }
    }

    if(wholeNumbers > 0 && remainder > 0)
    {
        // prints out whole number and fraction parts
        return("Simplified fraction of \(numerator)/\(denominator) = \(wholeNumbers) \(finalNumerator)/\(finalDenominator)");
    }
    else if (wholeNumbers > 0 && remainder == 0)
    {
        // prints out whole number only
        return("Simplified fraction of \(numerator)/\(denominator) = \(wholeNumbers)");
    }
    else
    {
        // prints out fraction part only
        return("Simplified fraction of \(numerator)/\(denominator) = \(finalNumerator)/\(finalDenominator)");
    }
}

我的问题我想这样做它不只是添加整数而是添加整个分数。 如果您需要任何澄清或问题,请在下面发表评论

3 个答案:

答案 0 :(得分:1)

//如果您正在使用分数,则应使用Double而不是Int。另外,乘以10时,应使用10.0。小心你将Int(整数)与Double(分数)混合。同样,如果省略它就将vars声明为参数,默认情况下它将是一个常量,如果你想改变它,你不需要第二个var,只需在声明它时在它前面添加var)。 我想你应该从一开始就重新启动它,语法就可以了。不要忘记在需要时从Int转换为Double。

func printSimplifiedFraction(var myNumerator: Double, var myDenominator: Double = 100.0) -> String {
    var wholeNumbers:Int = Int(myNumerator / myDenominator)
    var remainder:Int = Int(myNumerator % myDenominator)
    var finalNumerator:Int = 0
    var finalDenominator:Double = 0.0
    if (remainder > 0) {
        // see if we can simply the fraction part as well
        if ((myDenominator % Double(remainder)) == 0) {
            myDenominator = myDenominator / Double(remainder)
            var finalNumerator:Double = Double(remainder) / Double(remainder)
        } else {
            var finalNumerator = remainder
            var finalDenominator = myDenominator
        }
    }

    if(wholeNumbers > 0 && remainder > 0) {
        // prints out whole number and fraction parts
        return "Simplified fraction of \(myNumerator)/\(myDenominator) = \(wholeNumbers) \(finalNumerator)/\(finalDenominator)"
    } else {
        if (wholeNumbers > 0 && remainder == 0) {
            // prints out whole number only
            return "Simplified fraction of \(myNumerator)/\(myDenominator) = \(wholeNumbers)"
        } else {
            // prints out fraction part only
            return "Simplified fraction of \(myNumerator)/\(myDenominator) = \(finalNumerator)/\(finalDenominator)"
        }
    }
}
printSimplifiedFraction(1.0, myDenominator: 2.0)

答案 1 :(得分:0)

我认为你的数学在你现在的代码中。以下代码计算分数并返回带有结果的字符串。

    ...
    var wholeTotal = firstWholeInTextField! + secondWholeInTextField!
    var numeratorTotal = (firstNumInTextField! * secondDenomInTextField!) + (secondNumInTextField! * firstDenomInTextField!)
    let denominatorTotal = firstDenomInTextField! * secondDenomInTextField!
    while numeratorTotal > denominatorTotal {
        wholeTotal++
        numeratorTotal -= denominatorTotal
    }
    let resultString = simplifiedFractionStringWith(wholeTotal, numerator: numeratorTotal, denominator: denominatorTotal)
    answerLabel.text = ("The result is: " + resultString)


func simplifiedFractionStringWith(wholeNumber: Int, numerator: Int, denominator: Int) -> String {
    if numerator > 0 {
        return ("\(wholeNumber) \(numerator)/\(denominator)")
    } else {
        return ("\(wholeNumber)")
    }
}

据说我真的相信整个事情更好地解决了创建一个分数结构......

答案 2 :(得分:0)

就像我在其他答案中提到的那样,我认为通过为您的分数创建struct可以更好地解决整个问题。因为我(显然)没有生命,我把另一个例子放在一起:

struct fraction {
   var wholeNumber = 0
   var numerator = 0
   var denominator = 0

   func asString()-> String {
      if numerator > 0 {
         return ("\(wholeNumber) \(numerator)/\(denominator)")
      } else {
         return ("\(wholeNumber)")
      }
   }

   func combinedWith(aFraction: fraction) -> fraction {
      var wholeTotal = wholeNumber + aFraction.wholeNumber
      var numeratorTotal = (numerator * aFraction.denominator) + (aFraction.numerator * denominator)
      let denominatorTotal = denominator * aFraction.denominator
      while numeratorTotal > denominatorTotal {
         wholeTotal++
         numeratorTotal -= denominatorTotal
      }
      let combinedFraction = fraction(wholeNumber: wholeTotal, numerator: numeratorTotal, denominator: denominatorTotal)
      return combinedFraction
   }
}

然后显示两个分数总和的代码看起来像这样(在你的应用程序中):

let firstFraction = fraction(wholeNumber: firstWholeInTextField!, numerator: firstNumInTextField!, denominator: firstDenomInTextField!)
let secondFraction = fraction(wholeNumber: secondWholeInTextField!, numerator: secondNumInTextField!, denominator: secondDenomInTextField!)
let combinedFraction = firstFraction.combinedWith(secondFraction)
answerLabel.text = ("The result is: " + combinedFraction.asString())