Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


fraction

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
.exe
Cheater
Reputation: 0

Joined: 28 Dec 2009
Posts: 46

PostPosted: Sun Apr 17, 2011 10:53 am    Post subject: fraction Reply with quote

what is the correct data type for fraction? in c++ Question
Back to top
View user's profile Send private message
Slugsnack
Grandmaster Cheater Supreme
Reputation: 71

Joined: 24 Jan 2007
Posts: 1857

PostPosted: Sun Apr 17, 2011 11:25 am    Post subject: Reply with quote

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
View user's profile Send private message
pkedpker
Master Cheater
Reputation: 1

Joined: 11 Oct 2006
Posts: 412

PostPosted: Tue Apr 19, 2011 11:16 pm    Post subject: Reply with quote

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;
}

_________________
Hacks I made for kongregate.
Kongregate Universal Badge Hack: http://forum.cheatengine.org/viewtopic.php?p=4129411
Kongreate Auto Rating/Voter hack: http://forum.cheatengine.org/viewtopic.php?t=263576
Took a test lol
Back to top
View user's profile Send private message
Slugsnack
Grandmaster Cheater Supreme
Reputation: 71

Joined: 24 Jan 2007
Posts: 1857

PostPosted: Wed Apr 20, 2011 2:15 am    Post subject: Reply with quote

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
View user's profile Send private message
atom0s
Moderator
Reputation: 205

Joined: 25 Jan 2006
Posts: 8587
Location: 127.0.0.1

PostPosted: Wed Apr 20, 2011 5:11 am    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
Jani
Grandmaster Cheater
Reputation: 2

Joined: 29 Dec 2006
Posts: 804

PostPosted: Wed Apr 20, 2011 2:48 pm    Post subject: Reply with quote

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
View user's profile Send private message
Cheat Engine User
Something epic
Ban
Reputation: 60

Joined: 22 Jun 2007
Posts: 2071

PostPosted: Wed Apr 20, 2011 3:17 pm    Post subject: Reply with quote

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
View user's profile Send private message
.exe
Cheater
Reputation: 0

Joined: 28 Dec 2009
Posts: 46

PostPosted: Tue Apr 26, 2011 8:01 am    Post subject: Reply with quote

thanks guys!
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites