Sie sind auf Seite 1von 12

LAB 1 REPORT

Name: Le Hoang Nhat 09ES


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

2.
#include <iostream>
#include <iomanip>

class Rectangle {
private:
float a, b;
public:
void init();
float area();
void display();

};
void Rectangle::init() {
printf("Height = ");
scanf("%f",&a);
printf("\nWidth = ");
scanf("%f",&b);
}
float Rectangle::area() {
return a*b;
}
void Rectangle::display() {
printf("\nHeight and width of trianle = %0.2f and %0.2f",a,b);
printf("\n\nArea = %0.2f\n",area());
}
main() {
Rectangle A;
A.init();
A.display();
system("pause");
}

3.
#include <iostream>
#include <conio.h>
#include <math.h>
class Triangle {
public:
float e1,e2,e3;
void init();
bool isTriangle(float,float,float);
bool isEquilateral(float,float,float);
bool isIsosceles(float,float,float);
bool isRight(float,float,float);
bool isRightIsosceles(float,float,float);
void getType(float,float,float);
float area(float,float,float);
void display(float,float,float,float);
};
void Triangle::init(){
printf("Enter three edge of Triangle A !");
printf("\n\nEdge 1 : ");
scanf("%f",&e1);
printf("\nEdge 2 : ");
scanf("%f",&e2);
printf("\nEdge 3 : ");
scanf("%f",&e3);
}
bool Triangle::isTriangle(float a, float b, float c) {
if ((a+b>c) && (b+c>a) && (c+a>b)) return true;
else return false;
}
bool Triangle::isEquilateral(float a, float b, float c) {
if ((a==b) && (b==c)) return true;
else return false;
}
bool Triangle::isIsosceles(float a, float b, float c) {
if ((a==b) && (b!=c)) return true;
if ((a==c) && (c!=b)) return true;
if ((c==b) && (b!=a)) return true;

else return false;


}
bool Triangle::isRight(float a, float b, float c) {
if (((a*a + b*b) == c*c) || ((a*a + c*c) == b*b) || ((c*c + b*b) == a*a)) return
true;
else return false;
}
bool Triangle::isRightIsosceles(float a, float b, float c) {
if (isRight(a,b,c)) {
if ((a==b) && (b!=c)) return true;
if ((a==c) && (c!=b)) return true;
if ((c==b) && (b!=a)) return true;
else return false;
}
else return false;
}
void Triangle::getType(float a, float b, float c) {
if (isTriangle(a,b,c)) {
if (isIsosceles(a,b,c)) printf("\nA is Isosceles Triangle!");
if (isEquilateral(a,b,c)) { printf("\nA is Equilateral Triagnle!");}
if (isRightIsosceles(a,b,c)) { printf("\nA is Right Isosceles Triangle!"); }
if (isRight(a,b,c)) printf("\nA is Right Triangle!");
}
else printf("\nA is not Triagnle!");
}
float Triangle::area(float a, float b, float c) {
if (isTriangle(a,b,c)) {
float s=(a+b+c)/2;
return sqrt(s*(s-a)*(s-b)*(s-c));
}
else return 0;
}
void Triangle::display(float a, float b, float c, float d) {
printf("\n\nThree edge of triangle: %0.2f , %0.2f and %0.2f",a,b,c);
printf("\n\nArea = %0.2f\n\n",d);
}
main() {
Triangle A;
A.init();
A.getType(A.e1,A.e2,A.e3);

float s=A.area(A.e1,A.e2,A.e3);
A.display(A.e1,A.e2,A.e3,s);
system("pause");
}

4.

#include <iostream>
#include <conio.h>
#include <math.h>
class Point2D {
float x,y;

public:
Point2D();
Point2D(float, float);
void move(float, float);
float distance(Point2D);
void display();
};
Point2D::Point2D() {
x=0; y=0;
}
Point2D::Point2D(float a, float b) {
x=a; y=b;
}
void Point2D::move(float dx, float dy) {
x+=dx; y+=dy;
}
float Point2D::distance(Point2D a) {
return sqrt((a.x-x)*(a.x-x) + (a.y-y)*(a.y-y));
}
void Point2D::display() {
printf("\tx = %0.2f\n\t\t\ty = %0.2f",x,y);
}
main() {
Point2D A(3.5,4.5);
Point2D B(5.5,6.5);
printf("==============Before Moving==============");
printf("\n\nCo-ordinate of A :");
A.display();
printf("\n\nCo-ordinate of B :");
B.display();
printf("\n\nDistance between A & B = %0.2f\n\n",A.distance(B));
A.move(10,15);
printf("==============After Moving==============");
printf("\n\nCo-ordinate of A :");
A.display();
printf("\n\nCo-ordinate of B :");
B.display();
printf("\n\nDistance between A & B = %0.2f\n\n",A.distance(B));

system("pause");
}

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

class Complex {

public:
float x,y;

Complex();
Complex(float, float);
void add(Complex);
void sub(Complex);
void product(Complex);
void division(Complex);
void display();
};

Complex::Complex() {
x=0; y=0;
}

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

void Complex::display() {
printf("%0.1f + %0.1f*i",x,y);
}

void Complex::add(Complex b) {
Complex c;

c.x = x+ b.x;
c.y = y+ b.y;
c.display();
}

void Complex::sub(Complex b) {
Complex c;
c.x = x - b.x;
c.y = y - b.y;
c.display();
}

void Complex::product(Complex b) {
Complex c;
c.x = x*b.x - y*b.y;
c.y = x*b.y + b.x*y;
c.display();
}

void Complex::division(Complex b) {
Complex c;
float m = b.x*b.x + b.y*b.y;
float t1 = (x*b.x + y*b.y);
float t2 = (y*b.x - x*b.y);

printf("%0.1f + %0.1f*i\n\n",t1/m,t2/m);

main() {
Complex A(3,4);
Complex B(5,6);
printf("Number A = ");
printf("%0.1f + %0.1f*i",A.x,A.y);
printf("\n\nNumber B = ");
printf("%0.1f + %0.1f*i",B.x,B.y);

printf("\n\nADD RESULT\t");
A.add(B);
printf("\n\nSUB RESULT\t");
A.sub(B);
printf("\n\nPRODUCT RESULT\t");
A.product(B);
printf("\n\nDIVISION RESULT\t");
A.division(B);
printf("\n\n");
system("pause");
}

Das könnte Ihnen auch gefallen