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 calculater-source code included

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
fatal rival
How do I cheat?
Reputation: 0

Joined: 08 Jul 2007
Posts: 8

PostPosted: Sun Jul 08, 2007 6:51 pm    Post subject: fraction calculater-source code included Reply with quote


this is simple fraction calculater i made in c++.
here are the source codes

i perfer to use modulo for this code(for the fraction class), so it is more readable.

enjoy! Very Happy
Code:

// fraction calculater : Defines the entry point for the console application.
//

#include "stdafx.h"
using namespace std;

class fraction{
public:
   int num,den;
   void set(int n, int d){num=n;den=d;normalize();}
   int getnum() {return num;}
   int getden() {return den;}
   int div(int a,int b,int c,int d);
   int multi(int a,int b,int c,int d);
   int add(int a,int b,int c,int d);
   int sub(int a,int b,int c,int d);
private:
   void normalize();
   int gcf(int a,int b);
   int lcm(int a, int b);

};

int _tmain(int argc, _TCHAR* argv[]){
   cout<<"\tThis is a calculater only for fractions.";
   while(1){
   int a=0,b=0,c=0,d=0;
   int o=0;
   fraction fract;
   int k=0;
   cout<<"\nEnter 1 to multiply or enter 2 to divide or 3 to add or 4 to subtract.";
   cin>>k;
   switch(k){
default:
      break;
            case 1:
      cout<<"\nget first numerator";
      cin>>a;
      cout<<"\nget first denominator";
      cin>>b;
      fract.set(a,b);
      cout<<"\nget second numerator";
      cin>>c;
      cout<<"\nget second denominator";
      cin>>d;
      fract.set(c,d);
      fract.multi(a,b,c,d);
      fract.set(fract.num,fract.den);
      cout<<fract.getnum()<<"/"<<fract.getden();
      
      break;

      case 2:
      cout<<"\nget first numerator";
      cin>>a;
      cout<<"\nget first denominator";
      cin>>b;
      fract.set(a,b);
      cout<<"\nget second numerator";
      cin>>c;
      cout<<"\nget second denominator";
      cin>>d;
      fract.set(c,d);
      fract.div(a,b,c,d);
      fract.set(fract.num,fract.den);
      cout<<fract.getnum()<<"/"<<fract.getden();

   break;

         case 3:
      cout<<"\nget first numerator";
      cin>>a;
      cout<<"\nget first denominator";
      cin>>b;
      fract.set(a,b);
      cout<<"\nget second numerator";
      cin>>c;
      cout<<"\nget second denominator";
      cin>>d;
      fract.set(c,d);
      fract.add(a,b,c,d);
      fract.set(fract.num,fract.den);
      cout<<fract.getnum()<<"/"<<fract.getden();

   break;

            case 4:
      cout<<"\nget first numerator";
      cin>>a;
      cout<<"\nget first denominator";
      cin>>b;
      fract.set(a,b);
      cout<<"\nget second numerator";
      cin>>c;
      cout<<"\nget second denominator";
      cin>>d;
      fract.set(c,d);
      fract.sub(a,b,c,d);
      fract.set(fract.num,fract.den);
      cout<<fract.getnum()<<"/"<<fract.getden();

   break;
}
   cout<<"\nenter any number to conti or 0 to quit";
   cin>>o;
   if(o==0)
      break;
}
   return 0;
}

void fraction::normalize(){
   if(den==0||num==0){
      num=0;
      den=1;
   }

   if (den<0){
      num*=-1;
      den*=-1;
   }
   int n=gcf(num,den);
   num/=n;
   den/=n;
}

int fraction::gcf(int a,int b){
   if (a%b==0)
      return abs(b);
   else
      return gcf(b,a%b);
}
int fraction::lcm(int a,int b){
   return (a/gcf(a,b))*b;
}
int fraction::div(int a,int b,int c,int d){
   num=a*d;
   den=b*c;
   return(num,den);
}
int fraction::multi(int a,int b,int c,int d){
   num=a*c;
   den=b*d;
   return(num,den);
}
int fraction::add(int a,int b,int c,int d){
   den=lcm(b,d);
   num=(a*den/b)+(c*den/d);
   return (num,den);
}
int fraction::sub(int a,int b,int c,int d){
   den=lcm(b,d);
   num=(a*den/b)-(c*den/d);
   return (num,den);
}


Last edited by fatal rival on Sun Jul 08, 2007 7:51 pm; edited 2 times in total
Back to top
View user's profile Send private message
appalsap
Moderator
Reputation: 0

Joined: 27 Apr 2006
Posts: 6753
Location: Pakistan

PostPosted: Sun Jul 08, 2007 6:54 pm    Post subject: Reply with quote

wrap your code in code tags [code] like so [/code], this preserves the spacing and makes it easier to read
Back to top
View user's profile Send private message
Ouch!
How do I cheat?
Reputation: 0

Joined: 09 Jan 2007
Posts: 0

PostPosted: Sun Jul 08, 2007 8:18 pm    Post subject: Reply with quote

Code:

int getnum() {return num;}
int getden() {return den;}


there is no point in adding these functions, as 'num' and 'den' are both public.

you could use
Code:
cout<<fract.num<<"/"<<fract.den;


intstead of
Code:
cout<<fract.getnum()<<"/"<<fract.getden();

_________________
Haha, You Actually Bothered To Read This!

____________
Back to top
View user's profile Send private message
appalsap
Moderator
Reputation: 0

Joined: 27 Apr 2006
Posts: 6753
Location: Pakistan

PostPosted: Sun Jul 08, 2007 8:23 pm    Post subject: Reply with quote

Good example of what happens when someone gets taught C++ before they're taught C so they think the examples given for object orientation are real usable techniques.
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