初学者-C#分数计算器负分数的简化
我一直很难让我的分数计算器工作。我试图让简化工作,它正确地简化正分数,但如果我放一个负分数,它不会简化它,我不确定我做错了什么,我已经读了很多遍(Gcd和Reduce函数) 我对这一切都是陌生的,任何帮助我都感激 我的Reduce和GCD功能:初学者-C#分数计算器负分数的简化,c#,calculator,simplify,C#,Calculator,Simplify,我一直很难让我的分数计算器工作。我试图让简化工作,它正确地简化正分数,但如果我放一个负分数,它不会简化它,我不确定我做错了什么,我已经读了很多遍(Gcd和Reduce函数) 我对这一切都是陌生的,任何帮助我都感激 我的Reduce和GCD功能: public int gcd() { // assigned x and y to the answer Numerator/Denominator, as well as an // empty integer, this is t
public int gcd()
{
// assigned x and y to the answer Numerator/Denominator, as well as an
// empty integer, this is to make code more simple and easier to read
int x = answerNumerator;
int y = answerDenominator;
int m;
// check if numerator is greater than the denominator,
// make m equal to denominator if so
if (x > y)
m = y;
else
// if not, make m equal to the numerator
m = x;
// assign i to equal to m, make sure if i is greater
// than or equal to 1, then take away from it
for (int i = m; i >= 1; i--)
{
if (x % i == 0 && y % i == 0)
{
//return the value of i
return i;
}
}
return 1;
}
public void Reduce()
{
try
{
//assign an integer to the gcd value
int gcdNum = gcd();
if (gcdNum != 0)
{
answerNumerator = answerNumerator / gcdNum;
answerDenominator = answerDenominator / gcdNum;
}
if (answerDenominator < 0)
{
answerDenominator = answerDenominator * -1;
answerNumerator = answerNumerator * -1;
}
}
catch (Exception exp)
{
// display the following error message
// if the fraction cannot be reduced
throw new InvalidOperationException(
"Cannot reduce Fraction: " + exp.Message);
}
}
public int gcd()
{
//将x和y分配给答案分子/分母,以及
//空整数,这是为了使代码更简单,更易于阅读
int x=应答分子;
int y=应答分母;
int m;
//检查分子是否大于分母,
//使m等于分母,如果是这样
如果(x>y)
m=y;
其他的
//如果不是,则使m等于分子
m=x;
//指定i等于m,确保i大于m
//大于或等于1,然后从中移除
对于(int i=m;i>=1;i--)
{
如果(x%i==0&&y%i==0)
{
//返回i的值
返回i;
}
}
返回1;
}
公共空间减少()
{
尝试
{
//为gcd值指定一个整数
int gcdNum=gcd();
如果(gcdNum!=0)
{
answerNumerator=answerNumerator/gcdNum;
应答分母=应答分母/gcdNum;
}
如果(应答分母<0)
{
应答分母=应答分母*-1;
answerNumerator=answerNumerator*-1;
}
}
捕获(异常扩展)
{
//显示以下错误消息
//如果分数不能减少
抛出新的InvalidOperationException(
“无法减少分数:”+exp.Message);
}
}
我认为问题在于,当您确定GCD时,您正在检查for
循环中的值是否=1
,即使它可能是负数。为了避免这种情况,在确定GCD时,应获取分子和分母的绝对值
例如,这应该可以解决它:
public int gcd()
{
// assigned x and y to the absolute values of the answer Numerator/Denominator,
// as well as an empty integer, this is to make code more simple and easier to read
int x = Math.Abs(answerNumerator);
int y = Math.Abs(answerDenominator);
int m;
// check if numerator is greater than the denominator,
// make m equal to denominator if so
if (x > y)
m = y;
else
// if not, make m equal to the numerator
m = x;
// assign i to equal to m, make sure if i is greater
// than or equal to 1, then take away from it
for (int i = m; i >= 1; i--)
{
if (x % i == 0 && y % i == 0)
{
//return the value of i
return i;
}
}
return 1;
}
简短回答
你需要:
int x = Math.Abs(answerNumerator);
int y = Math.Abs(answerDenominator);
运行代码
这是给你的小提琴:
输出:
Initial: 2/4
Reduced: 1/2
---
Initial: 2/-4
Reduced: -1/2
---
运行代码:
using System;
public class Program
{
public static void Main()
{
Calc.Reduce(2,4);
Calc.Reduce(2,-4);
}
}
public static class Calc
{
public static int gcd(int answerNumerator, int answerDenominator)
{
// assigned x and y to the answer Numerator/Denominator, as well as an
// empty integer, this is to make code more simple and easier to read
int x = Math.Abs(answerNumerator);
int y = Math.Abs(answerDenominator);
int m;
// check if numerator is greater than the denominator,
// make m equal to denominator if so
if (x > y)
m = y;
else
// if not, make m equal to the numerator
m = x;
// assign i to equal to m, make sure if i is greater
// than or equal to 1, then take away from it
for (int i = m; i >= 1; i--)
{
if (x % i == 0 && y % i == 0)
{
//return the value of i
return i;
}
}
return 1;
}
public static void Reduce(int answerNumerator, int answerDenominator)
{
Console.Write("Initial: ");
WriteFraction(answerNumerator, answerDenominator);
try
{
//assign an integer to the gcd value
int gcdNum = gcd(answerNumerator, answerDenominator);
if (gcdNum != 0)
{
answerNumerator = answerNumerator / gcdNum;
answerDenominator = answerDenominator / gcdNum;
}
if (answerDenominator < 0)
{
answerDenominator = answerDenominator * -1;
answerNumerator = answerNumerator * -1;
}
}
catch (Exception exp)
{
// display the following error message
// if the fraction cannot be reduced
throw new InvalidOperationException("Cannot reduce Fraction: " + exp.Message);
}
Console.Write("Reduced: ");
WriteFraction(answerNumerator, answerDenominator);
Console.WriteLine("---");
}
public static void WriteFraction(int answerNumerator, int answerDenominator)
{
Console.WriteLine(string.Format("{0}/{1}", answerNumerator, answerDenominator));
}
}
使用系统;
公共课程
{
公共静态void Main()
{
计算减少(2,4);
计算减少(2,-4);
}
}
公共静态类计算
{
公共静态int gcd(int应答分子,int应答分母)
{
//将x和y分配给答案分子/分母,以及
//空整数,这是为了使代码更简单,更易于阅读
int x=数学Abs(应答分子);
int y=数学绝对值(应答分母);
int m;
//检查分子是否大于分母,
//使m等于分母,如果是这样
如果(x>y)
m=y;
其他的
//如果不是,则使m等于分子
m=x;
//指定i等于m,确保i大于m
//大于或等于1,然后从中移除
对于(int i=m;i>=1;i--)
{
如果(x%i==0&&y%i==0)
{
//返回i的值
返回i;
}
}
返回1;
}
公共静态void Reduce(整数应答分子,整数应答分母)
{
控制台。写(“首字母:”);
写操作(应答分子、应答分母);
尝试
{
//为gcd值指定一个整数
int gcdNum=gcd(应答分子、应答分母);
如果(gcdNum!=0)
{
answerNumerator=answerNumerator/gcdNum;
应答分母=应答分母/gcdNum;
}
如果(应答分母<0)
{
应答分母=应答分母*-1;
answerNumerator=answerNumerator*-1;
}
}
捕获(异常扩展)
{
//显示以下错误消息
//如果分数不能减少
抛出新的InvalidOperationException(“无法减少分数:+exp.Message”);
}
控制台。写入(“减少:”);
写操作(应答分子、应答分母);
Console.WriteLine(“--”);
}
公共静态无效写操作(内部应答分子,内部应答分母)
{
WriteLine(string.Format(“{0}/{1}”,应答分子,应答分母));
}
}
一般性评论:如果函数采用参数而不是依赖全局变量并作用于全局变量,则会更好。你能举一个“负分数”的例子吗?是不是简单地说,应答分子
或应答分母
都小于零?我指的是负分数。我把1 1/2-4 1/4放在计算器里,得到的是-2-6/8,而不是-2 3/4。这是什么意思?代码没有显示,但在内部你只存储分子和分母,对吗?因此,即使显示1 1/2
,在内部也将其存储为分子=3;分母=2
?