Tải bản đầy đủ (.docx) (80 trang)

Bài tập ngôn ngữ lập trình C/C++ có code

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (251.97 KB, 80 trang )

Bài tập ngôn ngữ lập trình
Họ và tên: Mai Xuân Hòa
MSSV :20111596
Lớp : Điện tử 07
1. Chương trình in ma trận
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int m,n;
cout<<"Nhap vao m, n ";
cin>>m>>n;
for(int i=0, k=1; i<=m; i++)
{
for(int j=0; j<=n; j++,k++)
cout<<k<<'\t';
cout<<endl;
}
system("pause");
}

2. Xác định tam giác
#include<iostream>
#include<math.h>
using namespace std;
int main()
{
double a, b,c;
while(1){
cout<<"nhap 3 canh : ";


cin>>a>>b>>c;
if(a>=b+c||b>=a+c||c>=a+b) break;
int canh=(a==b)|((b==c)<<1)|((c==a)<<2);
a*=a; b*=b; c*=c;
int goc=(c==a+b)|((a==b+c)<<1)|((b==c+a)<<2);
cout<<"tam giac ";
switch((goc<<4)|canh) {
case 0: cout<<"thuong"; break;
case 7: cout<<"deu"; break;
default:
if(goc) cout<<"vuong ";
if(canh) cout<<"can";
cout<<"tai "<<(char)(((goc|canh)>>1)+65);
}
cout<<"\n\n";
}//while(1)
{
cout<<"thoat ";
}
cout<<endl;
system("pause");

}

3. Viết chương trình in ra màn hình lich của năm nay theo mẫu 2000
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{

int year;
cout<<"Lich nam: "; cin>>year;
int dy=year-2000;
int i=(dy*365+(dy-1)/4)%7;
for(int month=1; month<13; month++)
{
int days=31;
switch (month)
{
case 2: days=(year&3 ?28:29); break;
case 4: case 6: case 9: case 11: days=30;
cout<<"Thang "<<month<<"\nCN \t T2 \t T3 \t T4 \t T5 \t T6 \t T7 ";
for(int t=0; t<i; t++) cout<<"\t";
for(int d=1; d<=days; d++)
{
cout<<d;
if(i==6){
cout<<endl;
i=0;
}
else{
cout<<"\t";
i++;
}
}
cout<<"\n \n";
}
}
system("pause");
}


4. Hàm giải phương trình bậc 2
// Phuong_trinh_bac_2.cpp : Defines the entry point for the console
application.
//
#include "stdafx.h"
#include <iostream.h>
using namespace std
int FindRoot(double a,double b, double c, double *x1, double *x2)
{ double d=b*b-4*a*c;
if (d<0)
return 0;
a*=2;
if (d=0)
{
*x1=*x2=-b/a;
return 1;
}
d=sqrt(d);
*x1=(-b-d)/a;
*x2=(-b+d)/a;
return 2;
}
void main()
{
double a,b,c;
while(1)
{
cout<<"Nhap vao 3 so a, b, c :";
cin>>a>>b>>c;

if(a==o) break;
double x1, x2;
switch(FindRoot( a, b, c, &x1, &x2))
{
case 0: cout<<"Vo nghiem"<<endl; break;
case 1: cout<<"Pt co nghiem kep x= "<<x1; break;
default:
cout<<"Pt co 2 nghiem phan biet "<<"x1=
"<<x1<<endl<<"x2= "<<x2;
}
cout<<endl;
}
system("pause");
}

5. Class complex
#include "stdafx.h"
#include <iostream>
#include <iomanip>
using namespace std;
class complex
{
private:
double re,im;
public:
//hàm tao thiet lap tu hai tham so
complex(double r=0, double i=0) : re(r), im(i) {}
//hàm tao copy tu mot so phuc
complex(const complex &c) : re(c.re), im(c.im) {}
public:

complex operator+ (complex c);
complex operator- (complex c);
complex operator* (complex c);
complex operator/ (complex c);
public:
//Ham dinh nghia toan tu luong ra
friend ostream& operator<<(ostream &out, complex c)
{
return(out<<"("<<c.re<<","<<c.im<<"i)");
}
};
complex complex::operator+(complex c)
{
return complex(this->re+c.re, this->im+c.im);
}
complex complex::operator-(complex c)
{
return complex(this->re-c.re, this->im-c.im);
}
complex complex::operator*(complex c)
{
return complex((this->re*c.re)-(this->im*c.im),(this-
>re*c.im)+(c.re*this->im));
}
complex complex::operator/(complex c)
{
complex d;
if((c.re!=0)&&(c.im!=0.0))
{
d.re=((this->re*c.re)+(this->im*c.im))/((c.re*c.re)+

(c.im*c.im));
d.im=((c.re*this->im)-(this->re*c.im))/((c.re*c.re)+
(c.im*c.im));
}
return d;
}
int main(){
//khai bao 2 so phuc y va z, moi so dc khoi tao tu 1 hoac 2 so thuc bat ky
//khai bao bien double a va khoi tao bang mot so thuc bat ky
complex y(2.0,2.9), z(3.0,3.0);
double a=0.3;
cout<<y<<"+"<<z<<"+"<<a<<"="<<y+z+a<<endl;
cout<<y<<"-"<<z<<"="<<y-z<<endl;
cout<<y<<"*"<<z<<"="<<y*z<<endl;
cout<<y<<"/"<<z<<"="<<y/z<<endl;
system("pause");
}

6. Class fractions
#include "stdafx.h"
#include "iostream.h"
#include "iomanip.h"
class phanso
{
int a,b;
public:
phanso(int a=0, int b=1)
:a(a), b(b){}
phanso(const phanso &p)
:a(p.a), b(p.b){}

public:
phanso operator + (phanso p);
phanso operator - (phanso p);
phanso operator * (phanso p);
phanso operator / (phanso p);
public:
friend ostream& operator(ostream &out,phan so p)
{
return phanso(out<<p.a<<"/"<<p.b);
}
};
phanso phanso::operator + (phanso p)
{
return phanso(this->a*p.b+this->b*p.a, this->b*p.b);
}
phanso phanso::operator - (phanso p)
{
return phanso(this->a*p.b-this->b*p.a, this->b*p.b);
}
phanso phanso::operator * (phanso p)
{
return phanso(this->a*p.a,this->b*p.b);
}
phanso phanso::operator / (phanso p)
{
return phanso(this->a*p.b, this->b*p.a);
}
int main()
{
phanso x(5,6),y(7,8);

cout<<x<<"+"<<y<<"="<<x+y<<endl;
cout<<x<<"-"<<y<<"="<<x-y<<endl;
cout<<x<<"*"<<y<<"="<<x*y<<endl;
cout<<x<<"/"<<y<<"="<<x/y<<endl;
system("pause");
}

7. Class Matrix

#include "stdafx.h"
#include<iostream>
#include<math.h>
using namespace std;
template<class _T>
class Matrix
{
protected:
int rows, cols;
_T** data;
public:
void CreateData();
void deleteData();
void CreateData(int m, int n);
public:
Matrix():data(0){}
Matrix(int m, int n=0);
Matrix(int m, int n, const _T*);
Matrix(const Matrix& M);
~Matrix(){deleteData();}
public:

_T& operator()(int i, int j){return data[i][j];}
Matrix operator+(const Matrix& M);
Matrix& operator=(const Matrix& M);
friend ostream& operator<<(ostream& left, Matrix& right)
{
for(int i=0; i<right.rows; i++)
for(int j=0; j<right.cols; j++)
{
left<<right.data[i][j]<<"\t";
if((j+1)%right.cols==0)
left<<endl;
}
return left;
}
};
template<class _T>
void Matrix<_T>::CreateData()
{
data = new _T* [rows];
for(int i=0; i<rows; i++)
data[i] = new _T [cols];
for(int i=0; i<rows; i++)
for(int j=0; j<cols; j++)
data[i][j]=rand()%100;
}
template<class _T>
void Matrix<_T>::CreateData(int m, int n)
{
rows = m;
cols = n;

CreateData();
}
template<class _T>
void Matrix<_T>::deleteData()
{
if(data==0) return;
for(int i=0;i<rows;i++)
delete []data[i];
delete data;
}
template<class _T> Matrix<_T>::Matrix(int m, int n=0)
{
n = (n!=0?m:n);
CreateData(m,n);
}
template<class _T> Matrix<_T>::Matrix(const Matrix& M)
{
rows = M.rows;
cols = M.cols;
CreateData(rows,cols);
for(int i=0;i<rows;i++)
for(int j=0;j<cols;j++)
data[i][j]=M.data[i][j];
}
template<class _T> Matrix<_T>::Matrix(int m, int n, const _T* A)
{
CreateData(m,n);
rows=m;
cols=n;
for(int i=0;i<rows;i++)

data[i]=*(A+i);
}
template<class _T> Matrix<_T> Matrix<_T>::operator+(const Matrix& M)
{
Matrix<_T> temp(rows,cols);
if(rows==M.rows&&cols==M.cols)
{
for(int i=0; i<rows; i++)
for(int j=0; j<cols; j++)
{
temp.data[i][j]=data[i][j]+M.data[i][j];
}
return temp;
}
}
template<class _T> Matrix<_T>& Matrix<_T>::operator=(const Matrix& M)
{
if(rows!=M.rows||cols!=M.cols)
{
for(int i=0; i<rows; i++)
delete[] data[i];
delete[] data;
rows=M.rows;
cols=M.cols;
}
CreateData(rows, cols);
for(int i=0; i<rows; i++)
for(int j=0; j<cols; j++)
data[i][j]=M.data[i][j];
return *this;

}
class Graphic:public Matrix<int>
{
int size;
public:
Graphic(int sz, int *A)
:size(sz)
,Matrix(sz,az,A)
private:
void DFT(int v, int *mark);
};
void Graphic DFT(int v)
{
int mark=new int[size];
for(int i=0; i<size; i++)
mark[i]=0;
DFT(v,mark);
delete []mark;
}
void Graphic::DFT(int v, int *mark)
{
if(mark[v]) return;
mark[v]=1; cout<<v<<' ';
for(int w=0; w<size; w++)
{
if((*this)(v,w)==1 && mark[w]=0)
DFT(w,mark);
}
}
int main()

{
Matrix<int> A(3,3);
Matrix<int> B(3,3);
A=B;
cout<<"A = \n"<<A<<endl<<"B = \n"<<B<<endl<<"A + B = \n"<<A+B;
system("pause");
}
PROGRAM 1: Tạo lớp Phân số có các thành phần sau:
- Các thuộc tính: ts,ms;
- Hàm tạo có sử dụng tham số mặc định
- Nạp chồng các toán tử sau:
+ Toán tử cộng (+)
+ Toán tử trừ (-)
+ Toán tử nhân (*)
+ Toán tử chia (/)
+ Toán tử nhập (>>)
+ Toán tử xuất (<<)
SOLUTION 1:
#include "iostream.h"
#include "conio.h"
#include "math.h"
class PS
{
int t,m;
public:
friend ostream& operator<<(ostream& os, PS p);
friend istream& operator>>(istream& is, PS &p);
friend PS rutgon(PS p);
PS operator+(PS p);
PS operator-(PS p);

PS operator*(PS p);
PS operator/(PS p);
};

int USCLN(int x,int y)
{
if(x==0)
return y;
if (y==0)
return x;
while (x!=y)
{
if(x>y)
x-=y;
else
y-=x;
}
}
ostream& operator<<(ostream& os, PS p)
{
os<<p.t<</<<p.m<<endl;
return os;
}

istream& operator>>(istream& is, PS &p)
{
is>>p.t>>p.m;
return is;
}


PS rutgon(PS p)
{
PS q;
int x;
x=uscln(p.t,p.m);
q.t=p.t/x;
q.m=p.m/x;
return q;
}

PS PS::operator+(PS p)
{
PS q;
q.t=t*p.m+p.t*m;
q.m=m*p.m;
return rutgon(q);
}

PS PS::operator-(PS p)
{
PS q;
q.t=t*p.m-p.t*m;
q.m=m*p.m;
return rutgon(q);
}

PS PS::operator*(PS p)
{
PS q;
q.t=t*p.t;

q.m=m*p.m;
return rutgon(q);
}

PS PS::operator/(PS p)
{
PS q;
q.t=t*p.m;
q.m=m*p.t;
return rutgon(q);
}

void main()
{
PS p1,p2,p;
cout<<"Nhap p1=";cin>>p1;
cout<<"Nhap p2=";cin>>p2;
p=p1+p2;
cout<<"p="<<p<<endl;
getch();
}


PROGRAM 2: Tạo lớp complex (real + image *i ) có các thành phần sau:
- Các thuộc tính : real , image;
- Hàm tạo có sử dụng tham số mặc định
- Nạp chồng các toán tử sau:
+ Toán tử cộng (+)
+ Toán tử cộng thành phần real của số phức b lên x (b.real + x)
+ Toán tử trừ (-)

+ Toán tử nhập (>>)
+ Toán tử xuất (<<)
SOLUTION 2:
#include <iostream.h>
#include <conio.h>
class complex
{
private:
float real,image;
public:
complex(float r=0,float i=0);
complex operator+(complex c);
complex operator-(complex c);
friend complex operator+(float x,complex c);
friend ostream& operator<<(ostream &out,complex c);
friend istream& operator>>(istream &inp,complex& c);
};

complex::complex(float r,float i)
{
real=r;
image=i;
}

complex complex::operator+(complex c)
{
complex temp;
temp.real=real+c.real;
temp.image=image+c.image;
return temp;

}

complex complex::operator-(complex c)
{
complex temp;
temp.real=real-c.real;
temp.image=image-c.image;
return temp;
}

complex operator+(float x, complex c)
{
complex temp;
temp.real=c.real+x;
temp.image=c.image;
return temp;
}

ostream& operator<<(ostream &out,complex c)
{
cout<<c.real<<"+"<<c.image<<"i"<<endl;
return out;
}

istream& operator>>(istream &inp,complex &c)
{
cout<<" Nhap vao phan thuc:";
cin>>c.real;
cout<<" Nhap vao phan ao:";
cin>>c.image;

return inp;
}

void main()
{
complex c,c1,c2,c3;
float x;
cin>>c;
cin>>c1;
c2=c+c1;
cout<<" Cong hai so phuc :"<<c2;
c3=c1-c;
cout<<" Tru hai so phuc :"<<c3;
cout<<" Nhap vao x :";
cin>>x;
c3=c3+x;
cout<< c3;
getch();
}

PROGRAM 3: Xây dựng lớp xaukytu có các thành phần sau :
- Các thuộc tính : char * str, int length
- Hàm tạo không tham số
- Hàm tạo một tham số ( char * s)
- Hàm hiển thị xâu và độ dài xâu
- Hàm nối xâu để cộng hai xâu (xaukytu s)
- Hàm huỷ .
Viết chương trình kiểm tra
SOLUTION 3:
File xaukytu.h

#ifndef xaukytu_h
#define xaukytu_h
class xaukytu
{
private:
int length;
char * str;
public:
xaukytu(char * s);
xaukytu();
void htxau();
xaukytu noixau(xaukytu s1);
~xaukytu();
};
#endif
File xaukytu.cpp
#include <iostream.h>
#include "xaukytu.h"
#include <string.h>
#include <conio.h>
xaukytu::xaukytu()
{
str=" ";
length=0;
cout<<" xau rong"<<endl;
}
xaukytu::xaukytu(char * s)
{
length=strlen(s);
str=new char[length+1];

strcpy(str,s);
}
void xaukytu::htxau()
{
cout<<"Xau ky tu la:"<<str<<endl;
cout<<" Do dai xau la:"<<length<<endl;
}
xaukytu::~xaukytu()
{
delete str;
getch();
}
xaukytu xaukytu::noixau(xaukytu s)
{
char * temp = str;
length+=s.length;
str=new char[length];
strcpy(str,temp); //copy xau temp vao xau str
strcat(str,s.str);//noi hai xau s.str va str
delete temp;
return *this;
}
void main()
{
xaukytu s;
s.htxau();
xaukytu s1(" Truong Dai Hoc ");
xaukytu s2(" Dien Luc ");
s1.noixau(s2);
s1.htxau();

getch();
}

PROGRAM 4: Tạo một lớp vector gồm có các thành phần sau:
- Các thuộc tính : float * v; int n
- Hàm thiết lập không tham số
- Hàm thiết lập một tham số
- Hàm thiết lập hai tham số
- Hàm hiển thị
- Hàm huỷ
Viết một chương trình kiểm tra.
SOLUTION 4:
#include <iostream.h>
#include <conio.h>
class vector
{
private:
int n;
float *v;
public:
vector();//ham thiet lap khong tham so
vector(int size);//ham thiet lap mot tham so
vector(int size,float * a);//ham thiet lap hai tham so
void display();
~vector();//ham huy
};

vector::vector()
{
cout<<" Su dung ham thiet lap khong tham so :"<<endl;

cout<<" Tao mot doi tuong lop vecto co dia chi tai:"<<this<<endl;
cout<<" Nhap vao so toa do :";
cin>>n;
v=new float[n];
cout<<" Xin cap phat mot vung bo nho cho "<<n<<" so thuc "<<" tai dia chi
la: "<<v<<endl;
for(int i=0;i<n;i++)
{
cout<<" Toa do thu "<<(i+1)<<":";
cin>>v[i];
}
}
vector::vector(int size)
{ cout<<" Su dung ham thiet lap mot tham so :"<<endl;
cout<<" Tao mot doi tuong lop vecto co dia chi tai:"<<this<<endl;
n=size;
v=new float[n];
cout<<" Xin cap phat mot vung bo nho cho "<<n<<"so thuc"<<" tai dia chi
la:"<<v<<endl;
for(int i=0;i<n;i++)
{
cout<<" Toa do thu "<<(i+1)<<":";
cin>>v[i];
}
}

vector::vector(int size,float * a)
{
cout<<" Su dung ham thiet lap hai tham so :"<<endl;
cout<<" Tao mot doi tuong lop vecto co dia chi tai:"<<this<<endl;

n=size;
v=new float[n];
cout<<" Xin cap phat mot vung bo nho cho "<<n<<" so thuc"<<" tai dia chi
la:"<<v<<endl;
for(int i=0;i<n;i++)
v[i]=a[i];
}
vector::~vector()
{
cout<<" Giai phong "<<v<<" cua doi tuong co dia chi tai "<<this<<endl;
delete v;
getch();
}
void vector::display()
{
cout<<" Hien thi doi tuong co dia chi tai:"<<this<<endl;
for(int i=0;i<n;i++)
cout<<" " <<v[i]<<" ";
cout<<endl;
}
void main()
{
vector s;
s.display();
vector s1(5);
s1.display();
float a[5]={1,2,3,4,5};
vector s2(5,a);
s2.display();
getch();

}

PROGRAM 5: Xây dựng một lớp tamgiac có các thành phần sau:
- Các thuộc tính là các cạnh a, b, c
- Các hàm thành phần bao gồm:
+ Hàm nhập giá trị cho các cạnh (Kiểm tra tính hợp lệ đảm bảo là 3
cạnh của một tam giác)
+ Hàm tính diện tích tam giác
+ Hàm kiểm tra tam giác(đều, vuông cân, cân, vuông, thường)
+ Hàm hiển thị thông tin( diện tích, tính chất tam giác)
Viết một chương trình kiểm tra.
SOLUTION 5:
#include <iostream.h>
#include <conio.h>
#include <math.h>
class tamgiac
{
private:
int a,b,c;
float dientich();
int kttamgiac();
public:
void nhap();
void in();
};
void tamgiac::nhap()
{
do
{
cout<<"Nhap canh a:";cin>>a;

cout<<"Nhap canh b:";cin>>b;
cout<<"Nhap canh c:";cin>>c;
}while(a+b<c||a+c<b||b+c<a);
}
float tamgiac::dientich()
{
float p;
p=(a+b+c)/2;
return (sqrt(p*(p-a)*(p-b)*(p-c)));
}
int tamgiac::kttamgiac()
{
if(a==b||b==c||c==a)
if(a==b&&b==c)
return 1;
else
if(a*a==b*b+c*c||b*b==a*a+c*c||c*c==a*a+b*b)
return 2;
else
return 3;
else
if(a*a==b*b+c*c||b*b==a*a+c*c||c*c==a*a+b*b)
return 4;
else
return 5;
}
void tamgiac::in()
{
cout<<"Dien tich tam giac la:"<<dientich()<<endl;
int kt=kttamgiac();

switch(kt)
{
case 1:
cout<<" Tam giac deu"<<endl;
break;

case 2:
cout<<" Tam giac vuong can"<<endl;
break;
case 3:
cout<<" Tam giac can "<<endl;
break;
case 4:
cout<<" Tam giac vuong"<<endl;
break;
case 5:
cout<<" Tam giac thuong"<<endl;
break;
}

}
void main()
{
tamgiac a;
a.nhap();
a.in();
getch();
}

