java生成有理数,用java对象表示有理数

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值