xcode 返回字符串的函数 - Swift

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26441733/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 05:54:52  来源:igfitidea点击:

Functions Returning A String - Swift

iosxcodeswift

提问by alex

I'm doing a fraction calculator and I'm trying to add 2 fractions when I put in my fractions it just adds my Whole numbers only and nothing else if my fraction is 2 3/4 + 2 3/5 it add the whole numbers and outputs 4

我正在做一个分数计算器,当我输入分数时,我试图添加 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

printSimplifiedFraction 函数

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)");
    }
}

My QuestionI want to make it so it does not just add the whole numbers but add the whole fraction. If you need any clarifications or questions please comment them down below

我的问题我想让它不只是添加整数,而是添加整个分数。如果您需要任何说明或问题,请在下方评论

回答by Leo Dabus

If you are working with fractions, you should use Double instead of Int. Also when multiplying by 10 you should use 10.0 instead. Be careful you are mixing Int(Integers) with Double(fractions). Also when declaring vars as parameters if you omit it will be a constant by default, if you would like to change it you don't need a second var, just add var in front of it when declaring it there). I think You should restart it from the beginning, Syntax is OK. Don't forget to convert from Int to Double when needed.

如果您正在处理分数,则应使用 Double 而不是 Int。此外,当乘以 10 时,您应该使用 10.0。小心你混合 Int(Integers) 和 Double(fractions)。此外,在将 vars 声明为参数时,如果您省略它,默认情况下它将是一个常量,如果您想更改它,则不需要第二个 var,只需在声明它时在它前面添加 var 即可)。我认为您应该从头开始重新启动它,语法是可以的。不要忘记在需要时从 Int 转换为 Double。

回答by T. Benjamin Larsen

Like I mentioned in my other answer I think the whole thing would be much better solved by creating a structfor your fraction. As I (clearly) have no life, I've put together another example:

就像我在其他答案中提到的那样,我认为通过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
   }
}

Then the code to show the total of two fractions would look something like this (in your app):

然后显示两个分数总和的代码将如下所示(在您的应用程序中):

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())

回答by T. Benjamin Larsen

I think your math is somewhat of in the code your present. The following code calculates the fraction and returns a string with the result.

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

    ...
    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)")
    }
}

That being said I reallybelieve the whole thing is much better solved creating a fraction-struct...

话虽如此,我真的相信整个事情都可以更好地解决创建分数结构......