cs202 lab1 letrunghieu 10es

13
CS 202 – Homework 1 – 2nd Submission Name: Le Trung Hieu Class: 10ES 1. Write and run the sample below #include <iostream.h> #include <conio.h> class Rectangle { private: float a, b; public: void init() { cout<<"Height = "; cin>>a; cout<<"Width = "; cin>>b; } float area() { return a*b; } void display() { cout<<"Height and width of rectangle = "<<a << " and " <<b ; cout<<"\nArea ="<<area(); } }; main() { Rectangle A; A.init(); A.display(); getch(); }

Upload: le-hieu

Post on 27-May-2017

215 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: CS202 lab1 LeTrungHieu 10ES

CS 202 – Homework 1 – 2nd SubmissionName: Le Trung HieuClass: 10ES

1. Write and run the sample below

#include <iostream.h>#include <conio.h>class Rectangle{private: float a, b;public: void init() { cout<<"Height = "; cin>>a; cout<<"Width = "; cin>>b; } float area() { return a*b; } void display() { cout<<"Height and width of rectangle = "<<a << " and " <<b ; cout<<"\nArea ="<<area(); }};main(){Rectangle A;A.init();A.display();getch();}

The Code:

Page 2: CS202 lab1 LeTrungHieu 10ES

Result:

2. Re-write the above program that all member functions are defined outside the class (using the scope resolution operator)

The Code:

#include <iostream.h>#include <conio.h>class Rectangle{private: float a, b;public: void init() ; float area() ; void display() ;};void Rectangle::init(){ cout<<"Height = "; cin>>a; cout<<"Width = "; cin>>b;} ;float Rectangle::area() { return a*b; } ;void Rectangle::display() { cout<<"Height and width of rectangle = "<<a << " and " <<b ; cout<<"\nArea ="<<area();

Page 3: CS202 lab1 LeTrungHieu 10ES

} ;

main(){Rectangle A;A.init();A.display();getch();}

Result:

3. Create a Triangle class including following member functions :

Functions Descriptioninit() Enter values of edges from keyboardisTriangle Check if its three edges are validisEquilateral() Check if the triangle is equilateralisIsosceles Check if the triangle is isoscelesisRight Check if the triangle is rightisRightIsosceles() Check if the triangle is right isoscelesgetType() get triangle type (equilateral, isosceles, right, right isosceles,

scalene)area() Area of the rectangle (Heron’s formula: sqrt(s(s-a)*(s-b)*(s-c)),

when s = (a+b+c)/2display() Show values of three edges and area of the triangle

Page 4: CS202 lab1 LeTrungHieu 10ES

The Code:

#include <iostream.h>#include <conio.h>#include <math.h>class Rectangle{private: float a, b,c;public: void init() { cout<<"a = "; cin>>a; cout<<"b = "; cin>>b; cout<<"c = "; cin>>c; }

bool isTriangle() { return ((a+b>c)&&(a+c>b)&&(b+c>a))&&(a>0)&&(b>0)&&(c>0); } bool isEquilateral() { return (isTriangle()&&(a==b)&&(b==c)); } bool isIsosceles() { return (isTriangle()&&!isEquilateral()&&((a==b)||(b==c)||(c==a))); } bool isRight() { return (isTriangle()&&((a*a==b*b+c*c)||(b*b==a*a+c*c)||(c*c==b*b+a*a))); } bool isRightIsosceles() { return (isRight()&&isIsosceles()); } void getType() { if (!isTriangle()) cout<<"This is not triangle"; else if (isEquilateral()) cout<<"This trianlge is equilateral"; else if (isIsosceles()) cout<<"This triangle is isosceles "; else if (isRight()) cout<<"This triangle is right"; else

Page 5: CS202 lab1 LeTrungHieu 10ES

if (isRightIsosceles()) cout<<"This triangle is right isosceles"; else cout<<"This triangle is scalene"; }

float area() { float s= (a+b+c)/2; return sqrt(s*(s-a)*(s-b)*(s-c)); } void display() { cout<<"\nValue of three edges: "<<a<<" , "<<b<<" , "<<c<<"\n"; cout<<"\nArea ="<<area()<<"\n"; }};main(){Rectangle A;A.init();A.getType();A.display();getch();}

