亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

表單中的 C# 分數計算器

表單中的 C# 分數計算器

C#
臨摹微笑 2022-01-09 14:53:15
我正在嘗試以 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;            }    }
查看完整描述

2 回答

?
眼眸繁星

TA貢獻1873條經驗 獲得超9個贊

我不知道您的邏輯是否正確,但我看到了第一個問題-> 可能來自平等測試:Ekstra_greatnes == whole


很難比較雙倍,


您可以嘗試使用以十進制表示法存儲數字的十進制類型。因此 0.1 將可以精確表示,或者您可以使用(在 microsoft 網站上看到)一個 epsilon 值來比較這兩個值:


// 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.");


查看完整回答
反對 回復 2022-01-09
?
富國滬深

TA貢獻1790條經驗 獲得超9個贊

我建議將分子和分母分開存儲。最好的辦法是Fraction為此目的創建一個新結構。然后你可以使用歐幾里得算法來計算最大公約數。有了這些信息,您就可以格式化您的結果。


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;

}

此代碼示例假定您正在使用支持字符串插值 ($"<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


查看完整回答
反對 回復 2022-01-09
  • 2 回答
  • 0 關注
  • 260 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號