用C#辅助分数 [英] Assist with fractions in C#

查看:94
本文介绍了用C#辅助分数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

实现一个C#控制台应用程序,它将提示用户输入两个分数的整数值。请注意,每个分数由整数分子和整数分母部分组成。



您的程序需要以下列格式显示两个分数:

原始分数:

分数1:1/2

分数2:2/5



您的程序还需要以非简化形式计算和显示两个分数的总和和产品

,如下所示:

具有共同分母的新分数:

分数1:5/10

分数2:4/10

总和:5/10 + 4/10 = 9/10
产品:1/2 x 2/5 = 2/10



我尝试过:



Implement a C# Console Application that will prompt the user to enter integer values for two fractions. Take note that each fraction consists of an integer numerator and an integer denominator part.

Your program needs to display the two fractions in the following format:
Original Fractions:
Fraction 1: 1 / 2
Fraction 2: 2 / 5

Your program also needs to calculate and display the sum and the product of the two fractions
in a non-simplified form as follows:
New Fractions with Common Denominator:
Fraction 1: 5 / 10
Fraction 2: 4 / 10
Sum: 5 / 10 + 4 / 10 = 9 / 10
Product: 1 / 2 x 2 / 5 = 2 / 10

What I have tried:

int numerator1 = 1, denominator1 = 2;
            int numerator2 = 2, denominator2 = 5;

            int fraction1;
            int fraction2;

            Console.WriteLine("Original Fractions:");
            Console.WriteLine("Fraction 1:    {0}/{1}",numerator1,denominator1);
            Console.WriteLine("Fraction 2:    {0}/{1}", numerator2,denominator2);


            Console.Write("Enter a fraction1 ");
            fraction1 = int.Parse(Console.ReadLine());
            Console.Write("Enter a fraction 2 ");
            fraction2 = int.Parse(Console.ReadLine());

推荐答案

我这样做的方法是创建一个包含分子和分母的Fraction类。我还会覆盖ToString以使其返回人类可读的形式:

The way I would do it is to create a Fraction class which contains a numerator and a denominator. I'd also override ToString to have it return a "human readable" form:
numerator / denominator



我还创建了Add和Product方法,它们将两个分数组合在一起,将sum和product分别作为一个新的Fraction实例返回。



然后在我的Main方法中,我从用户那里得到两个值 - 就像你被问到的那样 - 使用值构造一个Fraction类的实例。创建第二个,并使用Add和Product方法与ToString一起打印结果。



听起来很复杂?它不是,不是真的 - 它实际上比你试图做的更简单一旦你绕过它。



但是......这是你的作业,所以我不会给你代码。

试一试,看看你有多远!


I'd also create Add and Product methods which combined two fractions to return the sum and product respectively as a new Fraction instance.

Then in my Main method, I'd get the two values from the user - like you are asked - construct an instance of the Fraction class using the values. Create a second, and use the Add and Product methods together with ToString to print the result.

Sounds complicated? It isn't, not really - it's actually simpler than what you are trying to do once you get your head round it.

But ... this is your homework, so I'll not give you the code.
Give it a try, and see how far you get!


我们不做你的HomeWork。 br $>
HomeWork不会在乞求其他人做你的工作时测试你的技能,它会让你思考并帮助你的老师检查你对你所学课程的理解以及问题你应用它们。

你的任何失败都会帮助你的老师发现你的弱点并设定补救措施。

所以,试一试,重读你的课程并开始工作。如果您遇到特定问题,请显示您的代码并解释这个问题,我们可能会提供帮助。



作为程序员,您的工作是创建算法解决特定问题,你不能依赖别人永远为你做,所以有一段时间你必须学会​​如何。而且越快越好。

当你要求解决方案时,就像试图通过培训其他人来学习开车一样。

创建算法基本上是找到数学并做出必要的调整以适应你的实际问题。
We do not do your HomeWork.
HomeWork is not set to test your skills at begging other people to do your work, it is set to make you think and to help your teacher to check your understanding of the courses you have taken and also the problems you have at applying them.
Any failure of you will help your teacher spot your weaknesses and set remedial actions.
So, give it a try, reread your lessons and start working. If you are stuck on a specific problem, show your code and explain this exact problem, we might help.

As programmer, your job is to create algorithms that solve specific problems and you can't rely on someone else to eternally do it for you, so there is a time where you will have to learn how to. And the sooner, the better.
When you just ask for the solution, it is like trying to learn to drive a car by having someone else training.
Creating an algorithm is basically finding the maths and make necessary adaptation to fit your actual problem.


这篇关于用C#辅助分数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