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


 
 
Thread Tools Search this Thread Rating: Thread Rating: 2 votes, 5.00 average. Display Modes
Prev Previous Post   Next Post Next
  #5  
Old 18-09-2008, 04:59 AM
BSIT07-01's Avatar
Addicted to Computer


 
Join Date: Sep 2007
Location: ------------
Age: 36
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
 

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 10:19 PM.
Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2026, Jelsoft Enterprises Ltd.