C#分数计算器的形式

C#分数计算器的形式,c#,forms,math,C#,Forms,Math,我想用C形式做一个分数计算器 我只是不能正确地计算 这是表格 我希望它能够计算出结果。 输入10/5和10/7时,结果应为3/7 还是像这样 我得到了什么 这是我的密码 private void button1_Click(object sender, EventArgs e) // result bottom { double box_In_Top_Left = Convert.ToDouble(textBox1.Text); // Right UPPER B

我想用C形式做一个分数计算器

我只是不能正确地计算 这是表格

我希望它能够计算出结果。 输入10/5和10/7时,结果应为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;
            }
            else
            {
                string string_Part_Whole = Convert.ToString(Part_Whole);
                textBox8.Text = string_Part_Whole;
            }


        }
        else // For if the the part is not bigger then the whole
        {

            textBox8.Text = part_String; // Displayes part in the box in the right corner
        }

    }

我不知道你的逻辑是否正确,但我看到了第一个问题->可能来自相同的测试:Ekstra_greatnes==整体

很难比较双,

您可以尝试使用十进制类型,它以十进制表示法存储数字。因此,0.1将精确表示 或者您可以使用(在microsoft网站上看到)一个ε值来比较这两个值:

// Initialize two doubles with apparently identical values
double double1 = .333333;
double double2 = (double) 1/3;
// Define the tolerance for variation in their values
double difference = Math.Abs(double1 * .00001);

// Compare the values
// The output to the console indicates that the two values are equal
if (Math.Abs(double1 - double2) <= difference)
   Console.WriteLine("double1 and double2 are equal.");
else
   Console.WriteLine("double1 and double2 are unequal.");
//用明显相同的值初始化两个double
双双1=.333333;
双双2=(双)1/3;
//定义其值变化的公差
双差=Math.Abs(double1*.00001);
//比较这些值
//控制台的输出表明这两个值相等

如果(Math.Abs(double1-double2),我建议将分子和分母分开存储。最好是为此创建一个新的结构
分数
。然后您可以使用计算最大公约数。有了这些信息,您可以格式化您的结果

string FormatFraction(int numerator, int denominator)
{
    int gcd = Gcd(numerator, denominator );
    numerator /= gcd;
    denominator /= gcd;
    return $"{numerator/denominator} {Math.Abs(numerator/denominator)}";
}

int Gcd(int numerator, int denominator)
{
    int a = Math.Abs(numerator);
    int b = Math.Abs(denominator);
    while (b != 0)
    {
        int temp = b;
        b = a % b;
        a = temp;
    }

    return a;
}
此代码示例假定您使用的是支持字符串插值($“”)的c#版本。如果不支持,则可以用字符串串联替换格式分数的最后一行


要添加两个分数
a=a_1/a_2
b=b_1/b_2
可以使用简单的公式
a+b=c=c=c_1/c_2=a_1*b_2+a_2*b_1/a_2*b_2

我不需要小数,框9必须没有小数