|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| ||||||||||||||||||||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Hey guys I'm haveing trouble with a c++ program I'm trying to creat it has to deal with fractions and a menu to choose to add subtract multiply and dived or just to simply exit. Also i need to simply the fractions to the lowest form. It compiles but doesnt simplify or actually choose the right number you input where the menu part is. I'll place a copy of it on the bottom of this if you can help it would be very appreciated.
Thank you. #include <iostream> #include <conio.h> #include <iomanip> using namespace std; class fracType { private: int numerator, denominator, n1, n2, d1, d2; void simplify (); public: fracType (int = 2, int = 3); ~fracType(){cout << "\nDestructor is Running";} void add (const fracType&, const fracType&); void subtract (const fracType&, const fracType&); void multiply (const fracType&, const fracType&); void divide (const fracType&, const fracType&); void showFrac () const ; void setFrac () ; }; fracType::fracType(int n, int d) { numerator = n; denominator = d; } void fracType::add(const fracType& frac1, const fracType& frac2) { numerator = (frac1.n1 * frac2.d2 + frac2.n2 * frac1.d1); denominator = (frac1.d1 * frac2.d2); } void fracType::subtract(const fracType & frac1, const fracType & frac2) { numerator = (frac1.n1 * frac2.d2 - frac2.n2 * frac1.d1); denominator = (frac1.d1 * frac2.d2); } void fracType::multiply(const fracType & frac1, const fracType & frac2) { numerator = (frac1.n1 * frac2.n2); denominator = (frac1.d1 * frac2.d2); } void fracType::divide(const fracType & frac1, const fracType & frac2) { numerator = (frac1.n1 * frac2.d2); denominator = (frac1.d1 * frac2.n2); } void fracType::setFrac() { cout << "Enter a Numerator and a Denominator of a Fraction: "; cin >> n1 >> d1; cout << "\n\nEnter another Numerator and a Denominator of a Fraction: "; cin >> n2 >> d2; } void fracType::showFrac() const { cout << "The Option you have choosen has Equaled: " << numerator << "/" << denominator; } int main() { char c; fracType x0, x1, ADD, SUB, MULT, DIV; x1.setFrac(); cout << setw(30) << "Fraction Calculation Menu: \n"; cout << setw(5) << "1 -- ADDITION \n" << setw(5) << "2 -- SUBTRACTION \n" << setw(5) << "3 -- MULTIPLICATION \n" << setw(5) << "4 -- DIVISION \n" << setw(5) << "5 -- EXIT \n" << setw(5) << "Enter a Number of 1-5: "; cin >> c; if(1) { ADD.add(x0,x1); ADD.showFrac(); } else if (2) { SUB.subtract(x0,x1); SUB.showFrac(); } else if (3) { MULT.multiply(x0,x1); MULT.showFrac(); } else if (c = 4) { DIV.divide(x0,x1); DIV.showFrac(); } else if (5) { cout << "Good Bye"; _getch(); } else { x0.showFrac(); } _getch (); return 0; } |
|
#2
|
|||
|
|||
|
Hey everyone thats looking at this.. ive been working on it all weekend and i still cant figure it out.. plz if u can help me id really apprecait it.. please help ty for all thats looking at my forum though..
|
|
#3
|
|||
|
|||
|
yo
you were close, just needed to make things match up.
#include <iostream> #include <conio.h> #include <iomanip> using namespace std; class fracType { private: int numerator, denominator, n, d, fn, fd, wn; void simplify (); public: fracType (int = 0, int = 1); ~fracType(){cout << "\nDestructor is Running";} void add (const fracType&, const fracType&); void subtract (const fracType&, const fracType&); void multiply (const fracType&, const fracType&); void divide (const fracType&, const fracType&); void showFrac () const ; void setFrac () ; }; fracType::fracType(int numerator, int denominator) { fn = numerator; fd = denominator; } void fracType::add(const fracType& frac1, const fracType& frac2) { fn = (frac1.n * frac2.d + frac2.n * frac1.d); fd = (frac1.d * frac2.d); } void fracType::subtract(const fracType & frac1, const fracType & frac2) { fn = (frac1.n * frac2.d - frac2.n * frac1.d); fd = (frac1.d * frac2.d); } void fracType::multiply(const fracType & frac1, const fracType & frac2) { fn = (frac1.n * frac2.n); fd = (frac1.d * frac2.d); } void fracType::divide(const fracType & frac1, const fracType & frac2) { fn = (frac1.n * frac2.d); fd = (frac1.d * frac2.n); } void fracType::setFrac() { char x; cout << "Enter a Numerator and a Denominator of a Fraction: "; cin >> n >> x >> d; } void fracType::showFrac() const { cout << "The Option you have choosen has Equaled: " << fn << "/" << fd; } int main() { int c; fracType x0, x1, ADD, SUB, MULT, DIV; cout << setw(30) << "Fraction Calculation Menu: \n"; cout << setw(5) << "1 -- ADDITION \n" << setw(5) << "2 -- SUBTRACTION \n" << setw(5) << "3 -- MULTIPLICATION \n" << setw(5) << "4 -- DIVISION \n" << setw(5) << "5 -- EXIT \n" << setw(5) << "Enter a Number of 1-5: "; cin >> c; x0.setFrac(); x1.setFrac(); if(c == 1) { ADD.add(x0,x1); ADD.showFrac(); } else if (c == 2) { SUB.subtract(x0,x1); SUB.showFrac(); } else if (c == 3) { MULT.multiply(x0,x1); MULT.showFrac(); } else if (c == 4) { DIV.divide(x0,x1); DIV.showFrac(); } else if (c == 5) { cout << "Good Bye"; _getch(); } else { cout << "Enter valid menu choice: "; cin >> c; } _getch (); return 0; } |
![]() |
| Viewing: Dev Articles Community Forums > Programming > ASP Development > ASP 1.0 - Needing some help with my C++ program. |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|