| View previous topic :: View next topic |
| Author |
Message |
.exe Cheater
Reputation: 0
Joined: 28 Dec 2009 Posts: 46
|
Posted: Sun Apr 17, 2011 10:53 am Post subject: fraction |
|
|
what is the correct data type for fraction? in c++
|
|
| Back to top |
|
 |
Slugsnack Grandmaster Cheater Supreme
Reputation: 71
Joined: 24 Jan 2007 Posts: 1857
|
Posted: Sun Apr 17, 2011 11:25 am Post subject: |
|
|
| there isn't. the closest you can get with basic types is float or double. if you really must use fractions you can make your own class. define 2 properties of that class object which are denominator and numerator.
|
|
| Back to top |
|
 |
pkedpker Master Cheater
Reputation: 1
Joined: 11 Oct 2006 Posts: 412
|
Posted: Tue Apr 19, 2011 11:16 pm Post subject: |
|
|
you can use a struct.
| Code: |
#include <iostream>
struct Fraction
{
int nNumerator;
int nDenominator;
};
void Multiply(Fraction sF1, Fraction sF2)
{
using namespace std;
// Don't forget the static cast, otherwise the compiler will do integer division!
cout << static_cast<float>(sF1.nNumerator * sF2.nNumerator) /
(sF1.nDenominator * sF2.nDenominator);
}
int main()
{
using namespace std;
// Allocate our first fraction
Fraction sF1;
cout << "Input the first numerator: ";
cin >> sF1.nNumerator;
cout << "Input the first denominator: ";
cin >> sF1.nDenominator;
// Allocate our second fraction
Fraction sF2;
cout << "Input the second numerator: ";
cin >> sF2.nNumerator;
cout << "Input the second denominator: ";
cin >> sF2.nDenominator;
Multiply(sF1, sF2);
return 0;
}
|
_________________
|
|
| Back to top |
|
 |
Slugsnack Grandmaster Cheater Supreme
Reputation: 71
Joined: 24 Jan 2007 Posts: 1857
|
Posted: Wed Apr 20, 2011 2:15 am Post subject: |
|
|
| if you're going to use c++ then you should make it a class. your multiply method is also misleading. the expected behaviour is that it modifies the first operand whereas yours just makes an output.
|
|
| Back to top |
|
 |
atom0s Moderator
Reputation: 205
Joined: 25 Jan 2006 Posts: 8587 Location: 127.0.0.1
|
Posted: Wed Apr 20, 2011 5:11 am Post subject: |
|
|
| Slugsnack wrote: | | if you're going to use c++ then you should make it a class. your multiply method is also misleading. the expected behaviour is that it modifies the first operand whereas yours just makes an output. |
The code is just taken from:
http://www.learncpp.com/cpp-tutorial/47-structs/
_________________
- Retired. |
|
| Back to top |
|
 |
Jani Grandmaster Cheater
Reputation: 2
Joined: 29 Dec 2006 Posts: 804
|
Posted: Wed Apr 20, 2011 2:48 pm Post subject: |
|
|
Yeah, write your own class as an exercise. It's actually a good exercise. Write all the overloaded operators with proper friendships etc.
I bet many of the "C++ coders" of this forum can't even properly implement this class.
|
|
| Back to top |
|
 |
Cheat Engine User Something epic
Reputation: 60
Joined: 22 Jun 2007 Posts: 2071
|
Posted: Wed Apr 20, 2011 3:17 pm Post subject: |
|
|
| And if you REALLY want to hit the right spot, go for some fixed point instead of floating point. It's actually a nice exercise to create a struct or class that does this.
|
|
| Back to top |
|
 |
.exe Cheater
Reputation: 0
Joined: 28 Dec 2009 Posts: 46
|
Posted: Tue Apr 26, 2011 8:01 am Post subject: |
|
|
| thanks guys!
|
|
| Back to top |
|
 |
|