Based on my code please implement the following. (c++) Really need this tonight.
ID: 652622 • Letter: B
Question
Based on my code please implement the following. (c++) Really need this tonight.
- Use try/catch structure for error handling.
- Define a linked list node class with data element being a Shape pointer which pointers either to a Rectangle or Triangle object. ( implementation should contain functions such as InsertAfter(), Remove(), PrintArea(), Search(), and InsertInOrder(). )
- program should read from files and then build a linked list containing Shape pointers which point either to a Rectangle or Triangle object, depending on sides you read. The linked list is sorted by area of the shape with ascending order. Then, it keeps performing the following operations:
prints out areas of all shapes in the list.
prompts the user to enter an area value to search. Note that we don't do random number but ask user to enter.
If there is a shape with the same area value, the shape element will be removed from the linked list (also from the memory heap). Then, prints out areas of all shapes in the list.
Otherwise, prints out "Not found!"
repeat the above steps until user decides to quit. You should ask user "do you want to quit(y/n)?" and quit if "y" is typed, or something similar.
------------------------------------------------------------ My code from the last project
using namespace std;
char userAnswer = ' ';
double areaVal;
class Shape{
public:
virtual double GetArea() = 0;
virtual bool CheckValidity() = 0;
};
class Rectangle : public Shape{
public:
Rectangle(double a, double b);
double getA() const { return a; }
double getB() const { return b; }
void setA(double val){ a = val; }
void setB(double val){ b = val; }
bool CheckValidity(){
if (a > 0 && b > 0){
return true;
}
return false;
}
double GetArea(){
return a * b;
}
private:
double a, b;
};
class Triangle : public Shape {
private:
double a, b, c;
public:
Triangle(double a, double b, double c);
double getA() const { return a; }
double getB() const { return b; }
double getC() const { return c; }
void setA(double val){ a = val; }
void setB(double val){ b = val; }
void setC(double val){ c = val; }
bool CheckValidity(){
if (!((a + b > c)&(a + c > b)&(b + c > a)&(a > 0)&(b > 0)&(c > 0))){
return false;
}
return true;
};
double GetArea(){
double p = (a + b + c) / 2;
return sqrt(p * (p - a) * (p - b) * (p - c));
}
};
Rectangle::Rectangle(double a, double b)
{
setA(a);
setB(b);
}
Triangle::Triangle(double a, double b, double c){
setA(a);
setB(b);
setC(c);
}
template // Template function that takes both Triangle and Rectangle to sort.
void selSort(vector& ta){
int i = 0;
int j = 0;
int indexSmallest = 0;
for (i = 0; i < ta.size(); ++i){
indexSmallest = i;
for (j = i + 1; j < ta.size(); ++j){
if (ta.at(j).GetArea() < ta.at(indexSmallest).GetArea()){
indexSmallest = j;
}
}
T temp = ta.at(i);
ta.at(i) = ta.at(indexSmallest);
ta.at(indexSmallest) = temp;
}
return;
}
template
int valSearch(vector& ab, double areaVal){
int i = 0;
for (i = 0; i < ab.size(); ++i){
if (ab.at(i).GetArea() == areaVal){
return i;
}
}
return -1;
}
int main(){
Triangle;
vector vecRect;
vector vecTriangle;
ifstream input;
input.open("Tsides.txt");
double a, b, c;
if (input.is_open()){
while (input >> a){
input >> b >> c;
Triangle temp = Triangle(a, b, c);
if (temp.CheckValidity()){
vecTriangle.push_back(temp);
}
}
}
input.close();
input.open("Rsides.txt");
if (input.is_open()){
while (input >> a){
input >> b;
Rectangle temp = Rectangle(a, b);
if (temp.CheckValidity()){
vecRect.push_back(temp);
}
}
}
selSort(vecRect); // Sort Rectangle by area
selSort(vecTriangle); // Sort Triangle by area
cout << "Enter 'q' to quit at any time and save areas to file." << endl;
while (userAnswer != 'q'){
cout << "Would you like to search for a Triangle or Rectangle area? Enter 't' or 'r'." << endl;
cin >> userAnswer;
if (userAnswer == 'r'){
cout << "Enter Area Value: ";
cin >> areaVal;
valSearch(vecRect, areaVal);
if (valSearch(vecRect, areaVal) == -1){
cout << "Rectangle area not found." << endl;
}
else {
cout << "Rectangle at index " << (valSearch(vecRect, areaVal) + 1) << " has an area of " << areaVal << endl;
}
}
if (userAnswer == 't'){
cout << "Enter Area Value: ";
cin >> areaVal;
valSearch(vecTriangle, areaVal);
if (valSearch(vecTriangle, areaVal) == -1){
cout << "Triangle area not found." << endl;
}
else {
cout << "Triangle at index " << (valSearch(vecTriangle, areaVal) + 1) << " has an area of " << areaVal << endl;
}
}
};
ofstream output;
output.open("Rareas.txt");
for (int i = 0; i < vecRect.size(); ++i){
output << vecRect[i].GetArea() << endl;
}
output.close();
input.close();
output.open("Tareas.txt");
for (int i = 0; i < vecTriangle.size(); ++i){
output << vecTriangle[i].GetArea() << endl;
}
output.close();
}
Explanation / Answer
#include<iostream.h>
class Sape
{
protected:
double x,y;
public:
virtualvoid get_data()=0;
virtualvoid display_area()=0;
};
class triangle : public Shape
{
public://prototype...
void get_data(void)
{
cout<<"Enter Dimensions : ";
cin>>x>>y;
}
void display_area(void)
{
cout<<"Area Triangle";
double aot;//decalration of variable
aot = 0.5 * x * y;
//it calculates the area of traingle
cout<<" Triangle Area "<<aot;
}
}
class rectangle : public Shape
{
public:
void get_data(void)//this method calculates the area of rectangle...
{
cout<<" two sides : ";
cin>>x>>y;input the two values
}
void display_area(void)
{//this method calculates the area
double aor;
aor = x * y;
cout<<"Rectangle ares "<<aor;
}
};
void main()
{
clrscr();
triangle tri;
rectangle rect;
shape *list[2];
list[0]=&tri;
list[1]=▭
int ch;
while(1)
{
cout<<" select Sape";
cout<<"1) Area of Triangle ";
cout<<"2) Area of Rectangle ";
cout<<"3) Exit ";
cout<<"ch:-";
cin>>ch;
switch(ch)
{
case 1 : list[0]->get_data();
list[0]->display_area();
getch();
break;
case 2 : list[1]->get_data();
list[1]->display_area();
getch();
break;
case 3 : goto end;
default: cout<<"some thing wrong try again...";
getch();
}
}
end:
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.