对一些代码的建议 [英] Advice on some code

查看:56
本文介绍了对一些代码的建议的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我会在下一篇文章中发布代码我只想列出一个解释和一些已知的错误。


注意:这不是一个家庭作业,我'这样做只是因为我尝试这样做很有意思。


这段代码是一个用分子和分母表示有理数的对象,并提供简单的加法减法乘法和除法函数。


可能的低效率:

我最好使用素数表来检查最大公因数,但这可能需要太多的记忆力。


另一种选择是从较小的数字开始并通过递减计数变量来反向检查,直到达到较小数字的平方根,如果两者都可以被数字除,那么它是最大的共同因素。我不知道是否会更有效率。


已知/疑似错误。

如果尝试进行数学运算导致乘法运算(加法可能导致乘法,使得分数分母匹配),并且得到的分子或分母的绝对值对于变量类型来说太大,即使答案已经简化为范围内,程序也可能会崩溃。 />

我不知道如何解决这个问题,我一直在思考,我迄今为止唯一的想法就是限制分子/分母最大值是正常最大值的平方根。这个解决方案的唯一问题是素数可能会妨碍在这些情况下的简化。


有没有人有任何想法或看到任何其他错误?

I''ll be posting the code in the next post I just want to list an explanation and some known bugs.

Note: This is NOT a homework assignment, I''m doing this just because I was interesting in trying to do it.

This code is to be an object that represents a rational number with a numerator and denominator and provides the simple addition subtraction multiplication and division functions.

Possible inefficiency:
It might be best for me to check for greatest common factor using a table of primes, but that might take too much memory to hold.

Another option is to start at the smaller number and check in reverse by decrementing a count variable until the square root of the smaller number is reached, if both are divisable by a numer, then its the greatest common factor. I don''t know if that would be more efficient or not.

Known/suspected bug.
If a a mathematical operation is attempted that causes multiplication (addition might cause multiplication so that the fraction denominators match), and the absolute value of the resulting numerator or denominator is too large for the variable type, even if the answer would have simplified to be in-range, the program will probably crash

I don''t know how to solve this, I''ve been thinking it through, and the only idea I''ve had so far was to limit the numerator/denominator to have max values that are the square root of the normal max-value. The only problem with this solution is prime numbers might prevent simplification in these circumstances.

Does anyone have any ideas or see any other bugs?

推荐答案

展开 | 选择 | Wrap | 行号


我使用long来保存整数,为较大和较小的数字创建了另一个版本

I made another version of this for largerand smaller numbers using longs to hold the integers

展开 | 选择 | Wrap | 行号


我会发布我的测试驱动程序代码,但它大于每条消息的字符数限制。
I''d post my test driver code, but its larger than the Limit for the number of characters per message.


这篇关于对一些代码的建议的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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