Chào các bạn! Truyen4U chính thức đã quay trở lại rồi đây!^^. Mong các bạn tiếp tục ủng hộ truy cập tên miền Truyen4U.Com này nhé! Mãi yêu... ♥

chuong 1 1-10

 

/**

  * Bai lam cau 1.1

  */

#include "iostream.h"

#include "conio.h"

 

/**

  * Tao class complex mo ta doi tuong so phuc

  */

 class complex{

private:

int a;

int b;

public:

//Ham thiet lap

complex(){

a = 0;

b = 0;

}

 

//Ham nhap so phuc

void init(){

cout << "

Nhap phan thuc va phan ao : ";

cin >> a >> b;

}

 

//Ham hien thi so phuc

void display(){

if(b < 0) cout << a << b << "i";

else cout << a << "+" << b << "i";

}

 

//Khai bao toan tu + la ham ban cua class

friend complex operator + (complex x, complex y);

 

//Khai bao toan tu + la ham ban cua class

friend complex operator - (complex x, complex y);

 }; // End class complex

 

/**

  * Toan tu + 

  * @param : complex x - so hang thu nhat

  * @param : complex y - so hang thu hai

  * @return : complex z - tong cua a + b;  

  */

complex operator + (complex x, complex y){

complex z;

z.a = x.a + y.a;

z.b = x.b + y.b;

return z;

}

 

/**

  * Toan tu - 

  * @param : complex x - so bi tru

  * @param : complex y - so tru

  * @return : complex z - hieu cua a - b;  

  */

complex operator - (complex x, complex y){

complex z;

z.a = x.a - y.a;

z.b = x.b - y.b;

return z;

}

 

/**

  * Chuong trinh chinh

  */

void main(){

clrscr();

complex x,y;

cout << "

Nhap so phuc x: ";

x.init();

cout << "

Nhap so phuc y: ";

y.init();

cout << "

Tong x + y = "; (x+y).display();

cout << "

Hieu x - y = "; (x-y).display();

getch();

}

 /**

  * Bai lam cau 1.2

  */

#include "iostream.h"

#include "conio.h"

 

/**

  * Tao class complex mo ta doi tuong so phuc

  */

 class complex{

private:

int a;

int b;

public:

//Ham thiet lap

complex(){

a = 0;

b = 0;

}

 

//Ham nhap so phuc

void init(){

cout << "

Nhap phan thuc va phan ao : ";

cin >> a >> b;

}

 

//Ham hien thi so phuc

void display(){

if(b < 0) cout << a << b << "i";

else cout << a << "+" << b << "i";

}

 

//Toan tu = 

void operator = (complex &x){

a = x.a;

b = x.b;

}

 

//Toan tu +

complex operator + (complex &x){

complex y;

y.a = a + x.a;

y.b = b + x.b;

return y;

}

 

 }; // End class complex

 

/** 

  * Ham tinh tong cua day so phuc

  * @param complex arr[] - dia chi cua mang cac so phuc

  * @param int n - so phan tu cua mang arr;

  * @return complex s - tong cua mang so phuc arr

  */

complex sum(complex arr[], int n){

complex s;

for(int i = 0; i < n; i++){

s = s + arr[i];

}

return s;

}

 

/**

  * Chuong trinh chinh

  */

void main(){

clrscr();

complex arr[10];

int n;

cout << "

Nhap n = ";

cin >> n;

for(int i = 0; i < n; i++){

arr[i].init();

}

cout << "

Tong cua day so phuc arr la : ";

sum(arr,n).display();

getch();

}

 

/**

  * Bai lam cau 1.3

  */

#include "iostream.h"

#include "conio.h"

#include "stdlib.h"

//Class mydate mo ta thong tin ngay than nam

class mydate{

private:

int d;

int m;

int y;

public:

//Ham thiet lap

mydate(){

d = 0;

m = 0;

y = 0;

}

 

//Ham nhap ngay - thang - nam

void init(){

cout << "

Nhap ngay - thang - nam: ";

cin >> d >> m >> y;

}

 

//Ham hien thi ngay - thang - nam

void display(){

cout << d << "-" << m << "-" << y;

}

 

//Khai bao toan tu >= la ham ban cua lop

friend int operator >= (mydate a, mydate b);

};//End class mydate

/**

  * Toan tu >=

  * @param : mydate a - doi so thu nhat

  * @param : mydate b - doi so thu hai

  * @return : int 1 or int 0 - 1 if a >= b 0 if a !>= b;

  */

int operator >= (mydate a, mydate b){

if(a.y > b.y) return 1;

if((a.y == b.y) && (a.m > b.m)) return 1;

if((a.y == b.y) && (a.m == b.m) && (a.d >= b.d)) return 1;

return 0;

}  

//Ham chinh

void main(){

clrscr();

mydate arr[5];

mydate max;

int i;

cout << "

Nhap ngay:

";

for(i = 0; i < 5; i++){

arr[i].init();

}

cout << "

Xem ngay vua nhap:

";

for(i = 0; i < 5; i++){

arr[i].display();

cout << "

";

}

cout << "

Ngay lon nhat: ";

max = arr[0];

for(i = 0; i < 5; i++){

if(arr[i] >= max) max = arr[i];

}

max.display();

getch();

}

#include "iostream.h"

#include "conio.h"

class vector{

private:

int n;

float *v;

public:

//Ham thiet lap 1 tham so

vector(int n);

//Ham thiet lap sao chep

vector(vector &a);

//Ham huy bo

~vector();

//Ham nhap cac phan tu cua vector

void init();

//Ham hien thi cac phan tu cua vector

void display();

//Toan tu gan la con cua class

void operator = (vector &a);

//Toan tu + la ham ban cua vector

friend vector operator + (vector &a, vector &b);

};//End class vector

/**

  * Ham thiet lap 1 tham so

  * @param : int nn - so phan tu cua vector v

  */

vector :: vector(int nn){

n = nn;

v = new float[n];

}

/**

  * Ham thiet lap sao chep

  * @param : vector &a - vector duoc sao chep

  */

vector :: vector(vector &a){

n = a.n;

for(int i = 0; i < n; i++){

v[i] = a.v[i];

}

}

/**

  * Ham huy bo

  */

vector :: ~vector(){

delete v;

}

/**

  * Ham nhap cac phan tu cua vector

  */

void vector :: init(){

for(int i = 0; i < n; i++){

cout << "

Nhap phan tu v[" << i << "] = ";

cin >> v[i];

}

}

/**

  * Ham hien thi cac phan tu cua vector

  */

void vector :: display(){

for(int i = 0; i < n; i++){

cout << v[i] << "  ";

}

cout << "

";

}

/**

  * Toan tu = la thanh phan cua lop

  * @param : vector &a - vector duoc gan

  */

void vector :: operator = (vector &a){

for(int i = 0; i < n; i++){

v[i] = a.v[i];

}

}

/** 

  * Toan tu + la ham ban cua vector

  * @param : vector a - so hang thu nhat

  * @param : vector b - so hang thu hai

  * @return : vector c - tong cua a + b

  */

vector operator + (vector &a, vector &b){

vector c(a.n);

for(int i = 0; i < c.n; i++){

c.v[i] = a.v[i] + b.v[i];

}

return c;

}

//Ham chinh

void main(){

clrscr();

vector p(3),q(3),z(3);

cout << "

Nhap vector p:

";

p.init();

cout << "

Nhap vector q:

";

q.init();

cout << "

Cac vector da nhap la

";

p.display();

cout << "

";

q.display();

cout << "

Vector tong cua 2 vector p,q la :

";

(p+q).display();

getch();

}

/**

  * Bai lam cau 1.5

  */

#include "iostream.h"

#include "conio.h"

//Class ps mo ta phan so

class ps{

private:

int tu;

int mau;

public:

//ham thiet lap

ps(int t, int m);

//Ham nhap phan so

void set();

//Ham hien thi phan so

void display();

//Ham rut gon phan so

void archive();

//Toan tu + la thanh phan cua class

ps operator + (ps &a);

};//End class ps

/**

  * Ham tinh uoc chung lon nhat (su dung khi rut gon phan so)

  * @param: int a - tham so thu nhat

  * @param: int b - tham so thu hai

  * @return: int c - la ucln cua a,b

  */

int ucln(int a, int b){

if(a * b == 0) return 1;

while(a != b){

if(a > b) a = a - b;

else b = b - a;

}

return a;

}

