用java对象表示有理数 [英] representing rational numbers using java objects

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

问题描述

非常感谢你能够参加我,如果有任何人,他是编程方面的专家,那我该如何编写可以计算e

解决方案<的近似值的程序/ div>

有很多方法来估算e的值。您希望实现哪种方法?


在此分配中,我们将使用对象查看java中有理数的可能表示。 java语言使用与其他实数相同的表示来表示有理数。这是浮点表示。但是我们都知道,浮点数非常不准确。这意味着 ?实际上可能表示为0.49998,对于某些应用程序可能不够好。

在这个赋值中,我们将探索一种使用结构表示有理数的方法。对于每个有理数,我们将分子/分母表示/存储为整数。我们将使用这样的结构:

struct Rational {int a,int b;};


表示一个数字。在C中,我们使用以下方式声明一个新结构来表示我们的有理数:

typedef struct Rational Rational;


这将声明新类型Rational作为别名struct Rational。我们现在可以定义这种数据的操作。特别是我们需要实现有理数运算。我们将仅限于加法和乘法的运算。 (请注意,减法实际上是伪装的补充并且不再复杂。)

您的第一个任务是实现函数:

Rational R_init(int a,int b );
Rational R_add(Rational x,Rational y);

Rational R_mult(Rational x,Rational y);

int R_show(Rational x);


函数应该执行以下操作:R_init应返回a和b为整数的有理表示。 R_add应该添加两个返回其总和的有理数,R_mult应该返回它传递的两个有理数参数的乘积。 R_show应该打印出有理数(形式为a / b,而不是实数十进制)。

这部分应该非常简单。您不应该将有理数减少到最低形式。也就是说,如果你添加和,可以报告2/6作为答案,而不是1/3。


分配的下一部分要求你计算一个关闭e的有理逼近,通常近似为2.17?注意,e实际上是1 + 1/1 + 1/2 + 1/6 + 1/24 + 1/120 +?尽可能计算这个总和,而不会给出整数溢出问题。简而言之,写一个函数:

void e(void);

这个函数应该使用你上面写的有理算术函数来计算和打印出来。



帮帮我请回答!!!!


本网站的专家非常乐意帮助您解决问题,但他们不能为你做任务/计划。首先尝试自己的作业/计划并发布有关您遇到的任何困难或有关您不知道如何实现的特定代码功能的问题。


请阅读< a href =http://www.thescripts.com/forum/faq.php?faq=posting_guidelines\"target =_ blank>发布指南,特别是课程作业发布指南


然后当你准备发布一个这个主题中的新问题。


主持人


thank you very much for attending to me,if there is any one,who is an expert in programming please how can i write the programme that can compute the approximated value of e

解决方案

There are many ways to approximate the value of e. Which method were you looking to implement?


In this assignment we shall look at a possible representation of rational numbers in java using objects. The java language represents rational numbers using the same representation used for other real numbers. This is the floating-point representation. However as we may all know, floating-point numbers are quite inaccurate. This means that ? might actually be represented as 0.49998, which may not be good enough for some applications.
In this assignment we shall explore a way of representing rational numbers using structures. For each rational number we shall represent/store the numerator and denominator as integers. We shall use a structure like this:
struct Rational {int a, int b;};

to represent a number . In C we declare a new structure to represent our rational number using:
typedef struct Rational Rational;

This declares the new type Rational to be an alias for struct Rational. We can now define operations on data of this kind. In particular we need to implement rational number arithmetic. We shall limit ourselves to the operations of addition and multiplication. (Note that subtraction is really addition in disguise and is no more complicated.)
Your first task will be to implement the functions:
Rational R_init(int a, int b);
Rational R_add(Rational x, Rational y);
Rational R_mult(Rational x, Rational y);
int R_show(Rational x);

The functions should do the following: R_init should return a rational representation for where a and b are the integers. R_add should add two rational numbers returning their sum, R_mult should return the product of the two rational arguments it is passed. R_show should print out the rational number (in the form a/b, not as real decimal).
This part should be pretty straightforward. You should not need to reduce the rational numbers to their lowest form. That is, if you add and , it is OK to report 2/6 as the answer, rather than 1/3.

The next part of the assignment requires you to compute a close rational approximation to e, which is often approximated to 2.17? Note that e is actually 1 + 1/1+1/2+1/6+1/24+1/120 + ? Compute this sum as far as it will go without giving you integer overflow problems. In short, write a function:
void e(void);
This function should compute and print out e using the rational arithmetic functions you wrote above.


help me please answer me!!!!


The experts on this site are more than happy to help you with your problems but they cannot do your assignment/program for you. Attempt the assignment/program yourself first and post questions regarding any difficulties you have or about a particular function of the code that you don''t know how to achieve.

Please read the Posting Guidelines and particularly the Coursework Posting Guidlines.

Then when you are ready post a new question in this thread.

MODERATOR


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

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