Upload
winsieutoc
View
38
Download
4
Embed Size (px)
Citation preview
Bi tp ngn ng lp trnhH v tn: Mai Xun Ha
MSSV :20111596
Lp : in t 07
1. Chng trnh in ma trn#include #include using namespace std;int main(){ int m,n; coutm>>n; for(int i=0, k=1; i
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
template void Matrix::CreateData(){
data = new _T* [rows];for(int i=0; i
template Matrix Matrix::operator+(const Matrix& M){
Matrix temp(rows,cols);if(rows==M.rows&&cols==M.cols){
for(int i=0; i
}}
int main(){
Matrix A(3,3);Matrix B(3,3);A=B;cout
{ 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(){
- Hm hin th xu v di xu - Hm ni xu cng hai xu (xaukytu s) - Hm hu .Vit chng trnh kim tra
SOLUTION 3:
File xaukytu.h#ifndef xaukytu_h#define xaukytu_hclass xaukytu{ private: int length; char * str; public: xaukytu(char * s); xaukytu(); void htxau(); xaukytu noixau(xaukytu s1); ~xaukytu();};#endifFile xaukytu.cpp #include #include "xaukytu.h"#include #include xaukytu::xaukytu(){ str=" "; length=0; cout
}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: To mt lp vector gm c cc thnh phn sau: - Cc thuc tnh : float * v; int n - Hm thit lp khng tham s - Hm thit lp mt tham s - Hm thit lp hai tham s - Hm hin th - Hm huVit mt chng trnh kim tra.
SOLUTION 4:#include #include class vector
cnh ca mt tam gic) + Hm tnh din tch tam gic + Hm kim tra tam gic(u, vung cn, cn, vung, thng) + Hm hin th thng tin( din tch, tnh cht tam gic) Vit mt chng trnh kim tra.
SOLUTION 5:#include #include #include class tamgiac{ private: int a,b,c; float dientich(); int kttamgiac(); public: void nhap(); void in();};void tamgiac::nhap(){ do { couta; coutb; coutc; }while(a+b
giy.Lp Time c cc thnh phn sau:- Cc thuc tnh m t gi, pht, giy;- Cc hm thnh phn dng xc lp gi tr cho tng thnh phn
gi, pht, giy(C kim tra iu kin gi (0->23), pht(0->59), giy(0->59);
- Hm thnh phn setTime(int,int,int) xc lp thi gian- Hm hin th gi theo nh dng 24 ting (vd : 23:54:40);- Hm hin th gi theo nh dng 12 ting( vd : 11:54:40 PM);- Hm tngGiy() tng thi gian mi ln ln mt giy. Ch cc
trng hp tng sang pht tip theo, tng sang gi tip theo,tng sang ngy tip theo.
Vit chng trnh chnh khai bo mt i tng thi gian l 23:59:58 v thc hin tng thi gian 5 giy ng thi hin th thi gian cho mi ln tng.
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) {
PROGRAM 7: Vit mt chng trnh xy dng hai lp: mt lp th sinh v mt lp danh sch th sinh. Trong lp th sinh c d liu bao gm cc thng tin: s bo danh, im ton, im ho, im l. Lp danh sch th sinh c d liu mt mng cc th sinh v s lng phn t thuc mng . Vit chng trnh thc hin cc cng vic sau:1. Nhp v hin th mt danh sch cc th sinh t bn phm2. Sp xp danh sch cc th sinh theo th t tng dn v im s3. Hin th thng tin ca cc sinh vin c tng im trn 18
SOLUTION 7:#include #include class thisinh{ private: char sbd[25]; float dtoan,dly,dhoa; public: void nhap(); void in(); float tdiem();};void thisinh::nhap(){ coutsbd; coutdtoan; coutdly; coutdhoa;}void thisinh::in(){ cout
PROGRAM 12:
To lp im (point) c thuc tnh l cc to x, y.Xy dng cc hm cn thit nh nhp v hin th cc to , tnh khong cch gia hai im.Vit chng trnh nhp vo n im t bn phm.Tm v hin th khong cch ln nht gia hai im trong n im .
SOLUTION 12:
#include "conio.h"#include "math.h"class point{ int x,y; public: void nhap(){ coutx; couty;}void in(){ cout
class hinhvuong:public haichieu{ float a; public: hinhvuong(float a1) { a=a1; } float chuvi() { return 4*a; } float dientich() { return a*a; } char *ten() { return " hinh vuong "; }}; class tamgiacdeu:public haichieu{ float a; public: tamgiacdeu(float a1) { a=a1; } float chuvi() { return 3*a; } float dientich() { return a*a*sqrt(3)/2; } char *ten() {
return " hinh tam giac deu"; }}; class hinhcau:public bachieu{ float r; public: hinhcau(float r1) { r=r1; } float thetich() { return(r*r*r*M_PI/4) ; } float dientich() { return r*r*4*M_PI; } char *ten() { return " hinh cau "; }}; class lapphuong:public bachieu{ float a; public: lapphuong(float a1) { a=a1; } float thetich() { return(a*a*a) ; } float dientich() { return a*a*6; }
char *ten() { return " hinh lap phuong "; }}; void main(){ hinhvuong hv(3); hv.in(); hinhtron ht(4); ht.in(); tamgiacdeu tgd(5); tgd.in(); lapphuong lp(3); lp.in(); hinhcau hc(2); hc.in(); getch();}PROGRAM 15: Khun hnh (template)(C++20) (02/11/2006) Chng trnh s dng khun hnh (template) thc hin vic tm phn s nh nht, sau a kt qu ln mn hnh.
#include "iostream.h"#include "conio.h"template T min(T a, T b){ if (a
PS p1(1,2),p2(3,3); min(p1,p2).display();getch();}
PROGRAM 16:
Khun mu lp C++(24) (15/11/2006) S dng khun hnh lp xy dng lp List c cc tnh cht sau:- Cc thuc tnh :T a[max], int size;- Cc phng thc : Chn, xo , tm kim, sp xp cc phn
t ca mt mng#include #include templateclass list{ private: T a[max]; int size; public: list(); int get_size(); bool is_empty(); void insert(int index, T &itemp); void remove(int index); T retrieve(int index); int find_max(int start,int end); int search(T & itemp); void append(T &itemp); void sort(int start,int end); }; templatelist::list(){ size=0;} templatevoid list::append(T &itemp)
Vit chng trnh ng dng cc hm trn
3. Vit mt hm ng thi tm gi tr ln nht v gi tr nh nht ca 1 mng.Vit 1 chng trnh ng dng hm .
SOLUTION 21:1. Hm tm gi tr ln nht ca 1 mng
int gtln (int *a,int n){int *q;q=a;int max=*q;while (qmax) max=*q;q++;
};
return max;}
Chng trnh ng dng hm trn:
#include "stdafx.h"#includeusing namespace std;void nhap (int *a, int n);int gtln (int *a,int n);int _tmain(int argc, _TCHAR* argv[]){ int a[100],n; int *p;
p=a;
do { cout
//ham nhapvoid nhap (int *a,int n){
int *p;p=a; cout
void nhap (int *a, int n);int diachi (int *a,int n);int main(){ int a[100],n; int *p;p=a;
do{cout
-------------------------------------------------------------------------
PROGRAM 22: CC TIN CH V CHUI (STRING)
#include
#include
using namespace std;
class String
{
private:char* str;
char* tmp; //Khai bao cac bien string
long length;
long l;
public://Empty constructor;
String(){str=""; length=0; tmp=new char[1];} //tmp is created as new as to delete in Destructor
//String Constructor
// Assign the string passed
String(char* s){ length=strlen(s); l=length+1;
str=new char[l]; strcpy(str,s);
*(str+length)='\0'; tmp=new char[1];}
//Destructor
~String() { delete str; delete tmp; }
//Length of the string
long Length(){length=strlen(str);return length;}
//like in Java, toString returns the entire string
char* toString(){ return str;};
//Get the substring providing start and length parameters
char* subString(long stt,long len);
//Get the wordcount "default delimiter is spaces and CRLF
long WordCount();
//Get the location of a particular string or character inside the actual string
long getCharLoc(char*,long);
//set this string overriding the initial
void setString(char* s)
{
delete str;
length=strlen(s); l=length+1 ;
str=new char[l]; strcpy(str,s);
*(str+length)='\0'; tmp=new char[1];
}
};
long String::getCharLoc(char* sch,long stt)
{
long i,j;
long schlen=strlen(sch);
if (stt< 1 || stt>length)
return 0;
else
{
for(j=0;j
return 0;
}
}
char* String::subString(long stt,long len){
long a,b;
if( stt>length || len > length || (length-stt+1)
}int main()
{
//Define default string
String x("Hello world This is new C++");
//show the string to check
cout
Bull ****
The new string length is 9
-------------------------------------------------------------------------
PROGRAM 23: CLASS STRING T NH NGHA
#include
#include
#include
#include
class String
{
private:
char *Ptr; //Con tro tro den diem bat dau cua chuoi
int Length; //Chieu dai chuoi
public:
String(const char * = ""); //Constructor chuyen doi
String(const String &); //Constructor sao chep
~String(); //Destructor
const String &operator=(const String &);
//Phep gan
String &operator+=(const String &);
int operator!() const;
int operator==(const String &) const; //So sanh bang`
int operator!=(const String &) const; //So sanh khong bang
int operator(const String &) const; //So sanh lon hon
int operator>=(const String &) const; //So sanh >=
int operator
return *this;
}
String &String::operator+=(const String &Right)
{
char *TempPtr = Ptr;
Length += Right.Length;
Ptr = new char[Length + 1];
assert(Ptr != 0);
strcpy(Ptr, TempPtr);
strcat(Ptr, Right.Ptr);
delete [] TempPtr;
return *this;
}
int String::operator!() const
{
return Length == 0;
}
int String::operator==(const String &Right) const
{
return strcmp(Ptr, Right.Ptr) == 0;
}
int String::operator!=(const String &Right) const
{
return strcmp(Ptr, Right.Ptr) != 0;
}
int String::operator(const String &Right) const
{
return strcmp(Ptr, Right.Ptr) > 0;
}
int String::operator>=(const String &Right) const
{
return strcmp(Ptr, Right.Ptr) >= 0;
}
int String::operator
}
String &String::operator()(int Index, int SubLength)
{
assert(Index >= 0 && Index < Length && SubLength>= 0);
String *SubPtr = new String;
assert(SubPtr != 0);
if ((SubLength == 0) || (Index + SubLength > Length))
SubPtr->Length = Length - Index + 1;
else
SubPtr->Length = SubLength + 1;
delete SubPtr->Ptr;
SubPtr->Ptr = new char[SubPtr->Length];
assert(SubPtr->Ptr != 0);
strncpy(SubPtr->Ptr, &Ptr[Index], SubPtr->Length);
SubPtr->Ptr[SubPtr->Length] = '\0';
return *SubPtr;
}
int String::GetLength() const
{
return Length;
}
ostream &operator
return Output;
}
istream &operator>>(istream &Input, String &S)
{
char Temp[100];
Input >> setw(100) >> Temp;
S = Temp;
return Input;
}
int main()
{
String S1("happy");
String S2(" birthday") ;
String S3;
cout
return 0;
}
PROGRAM 24: To mt lp " a thc mt bin "; bao gm " bc a thc " , " cc h s a thc "; Cc php ton : cng , tr, nhn a thc; tnh gi tr a thc vi gi tr c th ca bin nhp vo, ghi ra mn hnh kt qu cc php ton. Vit chung trnh s dng class trn, trong cha menu la cc php ton.
SOLUTION 24:
#include#include#include
//************************************************************class node{ int heso; int mu; public :
node *next; node(int hs,int m,node *n ):heso(hs),mu(m),next(n) {} int get_heso() { return heso; } int get_mu() { return mu; }
};//*******************************************************************
class dathuc{private :
node *first,*last; int bac; public :
dathuc() {
first=NULL; last=NULL;
} void chensau(int a,int b) { node *p=new node(a,b,NULL);
if (first==NULL) { first=p;
last=p; } else { last->next=p; last=p; }
} void nhap() ; void hienthi(); friend dathuc operator+(dathuc ob1,dathuc ob2); friend dathuc operator-(dathuc ob1,dathuc ob2); friend dathuc operator*(dathuc ob1,dathuc ob2);
};
//************************************************************
void dathuc::nhap(){ int n,t; coutbac; for(int i=0;i
dathuc operator+(dathuc ob1,dathuc ob2) { dathuc ob3;
node *p=ob1.first; node *q=ob2.first; int t; while ((p!=NULL) && (q!=NULL)) { if (p->get_mu() == q->get_mu())
{ t= p->get_heso() + q->get_heso(); ob3.chensau(t,p->get_mu()); p=p->next; q=q->next; }
elseif (p->get_mu() > q->get_mu()){ t= p->get_heso(); ob3.chensau(t,p->get_mu()) ; p=p->next;}else
{ t= q->get_heso(); ob3.chensau(t,q->get_mu()); q=q->next;}
} while (p!=NULL)
{ t= p->get_heso(); ob3.chensau(t,p->get_mu()); p=p->next; }
while (q!=NULL) { t= q->get_heso(); ob3.chensau(t,q->get_mu()); q=q->next; }
return ob3; }//********************************************************************** dathuc operator-(dathuc ob1,dathuc ob2) { dathuc ob3;
node *p=ob1.first; node *q=ob2.first; int t; while ((p!=NULL) && (q!=NULL)) { if (p->get_mu() == q->get_mu())
{ t= p->get_heso() - q->get_heso(); ob3.chensau(t,p->get_mu()); p=p->next; q=q->next; }
elseif (p->get_mu() > q->get_mu()){ t= p->get_heso(); ob3.chensau(t,p->get_mu()) ; p=p->next;
}else
{ t= q->get_heso(); ob3.chensau(-t,q->get_mu()); q=q->next;}
} while (p!=NULL)
{ t= p->get_heso(); ob3.chensau(t,p->get_mu()); p=p->next; }
while (q!=NULL) { t= q->get_heso(); ob3.chensau(t,q->get_mu()); q=q->next; }
return ob3; }
//********************************************************************
void main(){ clrscr(); dathuc ob1,ob2,ob3; ob1.nhap(); ob2.nhap(); cout
public: void nhap() { coutx>>y; }
void xuat() { cout
friend ps operator+(int x, ps a)
ps operator-(ps a) { ps kq; kq.t= t*a.m - a.t*m; kq.m= m * a.m; return kq; }
ps operator*(ps a) { ps kq; kq.t= t*a.t; kq.m= m*a.m; return kq; }
ps operator/(ps a) { ps kq; kq.t= t*a.m; kq.m= m*a.t; return kq; } friend ostream &operator(istream &is, ps &a)};
ps operator+(int x, ps a){ ps kq; kq.t=a.t + x*a.m; kq.m=a.m; return kq;}
ostream &operator
couta.t>>a.m; return is;}
void main(){ clrscr(); int x; ps a,b; couta; coutb; cout