HOME BZU Mail Box Online Games Radio and TV Live Cricket Score All Albums
Go Back   BZU PAGES: Find Presentations, Reports, Student's Assignments and Daily Discussion; Bahauddin Zakariya University Multan Institute of Computing Bachelor of Science in Information Technology BsIT 3rd Semester Object Oriented Programming

Object Oriented Programming By Mam Sidra Malik

Reply
 
Thread Tools Search this Thread Rating: Thread Rating: 2 votes, 5.00 average. Display Modes
  #1  
Old 08-09-2008, 11:39 PM
BSIT07-01's Avatar
Addicted to Computer


 

Join Date: Sep 2007
Location: ------------
Age: 22
Posts: 1,342
Thanks: 598
Thanked 363 Times in 292 Posts
Contact Number: ---------------
Program / Discipline: BSIT
Class Roll Number: 07-01
BSIT07-01 has a brilliant futureBSIT07-01 has a brilliant futureBSIT07-01 has a brilliant futureBSIT07-01 has a brilliant futureBSIT07-01 has a brilliant futureBSIT07-01 has a brilliant futureBSIT07-01 has a brilliant futureBSIT07-01 has a brilliant futureBSIT07-01 has a brilliant futureBSIT07-01 has a brilliant futureBSIT07-01 has a brilliant future
Time Spent Online: 1 Month 3 Days 9 Hours 55 Minutes 31 Seconds
Default 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 .

#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; } } }
Attached Files
File Type: exe Mera Calculator.exe (512.1 KB, 228 views)
Reply With Quote
The Following User Says Thank You to BSIT07-01 For This Useful Post:
.BZU. (09-09-2008)
  #2  
Old 09-09-2008, 07:06 AM
.BZU.'s Avatar
Your Friendly Admin
Innocent Baby
BSIT Professional


 

