BZU PAGES: Find Presentations, Reports, Student's Assignments and Daily Discussion; Bahauddin Zakariya University Multan Right Header

HOME BZU Mail Box Online Games Radio and TV Cricket 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, 10:39 PM
BSIT07-01's Avatar
Addicted to Computer


 
Join Date: Sep 2007
Location: ------------
Age: 35
Posts: 1,309
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
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 .

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

Attached Files
File Type: exe Mera Calculator.exe (512.1 KB, 521 views)
Reply With Quote
  #2  
Old 09-09-2008, 06:06 AM
.BZU.'s Avatar


 
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
.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
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, 12:13 AM
shmsa's Avatar
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
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
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, 12:20 AM
BSIT07-01's Avatar
Addicted to Computer


 
Join Date: Sep 2007
Location: ------------
Age: 35
Posts: 1,309
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
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, 04:59 AM
BSIT07-01's Avatar
Addicted to Computer


 
Join Date: Sep 2007
Location: ------------
Age: 35
Posts: 1,309
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
Default 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
-------------------------------------*/

Reply With Quote
  #6  
Old 07-10-2011, 06:17 PM
Dergk's Avatar
Little Baby

 
Join Date: Sep 2011
Location: Karachi
Posts: 3
Program / Discipline: BSCS
Class Roll Number: 07-20
Dergk is on a distinguished road
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: 35
Posts: 1,309
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
Default 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
Code:
calculator mycal;
then where all functions are called in the main part of program . call them using
Code:
mycall.myfunction();
i think this should work...
Reply With Quote
  #8  
Old 08-10-2011, 10:02 AM
Dergk's Avatar
Little Baby

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

Quote:
Originally Posted by BSIT07-01 View Post
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
Code:
calculator mycal;
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??
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
how to make four fraction calculator in c++ using classes and operator overloading Dergk Object Oriented Programming 0 07-10-2011 06:25 PM
[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
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 07:32 PM

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.