






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
This is code assigned in lab of course. It was assigned by Jay Singh at Aligarh Muslim University for Data Structure course. It includes: Catch, Bug, Semicolon, Error, Function, Declared, Instance, Accessed, Class, Scope, Allocated, Array
Typology: Exercises
1 / 10
This page cannot be seen from the preview
Don't miss anything!







3 Point
3.1 geometry.h
1 class Point { 2 int x , y ; 3 4 public : 5 Point ( int xx =0 , int yy =0) { x = xx ; y = yy ;} 6 int getX () const { return x ;} 7 int getY () const { return y ;} 8 void setX ( const int xx ) { x = xx ;} 9 void setY ( const int yy ) { y = yy ;} 10 };
4 PointArray
4.1 geometry.h
class PointArray { int size ; Point * points ;
void resize ( int size ) ;
public : PointArray () ; PointArray ( const Point pts [] , const int size ) ; PointArray ( const PointArray & pv ) ; ~ PointArray () ;
void clear () ; int getSize () const { return size ;} void push_back ( const Point & p ) ; void insert ( const int pos , const Point & p ) ; void remove ( const int pos ) ; Point * get ( const int pos ) ; const Point * get ( const int pos ) const ; };
44 void PointArray :: push_back ( const Point & p ) { 45 resize ( size + 1) ; 46 points [ size - 1] = p ; 47 // Could also just use insert ( size , p ) ; 48 } 49 50 void PointArray :: insert ( const int pos , const Point & p ) { 51 resize ( size + 1) ; 52 53 for ( int i = size - 1; i > pos ; i - -) { 54 points [ i ] = points [i -1]; 55 } 56 57 points [ pos ] = p ; 58 } 59 60 void PointArray :: remove ( const int pos ) { 61 if ( pos >= 0 && pos < size ) { // pos < size implies size > 0 62 // Shift everything over to the left 63 for ( int i = pos ; i < size - 2; i ++) { 64 points [ i ] = points [ i + 1]; 65 } 66 resize ( size - 1) ; 67 } 68 } 69 70 Point * PointArray :: get ( const int pos ) { 71 return pos >= 0 && pos < size? points + pos : NULL ; 72 } 73 74 const Point * PointArray :: get ( const int pos ) const { 75 return pos >= 0 && pos < size? points + pos : NULL ; 76 }
5 Polygon and friends
5.1 Polygon
1 class Polygon { 2 protected : 3 static int numPolygons ; 4 PointArray points ; 5 6 public : 7 Polygon ( const PointArray & pa ) ; 8 Polygon ( const Point points [] , const int numPoints ) ; 9 virtual double area () const = 0; 10 static int getNumPolygons () { return numPolygons ;} 11 int getNumSides () const { return points. getSize () ;} 12 const PointArray * getPoints () const { return & points ;} 13 ~ Polygon () { - - numPolygons ;} 14 };
1 int Polygon :: n = 0; 2 3 Polygon :: Polygon ( const PointArray & pa ) : points ( pa ) { 4 ++ numPolygons ; 5 } 6 7 Polygon :: Polygon ( const Point pointArr [] , const int numPoints ) : points ( pointArr , numPoints ) { 8 ++ numPolygons ; 9 }
5.2 Rectangle
1 class Rectangle : public Polygon { 2 public : 3 Rectangle ( const Point &a , const Point & b ) ;
2 public : 3 Triangle ( const Point &a , const Point &b , const Point & c ) ; 4 virtual double area () const ; 5 };
5.4 geometry.cpp
1 Triangle :: Triangle ( const Point &a , const Point &b , const Point & c ) 2 : Polygon ( updateConstructorPoints (a , b , c ) , 3) {} 3 4 double Triangle :: area () const { 5 int dx01 = points. get (0) -> getX () - points. get (1) -> getX () , 6 dx12 = points. get (1) -> getX () - points. get (2) -> getX () , 7 dx20 = points. get (2) -> getX () - points. get (0) -> getX () ; 8 int dy01 = points. get (0) -> getY () - points. get (1) -> getY () , 9 dy12 = points. get (1) -> getY () - points. get (2) -> getY () , 10 dy20 = points. get (2) -> getY () - points. get (0) -> getY () ; 11 12 double a = std :: sqrt ( dx01 * dx01 + dy01 * dy01 ) , 13 b = std :: sqrt ( dx12 * dx12 + dy12 * dy12 ) , 14 c = std :: sqrt ( dx20 * dx20 + dy20 * dy20 ) ; 15 16 double s = ( a + b + c ) / 2; 17 18 return std :: sqrt ( s * (s - a ) * (s - b ) * (s - c ) ) ; 19 }
5.5 main.cpp
1 # include < iostream > 2 using namespace std ; 3 4 # include " geometry. h " 5 6 void printAttributes ( Polygon * p ) { 7 cout << " p ’s area is " << p - > area () << " .\ n " ; 8 9 cout << " p ’s points are :\ n " ; 10 const PointArray * pa = p - > getPoints () ; 11 for ( int i = 0; i < pa - > getSize () ; ++ i ) { 12 cout << " ( " << pa - > get ( i ) -> getX () << " , " << pa - > get ( i ) -> getY () << " ) \ n " ; 13 }
16 int main ( int argc , char * argv []) { 17 cout << " Enter lower left and upper right coords of rectangle as four space separated integers : " ; 18 int llx , lly , urx , ury ; 19 cin >> llx >> lly >> urx >> ury ; 20 Point ll ( llx , lly ) , ur ( urx , ury ) ; 21 Rectangle r ( ll , ur ) ; 22 printAttributes (& r ) ; 23 24 cout << " Enter three coords of triangle as six space separated integers : " ; 25 int x1 , y1 , x2 , y2 , x3 , y3 ; 26 cin >> x1 >> y1 >> x2 >> y2 >> x3 >> y3 ; 27 Point a ( x1 , y1 ) , b ( x2 , y2 ) , c ( x3 , y3 ) ; 28 Triangle t (a , b , c ) ; 29 printAttributes (& t ) ; 30 31 return 0; 32 }
5.6 Questions
6 Strings
1 const string vowels = " aeiou " ; 2 3 string pigLatinify ( const string s ) { 4 if ( s. size () == 0) { 5 // oops , empty string 6 return s ; 7 } 8
MIT OpenCourseWare http://ocw.mit.edu
January (IAP) 2011
For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms.