表单中的 C# 分数计算器

我正在尝试以 C# 形式制作分数计算器。

我只是无法正确计算这是 我的表格

我希望它能够计算结果。当您输入 10/5 和 10/7 时,结果应该是 3 3/7 或者像这样 结果应该如何

我得到什么 我的结果

这是我的代码

private void button1_Click(object sender, EventArgs e) // result bottom


    {


        double box_In_Top_Left = Convert.ToDouble(textBox1.Text); // Right UPPER BOX

        double box_In_Down_Left = Convert.ToDouble(textBox2.Text); // Venstra Nederst string


        double box_In_Top_Right = Convert.ToDouble(textBox3.Text); // Højre OP string

        double box_In_Down_Right = Convert.ToDouble(textBox4.Text); // Højre Nederst String



        double whole = box_In_Down_Right * box_In_Down_Left; // Whole (Bottom Part of A fraction


        string whole_String = Convert.ToString(whole); // Converts the Whole to a string

        textBox7.Text = whole_String; // Shows the Answer in the box in the bottom right 


        double Calculation1 = box_In_Top_Left * box_In_Down_Right;  // Calculates the top lefts box result


        double Calculation2 = box_In_Top_Right * box_In_Down_Left; // Calculates the top right box Result


        double part = Calculation2 + Calculation1; // Calculates answer for the top box


        string part_String = Convert.ToString(part);



        if (part >= whole) // if the part is bigger then the whole

        {



            double Amount_Of_times_greater = part / whole;



            string string_Amount_Of_times_greater = Convert.ToString(Amount_Of_times_greater);


            double Ekstra_greatnes = part / Amount_Of_times_greater;


            textBox6.Text = string_Amount_Of_times_greater;

            double Part_Whole = (part / Amount_Of_times_greater);



            if (Ekstra_greatnes == whole)

            {


                Part_Whole = Part_Whole - whole;

                string string_Part_Whole = Convert.ToString(Part_Whole);


                textBox8.Text = string_Part_Whole;

            }

    }


临摹微笑
浏览 262回答 2
2回答

眼眸繁星

我不知道您的逻辑是否正确,但我看到了第一个问题-> 可能来自平等测试:Ekstra_greatnes == whole很难比较双倍,您可以尝试使用以十进制表示法存储数字的十进制类型。因此 0.1 将可以精确表示,或者您可以使用(在 microsoft 网站上看到)一个 epsilon 值来比较这两个值:// Initialize two doubles with apparently identical valuesdouble double1 = .333333;double double2 = (double) 1/3;// Define the tolerance for variation in their valuesdouble difference = Math.Abs(double1 * .00001);// Compare the values// The output to the console indicates that the two values are equalif (Math.Abs(double1 - double2) <= difference)&nbsp; &nbsp;Console.WriteLine("double1 and double2 are equal.");else&nbsp; &nbsp;Console.WriteLine("double1 and double2 are unequal.");

富国沪深

我建议将分子和分母分开存储。最好的办法是Fraction为此目的创建一个新结构。然后你可以使用欧几里得算法来计算最大公约数。有了这些信息,您就可以格式化您的结果。string FormatFraction(int numerator, int denominator){&nbsp; &nbsp; int gcd = Gcd(numerator, denominator );&nbsp; &nbsp; numerator /= gcd;&nbsp; &nbsp; denominator /= gcd;&nbsp; &nbsp; return $"{numerator/denominator} {Math.Abs(numerator/denominator)}";}int Gcd(int numerator, int denominator){&nbsp; &nbsp; int a = Math.Abs(numerator);&nbsp; &nbsp; int b = Math.Abs(denominator);&nbsp; &nbsp; while (b != 0)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; int temp = b;&nbsp; &nbsp; &nbsp; &nbsp; b = a % b;&nbsp; &nbsp; &nbsp; &nbsp; a = temp;&nbsp; &nbsp; }&nbsp; &nbsp; return a;}此代码示例假定您正在使用支持字符串插值 ($"<interpolated string>") 的 c# 版本。如果不是,您可以用字符串连接替换格式分数的最后一行。要添加两个分数a = a_1 / a_2,b = b_1 / b_2您可以使用简单的公式a + b = c = c_1 / c_2 = a_1 * b_2 + a_2 * b_1 / a_2 * b_2
打开App,查看更多内容
随时随地看视频慕课网APP