PROGRAM 6: Xây dựng một lớp Time mô tả các thông tin vê giờ, phút,

giây.Lớp Time có các thành phần sau:
- Các thuộc tính mô tả giờ, phút, giây;
- Các hàm thành phần dùng để xác lập giá trị cho từng thành phần
giờ, phút, giây(Có kiểm tra điều kiện giờ (0->23), phút(0->59), giây(0-
>59);
- Hàm thành phần setTime(int,int,int) để xác lập thời gian
- Hàm hiển thị giờ theo định dạng 24 tiếng (vd : 23:54:40);
- Hàm hiển thị giờ theo định dạng 12 tiếng( vd : 11:54:40 PM);
- Hàm tăngGiây()để tăng thời gian mỗi lần lên một giây. Chú ý các
trường hợp tăng sang phút tiếp theo, tăng sang giờ tiếp theo,tăng sang
ngày tiếp theo.
Viết chương trình chính khai báo một đối tượng thời gian là 23:59:58 và
thực hiện tăng thời gian 5 giây đồng thời hiển thị thời gian cho mỗi lần
tăng.
SOLUTION 6:
#include <iostream.h>
#include <conio.h>

class time
{
private:
int hour;
int minute;
int second;
void incHour();
void incMinute();
public:
void setTime(int,int=0,int=0);
void setHour(int);
void setMinute(int);

void setSecond(int);
void print12h();
void print24h();
void incSecond();
};
void time::incHour()
{
hour++;
if(hour==24) hour=0;
}
void time::incMinute()
{
minute++;
if(minute==60)
{
minute=0;
incHour();
}
}

void time::incSecond()
{
second++;
if(second==60)
{
second=0;
incMinute();
}



}
void time::setHour(int h)
{
hour=(h>=0 &&h<24)? h:0;
}
void time::setMinute(int m)
{
minute=(m>=0 &&m<60) ? m:0;
}
void time::setSecond(int s)
{
second=(s>=0 &&s<60) ? s:0;
}
void time::setTime(int h,int m,int s)
{
setHour(h);
setMinute(m);
setSecond(s);
}
void time::print24h()
{
cout<<(hour<10 ? "0" : "")<<hour<<":"<<(minute <10 ? "0" : "")<<minute<<":";
cout<<(second<10 ? "0" : "")<<second<<endl;
}
void time::print12h()
{
cout<<(hour%12)<<":"<<(minute<10?"0":"")<<minute<<":";
cout<<(second<10 ? "0":"")<<second<<(hour<12?" AM":"PM")<<endl;
}



void main()
{
time t2;
t2.setTime(23,59,58);
t2.print12h();
t2.print24h();
for(int i=0;i<3;i++)
{
t2.incSecond();
t2.print12h();
t2.print24h();
}

getch();
}

PROGRAM 7: Viết một chương trình xây dựng hai lớp: một lớp thí sinh và một
lớp danh sách thí sinh. Trong đó lớp thí sinh có dữ liệu bao gồm các thông
tin: số báo danh, điểm toán, điểm hoá, điểm lý. Lớp danh sách thí sinh có dữ
liệu một mảng các thí sinh và số lượng phần tử thuộc mảng đó. Viết chương
trình thực hiện các công việc sau:
1. Nhập và hiển thị một danh sách các thí sinh từ bàn phím
2. Sắp xếp danh sách các thí sinh theo thứ tự tăng dần về điểm số
3. Hiển thị thông tin của các sinh viên có tổng điểm trên 18
SOLUTION 7:
#include <iostream.h>
#include <conio.h>
class thisinh
{

private:
char sbd[25];
float dtoan,dly,dhoa;
public:
void nhap();
void in();
float tdiem();
};
void thisinh::nhap()
{
cout<<"Nhap vao so bao danh :";
cin>>sbd;
cout<<"Nhap diem toan :";
cin>>dtoan;
cout<<"Nhap diem ly:";
cin>>dly;
cout<<"Nhap diem hoa:";
cin>>dhoa;
}
void thisinh::in()
{
cout<<"So bao danh "<<sbd;
cout<<" Diem toan:"<<dtoan<<" Diem ly:"<<dly<<" Diem hoa:"<<dhoa;
cout<<" Tong diem: "<<tdiem()<<endl;
}

float thisinh::tdiem()
{
return(dtoan+dly+dhoa);
}


class dsts
{
private:
int n;
thisinh dsts[100];

public:
void nhapds();
void inds();
void sapxep();
void tdtren18();
};

void dsts::nhapds()
{
cout<<"Nhap vao so luong thi sinh:";
cin>>n;
for(int i=0;i<n;i++)
dsts[i].nhap();
}

void dsts::inds()
{
for(int i=0;i<n;i++)
dsts[i].in();
}
void dsts::sapxep()
{
for(int i=0;i<n-1;i++)

for(int j=i+1;j<n;j++)
if(dsts[i].tdiem()>dsts[j].tdiem())
{
thisinh temp;
temp= dsts[i];
dsts[i]=dsts[j];
dsts[j]=temp;
}
}

void dsts::tdtren18()
{
for(int i=0;i<n;i++)
if(dsts[i].tdiem()>18)
dsts[i].in();
}

void main()
{
dsts ds;
ds.nhapds();
cout<<"Sap xep danh sach cac thi sinh theo thu tu tang dan ve tong
diem:"<<endl;
ds.sapxep();
ds.inds();
cout<<"Danh sach cac thi sinh co tong diem tren 18 la:"<<endl;
ds.tdtren18();
getch();
}


PROGRAM 8: Chương trình dưới đây xây dựng một lớp hình tròn đơn giản có thành
phần dữ liệu là bán kính r và có các phương thức như: nhập dữ liệu cho r,
tính toán và hiển thị chu vi, diện tích của hình tròn đó.
SOLUTION 8:
#include <iostream.h>
#include <conio.h>
#include <math.h>
#define PI 3.14
class hinhtron
{
private:
float r;
public:
void nhap();
float chuvi();
float dientich();
void in();
};

void hinhtron::nhap()
{
cout<<"Nhap vao ban kinh:";
cin>>r;
}

float hinhtron::chuvi()
{
return (2*PI*r);
}


float hinhtron::dientich()
{
return (PI*pow(r,2));
}

void hinhtron::in()
{
cout<<"Chu vi hinh tron la:"<<chuvi()<<endl;
cout<<"Dien tich hinh tron la:"<<dientich()<<endl;
}
void main()
{
hinhtron ht;
ht.nhap();
ht.in();
getch();
}

PROGRAM 9: Viết một chương trình nhập vào hai danh sách sinh viên vào 2 mảng
một chiều,sắp xếp hai mảng giảm dần theo điểm số của sinh viên và trộn 2 mảng
trên thành một mảng giảm dần.Trong chương trình sử dụng các chương trình con
sau:NHAP(sinhvien *sv, int n );HIENTHI(sinhvien *sv, int n );SAPXEP(sinhvien
*sv, int n);SWAP(sinhvien *x,sinhvien *y);sinhvien *TRON_DS(sinhvien *
sv1,sinhvien * sv2,int n1,int n2);
Lưu ý : Việc trộn mảng thì luôn có một mảng kết thúc trước do vậy phải biết
mảng nào chưa hết phải thêm vào.
SOLUTION 9:
#include "stdio.h"
#include "conio.h"
struct sinhvien

{
char hoten[30];
char lop[30];

×