/**

  * Ham thiet lap

  * @param : int t - tu so co gia tri mac dinh = 0

  * @param : int m - mau so co gia tri mac dinh = 1

  */

ps :: ps(int t = 0, int m = 1){

tu = t;

mau = m;

}

/**

  * Ham nhap phan so

  */

void ps :: set(){

cout << "

Nhap tu so : ";

cin >> tu;

nhap : cout << "

Nhap mau so : ";

cin >> mau;

if(mau == 0){

cout << "

Nhap lai mau so: ";

goto nhap;

}

}

/**

  * Ham hien thi phan so

  */

void ps :: display(){

if(mau < 0){

tu = -tu;

mau = -mau;

}

cout << tu << "/" << mau;

}

/**

  * Ham rut gon phan so

  */

void ps :: archive(){

int x = ucln(tu,mau);

tu = tu/x;

mau = mau/x;

}

/**

  * Toan tu +

  * @param: ps a - so hang se cong voi so hang goi toi ham

  * @return: ps c - tong cua a+ so hang goi toi ham

  */

ps ps :: operator + (ps &a){

ps c;

c.tu = (tu * a.mau) + (a.tu * mau);

c.mau = mau * a.mau;

c.archive();

return c;

}

//Ham chinh

void main(){

clrscr();

ps arr[10],tong;

int i,n;

cout << "

Nhap n = :";

cin >> n;

cout << "

Nhap cac phan so: ";

for(i =0; i < n; i++){

cout << "

Nhap phan so arr[" << i << "] = ";

arr[i].set();

}

cout << "

Tong cua mang ps : ";

for(i = 0; i < n; i++){

tong =tong + arr[i];

}

tong.display();

getch();

}

/**

  * Bai lam cau 1.6

  */

#include "iostream.h"

#include "conio.h"

//Class ps mo ta phan so

class ps{

private:

int tu;

int mau;

public:

//ham thiet lap

ps(int t, int m);

//Ham nhap phan so

void set();

//Ham hien thi phan so

void display();

//Toan tu > la thanh phan cua class

int operator > (ps &a);

};//End class ps

/**

  * Ham thiet lap

  * @param : int t - tu so co gia tri mac dinh = 0

  * @param : int m - mau so co gia tri mac dinh = 1

  */

ps :: ps(int t = 0, int m = 1){

tu = t;

mau = m;

}

/**

  * Ham nhap phan so

  */

void ps :: set(){

cout << "

Nhap tu so : ";

cin >> tu;

nhap : cout << "

Nhap mau so : ";

cin >> mau;

if(mau == 0){

cout << "

Nhap lai mau so: ";

goto nhap;

}

}

/**

  * Ham hien thi phan so

  */

void ps :: display(){

if(mau < 0){

tu = -tu;

mau = -mau;

}

cout << tu << "/" << mau;

}

/**

  * Toan tu >

  * @param: ps a - ps can so sanh

  * @return: 1 if true 0 if false

  */

int ps :: operator > (ps &a){

if((tu * a.mau) > (a.tu * mau)) return 1;

return 0;

}

/**

  * Ham sap xep

  * @param: ps *a - mang a gom cac phan so

  * @param: int n - so phan tu cua mang a

  * @return: mang duoc sap xep

  */

void sapxep (ps *a, int n){

ps tg;

for(int i = 0; i < n; i++){

for(int j = i; j < n; j++){

if(a[i] > a[j]){

tg = a[i];

a[i] = a[j];

a[j] = tg;

}

}

}

}

 

//Ham chinh

void main(){

clrscr();

ps arr[10];

int i,n;

cout << "

Nhap n :";

cin >> n;

cout << "

Nhap cac phan so: ";

for(i =0; i < n; i++){

cout << "

Nhap phan so arr[" << i << "] = ";

arr[i].set();

}

cout << "

Mang vua nhap : ";

for(i = 0; i < n; i++){

arr[i].display();

cout << "  ";

}

sapxep(arr,n);

cout << "

Mang sau khi sap xep : ";

for(i = 0; i < n; i++){

arr[i].display();

cout << "  ";

}

getch();

}

/**

  * Bai lam cau 1.7

  */

#include "iostream.h"

