This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
main() { Clock a ( 9,15,38 ); cout<<"Gio a: "; a.HienThi(); cout<<endl; Clock b; cout<<"Nhap gia tri cho b: "<<endl; b.Nhap(); cout<<"Gio b: ";b.HienThi(); cout<<endl; Clock c = b.Cong(1); cout<<"Gio b sau khi cong 45 phut: "; c.HienThi(); cout<<endl; getch(); }
Bài về tháng năm1. #include<iostream>
#include<conio.h> using namespace std; class Date { int ngay, thang, nam; public: Date() { ngay = 1; thang = 1; nam = 2000; } Date(int d, int m, int y) { ngay = d; thang = m; nam = y; } void Nhap(); void HienThi() { cout<<ngay<<"/"<<thang<<"/"<<nam; } int HopLe();
Date Cong(); Date Cong(int n); };
void Date::Nhap() { while(1) { cout<<"Nhap ngay: "; cin>>ngay; cout<<"Nhap thang: "; cin>>thang; cout<<"Nhap nam: "; cin>>nam; if(HopLe()) break; cout<<"Nhap khong hop le - Nhap lai !!! "<<endl; } }
Date Date::Cong() { Date kq = *this; if(kq.ngay==28 && kq.thang ==2 && kq.nam%4==0) return Date(29,2,nam); kq.ngay ++; int songay[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; if (kq.ngay > songay[kq.thang]) { kq.thang++; kq.ngay = 1; } if (kq.thang > 12 ) { kq.nam ++; kq.thang = 1; } return kq; }
Date Date::Cong(int n) { Date kq = *this; for(int i=0; i<n; i++) kq = kq.Cong(); return kq; }
main() { Date d1; cout<<"Nhap gia tri: "<<endl; d1.Nhap(); d1.HienThi();
cout<<"Nhap so ngay can cong them: "; int n; cin>>n; cout<<"Ngay ket qua sau khi cong them "<<n<<" ngay: "; d1.Cong(n).HienThi(); getch(); }
BÀI VIẾT CÙNG CHUYÊN MỤC
bài tập về tệp trong C Xử lý lỗi bỏ qua khi dùng hàm gets và... Trọn bộ hơn 130 video học lập trình C# Giáo trình C của Aptech [Elementary... Một số dạng bài tập C++ cơ bản Bạn nào có các bài tập đầy đủ về C up lên... [Nâng Cao] Simple C# FTP Class Automatically localizing applications with... [Nâng cao] C# Native Interop: Methods and... Tài liệu C# cơ bản, có kém theo ví dụ minh...
main(){ PhanSo a(3,7),b(4,9); a.InPS();cout<<endl; b.InPS();cout<<endl;PhanSo x,y;cout<<"nhap phan so x \n";x.NhapPS();x.InPS();cout<<endl;cout<<"nhap phan so y \n";y.NhapPS();y.InPS();cout<<endl;
Bài 2:chỉ có phần class ThueBaoDT (hàm main ta để riêng và lở tay xoá mất tiêu)PHP Code:#include<iostream>#include<conio.h>#include<string.h>using namespace std;class ThueBaoDT{private:char sdt[11];char* hoten;char* diachi;int sophut;public:ThueBaoDT();ThueBaoDT(char phone[11],char *name,char *address,int time);ThueBaoDT(const ThueBaoDT& a);~ThueBaoDT();ThueBaoDT &operator=(const ThueBaoDT& a);void NhapDT();void InDT();char *LayHoTen();char *LaySDT();char *LayDiaChi();char *LayTen();int Laysophut();int GiaCuoc(); };ThueBaoDT::ThueBaoDT(){hoten=new char[35];diachi=new char[100];strcpy(sdt,"");}ThueBaoDT::ThueBaoDT(char phone[11],char *name,char *address,int time){hoten=new char[35];diachi=new char[100];name=new char[35];
address=new char[100];strcpy(sdt,phone);strcpy(hoten,name);strcpy(diachi,address);sophut=time;}ThueBaoDT::ThueBaoDT(const ThueBaoDT &a){hoten=new char[35];diachi=new char[100];strcpy(sdt,a.sdt);strcpy(hoten,a.hoten);strcpy(diachi,a.diachi);sophut=a.sophut;}ThueBaoDT::~ThueBaoDT(){delete[] hoten;delete[] diachi;}ThueBaoDT& ThueBaoDT::operator=(const ThueBaoDT &a){delete[] hoten;delete[] diachi;hoten=new char[35];diachi=new char[100];strcpy(sdt,a.sdt);strcpy(hoten,a.hoten);strcpy(diachi,a.diachi);sophut=a.sophut;return *this;}void ThueBaoDT::NhapDT(){hoten=new char[35];diachi=new char[100];cout<<"Nhap so dien thoai : ";fflush(stdin);gets(sdt);cout<<"Nhap so ho ten : ";fflush(stdin);gets(hoten);cout<<"Nhap so dia chi : ";fflush(stdin);gets(diachi);cout<<"Nhap thoi gian goi : ";cin>>sophut;}void ThueBaoDT::InDT(){cout<<"----------------------------------"<<endl;cout<<"thong tin cua khac hang : "<<hoten<<endl;cout<<"so dien thoai "<<sdt<<endl;cout<<"dia chi : "<<diachi<<endl;cout<<"thoi gian goi : "<<sophut<<endl; }char *ThueBaoDT::LayHoTen(){return hoten;}char *ThueBaoDT::LaySDT(){return sdt;}char *ThueBaoDT::LayDiaChi(){return diachi;
chieudai=g.chieudai; chieurong=g.chieurong; return *this; } istream& operator>>(istream& cin,Gach &g) { cout<<"\nnhap ma so gach : ";fflush(stdin);gets(g.maso); cout<<"nhap mau gach : ";fflush(stdin);gets(g.mau); cout<<"nhap so luong gach : ";cin>>g.soluong; cout<<"nhap chieu dai gach : ";cin>>g.chieudai; cout<<"nhap so luong gach : ";cin>>g.chieurong; return cin; } ostream &operator<<(ostream& cout,Gach &g) { cout<<"\n In Thong tin : \n"; cout<<"-------------------------------"; cout<<"\nma so gach : "<<g.maso<<endl; cout<<"mau gach : "<<g.mau<<endl; cout<<"so luong gach : "<<g.soluong<<endl; cout<<"chieu dai gach : "<<g.chieudai<<endl; cout<<"so luong gach : "<<g.chieurong<<endl; return cout; } float Gach::GiaBanLe() { return giaban*0.2; } ///////////////////ham doc lap//////////////// int SoLuong(Gach g,int x,int y) {
int dientich=x*y; int s=g.DienTich(); int result=dientich/s; if (dientich%s) result++; return result; } int MauXam(Gach g[],int n) { int dem=0; for(int i=0;i<n;i++) if(strcmp(g[i].mau,"xam")==0) dem++; return dem; } //////////////////////////////////////////// main() { Gach ds[100]; int n; cout<<"nhap so luong danh sach gach : ";cin>>n; for(int i=0;i<n;i++) { cout<<"\nNhap danh sach thu "<<i+1; cin>>ds[i]; } for(int i=0;i<n;i++) { cout<<ds[i]; cout<<"dien tich : "<<ds[i].DienTich()<<endl; }; cout<<"so luong gach mau xam la : "<<MauXam(ds,n)<<endl; cout<<"so luong hop gach moi loai dung de loat dien tich 6x20 la :\n";
";cin>>diemthi[i];} } void ThiSinh::NhapTS() { cout<<"Nhap So bao danh : ";fflush(stdin);gets(sbd); cout<<"Nhap ho ten : ";fflush(stdin);gets(hoten); cout<<"Nhap ma nghanh : ";cin>>manghanh; cout<<"Nhap khu vuc : ";cin>>khuvuc; //NhapDiem(); } void ThiSinh::HienThi() { //cout<<"\n---------In Thong Tin--------------\n"; cout<<"so bao danh : "<<sbd<<endl; cout<<"Ho Ten Ts : "<<hoten<<endl; cout<<"Ma Nghanh : "<<manghanh<<endl; cout<<"Khu Vuc : "<<khuvuc<<endl; //for(int i=0;i<3;i++) //cout<<"Diem mon "<<i+1<<" la : "<<diemthi[i]<<endl; } //toan tu nhap xuat istream& operator>>(istream& cin,ThiSinh t) { cout<<"Nhap So bao danh : ";fflush(stdin);gets(t.sbd); cout<<"Nhap ho ten : ";fflush(stdin);gets(t.hoten); cout<<"Nhap ma nghanh : ";cin>>t.manghanh; cout<<"Nhap khu vuc : ";cin>>t.khuvuc; t.NhapDiem(); } ostream& operator<<(ostream &cout,ThiSinh t) { ofstream out_stream; out_stream.open("d:\\thisinh.txt"); out_stream<<"so bao danh : "<<t.sbd<<endl; out_stream<<"Ho Ten Ts : "<<t.hoten<<endl; out_stream<<"Ma Nghanh : "<<t.manghanh<<endl; out_stream<<"Khu Vuc : "<<t.khuvuc<<endl; for(int i=0;i<3;i++) out_stream<<"Diem mon "<<i+1<<" la : "<<t.diemthi[i]<<endl; out_stream.close(); return cout; } int ThiSinh::BiDiemLiet() { for(int i=0;i<3;i++) if(diemthi[i]==0) return 1; return 0; } int ThiSinh::TrungTuyen(float diemchuan,int nghanh) { if(nghanh!=manghanh) return 0;
switch (khuvuc) { case 1:diemchuan-=0.5;break; case 2:diemchuan-=1.0;break; case 3:diemchuan-=1.5;break; } float tongdiem=diemthi[0]+diemthi[1]+diemthi[2]; if(tongdiem>=diemchuan) return 1; } /////////////////////// //ham toan cuc void Nhap(ThiSinh t[],int n)
{ for(int i=0;i<n;i++) { cout<<"nhap ts thu "<<i+1<<endl; t[i].NhapTS();cout<<endl; } } void NhapDiemThi(ThiSinh t[],int &n) { for (int i=0;i<n;i++) { cout<<"Nhap diem cho Thi sinh co so bao danh : "<<t[i].sbd<<endl; t[i].NhapDiem(); } }
PhanSo PhanSo::operator*(PhanSo a) { PhanSo result; result.tuso=tuso*a.tuso; result.mauso=mauso*a.mauso; return result; } PhanSo PhanSo::operator/(PhanSo a) { PhanSo result; result.tuso=tuso*a.mauso; result.mauso=mauso*a.tuso; return result; } PhanSo PhanSo::operator+(PhanSo a) { PhanSo result; result.tuso=tuso*a.mauso+mauso*a.tuso; result.mauso=mauso*a.mauso; return result; } PhanSo PhanSo::operator-(PhanSo a) { PhanSo result; result.tuso=tuso*a.mauso-mauso*a.tuso; result.mauso=mauso*a.mauso; return result; } ////////////////rut gon////////////////// int UCLN(int a,int b) { int temp; while(b>0) { temp=a%b; a=b; b=temp; } return a; } void PhanSo::RutGon() { mauso=mauso/UCLN(tuso,mauso); tuso=tuso/UCLN(tuso,mauso); } //////////toan tu them vao////////////////// PhanSo PhanSo::operator=(const PhanSo &a) { tuso=a.tuso; mauso=a.mauso; return *this; }
int PhanSo::operator==(PhanSo a) { PhanSo b; b=*this; b.RutGon(); a.RutGon(); int t; (a.tuso==b.tuso)?(a.mauso==b.mauso)?t=1:t=0:t=0; return t; } int PhanSo::operator!=(PhanSo a) { return !((*this)==a); } int PhanSo::operator>(PhanSo a) { return GiaTriThuc()>a.GiaTriThuc(); }
/////////////////////////////////// main() { PhanSo a(3,7),b(4,9); a.InPS();cout<<endl; b.InPS();cout<<endl; PhanSo x,y; cout<<"nhap phan so x \n"; x.NhapPS(); x.InPS();cout<<endl; cout<<"nhap phan so y \n"; y.NhapPS(); y.InPS();cout<<endl; cout<<"phan so nghich dao cua x"<<endl; x.GiaTriNghichDao().InPS();cout<<endl; (a*b).InPS();cout<<endl; (a/b).InPS();cout<<endl; (x+y).InPS(); if(x>y)cout<<"ha"; ///////////////////////////////
ThueBaoDD::ThueBaoDD(const ThueBaoDD &t) { strcpy(sdt,t.sdt); strcpy(cmnd,t.cmnd); hoten=new char[100]; strcpy(hoten,t.hoten); ngayhh=t.ngayhh; sotien=t.sotien; } ThueBaoDD::~ThueBaoDD() { delete[] hoten; } ThueBaoDD &ThueBaoDD::operator=(const ThueBaoDD &t) { delete[] hoten; strcpy(sdt,t.sdt); strcpy(cmnd,t.cmnd); hoten=new char[100]; strcpy(hoten,t.hoten); ngayhh=t.ngayhh; sotien=t.sotien; return *this; } void ThueBaoDD::Nhap() { cout<<"Nhap so dien thoai : ";fflush(stdin);gets(sdt); cout<<"Nhap so cmnd : ";fflush(stdin);gets(cmnd); cout<<"Nhap ho ten khach hang : ";fflush(stdin);gets(hoten); cout<<"Nhap Ngay HH : ";ngayhh.Nhap(); cout<<"Nhap so Tien : ";cin>>sotien; } void ThueBaoDD::Xuat() { cout<<"so dien thoai : "<<sdt<<endl; cout<<"cmnd : "<<cmnd<<endl; cout<<"Ho ten khach hang : "<<hoten<<endl; cout<<"ngay het han : ";ngayhh.In(); cout<<"So tien : "<<sotien<<endl; } int ThueBaoDD::ConSD(Date HomNay) { return (sotien>0&&HomNay>ngayhh); } void ThueBaoDD::NapTien(Date ngaynap, long sotien) { if(sotien!=100000 && sotien!=200000 && sotien!=300000 && sotien!=500000) {
cout<<"So tien nap khong dung quy dinh"<<endl; return; } this->sotien += sotien; if(ngaynap>ngayhh) ngayhh=ngaynap; switch(sotien) { case 100000: ngayhh=ngayhh+30;break;//30, 70, 115, 215 ngày case 200000:ngayhh=ngayhh+70;break; case 300000:ngayhh=ngayhh+115;break; case 500000:ngayhh=ngayhh+215;break; }
}
Date ThueBaoDD::HanNghe() { return ngayhh+10; } long ThueBaoDD::Goi(int sogiay) { long kq=138; if(sogiay>6) kq+=(sogiay-6)*23L; sotien-=kq; if(sotien<0) sotien=0; return kq; } ///////////// main() { ThueBaoDD phone; phone.Nhap(); Date ngaynap(17,3,2012); phone.NapTien(ngaynap,100000); phone.Xuat(); phone.Goi(10); phone.Goi(10); phone.Goi(10);phone.Goi(10); phone.Goi(6); cout<<"-------------------------------"; cout<<"sau khi goi : "<<endl; phone.Xuat(); getch(); }
///////////ham chuc nang/////////// char *HocSinh::XepLoai() { float dtb=(toan+van+ngoaingu)/3; if(dtb>=8&&toan>=6.5&&van>=6.5&&ngoaingu>=6.5) return "Gioi"; else if(dtb>=6.5&&toan>=5&&van>=5&&ngoaingu>=5) return "Kha"; else if(dtb>=5&&toan>=3&van>=3&&ngoaingu>=3) return "TrungBinh"; else return "kem"; } /////main////////////////////// main() { int n; cout<<"Nhap so hoc sinh : ";cin>>n; HocSinh hs[100]; for(int i=0;i<n;i++){cout<<"Nhap thong tin cho hoc sinh thu "<<i+1<<endl;hs[i].Nhap();}
cout<<"---------------------------------------------------------\n"; for(int i=0;i<n;i++){cout<<"thong tin cho hoc sinh thu "<<i+1<<endl;hs[i].Xuat();cout<<endl;}cout<<"---------------------------------------------------------\n"; cout<<"hoc sinh kha gioi : "<<endl; for(int i=0;i<n;i++) { int g=strcmpi(hs[i].XepLoai(),"Gioi"); int k=strcmpi(hs[i].XepLoai(),"Kha"); if(g==0||k==0){hs[i].Xuat();cout<<endl;} } cout<<"---------------------------------------------------------\n"; int m; cout<<"Nhap so giao vien : ";cin>>m; GiaoVien gv[100]; for(int i=0;i<m;i++){cout<<"Nhap thong tin cho giao vien thu "<<i+1<<endl;gv[i].Nhap();} cout<<"---------------------------------------------------------\n"; for(int i=0;i<m;i++){cout<<"thong tin cho giao vien thu "<<i+1<<endl;gv[i].Xuat();cout<<endl;} cout<<"Giao vien co tham nien tren 10 nam va chuyen mon LY hoac toan "<<endl; for(int i=0;i<m;i++) { int t,l; t=strcmpi(gv[i].LayChuyenMon(),"toan"); l=strcmpi(gv[i].LayChuyenMon(),"ly"); if(gv[i].ThamNien()>10&&(t==0||l==0)){cout<<"---------------\n"; gv[i].Xuat();}
cout<<"nhap so thuc a ";cin>>a; cout<<"nhap so nguyen k ";cin>>k; d=tong(a,k); t=d+log(abs(k)+fabs(a)+1); cout<<t; getch(); } câu 2#include"iostream.h" #include"conio.h" void main() { int i; float a[50],n,x; //nhâp n voi dieu kien 1<=n<=40; do { cout<<"nhap so phan tu cua mang "; cin>>n; } while(n<1 || n>40); //nhap mang ; for(i=0;i<n;i++) { cout<<"a["<<i<<"]="; cin>>a[i]; } //in mang ra man hinh; cout<<"\n mang vua nhap la: \n"; for(i=0;i<n;i++) cout<<a[i]<<" "; //in ra cac so duong trong mang va gia tri trung binh cong cua chung; int d=0; //khai bao bien dem d; float t=0,tbc; //khai bao bien tong va trung binh cong; cout<<"\n cac so duong trong mang la: \n";// \n de xuong dong; for(i=0;i<n;i++) if(a[i]>0) { cout<<a[i]<<" "; d++; t=t+a[i]; } tbc=(float)t/d; cout<<"\n gia tri trung binh cong là: "<<tbc; //phan cuoi ne; cout<<"\n nhap so x \n"; cin>>x; for(i=0;i<n;i++) break if(a[i]==x) { cout<<x<<" co trong mang \n"; d=1; ;//co x xuat hien roi thi dung vong for; } if(d!=1) { for(i=n-1;i>=0;i--) a[i+1]=a[i]; a[0]=x; cout<<"mang sau khi chen them x la \n"; for(i=0;i<=n;i++) cout<<a[i]<<" ";
} getch(); }
câu 3#include"iostream.h"#include"conio.h"#include"string.h"#include"stdio.h"int kt(char a[]){if(a[0]==' ') return 1;elsefor(int i=0;i<strlen(a);i++)if(a[i]==a[i+1] && a[i+1]==' ')return 1;return 0;}void main(){char a[100];int i,j,d=0,k=0;do{cout<<"ten no la gi ";fflush(stdin);gets(a);if(kt(a)==1) cout<<"nhap lai ten di may \n";}while(kt(a)==1);cout<<"\nten no la "<<a<<" ha ";cout<<"\nma may nho la lan sau phai viet hoa nhu the nay nha: "<<strupr(a)<<endl;cout<<"viet moi chu mot dong cho oai ;))\n";for(i=0;i<strlen(a);i++)if(a[i]!=' ')k++;else break;for(i=0;i<k;i++)cout<<a[i];for(i=k;i<strlen(a);i++)if(a[i]==' ' && a[i+1]!=' '){cout<<endl;for(j=i+1;j<strlen(a);j++)if(a[j]!=' ') cout<<a[j];else break;}getch();}nêu xem nó như 1 con trỏ chứa địa chỉ dữ liệu (gồm nhiều thông tin) chứ không phải là mảng đây là 1 bài ví dụ
: Giải bài toán sau bằng cách viết các hàm con- Nhập danh sách hành hóa (tên hàng, số lượng, đơn giá, thành tiền= đơn giá*số lượng)- In danh sách ra màn hình- Tìm thông tin về mặt hàng có số lượng nhiều nhất- Tìm thông tin về mặt hàng có đơn giá đắt nhất- Xóa bỏ các mặt hàng có số lượng bằng 0- Sắp xếp theo thứ tự tăng dần của thành tiền
hanghoa hv[100];void nhap(int i ){int sl,dg;printf("Nhap mat hang thu %d",i+1);printf("\nTen hang:");gets(hv[i].tenhang);printf("Don gia(DV 1000d):");scanf("%d",&dg); hv[i].dongia=dg;printf("So luong:");scanf("%d",&sl);hv[i].soluong=sl;hv[i].thanhtien=hv[i].dongia*hv[i].soluong;printf("Thanh tien la:%d000 VND\n",hv[i].thanhtien);return;}void in(int i){printf("%12c",hv[i].tenhang);printf("%9d000 VND",hv[i].dongia);printf("%9d",hv[i].soluong);printf("%13d000 VND\n",hv[i].thanhtien); }void tim(int n) {int j,tg; tg=hv[0].soluong;for(j=1;j<n;j++)if(hv[j].soluong>tg) tg=hv[j].soluong;for(j=0;j<n;j++)if(hv[j].soluong==tg){printf("\nHang hoa co so luong nhieu nhat la:\n");printf("\n Ten hang Don gia So luong Thanh tien\n");in(j);}}void gia(int n) {int j,tg; tg=hv[0].dongia;for(j=1;j<n;j++)if(hv[j].dongia>tg) tg=hv[j].dongia;for(j=0;j<n;j++)if(hv[j].dongia==tg){printf("\nHang hoa co don gia dat nhat la:\n");printf("\n Ten hang Don gia So luong Thanh tien\n");in(j);}}void sx(int n){int i,j,tg;for(i=0;i<n-1;i++)
for(j=i+1;j<n;j++)if(hv[i].thanhtien>hv[j].thanhtien)i=j;for(i=0;i<n;i++)in(i);}main(){int n,i; printf("So luong hang hoa can nhap:");scanf("%d",&n);for(i=0;i<=n-1;i++) nhap(i);printf("\nDanh sach hang la:\n");printf("\n Ten hang Don gia So luong Thanh tien\n");for(i=0;i<=n-1;i++) in(i);tim(n); gia(n);printf("\nDanh sach hang co so luong khac 0 la:\n");printf("\n Ten hang Don gia So luong Thanh tien\n");for(i=0;i<=n-1;i++)if(hv[i].soluong!=0) in(i);printf("\nDanh sach hang theo thu tu tang dan cua thanh tien la:\n");printf("\n Ten hang Don gia So luong Thanh tien\n");sx(n);getch();}