Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   classFraction used in a program that generates fraction exercises in c++ (http://www.go4expert.com/forums/classfraction-program-generates-t25791/)

fmmctg01 14May2011 22:43

classFraction used in a program that generates fraction exercises in c++
 
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()).

xpi0t0s 17May2011 11:47

Re: classFraction used in a program that generates fraction exercises in c++
 
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?

fmmctg01 22May2011 00:06

Re: classFraction used in a program that generates fraction exercises in c++
 
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();
 }
 }


xpi0t0s 22May2011 02:05

Re: classFraction used in a program that generates fraction exercises in c++
 
I see you still haven't bothered reading the posting guidelines. They're there for a reason. Now READ THEM.


All times are GMT +5.5. The time now is 08:32.