#include "conio.h"

//Class ps mo ta phan so

class ps{

private:

int tu;

int mau;

public:

//ham thiet lap

ps(int t, int m);

//Ham nhap phan so

void set();

//Ham hien thi phan so

void display();

//Toan tu >= la ham ban cua class

friend int operator >= (ps &a, ps &b);

};//End class ps

/**

  * Ham thiet lap

  * @param : int t - tu so co gia tri mac dinh = 0

  * @param : int m - mau so co gia tri mac dinh = 1

  */

ps :: ps(int t = 0, int m = 1){

tu = t;

mau = m;

}

/**

  * Ham nhap phan so

  */

void ps :: set(){

cout << "

Nhap tu so : ";

cin >> tu;

nhap : cout << "

Nhap mau so : ";

cin >> mau;

if(mau == 0){

cout << "

Nhap lai mau so: ";

goto nhap;

}

}

/**

  * Ham hien thi phan so

  */

void ps :: display(){

if(mau < 0){

tu = -tu;

mau = -mau;

}

cout << tu << "/" << mau;

}

/**

  * Toan tu >=

  * @param: ps &a - tham chieu toi phan thu 1

  * @param: ps &b - tham chieu toi ps thu 2 

  * @return: 1 if a >= b; 0 if a !>= b

  */

int operator >= (ps &a, ps &b){

if((a.tu * b.mau) >= (b.tu * a.mau)) return 1;

return 0;

}

//Ham chinh

void main(){

clrscr();

ps arr[10], min, max;

int i,n;

cout << "

Nhap n :";

cin >> n;

cout << "

Nhap cac phan so: ";

for(i =0; i < n; i++){

cout << "

Nhap phan so arr[" << i << "] = ";

arr[i].set();

}

cout << "

Mang vua nhap : ";

for(i = 0; i < n; i++){

arr[i].display();

cout << "  ";

}

min = arr[0];

max = arr[0];

for(i = 0; i < n; i++){

if(min >= arr[i]) min = arr[i];

if(arr[i] >= max) max = arr[i];

}

cout << "

Phan so be nhat :"; min.display();

cout << "

Phan so lon nhat :"; max.display();

getch();

}

/**

  * Bai lam cau 1.8

  */

#include "iostream.h"

#include "conio.h"

//Class vecter mo ta 1 vector

class vector{

private:

int n;

float *v;

public:

//Ham thiet lap 1 tham so

vector(int n);

//Ham thiet lap sao chep

vector(vector &a);

//Ham huy bo

~vector();

//Ham nhap cac phan tu cua vector

void set();

//Ham hien thi cac phan tu cua vector

void display();

//Toan tu gan la con cua class

void operator = (vector &a);

//Toan tu * la ham ban cua vector

friend vector operator * (int k, vector &b);

};//End class vector

/**

  * Ham thiet lap 1 tham so

  * @param : int nn - so phan tu cua vector v

  */

vector :: vector(int nn){

n = nn;

v = new float[n];

}

/**

  * Ham thiet lap sao chep

  * @param : vector &a - vector duoc sao chep

  */

vector :: vector(vector &a){

n = a.n;

for(int i = 0; i < n; i++){

v[i] = a.v[i];

}

}

/**

  * Ham huy bo

  */

vector :: ~vector(){

delete v;

}

/**

  * Ham nhap cac phan tu cua vector

  */

void vector :: set(){

for(int i = 0; i < n; i++){

cout << "

Nhap phan tu v[" << i << "] = ";

cin >> v[i];

}

}

/**

  * Ham hien thi cac phan tu cua vector

  */

void vector :: display(){

for(int i = 0; i < n; i++){

cout << v[i] << "  ";

}

cout << "

";

}

/**

  * Toan tu = la thanh phan cua lop

  * @param : vector &a - vector duoc gan

  */

void vector :: operator = (vector &a){

for(int i = 0; i < n; i++){

v[i] = a.v[i];

}

}

/** 

  * Toan tu * la ham ban cua vector

  * @param : int k - he so tu do

  * @param : vector &a -tham chieu toi vector a 

  * @return : vector b - tich cua k*a

  */

vector operator * (int k, vector &a){

vector b(a.n);

for(int i = 0; i < a.n; i++){

b.v[i] = k * a.v[i];

}

return b;

}

