Design a class (ADT) that can represent for any numeric value. Write a complete
ID: 3688347 • Letter: D
Question
Design a class (ADT) that can represent for any numeric value. Write a complete C++ program to demonstrate for how to use the ADT. Validate your input data, and don’t let your program be crashed at the run time by any reason. My problem is that my result is correct but the teacher told me that he does not want me to do it this way. he wants operator overloading and another code for this program. the program is that you enter two number and it gives out the arithmatic operations. The following is the code I had which is not the way it should be done:
#include<iostream>
#include<iomanip>
using namespace std;
class arithmatic //member function declerations
{
public:
inline double add(double a, double b) //member function for addition
{
return (a + b);
}
inline double sub(double a, double b) //member function for subtraction
{
return (a - b);
}
inline double multip(double a, double b) //member function for multiplication
{
return (a*b);
}
inline double div(double a, double b) //member function for division
{
return (a / b);
}
};
int main() //starting function
{
int ch; //variable declarations
double x, y, t;
arithmatic obj;
do //we create a menue driven so the user can decide which arithmetic operation they want to do
{
cout << " MENU ";
cout << "1.Addition 2.Subtraction ";
cout << "3.Multiplication 4.Division 5.Exit ";
cout << " Enter your choice : ";
while (!(cin >> ch) || ch<1 || ch>5) //does not allow th user to choose anything than a integer from 1 to 5
{
cout << " Invalid input." << endl;
cout << " Re-enter choice or press 5 to exit : ";
fflush(stdin);
cin.clear();
}
switch (ch)
{
case 1: //if the user chooses to add the two numbers
cout << " Enter first number : ";
while (!(cin >> x))
{
cout << " Invalid input." << endl;
cout << " Re-enter first number: ";
fflush(stdin);
cin.clear();
}
cout << " Enter second number : ";
while (!(cin >> y))
{
cout << " Invalid input." << endl;
cout << " Re-enter second number: ";
fflush(stdin);
cin.clear();
}
t = obj.add(x, y);
cout << "Addition " << setprecision(2) << fixed << x << " + " << setprecision(2) << fixed << y << " = " << setprecision(2) << fixed << t << endl;
break;
case 2: //if the user chooses to subtract the two numbers
cout << " Enter first number : ";
while (!(cin >> x))
{
cout << " Invalid input." << endl;
cout << " Re-enter first number: ";
fflush(stdin);
cin.clear();
}
cout << " Enter second number : ";
while (!(cin >> y))
{
cout << " Invalid input." << endl;
cout << " Re-enter second number: ";
fflush(stdin);
cin.clear();
}
t = obj.sub(x, y);
cout << "Subtractin " << setprecision(2) << fixed << x << " - " << setprecision(2) << fixed << y << " = " << setprecision(2) << fixed << t << endl;
break;
case 3: //if the user chooses to multiply the two numbers
cout << " Enter first number : ";
while (!(cin >> x))
{
cout << " Invalid input." << endl;
cout << " Re-enter first number: ";
fflush(stdin);
cin.clear();
}
cout << " Enter second number : ";
while (!(cin >> y))
{
cout << " Invalid input." << endl;
cout << " Re-enter second number: ";
fflush(stdin);
cin.clear();
}
t = obj.multip(x, y);
cout << " Multiplication " << setprecision(2) << fixed << x << " * " << setprecision(2) << fixed << y << " = " << setprecision(2) << fixed << t << endl;
break;
case 4: //if the user chooses to divide the two numbers
cout << " Enter first number : ";
while (!(cin >> x))
{
cout << " Invalid input." << endl;
cout << " Re-enter first number: ";
fflush(stdin);
cin.clear();
}
cout << " Enter second number : ";
while (!(cin >> y))
{
cout << " Invalid input." << endl;
cout << " Re-enter second number: ";
fflush(stdin);
cin.clear();
}
t = obj.div(x, y);
cout << "Division" << setprecision(2) << fixed << x << " / " << setprecision(2) << fixed << y << " = " << setprecision(2) << fixed << t << endl;
break;
case 5:
break;
}
} while (ch != 5);
}
Explanation / Answer
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
class complex
{
double a;
public:
double operator+(double a2)
{
a=a+a2;
return a;
}
double operator-( double a2)
{
a=a-a2;
return a;
}
double operator*( double a2)
{
a=a*a2;
return a;
}
double operator/( double a2)
{
a=a/a2;
return a;
}
};
int main() //starting function
{
int ch; //variable declarations
double x,y,t;
do //we create a menu driven so the user can decide which arithmetic operation they want to do
{
cout << " MENU ";
cout << "1.Addition 2.Subtraction ";
cout << "3.Multiplication 4.Division 5.Exit ";
cout << " Enter your choice : ";
while (!(cin >> ch) || ch<1 || ch>5) //does not allow th user to choose anything than a integer from 1 to 5
{
cout << " Invalid input." << endl;
cout << " Re-enter choice or press 5 to exit : ";
fflush(stdin);
cin.clear();
}
switch (ch)
{
case 1: //if the user chooses to add the two numbers
cout << " Enter first number : ";
while (!(cin >> x))
{
cout << " Invalid input." << endl;
cout << " Re-enter first number: ";
fflush(stdin);
cin.clear();
}
cout << " Enter second number : ";
while (!(cin >> y))
{
cout << " Invalid input." << endl;
cout << " Re-enter second number: ";
fflush(stdin);
cin.clear();
}
z=x+y;
cout << "Addition " << setprecision(2) << fixed << x << " + " << setprecision(2) << fixed << y << " = " << setprecision(2) << fixed << t << endl;
break;
case 2: //if the user chooses to subtract the two numbers
cout << " Enter first number : ";
while (!(cin >> x))
{
cout << " Invalid input." << endl;
cout << " Re-enter first number: ";
fflush(stdin);
cin.clear();
}
cout << " Enter second number : ";
while (!(cin >> y))
{
cout << " Invalid input." << endl;
cout << " Re-enter second number: ";
fflush(stdin);
cin.clear();
}
t = x-y;
cout << "Subtractin " << setprecision(2) << fixed << x << " - " << setprecision(2) << fixed << y << " = " << setprecision(2) << fixed << t << endl;
break;
case 3: //if the user chooses to multiply the two numbers
cout << " Enter first number : ";
while (!(cin >> x))
{
cout << " Invalid input." << endl;
cout << " Re-enter first number: ";
fflush(stdin);
cin.clear();
}
cout << " Enter second number : ";
while (!(cin >> y))
{
cout << " Invalid input." << endl;
cout << " Re-enter second number: ";
fflush(stdin);
cin.clear();
}
t = x*y;
cout << " Multiplication " << setprecision(2) << fixed << x << " * " << setprecision(2) << fixed << y << " = " << setprecision(2) << fixed << t << endl;
break;
case 4: //if the user chooses to divide the two numbers
cout << " Enter first number : ";
while (!(cin >> x))
{
cout << " Invalid input." << endl;
cout << " Re-enter first number: ";
fflush(stdin);
cin.clear();
}
cout << " Enter second number : ";
while (!(cin >> y))
{
cout << " Invalid input." << endl;
cout << " Re-enter second number: ";
fflush(stdin);
cin.clear();
}
t = x/y;
cout << "Division" << setprecision(2) << fixed << x << " / " << setprecision(2) << fixed << y << " = " << setprecision(2) << fixed << t << endl;
break;
case 5:
break;
}
} while (ch != 5);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.