Result:

Page 6: CS202 lab1 LeTrungHieu 10ES

4. Create a Point2D class including following member functions :

The Code:

#include <iostream.h>#include <conio.h>#include <math.h>

using namespace std;class Point2D{private: float x,y;public: Point2D(); Point2D(float, float);

Page 7: CS202 lab1 LeTrungHieu 10ES

float distance(Point2D); void display();};

Point2D::Point2D(float a, float b){ x=0; y=0;};

Point2D::Point2D() { cout<<"x= "; cin>>x; cout<<"y= "; cin>>y; };

float Point2D::distance(Point2D A){ float C; C = sqrt(pow(x-A.x,2)+pow(y-A.y,2)); return C; };

void Point2D::display(){ cout<<"("<<x<<","<<y<<")\n"; };int main(){

cout<<"Co-ordinate of the current point: \n"; Point2D A; cout<<"Co-ordinate of the new point: \n"; Point2D B;

cout<<"Current point:";A.display(); cout<<"New point:";B.display(); float C; C= A.distance(B); cout<<"Distance between two points: "<<C<<"\n";

getch();}

Page 8: CS202 lab1 LeTrungHieu 10ES

Result:

5. Create a Complex class including following member functions:

Functions DescriptionComplex() Default constructor (real=0, image=0)Complex(float, float) Constructor with 2 argumentsadd(Complex) Addition of complex numbers

C.real = A.real + B.realC.image = A.image + B.image

sub(Complex Subtraction of complex numbersC.real = A.real - B.realC.image = A.image - B.image

product(Complex) Product of complex numbersC.real = A.real * B.real - A.image* B.imageC.image = A.real*B.image + B.real*A.image

division(Complex) Subtraction of complex numbers

display() Show the complex number in the format : real + j*image

Page 9: CS202 lab1 LeTrungHieu 10ES

The Code:

#include <stdio.h>#include <conio.h>#include <iostream>using namespace std;

class Complex{ private: float a,b; public:

Complex() { a=0; b=0; } Complex(float, float); Complex add(Complex A) { Complex C; C.a=a+A.a; C.b=b+A.b;

return C; } Complex sub(Complex A) { Complex D; D.a=a-A.a; D.b=b-A.b;

return D; } Complex product(Complex A) { Complex E; E.a=a*A.a-b*A.b; E.b=a*A.b+A.a*b; return E; } Complex division(Complex A) { Complex F; F.a=(a*A.a+b*A.b)/(A.a*A.a+A.b*A.b); F.b=(A.a*b-a*A.b)/(A.a*A.a+A.b*A.b);

Page 10: CS202 lab1 LeTrungHieu 10ES

return F; } void display(); };

Complex::Complex(float x, float y) { a =x; b =y; };

void Complex::display() { cout<<a<<"+"<<b<<"i\n"; }

int main() { float a,b,c,d; cout<<"Complex number A: \n"; cout<<"A real: "; cin>>a; cout<<"A image: "; cin>>b; cout<<"Complex number B: \n"; cout<<"B real: "; cin>>c; cout<<"B image: "; cin>>d; Complex A(a,b); Complex B(c,d); Complex C,D,E,F; C= A.add(B); D=A.sub(B); E=A.product(B); F=A.division(B);

cout<<"A = ";A.display(); cout<<"B = ";B.display(); cout<<"A + B = ";C.display(); cout<<"A - B = ";D.display(); cout<<"A * B = ";E.display(); cout<<"A / B = ";F.display(); getch(); }

Page 11: CS202 lab1 LeTrungHieu 10ES

Result: