我正在试着用C#格式做一个分数计算器。
我就是搞不懂数学这是表单My Form
我希望它能够计算结果。当您键入10/5和10/7时,结果应该是3 3/7或类似于下面的How the result should be
我得到了什么My result
这是我的代码
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
}
}发布于 2018-09-09 01:28:08
我不知道你的逻辑是否正确,但我认为第一个问题可能来自相等测试: Ekstra_greatnes == ->
很难比较两倍,
您可以尝试使用decimal类型,它以十进制表示法存储数字。因此,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.");发布于 2018-09-09 14:21:35
我建议将分子和分母分开存储。最好的方法是为此目的创建一个新的结构Fraction。然后,您可以使用euclidian algorithm计算最大公约数。有了这些信息,您就可以格式化结果了。
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_1 / c_2 = a_1 * b_2 + a_2 * b_1 / a_2 * b_2
https://stackoverflow.com/questions/52236096
复制相似问题