1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

classFraction used in a program that generates fraction exercises in c++

Discussion in 'C++' started by fmmctg01, May 14, 2011.

  1. fmmctg01

    fmmctg01 New Member

    Joined:
    Mar 31, 2011
    Messages:
    11
    Likes Received:
    0
    Trophy Points:
    0
    1. Save classFraction in a file called classFraction.cpp.
    2. Write the main function in a file called fractionExercises.cpp. You access class Fraction through the statement: #include “classFraction.cpp”. The main function consists of a do while loop that displays the following menu:
    *************************************
    What type of exercise would you like to see?
    +, -, * , or /
    *************************************
    The user enters their choice (+, -, * , or /) into a variable named choice. If choice is ‘*’, then a function called multFraction() is called. This function is not part of class Fraction. It is found in the same file as the main function.
    Function multFraction() generates a random multiplication exercise. Use: srand(time(0)), and rand()%15+1, to generate random integers between 1 and 15. Functions srand and rand are found in stdlib.
    Ex: f1.set(rand()%15+1, rand()%15+1);
    f2.set(rand()%15+1, rand()%15+1);
    f1.display();
    cout<<’*’;
    f2.display();
    cout<<’=’;
    //The above code displays a random multiplication example
    Next multFraction() prompts the user for the numerator and denominator of the answer. The function compares the user’s answer to the correct answer generated by a statement such as: corrAns = f1*f2; //where corrAns is of type Fraction.
    OVER
    The function tells the user if the answer is correct or not correct. Keep track of how
    many questions the user has answered and how many they have answered correctly.
    Display this information after the answer to each exercise is entered. Control is passed to
    the main menu.
    3. Finish class Fraction. It should contain the following member functions (methods):
    two constructors
    operator+,-,* and /
    reduce()
    operator==
    display()
    set()
    (You could add more methods as you need them.)
    When classFraction is complete, then you can write functions addFraction(),
    subFraction(), divFraction() (in addition to multFraction()).
     
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    How far have you got and where are you stuck? Note that we don't do homework here, but we can advise if you're stuck. Oh, and BTW please read the posting guideliines. The fact that you haven't used code tags shows you haven't, which we could interpreted as you can't be arsed about our time, so the question is why should we be arsed about helping you?
     
  3. fmmctg01

    fmmctg01 New Member

    Joined:
    Mar 31, 2011
    Messages:
    11
    Likes Received:
    0
    Trophy Points:
    0
    Code:
    #include <iostream>
     #include <cstdlib>
     #include <ctime>
     #include <conio.h>
     #include "classFraction.cpp"
     using namespace std;
     void multFraction();
     void addFraction();
     void subFraction();
     void divFraction();
     int correct=0,wrong=0;
     int main()
     {
     char choice;
     int numOfQuestions=0;
     do
     {
     cout<<"What type of exercise would you like to see?"<<endl;
     cout<<"*************************************"<<endl;
     cout<<"What type of exercise would you like to see?"<<endl;
     cout<<"+, -, * , or /"<<endl<<endl;
     cout<<"*************************************"<<endl;
     cin>>choice;
     switch(choice)
     {
     case '*':
     multFraction();
     numOfQuestions++; break;
     case '+':
     addFraction();
     numOfQuestions++; break;
     case '-':
     subFraction();
     numOfQuestions++; break;
     case '/':
     divFraction();
     numOfQuestions++; break;
     default:cout<<"Invalid choice";
     }
     cout<<"\n\nTotal Number of Questions asked: "<<numOfQuestions<<endl;
     cout<<"Corect Answers: "<<correct<<endl;
     cout<<"Wrong Answers: "<<wrong<<endl;
     cout<<"\n Do you want to continue? (Y or N): ";
     cin>>choice;
     }while(toupper(choice)=='Y');
     return 0;
     }
     void multFraction()
     srand(time(0));
     Fraction f1,f2,f3;//getting error
     f1.set(rand()%15+1,rand()%15+1);
     f2.set(rand()%15+1,rand()%15+1);
     f1.display();
     cout<<" * ";
     f2.display();
     cout<<" = "<<endl;
     int n,d;
     cout<<"Enter your answer: ";
     cin>>n;
     getchar();
     cin>>d;
     f3.set(n,d);
     Fraction corrAns=f1*f2;
     if(f3==corrAns)
     {
     correct++;
     cout<<"Your answer is correct"<<endl;
     }
     else
     {
     wrong++;
     cout<<"Your answer is not correct"<<endl;
     cout<<"correct answer is: ";
     corrAns.reduce();
     corrAns.display();
     }
     }
     void addFraction()
     {
     srand(time(0));
     Fraction f1,f2,f3;
     f1.set(rand()%15+1,rand()%15+1);
     f2.set(rand()%15+1,rand()%15+1);
     f1.display();
     cout<<" + ";
     f2.display();
     cout<<" = "<<endl;
     int n,d;
     cout<<"Enter your answer: ";
     cin>>n;
     getchar();
     cin>>d;
     f3.set(n,d);
     Fraction corrAns=f1+f2;
     if(f3==corrAns)
     {
     correct++;
     cout<<"Your answer is correct"<<endl;
     }
     else
     {
     wrong++;
     cout<<"Your answer is not correct"<<endl;
     cout<<"correct answer is: ";
     corrAns.reduce();
     corrAns.display();
     }
     }
     void subFraction()
     {
     srand(time(0));
     Fraction f1,f2,f3;
     f1.set(rand()%15+1,rand()%15+1);
     f2.set(rand()%15+1,rand()%15+1);
     f1.display();
     cout<<" - ";
     f2.display();
     cout<<" = "<<endl;
     int n,d;
     cout<<"Enter your answer: ";
     cin>>n;
     getchar();
     cin>>d;
     f3.set(n,d);
     Fraction corrAns=f1-f2;
     if(f3==corrAns)
     {
     correct++;
     cout<<"Your answer is correct"<<endl;
     }
     else
     {
     wrong++;
     cout<<"Your answer is not correct"<<endl;
     cout<<"correct answer is: ";
     corrAns.reduce();
     corrAns.display();
     }
     }
     void divFraction()
     {
     srand(time(0));
     Fraction f1,f2,f3;
     f1.set(rand()%15+1,rand()%15+1);
     f2.set(rand()%15+1,rand()%15+1);
     f1.display();
     cout<<" / ";
     f2.display();
     cout<<" = "<<endl;
     int n,d;
     cout<<"Enter your answer: ";
     cin>>n;
     getchar();
     cin>>d;
     f3.set(n,d);
     Fraction corrAns=f1/f2;
     if(f3==corrAns)
     {
     correct++;
     cout<<"Your answer is correct"<<endl;
     }
     else
     {
     wrong++;
     cout<<"Your answer is not correct"<<endl;
     cout<<"correct answer is: ";
     corrAns.reduce();
     corrAns.display();
     }
     }
     
    Last edited by a moderator: May 22, 2011
  4. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    I see you still haven't bothered reading the posting guidelines. They're there for a reason. Now READ THEM.
     

Share This Page