Join Date: Sep 2007
Location: near Govt College of Science Multan Pakistan
Posts: 9,684
Thanks: 2,338
Thanked 2,223 Times in 1,816 Posts
Contact Number: Removed
Program / Discipline: BSIT
Class Roll Number: 07-15
.BZU. has a reputation beyond repute.BZU. has a reputation beyond repute.BZU. has a reputation beyond repute.BZU. has a reputation beyond repute.BZU. has a reputation beyond repute.BZU. has a reputation beyond repute.BZU. has a reputation beyond repute.BZU. has a reputation beyond repute.BZU. has a reputation beyond repute.BZU. has a reputation beyond repute.BZU. has a reputation beyond repute
Time Spent Online: 3 Months 3 Weeks 5 Days 4 Hours 12 Minutes 13 Seconds
Default Re: Fraction Calculator with 4 functions in c++ [ex12 of ch#5]


Thank you bhai...
Achha hay ye
__________________
(¯`v´¯)
`*.¸.*`

¸.*´¸.*´¨) ¸.*´¨)
(¸.*´ (¸.
Bzu Forum

Don't cry because it's over, smile because it happened
Reply With Quote
  #3  
Old 10-09-2008, 01:13 AM
shmsa's Avatar
Grown up punk

 

Join Date: Oct 2007
Location: Muslim Town Multan
Posts: 196
Thanks: 20
Thanked 47 Times in 34 Posts
Contact Number: 0322-6196713
Program / Discipline: BSIT
Class Roll Number: 07-22
shmsa is a jewel in the roughshmsa is a jewel in the roughshmsa is a jewel in the roughshmsa is a jewel in the rough
Time Spent Online: 2 Days 16 Hours 54 Minutes 5 Seconds
Default 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
Reply With Quote
  #4  
Old 10-09-2008, 01:20 AM
BSIT07-01's Avatar
Addicted to Computer


 

Join Date: Sep 2007
Location: ------------
Age: 22
Posts: 1,342
Thanks: 598
Thanked 363 Times in 292 Posts
Contact Number: ---------------
Program / Discipline: BSIT
Class Roll Number: 07-01
BSIT07-01 has a brilliant futureBSIT07-01 has a brilliant futureBSIT07-01 has a brilliant futureBSIT07-01 has a brilliant futureBSIT07-01 has a brilliant futureBSIT07-01 has a brilliant futureBSIT07-01 has a brilliant futureBSIT07-01 has a brilliant futureBSIT07-01 has a brilliant futureBSIT07-01 has a brilliant futureBSIT07-01 has a brilliant future
Time Spent Online: 1 Month 3 Days 9 Hours 55 Minutes 31 Seconds
Default 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 ..
Reply With Quote
  #5  
Old 18-09-2008, 05:59 AM
BSIT07-01's Avatar
Addicted to Computer


 

Join Date: Sep 2007
Location: ------------
Age: 22
Posts: 1,342
Thanks: 598
Thanked 363 Times in 292 Posts
Contact Number: ---------------
Program / Discipline: BSIT
Class Roll Number: 07-01
BSIT07-01 has a brilliant futureBSIT07-01 has a brilliant futureBSIT07-01 has a brilliant futureBSIT07-01 has a brilliant futureBSIT07-01 has a brilliant futureBSIT07-01 has a brilliant futureBSIT07-01 has a brilliant futureBSIT07-01 has a brilliant futureBSIT07-01 has a brilliant futureBSIT07-01 has a brilliant futureBSIT07-01 has a brilliant future
Time Spent Online: 1 Month 3 Days 9 Hours 55 Minutes 31 Seconds
Default Re: Fraction Calculator with 4 functions in c++ [ex12 of ch#5] Robert Lafore


Fraction Calculator with Proper LCM Function
#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 -------------------------------------*/
Reply With Quote
  #6  
Old 07-10-2011, 06:17 PM
Dergk's Avatar
Little Baby

 

Join Date: Sep 2011
Location: Karachi
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Program / Discipline: BSCS
Class Roll Number: 07-20
Dergk is on a distinguished road
Time Spent Online: 1 Minute 57 Seconds
Default Re: Fraction Calculator with 4 functions in c++ [ex12 of ch#5] Robert Lafore


ahem...koi isko classes use krtay hwe bna skta hai??
Reply With Quote
  #7  
Old 07-10-2011, 07:09 PM
BSIT07-01's Avatar
Addicted to Computer


 

Join Date: Sep 2007
Location: ------------
Age: 22
Posts: 1,342
Thanks: 598
Thanked 363 Times in 292 Posts
Contact Number: ---------------
Program / Discipline: BSIT
Class Roll Number: 07-01
BSIT07-01 has a brilliant futureBSIT07-01 has a brilliant futureBSIT07-01 has a brilliant futureBSIT07-01 has a brilliant futureBSIT07-01 has a brilliant futureBSIT07-01 has a brilliant futureBSIT07-01 has a brilliant futureBSIT07-01 has a brilliant futureBSIT07-01 has a brilliant futureBSIT07-01 has a brilliant futureBSIT07-01 has a brilliant future
Time Spent Online: 1 Month 3 Days 9 Hours 55 Minutes 31 Seconds
Default Re: Fraction Calculator with 4 functions in c++ [ex12 of ch#5] Robert Lafore


well, just create a new class . something like this...
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
calculator mycal;
then where all functions are called in the main part of program . call them using
mycall.myfunction();
i think this should work...
Reply With Quote
The Following User Says Thank You to BSIT07-01 For This Useful Post:
.BZU. (07-10-2011)
  #8  
Old 08-10-2011, 10:02 AM
Dergk's Avatar
Little Baby

 

Join Date: Sep 2011
Location: Karachi
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Program / Discipline: BSCS
Class Roll Number: 07-20
Dergk is on a distinguished road
Time Spent Online: 1 Minute 57 Seconds
Default Re: Fraction Calculator with 4 functions in c++ [ex12 of ch#5] Robert Lafore


Quote:
BSIT07-01
View Post
well, just create a new class . something like this...
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
calculator mycal;
then where all functions are called in the main part of program . call them using
mycall.myfunction();
i think this should work...

i we have o add constructors, then how should we do it??
Reply With Quote
Reply

Tags
c++, calculator, ch#5, ex12, fraction, functions, lafore, lectures, robert


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
[Assignment] Solution Q no 1 ch#9 Object Oriented Programming in c++ by Robert Lafore BSIT07-01 Object Oriented Programming 1 15-03-2011 08:09 PM
Age, Calculator and your Character. Raheel ETC 0 26-01-2010 06:35 PM
SQL Plus Functions in Oracle (single row functions) .BZU. Introduction to Database 0 12-03-2009 04:32 AM
[Assignment] Q no 5 ch#9 Object Oriented Programming in c++ by Robert Lafore BSIT07-01 Object Oriented Programming 0 13-11-2008 01:02 AM
Solution of Ex 5,6,2 & 4 Ch# 5 Robert Lafore OOP c++ BSIT07-01 Object Oriented Programming 3 17-09-2008 08:32 PM


All times are GMT +5. The time now is 01:10 PM.

Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.