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

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

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Bài tập ngôn ngữ lập trình C/C++ Điện tử viễn thông ĐH Bách Khoa Hà Nội

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 thành phần sau: - Các thuộc tính: ts,ms; - Hàm tạo 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) [...]... "); s1.noixau(s2); s1.htxau(); getch(); } - PROGRAM 4: Tạo một lớp vector gồm 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 #include class vector { private: int n; float *v; public:... 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 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... - 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 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 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... !"

Ngày đăng: 24/03/2014, 22:51

Từ khóa liên quan

Tài liệu cùng người dùng

Tài liệu liên quan