//Ham chinh

void main(){

clrscr();

int k;

vector p(3);;

cout << "

Nhap he so k =";

cin >> k;

cout << "

Nhap vector p:

";

p.set();

cout << "

Vector da nhap la

";

p.display();

cout << "

Tich k*p la :

";

(k*p).display();

getch();

}

/**

  * Bai lam cau 1.9

  */

#include "iostream.h"

#include "conio.h"

#include "stdlib.h"

//Class mydate mo ta thong tin ngay than nam

class mydate{

private:

int d;

int m;

int y;

public:

//Ham thiet lap

mydate();

//Ham nhap ngay - thang - nam

void set();

//Ham hien thi ngay - thang - nam

void display();

//Khai bao toan tu >= la ham ban cua lop

int operator > (mydate &a);

};//End class mydate

/**

  * Ham thiet lap khong tham so

  */

mydate :: mydate (){

d = 0;

m = 0;

y = 0;

}

/**

  * Ham nhap 

  */

void mydate :: set(){

cout << "

Nhap ngay - thang - nam: ";

cin >> d >> m >> y;

}

/**

  * Ham hien thi

  */

void mydate :: display(){

cout << d << "-" << m << "-" << y;

}

/**

  * Toan tu >

  */

int mydate :: operator > (mydate &a){

if(y > a.y) return 1;

if((y == a.y) && (m > a.m)) return 1;

if((y == a.y) && (m == a.m) && (d > a.d)) return 1;

return 0;

}  

/**

  * Ham sapxep

  * @param: mydate *a - con tro tham chieu toi mang

  * @param: int n - so phan tu cung mang

  */

void sapxep(mydate *a, int n){

mydate tg;

for(int i =0; i < n; i++){

for(int j =i; j < n; j++){

if(a[i] > a[j]){

tg = a[i];

a[i] = a[j];

a[j] = tg;

}

}

}

}

//Ham chinh

void main(){

clrscr();

mydate arr[10];

int i,n;

cout << "

Nhap n = ";cin >> n;

cout << "

Nhap mang ngay: ";

for(i =0; i < n; i++){

cout << "

Nhap ngay arr[" << i << "] :";

arr[i].set();

}

cout << "

Cac ngay da nhap:";

for(i =0; i < n; i++){

arr[i].display();

cout << "

";

}

sapxep(arr,n);

cout << "

Ngay da sap xep: ";

for(i =0; i < n; i++){

arr[i].display();

cout << "

";

}

getch();

}

/**

  * Bai lam cau 1.20

  */

#include "iostream.h"

#include "conio.h"

/**

  * Tao class complex mo ta doi tuong so phuc

  */

 class complex{

private:

int a;

int b;

public:

//Ham thiet lap

complex();

 

//Ham nhap so phuc

void set();

 

//Ham hien thi so phuc

void display();

 

//Toan tu + 

complex operator + (complex &x);

 

//Toan tu - 

complex operator - (complex &x);

 }; // End class complex

 /**

  * @Ham thiet lap khong tham so

  */

complex :: complex(){

a = 0;

b = 0;

}

/**

 * Ham nhap so phuc

 */

void complex :: set(){

cout << "

Nhap phan thuc: "; cin >> a;

cout << "Nhap phan ao: "; cin >> b;

}

/**

 * Ham hien thi so phuc

 */

void complex :: display(){

if(b < 0) cout << a << b << "i";

else cout << a << "+" << b << "i";

}

/**

 * Toan tu +

 */

complex complex :: operator + (complex &sp){

complex t;

t.a = a + sp.a;

t.b = b + sp.b;

return t;

}

/**

 * Toan tu -

 */

complex complex :: operator - (complex &sp){

complex t;

t.a = a - sp.a;

t.b = b - sp.b;

return t;

}

 

/**

  * Chuong trinh chinh

  */

void main(){

clrscr();

complex x,y;

cout << "

Nhap x = ";

x.set();

cout << "

Nhap y = ";

y.set();

cout << "

Tong x + y = ";

(x+y).display();

cout << "

Hieu x - y = ";

(x-y).display();

getch();

}

 

 

Bạn đang đọc truyện trên: Truyen4U.Com

Tags: