Đăng ký Đăng nhập
Trang chủ Giáo dục - Đào tạo Trung học phổ thông Bài tập ngôn ngữ lập trình...

Tài liệu Bài tập ngôn ngữ lập trình

.PDF
80
422
135

Mô tả:

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 #include 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< #include 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< #include 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 "< 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"< #include 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 complex complex complex operator+ operatoroperator* operator/ (complex (complex (complex (complex c); c); c); c); public: //Ham dinh nghia toan tu luong ra friend ostream& operator<<(ostream &out, complex c) { return(out<<"("<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)) { (c.im*c.im)); (c.im*c.im)); d.re=((this->re*c.re)+(this->im*c.im))/((c.re*c.re)+ d.im=((c.re*this->im)-(this->re*c.im))/((c.re*c.re)+ } 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<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< #include using namespace std; template 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 void Matrix<_T>::CreateData() { data = new _T* [rows]; for(int i=0; i void Matrix<_T>::CreateData(int m, int n) { rows = m; cols = n; CreateData(); } template void Matrix<_T>::deleteData() { if(data==0) return; for(int i=0;i Matrix<_T>::Matrix(int m, int n=0) { n = (n!=0?m:n); CreateData(m,n); } template Matrix<_T>::Matrix(const Matrix& M) { rows = M.rows; cols = M.cols; CreateData(rows,cols); for(int i=0;i Matrix<_T>::Matrix(int m, int n, const _T* A) { CreateData(m,n); rows=m; cols=n; for(int i=0;i 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 Matrix<_T>& Matrix<_T>::operator=(const Matrix& M) { if(rows!=M.rows||cols!=M.cols) { for(int i=0; i { 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 A(3,3); Matrix B(3,3); A=B; cout<<"A = \n"<>) + 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<>(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="<>) + Toán tử xuất (<<) SOLUTION 2: #include #include 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<>(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 :"<>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 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 #include "xaukytu.h" #include #include xaukytu::xaukytu() { str=" "; length=0; cout<<" xau rong"< #include 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 :"<>n; v=new float[n]; cout<<" Xin cap phat mot vung bo nho cho "<>v[i]; } } vector::vector(int size) { cout<<" Su dung ham thiet lap mot tham so :"<>v[i]; } } vector::vector(int size,float * a) { cout<<" Su dung ham thiet lap hai tham so :"< #include #include 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+b23), 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 #include 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) {
- Xem thêm -

Tài liệu liên quan