Vous êtes sur la page 1sur 6

//----------------------------------------------------------------------------//

// Name: Ching-hung Chiang //


// Student ID: 1101227 //
// Assignment: #2 //
//----------------------------------------------------------------------------//

#include <iostream>
#include <string>
#include <iomanip>

using namespace std;


//class Shape
class Shape
{
friend istream& operator>>(istream &input, Shape &newShape);
public:
Shape();
int getArea()const; // must be used
int getLength()const;
int getWidth()const;
int getMaxX()const;
int getMaxY()const;
int getMinX()const;
int getMinY()const;
string getType()const; // must be used (returns 'Square' or 'Rectangle')
void setData(int ar, int l, int w, int xmx, int xmi, int ymx, int ymi, string tp);
private:
int area;
int length, width;
int xmax,xmin,ymax,ymin;
string type;
};

Shape::Shape()
{
area =0;
length = 0;
width =0;
xmax =0;
xmin =0;
ymax =0;
ymin =0;
}

int Shape::getArea()const
{
return area;
}

int Shape::getLength()const
{
return length;
}

int Shape::getWidth()const
{
return width;
}

int Shape::getMaxX()const
{
return xmax;
}

int Shape::getMaxY()const
{
return ymax;
}

int Shape::getMinX()const
{
return xmin;
}

int Shape::getMinY()const
{
return ymin;
}

string Shape::getType()const
{
return type;
}

void Shape::setData(int ar, int l, int w, int xmx, int xmi, int ymx, int ymi, string tp)
{
area = ar;
length = l;
width = w;
xmax = xmx;
xmin = xmi;
ymax = ymx;
ymin = ymi;
type = tp;
}
//overload to enter data
istream& operator>>(istream &input, Shape &newShape)
{
int ix[4], iy[4];
input>>ix[0];
input.ignore(1);
input>>iy[0];
input.ignore(1);
input>>ix[1];
input.ignore(1);
input>>iy[1];
input.ignore(1);
input>>ix[2];
input.ignore(1);
input>>iy[2];
input.ignore(1);
input>>ix[3];
input.ignore(1);
input>>iy[3];
input.ignore(1);
int mxX = -9999, mxY = -9999, miX = 9999, miY = 9999;
for(int i =0; i<4; i++)
{
if(ix[i]>mxX)
{
mxX=ix[i];
}
if(ix[i]<miX)
{
miX=ix[i];
}
if(iy[i]>mxY)
{
mxY=iy[i];
}
if(iy[i]<miY)
{
miY=iy[i];
}
}
newShape.xmax = mxX;
newShape.ymax = mxY;
newShape.xmin = miX;
newShape.ymin = miY;
newShape.length = mxY-miY;
newShape.width = mxX-miX;
newShape.area = newShape.length*newShape.width;
if (newShape.length==newShape.width) {
newShape.type ="Square";
} else {
newShape.type ="Rectangle";
}
return input;
}
//prototype
void load(Shape arr[]);
int print12(Shape arr[]);
int print3(Shape arr[]);
// main function
int main()
{
//declare an array to hold objects of type Shape
Shape s[10];
//global function1
load(s);
//global function2
int a_area = print12(s);
//global function3
int u_area = print3(s);
//reasult 4
float rate = (float)a_area / u_area * 100;
if (rate >= 90)
cout << "The layout is efficient, it uses " << setprecision(1) << rate << "% of the
space." << endl;
else
cout << "The layout is not efficient, it wastes " << fixed << setprecision(1) <<
100 - rate << "% of the space." << endl;

return 0;
}

// this function load all data of the shape


void load(Shape arr[]){
for(int i = 0; i<10; i++)
{
cout<<"Enter data of each shape "<< i+1<<":";
cin>>arr[i];
}
}
// this function print 1 and 2 result and return total area
int print12(Shape arr[]){
int tArea =0;
cout << left << setw(10) << "Shape#" << setw(15) << "Type" << right <<
setw(15) << "Area(sq/units)" << endl;
for (int j = 0; j < 10; j++)
{
tArea += arr[j].getArea();
cout << left << setw(10) << j + 1 << setw(15) << arr[j].getType() << right <<
setw(15) << arr[j].getArea() << endl;
}
cout << endl;
cout << "The total area of the shapes is " << tArea << " sq/units." << endl << endl;
return tArea;
}
// this function print result 3 and return used area
int print3(Shape arr[]){
int maxX = -999, minX = 999, maxY = -999, minY = 999;
int usedArea;
for (int k = 0; k < 10; k++)
{
if (arr[k].getMaxX() > maxX)
maxX = arr[k].getMaxX();
if (arr[k].getMinX() < minX)
minX = arr[k].getMinX();
if (arr[k].getMaxY() > maxY)
maxY = arr[k].getMaxY();
if (arr[k].getMinY() < minY)
minY = arr[k].getMinY();
}
usedArea = (maxX - minX)*(maxY - minY);
cout << "Minimum material dpecifications:" << endl;
cout << "Length: " << maxY - minY << "\tWidth: " << maxX - minX << "\tArea: "
<< usedArea << " sq/units" << endl << endl;
return usedArea;
}
/*
output:
Enter data of each shape 1:0 0 0 5 14 5 14 0
Enter data of each shape 2:0 10 5 10 5 5 0 5
Enter data of each shape 3:10 10 10 5 5 5 5 10
Enter data of each shape 4:13 7 10 7 10 8 13 8
Enter data of each shape 5:10 5 10 7 15 7 15 5
Enter data of each shape 6:0 14 4 10 0 10 4 14
Enter data of each shape 7:4 10 8 10 8 14 4 14
Enter data of each shape 8:8 10 8 14 12 14 12 10
Enter data of each shape 9:14 8 14 9 10 8 10 9
Enter data of each shape 10:10 9 10 10 11 10 11 9
Shape# Type Area(sq/units)
1 Rectangle 70
2 Square 25
3 Square 25
4 Rectangle 3
5 Rectangle 10
6 Square 16
7 Square 16
8 Square 16
9 Rectangle 4
10 Square 1

The total area of the shapes is 186 sq/units.

Minimum material dpecifications:


Length: 14 Width: 15 Area: 210 sq/units

The layout is not efficient, it wastes 11.4% of the space.

*/

Vous aimerez peut-être aussi