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

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 : ";

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");
}

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:

(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){}

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:

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++)

}
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()

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;
}


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ừ (-)

}

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;

#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];

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;

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();

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();

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 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;
}


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);
}

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();
}


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


Nhờ tải bản gốc

Tài liệu, ebook tham khảo khác

Music ♫

Copyright: Tài liệu đại học © DMCA.com Protection Status