Object Oriented Programming By Mam Sidra Malik
Rating:
08-09-2008, 10:39 PM
Addicted to Computer
Join Date: Sep 2007
Location: ------------
Age: 35
Posts: 1,309
Contact Number: ---------------
Program / Discipline: BSIT
Class Roll Number: 07-01
Fraction Calculator with 4 functions in c++ [ex12 of ch#5] Robert Lafore Fraction Calculator with 4 functions in c++ [ex12 of ch#5] It adds only fractions like 2/3+4/3 (requirement is question).
Program ki Samajh aaye tu mujhe bhi samjha daina!
Ab tu mujhe khud ko hi samjh nai aa rahi
Exectable is also attached for convenience .
Code:
#include <iostream>
using namespace std;
struct fraction
{
int numinator,denominator;
};
fraction functadd(fraction a,fraction b)
{
fraction ans;
ans.numinator=(a.numinator*b.denominator)+(b.numinator*a.denominator);
ans.denominator=a.denominator*b.denominator;
return ans;
}
fraction functsubtr(fraction a,fraction b)
{
fraction ans;
ans.numinator=(a.numinator*b.denominator)-(b.numinator*a.denominator);
ans.denominator=a.denominator*b.denominator;
return ans;
}
fraction functmulti(fraction a,fraction b)
{
fraction ans;
ans.numinator=a.numinator*b.numinator;
ans.denominator=a.denominator*b.denominator;
return ans;
}
fraction functdivide(fraction a,fraction b)
{
fraction ans;
ans.numinator=a.numinator*b.denominator;
ans.denominator=a.denominator*b.numinator;
return ans;
}
void main()
{
fraction number[2];
char c;
char oprtr;
fraction result;
for(int n=1;n!=0;)
{
int showans=1;
cout<<"\nEnter Values in this form 2/3+4/3 :";
cin>>number[0].numinator>>c>>number[0].denominator>>oprtr>>number[1].numinator>>c>>number[1].denominator;
if(oprtr=='+')
{
result=functadd(number[0],number[1]);
n=0;
}
else if(oprtr=='-')
{
result=functsubtr(number[0],number[1]);
n=0;
}
else if(oprtr=='*')
{
result=functmulti(number[0],number[1]);
n=0;
}
else if(oprtr=='/')
{
result=functdivide(number[0],number[1]);
n=0;
}
else
{
cout<<"You have used an invalid Operator, Please use any one of following operators\n+ , - , *, /\n\n";
n=1;
showans=0;
}
if (showans==1)
{
cout<<"Answer = "<<result.numinator<<"/"<<result.denominator;
}
cout<<"\n\nContinue? y/n";
char var;
cin>>var;
if(var=='y')
n=1;
else if(var=='n')
{
n=0;
}
}
}
09-09-2008, 06:06 AM
Join Date: Sep 2007
Location: near Govt College of Science Multan Pakistan
Posts: 9,693
Contact Number: Removed
Program / Discipline: BSIT
Class Roll Number: 07-15
Re: Fraction Calculator with 4 functions in c++ [ex12 of ch#5]
Thank you bhai...
Achha hay ye
10-09-2008, 12:13 AM
Grown up punk
Join Date: Oct 2007
Location: Muslim Town Multan
Posts: 195
Contact Number: 0322-6196713
Program / Discipline: BSIT
Class Roll Number: 07-22
Re: Fraction Calculator with 4 functions in c++ [ex12 of ch#5]
yar is main to - * or / waly ni han pleas tell me understand me
10-09-2008, 12:20 AM
Addicted to Computer
Join Date: Sep 2007
Location: ------------
Age: 35
Posts: 1,309
Contact Number: ---------------
Program / Discipline: BSIT
Class Roll Number: 07-01
Re: Fraction Calculator with 4 functions in c++ [ex12 of ch#5]
Sab kucch hay yar,
There are separate functions for subtraction, multiplication & division ...
We've just called that functions, and used if statement to recognize that to which function we've to call...
We can also use switches instead of if statement ..
18-09-2008, 04:59 AM
Addicted to Computer
Join Date: Sep 2007
Location: ------------
Age: 35
Posts: 1,309
Contact Number: ---------------
Program / Discipline: BSIT
Class Roll Number: 07-01
Re: Fraction Calculator with 4 functions in c++ [ex12 of ch#5] Robert Lafore Fraction Calculator with Proper LCM Function Code:
#include <iostream>
using namespace std;
struct fraction
{
int numerator,denominator;
};
fraction functadd(fraction a,fraction b);
fraction functsubtr(fraction a,fraction b);
fraction functmulti(fraction a,fraction b);
fraction functdivide(fraction a,fraction b);
int lcm(int val1, int val2);
void main()
{
fraction number[2];
char c;
char oprtr;
fraction result;
for(int n=1;n!=0;)
{
int showans=1;
cout<<"\nEnter Values in this form 2/3+4/3 :";
cin>>number[0].numerator>>c>>number[0].denominator>>oprtr>>number[1].numerator>>c>>number[1].denominator;
switch (oprtr)
{
case '+':
result=functadd(number[0],number[1]);
n=0;
break;
case '-':
result=functsubtr(number[0],number[1]);
n=0;
break;
case '*':
result=functmulti(number[0],number[1]);
n=0;
break;
case '/':
result=functdivide(number[0],number[1]);
n=0;
break;
default:
cout<<"You have used an invalid Operator, Please use any one of following operators\n+ , - , *, /\n\n";
n=1;
showans=0;
break;
}
if (showans==1)
{
cout<<"Answer = "<<result.numerator<<"/"<<result.denominator;
}
cout<<"\n\nContinue? y/n : ";
char var;
cin>>var;
if(var=='y')
n=1;
else if(var=='n')
{
n=0;
}
}
}
/*-----------------------------------------------------------------
Functions are Given Below
------------------------------------------------------------------*/
fraction functadd(fraction a,fraction b)
{
fraction ans;
ans.denominator=lcm(a.denominator,b.denominator);
ans.numerator=((ans.denominator/a.denominator)*a.numerator)+(ans.denominator/b.denominator)*b.numerator;
return ans;
}
fraction functsubtr(fraction a,fraction b)
{
fraction ans;
ans.denominator=lcm(a.denominator,b.denominator);
ans.numerator=((ans.denominator/a.denominator)*a.numerator)-(ans.denominator/b.denominator)*b.numerator;
return ans;
}
fraction functmulti(fraction a,fraction b)
{
fraction ans;
ans.numerator=a.numerator*b.numerator;
ans.denominator=a.denominator*b.denominator;
return ans;
}
fraction functdivide(fraction a,fraction b)
{
fraction ans;
ans.numerator=a.numerator*b.denominator;
ans.denominator=a.denominator*b.numerator;
return ans;
}
/*-----------------------------------
Function for LCM Starts Here
-------------------------------------*/
int lcm(int val1,int val2)
{
int lcm,check,big;
check=65536; //Any Bigger Value greater than big
if(val1>val2)
big=val1;
else if(val1<val2)
big=val2;
else
big=val1;
for(;big<=check;big++)
{
if(big%val1==0 && big%val2==0)
{
check=0;
lcm=big;
}
}
return lcm;
}
/*-----------------------------------
Function for LCM Ends Here
-------------------------------------*/
07-10-2011, 06:17 PM
Little Baby
Join Date: Sep 2011
Location: Karachi
Posts: 3
Program / Discipline: BSCS
Class Roll Number: 07-20
Re: Fraction Calculator with 4 functions in c++ [ex12 of ch#5] Robert Lafore
ahem...koi isko classes use krtay hwe bna skta hai??
07-10-2011, 07:09 PM
Addicted to Computer
Join Date: Sep 2007
Location: ------------
Age: 35
Posts: 1,309
Contact Number: ---------------
Program / Discipline: BSIT
Class Roll Number: 07-01
Re: Fraction Calculator with 4 functions in c++ [ex12 of ch#5] Robert Lafore
well, just create a new class . something like this...
Code:
class calculator
{
private:
public:
} then move all functions to the public part of class ... then in main part create new object from class like this
then where all functions are called in the main part of program . call them using
Code:
mycall.myfunction(); i think this should work...
08-10-2011, 10:02 AM
Little Baby
Join Date: Sep 2011
Location: Karachi
Posts: 3
Program / Discipline: BSCS
Class Roll Number: 07-20
Re: Fraction Calculator with 4 functions in c++ [ex12 of ch#5] Robert Lafore Quote:
Originally Posted by
BSIT07-01 well, just create a new class . something like this...
Code:
class calculator
{
private:
public:
} then move all functions to the public part of class ... then in main part create new object from class like this
then where all functions are called in the main part of program . call them using
Code:
mycall.myfunction(); i think this should work...
i we have o add constructors, then how should we do it??
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Thread Tools Search this Thread Display Modes Rate This Thread Linear Mode
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
Best view in Firefox Almuslimeen.info | BZU Multan | Dedicated server hosting Note: All trademarks and copyrights held by respective owners. We will take action against any copyright violation if it is proved to us. All times are GMT +5. The time now is 05:30 AM .
Powered by vBulletin® Version 3.8.2 Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.