diff --git a/practical_exercises/.README.md.un~ b/practical_exercises/.README.md.un~ new file mode 100644 index 0000000..4b629e0 Binary files /dev/null and b/practical_exercises/.README.md.un~ differ diff --git a/practical_exercises/10_day_practice/day1/打印练习.cpp b/practical_exercises/10_day_practice/day1/打印练习.cpp new file mode 100644 index 0000000..83ea075 --- /dev/null +++ b/practical_exercises/10_day_practice/day1/打印练习.cpp @@ -0,0 +1,24 @@ +#include +using namespace std; +int main(int argc, char const *argv[]) +{ + int i,j,k,f; + for (i=1;i<=4;i++){ + for (j=1;j<=30;j++) + cout<<" "; + for (k=1;k<=8-2*i;k++) + cout<<" "; + for (f=1;f<=2*i;f++) + cout<<'*'; + cout< +using namespace std; +int main(int argc, char const *argv[]) +{ + int year; + bool isLeapYear; + cout<<"Enter the year: "; + cin>>year; + isLeapYear = (((year%4==0)&&(year%100!=0))||(year%400==0)); + if(isLeapYear) + { + cout< + + +һָע + + +//һעͷ +#if 0 +asd +#endif + +//ע +//ָ +#if 1 +asData +#endif diff --git a/practical_exercises/10_day_practice/day1/联合体学习.cpp b/practical_exercises/10_day_practice/day1/联合体学习.cpp new file mode 100644 index 0000000..c0adee0 --- /dev/null +++ b/practical_exercises/10_day_practice/day1/联合体学习.cpp @@ -0,0 +1,18 @@ +#include +using namespace std; +//ͬڴַ +union myun +{ + struct { int x; int y; int z; }u; + int k; +}a; +int main() +{ + a.u.x =4; + a.u.y =5; + a.u.z =6; + a.k = 0; //ǵһintռֵ + printf("%d %d %d %d\n",a.u.x,a.u.y,a.u.z,a.k); + system("pause"); + return 0; +} \ No newline at end of file diff --git a/practical_exercises/10_day_practice/day10/readme.md b/practical_exercises/10_day_practice/day10/readme.md new file mode 100644 index 0000000..0929f2c --- /dev/null +++ b/practical_exercises/10_day_practice/day10/readme.md @@ -0,0 +1 @@ +# ļ diff --git a/practical_exercises/10_day_practice/day10/文件例题/12-1.cpp b/practical_exercises/10_day_practice/day10/文件例题/12-1.cpp new file mode 100644 index 0000000..2f1c9e4 --- /dev/null +++ b/practical_exercises/10_day_practice/day10/文件例题/12-1.cpp @@ -0,0 +1,18 @@ +//cinַʱַкпհ׾Ͳ롣ΪհַʱcinΪַˡ +#include +using namespace std; +int main(int argc, char const *argv[]) +{ + char a[50]; + cout<<"please input a string:"; + cin>>a; + cout< +using namespace std; +int main(int argc, char const *argv[]) +{ + char stu[5][10]; + int i; + for(i=0;i<5;i++) + cin.getline(stu[i],10,','); + for(i=0;i<5;i++) + cout< +using namespace std; +//ԭ +//put(char c) +//write(const char*c, int n) +int main(){ + char c; + char a[50]="this is a string..."; + cout<<"use get() input char:"; + while((c=cin.get())!='\n'){ + cout.put(c); + cout.put('\n'); + cout.put('t').put('h').put('i').put('s').put('\n'); + cout.write(a,12).put('\n'); + cout<<"look"<<"\t here!"< +#include +using namespace std; +int main(){ + char c[30]="this is string"; + double d=-1234.8976; + cout< +#include +using namespace std; +int main(int argc, char const *argv[]) +{ + fstream ioFile; + ioFile.open("./a.dat",ios::out); + ioFile<<""<<" "<<76<<" "<<98<<" "<<67<>name; + while(!ioFile.eof()) { + ioFile>>chinese>>math>>computer; + cout<>name; + } + ioFile.close(); + system("pause"); + return 0; +} diff --git a/practical_exercises/10_day_practice/day10/文件例题/12-7.cpp b/practical_exercises/10_day_practice/day10/文件例题/12-7.cpp new file mode 100644 index 0000000..863a30f --- /dev/null +++ b/practical_exercises/10_day_practice/day10/文件例题/12-7.cpp @@ -0,0 +1,21 @@ +//Eg12-7.cpp +#include +#include +using namespace std; +int main(){ + char ch; + ofstream out("/test.dat",ios::out|ios::binary); //L1 + for(int i=0;i<90;i++){ + if(i>0 && (i % 30)==0) + out.put('\n'); + out.put(i); + out.put(' '); + + } + out.close(); + ifstream in("/test.dat",ios::in|ios::binary); + while(in.get(ch)) + cout< +#include +#include +using namespace std; +class Employee{ +private: + int number ,age; + char name[20]; + double sal; +public: + Employee(){} + Employee(int num,char* Name,int Age, double Salary){ + number=num; + strcpy(name,Name); + age=Age; + sal=Salary; + } + void display(){ + cout< +using namespace std; +int main() +{ + char a,b,c,d; + cin.get(a); + cin.get(b); + c = cin.get(); + d = cin.get(); + cout< +using namespace std; +//cin.get(arrayname,size) ַ뵽arraynameУȲsize +int main() +{ + //get() + + //1.봮arraylengthԶarraynameСʹܱ + // char a[10]; + // cin.get(a,20); + // cout<sizeȽȡsizeַǴarraylengthǰarraylength-1ַ󲹳䡮\0 + // char c[5]; + // cin.get(c,10); + // cout<sizeȽȡsizeַСarraylengthѽȡУ󲹳䡮\0 + // char d[10]; + // cin.get(d,5); + // cout< +using namespace std; +/* +1cin.getline(arrayname,size)cin.get(arrayname,size) +cin.get(arrayname,size)[enter]ʱĿǰ룬ɾе[enter] +cin.getline(arrayname,size)[enter]ʱǰ룬ǻɾе[enter] +*/ +int main() +{ + /* + char a[10]; + char b; + cin.get(a,10); + cin.get(b); + cout< +using namespace std; + +int main(int argc, char const *argv[]) +{ + char c[30]="this is string"; + double d = -1231.232; + cout.width(30); + cout.fill('*'); + cout.setf(ios::left); + cout< +#include +//һܹ͵Ķ̬ +#include +#include +using namespace std; +class Person{ + private: + char name[20]; + char id[18]; + int age; + char addr[20]; + public: + Person(){} + Person(char *n,char *pid,int Age,char* Addr){ + strcpy(name,n); + strcpy(id,pid); + age=Age; + strcpy(addr,Addr); + } + void display(){ + cout< v; + vector::iterator pos;//һvectorãָvectorԪ + char ch; + ofstream out("d:/person.dat",ios::out|ios::app|ios::binary); + char Name[20],ID[18],Addr[20]; + int Age; + cout<<"------˵------"<>Name; + cout<<"֤ţ "; + cin>>ID; + cout<<"䣺 "; + cin>>Age; + cout<<"ַ "; + cin>>Addr; + Person per(Name,ID,Age,Addr); + out.write((char*)&per,sizeof(per)); + cout<<"Enter another Person(y/n)?"; + cin>>ch; + }while(ch=='y'); + out.close(); + ifstream in("d:/person.dat",ios::in|ios::binary); //L9 + Person s; + in.read((char*)&s,sizeof(s)); + while(!in.eof()){ + v.push_back(s); + in.read((char*)&s,sizeof(s)); + } + cout<<"\n---------ļж--------"< +using namespace std; +double power(double x, int n); + +int main(int argc, char const *argv[]) +{ + int x; + cin>>x; + int wei=0; + int sum=0; + int each,chu; + for(int i=0;i<8;i++) + { + each=x%10; + chu=x/10; + sum+=each*power(2,wei); + x=chu; + wei++; + } + cout< +#include +using namespace std; +int rolldice(); +int main(int argc, char const *argv[]) +{ + int flag; + unsigned seed; + cout<<"޷"<>seed; + srand(seed); + int sum = rolldice(); + int selfdim; + switch(sum) + { + case 7: + case 11: + flag=1; + break; + case 2: + case 3: + case 12: + flag=2; + break; + default: + flag=0; + selfdim=sum; + break; + } + while(flag==0) + { + sum=rolldice(); + if(sum==selfdim) + { + flag=1; + } + else if(sum==7) + { + flag=2; + } + } + if(flag==1) + { + cout<<"player win\n"; + } + else + { + cout<<"player loses\n"; + } + + system("pause"); + return 0; +} + +int rolldice() +{ + int sum=0; + int dim1 = rand()%6+1; + int dim2 = rand()%6+1; + sum = dim1+dim2; + cout<<"sum="< +using namespace std; + +enum weekday +{ + s,m,t,w,thu,f,s1 +}; + +int main(int argc, char const *argv[]) +{ + enum weekday wek=s; + // weekday wek=s; + for(int i=wek;i!=f;i++) + { + cout< +using namespace std; +double arctan(double x); +int main(int argc, char const *argv[]) +{ + double a = 16.0*arctan(1.0/5.0); + double b = 4.0*arctan(1.0/239.0); + double pi = a-b; + cout<1e-15) + { + art=(tail%4==1)? art+head/tail: art-head/tail; + head*=x*x; + tail+=2; + cout<<"---------------"< +using namespace std; +void move(char A, char B); +void hanoi(int n,char A, char B, char C); +int main(int argc, char const *argv[]) +{ + cout<<""; + int disks; + cin>>disks; + hanoi(disks,'A','B','C'); + system("pause"); + return 0; +} + + +void move(char A, char B) +{ + cout<"< +using namespace std; +struct student +{ + int num; + char name[20]; + char gender; +}; + +int main(int argc, char const *argv[]) +{ + student s={10,"asd",'M'}; + cout< +using namespace std; + +int f(int n); +int main(int argc, char const *argv[]) +{ + cout<<"input x:"; + int x; + cin>>x; + cout< +using namespace std; +int f(int n, int k); +int main(int argc, char const *argv[]) +{ + cout<<"nk"<>n; + cin>>k; + cout< +using namespace std; +int i=1; // i Ϊȫֱо̬ڡ +int main(void) +{ + static int a; // ֲ̬ȫֲɼ + int b=-10; // b, cΪֲж̬ڡ + int c=0; + void other(void); + cout<<"---MAIN---\n"; + cout<<" i: "< +using namespace std; +void swap(int & a, int & b); +int main(int argc, char const *argv[]) +{ + int x1(5); + int x2(7); + swap(x1,x2); + cout< +using namespace std; +// +inline double CalArea(double radius); +int main(int argc, char const *argv[]) +{ + double r(3.0); + double area; + area = CalArea(r); + cout< +using namespace std; +const float PI=3.14159; +const float FencePrice=35; +const float ConcretePrice=20; + +class Circle +{ + private: + float radius; + public: + Circle(float r); + float Circumference() const; + float Area() const; +}; +Circle::Circle(float r) +{ + radius=r; +} + +// Բܳ +float Circle::Circumference() const +{ + return 2 * PI * radius; +} + +// Բ +float Circle::Area() const +{ + return PI * radius * radius; +} + +int main(int argc, char const *argv[]) +{ + float radius; + float FenceCost, ConcreteCost; + + // ʾû뾶 + cout<<"Enter the radius of the pool: "; + cin>>radius; + + // Circle + Circle Pool(radius); + Circle PoolRim(radius + 3); + // դ۲ + FenceCost = PoolRim.Circumference() * FencePrice; + cout << "Fencing Cost is " << FenceCost << endl; + + // ۲ + ConcreteCost = (PoolRim.Area() - Pool.Area())*ConcretePrice; + cout << "Concrete Cost is " << ConcreteCost << endl; + system("pause"); + return 0; +} diff --git a/practical_exercises/10_day_practice/day3/类前向声明.cpp b/practical_exercises/10_day_practice/day3/类前向声明.cpp new file mode 100644 index 0000000..e2483b5 --- /dev/null +++ b/practical_exercises/10_day_practice/day3/类前向声明.cpp @@ -0,0 +1,40 @@ +/* +ʹǰȻԽһЩ⣬ܵġҪעǣ +ʹǰṩһ֮ǰĶ +ҲԱʹøĶ뿴ijΣ +*/ + +//һ +#include +class Fred; //ǰ +class Barney { + Fred x; //Fredв +}; +class Fred { + Barney y; +}; + + +//ڶ +class Fred; //ǰ + +class Barney { + public: + void method() + { + x->yabbaDabbaDo(); //FredĶڶ֮ǰʹ + } + private: + Fred* x; //ȷǰFredĶָ + }; + +class Fred { + public: + void yabbaDabbaDo(); + private: + Barney* y; +}; + +/* +ܽ᣺ʹǰʱֻʹñķţ漰κϸڡ +*/ \ No newline at end of file diff --git a/practical_exercises/10_day_practice/day3/静态成员函数1.cpp b/practical_exercises/10_day_practice/day3/静态成员函数1.cpp new file mode 100644 index 0000000..3edfa35 --- /dev/null +++ b/practical_exercises/10_day_practice/day3/静态成员函数1.cpp @@ -0,0 +1,30 @@ +/* +֪ʶ㣺 +̬Ա +ʹþ̬Ա +̬Աֻڸľ̬ݳԱ̬Ա +*/ +#include +using namespace std; +class Application +{ +public: + static void f(); + static void g(); +private: + static int global; +}; +int Application::global=0; +void Application::f() +{ global=5;} +void Application::g() +{ cout< +using namespace std; +class A +{ + public: + static void f(A a); + private: + int x; +}; +void A::f(A a) +{ + + //̬Աֻڸľ̬ݳԱ̬Ա + // cout< +using namespace std; +class Point +{ +public: + Point(int xx=0, int yy=0) {X=xx; Y=yy; countP++; } + Point(Point &p); + int GetX() {return X;} + int GetY() {return Y;} + void GetC() {cout<<" Object id="< +using namespace std; +void display(const double& r); + +class A +{ + public: + A(int i,int j) {x=i; y=j;} + private: + int x,y; +}; +int main() +{ double d(9.5); + display(d); + A const a(3,4); //adz󣬲ܱ + system("pause"); + return 0; +} +void display(const double& r) +//βΣںвܸ rõĶ +{ cout< +using namespace std; +class R +{ public: + R(int r1, int r2){R1=r1;R2=r2;} + //constֳԱغ + void print(); + void print() const; + private: + int R1,R2; +}; +/* +Ա˵ʽ˵ const; +constǺ͵һɲ֣ʵֲҲҪconstؼ֡ +constؼֿԱڲغ +ֻͨܵijԱ +*/ + +void R::print() +{ + cout<<"ͨ"< +#include +using namespace std; +class Point{ + public: + Point(int x=0,int y=0):X(x),Y(y){} + int GetX(){ + return X; + } + int GetY(){ + return Y; + } + friend float Distance(Point &a,Point &b); + private: + int X,Y;//˽ݳԱ +}; +//ͨһģΪһģԪһģܹõһģбDZصϢ +float Distance(Point &a, Point &b){ + double dx = a.X-b.X; + double dy = a.Y-b.Y; + return sqrt(dx*dx+dy*dy); +} + +int main() +{ + Point p1(3.0,5.0),p2(4.0,6.0); + cout<<"Ϊ"< +using namespace std; +/* +һΪһԪгԱܷʶԷ˽гԱ +﷨Ԫһʹfriend˵ +*/ + +/* +BAԪBijԱͿԷA˽кͱݣ +AijԱȴܷB˽Сݡ +*/ +class A{ + friend class B; + public: + void Display(){ + cout< +#include"clock.h" +using namespace std; +Clock::Clock(int NewH,int NewM,int NewS) +{ + this->Hour=NewH; + this->Minute=NewM; + this->Second=NewS; +} +Clock::Clock(Clock &c) +{ + this->Hour=c.Hour; + this->Minute=c.Minute; + this->Second=c.Second; +} +void Clock::SetTime(int NewH,int NewM,int NewS) +{ + //Ӳthisָ붼һ + this->Hour=NewH; + this->Minute=NewM; + this->Second=NewS; +} +void Clock::ShowTime() +{ + cout<Hour<Minute<Second< +using namespace std; +class Time{ +private: + int hh,mm,ss; +public: + Time(int h=0,int m=0,int s=0):hh(h),mm(m),ss(s){} + void operator()(int h,int m,int s) { + hh=h; + mm=m; + ss=s; + } + void ShowTime(){ + cout< +using namespace std; +class Time{ + public: + Time(int h=0,int m=0,int s=0){ + hour = h; + minute = m; + second = s; + } + void operator++(); + void showTime(){ + cout<<"ǰʱΪ"< +using namespace std; +class A { + int a; +public: + void setA(int x){ a=x; } + int getA(){ return a;} +}; +class B:public A{ + int b; +public: + void setB(int x){ b=x; } + int getB(){ return b;} +}; +void f1(A a, int x) +{ a.setA(x); } +void f2(A *pA, int x) +{ pA->setA(x); } +void f3(A &rA, int x) +{ rA.setA(x); } + +int main(){ + A a1,*pA; + B b1,*pB; + a1.setA(1); + b1.setA(2); + //ֵ + a1=b1; + cout<setA(20); + cout<getA()<getA()< +using namespace std; +class A { + int x; +public: + A(int i=0) { + x=i; + cout << "A-----"< +using namespace std; + +/* +ȹԱ +ٹù캯 +*/ + +class A { +public: + A() { cout<<"Constructing A"< +using namespace std; +class A { +public: + A(){ cout<<"Constructing A"< +using namespace std; +class Base{ +private: + int x; +public: + Base(int a){ + x=a; + cout<<"Base constructor x="< +using namespace std; +class A { +public: + A() { cout<<"Constructing A"< +using namespace std; +class Point{ +protected: + int x,y; +public: + Point(int a,int b=0) { + x=a; y=b; + cout<<"constructing point("< +class B +{ + private: + int i; + protected: + int j; + public: + int k; +}; +class D: public B +{ +public: + void f() + { + i=1;//cannot access ಻ɷʻ˽гԱ + j=2;//ԷʻౣԱ + k=3; + } +}; +int main() +{ + B b; + b.i=1;//cannot access ˽гԱʹ߲ܷ + b.j=2; //cannot access Աʹ߲ܷ + b.k=3; + system("pause"); + return 0; +} + diff --git a/practical_exercises/10_day_practice/day5/继承访问权限/保护继承.cpp b/practical_exercises/10_day_practice/day5/继承访问权限/保护继承.cpp new file mode 100644 index 0000000..a269f4e --- /dev/null +++ b/practical_exercises/10_day_practice/day5/继承访问权限/保护继承.cpp @@ -0,0 +1,37 @@ +/* +ʽΪprotectedļ̳гΪ̳Уּ̳зʽ£ +publicԱлprotectedԱ +protectedprivateԱбԭķȨ +*/ +#include +using namespace std; +class Base{ + int x; +protected: + int getx(){ return x; } +public: + void setx(int n){ x=n; } + void showx(){ cout< +using namespace std; +class base{ + int x; +public: + void setx(int n){ x=n; } + int getx(){ return x; } + void showx() { cout< +using namespace std; +class Base{ + int x; +public: + void setx(int n){x=n; } + int getx(){return x; } + void showx(){cout< +using namespace std; +class A { +public: + void vf() { + cout<<"I come from class A"< +using namespace std; +class A { +public: + void vf() { + cout<<"I come from class A"< +using namespace std; +class A { + int a; +public: + A(int x) { + a=x; + cout<<"Virtual Bass A..."< +using namespace std; +class A { + int a; +public: + A(){ cout<<"Constructing A"< +using namespace std; +class A { + int x; +public: + A(int aa) { + x=aa; + cout<<"Constructing A"< +using namespace std; +class Figure{ + protected: + double x,y; + public: + void set(double i, double j){ + x=i;y=j; + } + virtual void area()=0; +}; + +class Trianle:public Figure{ + public: + void area(){ + cout<<""<area(); + r.set(10,20); + pF=&r; + pF->area(); + // + Figure &rF=t; + rF.set(20,20); + rF.area(); + system("pause"); + return 0; +} + diff --git a/practical_exercises/10_day_practice/day6/虚函数/从基类继承的成员将访问到派生类版本.cpp b/practical_exercises/10_day_practice/day6/虚函数/从基类继承的成员将访问到派生类版本.cpp new file mode 100644 index 0000000..3caf8f6 --- /dev/null +++ b/practical_exercises/10_day_practice/day6/虚函数/从基类继承的成员将访问到派生类版本.cpp @@ -0,0 +1,17 @@ +#include +using namespace std; +class B{ +public: + void f(){ g(); } + virtual void g(){ cout << "B::g"; } +}; +class D : public B{ +public: + void g(){ cout << "D::g\n"; } +}; +int main(){ + D d; + d.f(); + system("pause"); + return 0; +} diff --git a/practical_exercises/10_day_practice/day6/虚函数/编程实例.cpp b/practical_exercises/10_day_practice/day6/虚函数/编程实例.cpp new file mode 100644 index 0000000..4e6be99 --- /dev/null +++ b/practical_exercises/10_day_practice/day6/虚函数/编程实例.cpp @@ -0,0 +1,92 @@ +/* +ij˾оԱСʱȶԱܼнԱÿµн800Ԫ +Ȼɣÿ1Ʒȡ5%СʱСʱнÿԱ֤ŵݡ +Ϊ⣬ѸԱĹϢɻEmployeeԱ̳иĹܡ +*/ +#include +#include +using namespace std; +class Employee{ + public: + Employee(string Name ,string id){ name=Name; Id=id; } + string getName(){ return name; } // + string getID(){ return Id; } //֤ + float getSalary(){ return 0.0; } //нˮ + void print(){ //֤ + cout<<": "<>week; + Manager m("С","11111111",week); + m.print(); + cout<<""; + int profit; + cin>>profit; + cout<<"ۼ"; + int x; + cin>>x; + SaleWorker s("С","222222",profit,x); + s.print(); + cout<<"빤Сʱ"; + int hour; + cin>>hour; + HourWorker h("С","333333",hour); + h.print(); + system("pause"); + return 0; +} \ No newline at end of file diff --git a/practical_exercises/10_day_practice/day6/虚函数/虚函数例子.cpp b/practical_exercises/10_day_practice/day6/虚函数/虚函数例子.cpp new file mode 100644 index 0000000..1650141 --- /dev/null +++ b/practical_exercises/10_day_practice/day6/虚函数/虚函数例子.cpp @@ -0,0 +1,66 @@ + +//Eg7-1.cpp +//ָָʱ,麯麯 +//EmployeeprintΪ麯ɷʵManagerprint麯ֻܷʵEmployeeprint +#include +#include +using namespace std; +class Employee{ + public: + Employee(string name, string id); + string getName(); + string getId(); + float getSalary(); + virtual void print(); + private: + string Name; + string Id; +}; +Employee::Employee(string name,string id){ + Name=name; + Id=id; +} +string Employee::getName(){ + return Name; +} +string Employee::getId(){ + return Id; +} +float Employee::getSalary(){ + return 0.0; +} +void Employee::print(){ + cout<<""<print(); + Employee &rM=m; + rM.print(); + system("pause"); + return 0; +} +//VirtualؼʵǸ֪ϵͳָΪvirtualĺö̬ʽ롣 diff --git a/practical_exercises/10_day_practice/day6/虚函数/虚函数特性.cpp b/practical_exercises/10_day_practice/day6/虚函数/虚函数特性.cpp new file mode 100644 index 0000000..d4e3d6c --- /dev/null +++ b/practical_exercises/10_day_practice/day6/虚函数/虚函数特性.cpp @@ -0,0 +1,30 @@ +#include +#include +using namespace std; +class A { +public: + void f(int i){cout<<"A"<f(1); //A::f + pB=&b; pB->f(1); //A::f + pB=&c; pB->f(1); //A::f + pB=&d; pB->f(1); //A::f + system("pause"); + return 0; +} diff --git a/practical_exercises/10_day_practice/day6/虚函数/虚析构函数.cpp b/practical_exercises/10_day_practice/day6/虚函数/虚析构函数.cpp new file mode 100644 index 0000000..489ddd9 --- /dev/null +++ b/practical_exercises/10_day_practice/day6/虚函数/虚析构函数.cpp @@ -0,0 +1,22 @@ + +#include +using namespace std; +class A{ +public: + virtual ~A(){ cout<<"call A::~A()"< +using namespace std; +class Time{ +private: + int hh,mm,ss; +public: + Time(int h=0,int m=0,int s=0):hh(h),mm(m),ss(s){} + void operator()(int h,int m,int s) { + hh=h; + mm=m; + ss=s; + } + void ShowTime(){ + cout< +using namespace std; +class X{ +public: + X &operator = (const X & x) + {cout << "a:"; return *this;}; +}; +int main () +{ + X obj1, obj2, obj3; + obj1 = obj2; //ء= + obj1.operator= (obj2); //ء= + obj1 = obj2 = obj3; //ء= + system("pause"); +} diff --git a/practical_exercises/10_day_practice/day7/[]与()例题/example1.cpp b/practical_exercises/10_day_practice/day7/[]与()例题/example1.cpp new file mode 100644 index 0000000..58a2827 --- /dev/null +++ b/practical_exercises/10_day_practice/day7/[]与()例题/example1.cpp @@ -0,0 +1,21 @@ +#include +using namespace std; +class X +{public: + int operator() (int i=0) + { cout << "X::operator(" << i << ")" << endl; return i; }; + int operator() (int i, int j) + { cout << "X::operator(" << i << "," << j << ")" << endl; + return i; }; + int operator[] (int i) + { cout << "X::operator[" << i << "]" << endl; return i; }; + int operator[] (char * cp) + { cout << "X::operator[" << cp << "]" << endl; return 0; }; +}; +int main (void) +{ X obj; int i = obj (obj (1), 2); + int a = obj[i]; int b = obj["abcd"]; + cout<<"a="< +#include +using namespace std; +struct Person{ //ְϢĽṹ + double salary; + char *name; +}; +class SalaryManaege{ + Person *employ; //ְϢ + int max; //±Ͻ + int n; //еʵְ +public: + SalaryManaege(int Max=0){ + max=Max; + n=0; + employ=new Person[max]; + } + double &operator[](char *Name) { //[] + Person *p; + for(p=employ;pname,Name)==0) + return p->salary; + // + p=employ + n++; + p->name=new char[strlen(Name)+1]; + strcpy(p->name,Name); + p->salary=0; + return p->salary; + } + + void display(){ + for(int i=0;i> + +һضԪ + +1Ԫĵʽ + +aa@bb ɽͳ aa.operator@(bb) + ͳ operator@(aa,bb) + +߶ж,Ͱؽ + +```c++ +class X{ +public: + void operator+(int); + X(int); +}; +void operator+(X,X); +void operator+(X,double); +``` +2ʽ + +1Ǿ̬Ա + +Աʽصʵʲһ1thisָʽݵġ + +```c++ +class Complex{ + double real,image; +public: + Complex operator+(Complex b){} +...... +}; +``` +2 Ԫ + +ΪԪأҪIJʵҪIJͬ磬ԪComplexļӷʽ£ + +```c++ +class Complex{ + + friend Complex operator+(Complex a,Complex b); // +//...... +}; + +Complex operator+(Complex a,Complex b){} // +``` + +һԪ + +1һԪ + +һԪֻҪһȡַ&?ӣ++ȡ + +2һԪʽΪ + +@a a@ //ʽʽ + +a.operator@() // ʽһԪ@ + +е@һԪa + +@aǰ׺һԪ㣬硰++a + +a@ʾ׺㣬硰a++ + +3@aC++Ϊʽ֮һ + +a.operator@() + +operator@(a) + +4.һԪΪԱʱҪʽ£ + +```c++ +class X{ + + T operator@(){}; +} +``` +T@ķ͡ʽϿΪԱصһԪûвʵһ + +öthisָ롣 + +5.ǰ()() + +C++ͨǷؼint ַʽ + +```c++ +//ǰ׺ +operator -- (); +operator -- (X & x); +//׺ +operator -- (int); +operator -- (X & x, int); +``` +ظֵ= + +1ֵ= + +ֵʱô + +ֻóԱ + +ҪûжʱԶɣð汾bit-by-bit + +ġظֵ[] + +1[ ]һԪʽ£ + +```c++ +class X{ + + X& operator[](int n); +}; +``` +2[]Ҫע +- []һԪ1ͨthisָ봫ݵģ2± +- []ȿԳڸֵ=ߣҲԳڸֵ=ұߣ[]ʱá +- **[]ֻܱΪķǾ̬ԱܱΪԪͨ** + +塢( ) + +1( )ǺҲܱءֻܱΪijԱ + +2( )ʽ£ + +```c++ +class X{ + + X& operator( )(); +} +``` + +еIJ԰ + +3( )ĵʽ£ + +X Obj; // + +Obj()(); //ʽ1 + +Obj(); //ʽ2 \ No newline at end of file diff --git a/practical_exercises/10_day_practice/day7/一元运算符/秒钟自增运算.cpp b/practical_exercises/10_day_practice/day7/一元运算符/秒钟自增运算.cpp new file mode 100644 index 0000000..1a63193 --- /dev/null +++ b/practical_exercises/10_day_practice/day7/一元运算符/秒钟自增运算.cpp @@ -0,0 +1,76 @@ +//һʱTimeܹӵ㡣 + +#include +using namespace std; + +class Time{ + private: + int hour,minute,second; + public: + Time(int h,int m, int s); + Time operator++(); + //ԪҪ + friend Time operator--(Time &t); + void display(); +}; + +Time::Time(int h, int m, int s){ + hour=h; + minute=m; + second=s; + if (hour>=24) + hour=0; + if (minute>=60) + minute=0; + if (second>=60) + second=0; +} + +Time Time::operator++(){ + ++second; + if (second>=60){ + second=0; + ++minute; + if(minute>=60){ + minute=0; + ++hour; + if(hour>=24) + hour=0; + } + } + return *this; +} +Time operator--(Time &t){ + --t.second; + if (t.second>=60){ + t.second=0; + ++t.minute; + if(t.minute>=60){ + t.minute=0; + ++t.hour; + if(t.hour>=24) + t.hour=0; + } + } + return t; +} +void Time::display(){ + cout< +using namespace std; +class Counter{ + private: + int n; + public: + Counter(int i=0):n(i){}; + Counter operator++(); + Counter operator++(int); + friend Counter operator--(Counter &c); + friend Counter operator--(Counter &c,int); + void display(); +}; +Counter Counter::operator++(){ + ++n; + return *this; +} +Counter Counter::operator++(int){ + n++; + return *this; +} +Counter operator--(Counter &c){ + --c.n; + return c; +} +Counter operator--(Counter &c,int){ + c.n--; + return c; +} +void Counter::display(){ + cout<<"counter number="< +using namespace std; +class Complex { + private: + double r,i; + public: + Complex(double R=0,double I=0):r(R),i(I){}; + Complex operator+(Complex b); + Complex operator-(Complex b); + Complex operator*(Complex b); + Complex operator/(Complex b); + void display(); +}; + +Complex Complex::operator +(Complex b) +{ + return Complex(r+b.r,i+b.i); +} +Complex Complex::operator -(Complex b) +{ + return Complex(r-b.r,i-b.i); +} +//˵㷨 +Complex Complex::operator *(Complex b) +{ + Complex t; + t.r=r*b.r-i*b.i; + t.i=r*b.i+i*b.r; + return t; +} +//㷨 +Complex Complex::operator /(Complex b) { + Complex t; + double x; + x=1/(b.r*b.r+b.i*b.i); + t.r=x*(r*b.r+i*b.i); + t.i=x*(i*b.r-r*b.i); + return t; +} +void Complex::display(){ + cout<0) cout<<"+"; + if (i!=0) cout< + + +class Complex{ + private: + double r,i; + public: + Complex(double R=0,double I=0):r(R),i(I){}; + friend Complex operator+(Complex a, Complex b); + friend Complex operator-(Complex a, Complex b); + friend Complex operator*(Complex a, Complex b); + friend Complex operator/(Complex a, Complex b); + + friend Complex operator+(Complex a,double b){ + return Complex(a.r+b,a.i); + } + friend Complex operator+(double a,Complex b){ + return Complex(a+b.r,b.i); + } + + void display(); +}; + +Complex operator+(Complex a, Complex b){ + return Complex(a.r+b.r,a.i+b.i); +} +Complex operator-(Complex a, Complex b){ + return Complex(a.r-b.r,a.i-b.i); +} +Complex operator*(Complex a, Complex b){ + Complex t; + t.r = a.r*b.r-a.i*b.i; + t.i = a.r*b.i+a.i*b.r; +} +Complex operator/(Complex a, Complex b){ + Complex t; + double x; + x = 1/(b.r*b.r+b.i*b.i); + t.r = x*(a.r*b.r+a.i*b.i); + t.i = x*(a.i*b.r-a.r*b.i); + return t; +} + +void Complex::display(){ + std::cout<0) + std::cout<<"+"; + if(i!=0) + std::cout<>=[ ]㡣 +#include +#include +using namespace std; +class String { +private: +int length; //ַ +char *sPtr; //ַָ +void setString( const char *s2); +friend ostream &operator<<(ostream &os, const String &s); +friend istream &operator>>(istream &is, String &s); // +public: + String( const char * = "" ); + const String &operator=(const String &R); //ظֵ = + const String &operator+=(const String &R); //ַ += + bool operator==(const String &R); //ַȱȽ == + bool operator!=(const String &R); //ַIJȱȽ != + bool operator!() ; //жַǷΪ + bool operator<(const String &R) const; //ַСڱȽ < + bool operator>(const String &R); //ַĴڱȽ > + bool operator>=(const String &R); //ַĴڵڱȽ + char &operator[](int); //ַ± + ~String(); +}; +const String &String::operator+=(const String &R) { + char *temp = sPtr; + length += R.length; + sPtr = new char[length+1]; + strcpy(sPtr,temp ); + strcat(sPtr,R.sPtr ); + delete [] temp; + return *this; +} +bool String::operator==(const String &R){return strcmp(sPtr,R.sPtr)==0;} +bool String::operator!=(const String & R){return !(*this==R);} +bool String::operator!(){return length ==0;} +bool String::operator<(const String &R)const{return strcmp(sPtr,R.sPtr)<0;} +bool String::operator>(const String &R){return R<*this;} +bool String::operator>=(const String &R){return !(*this s1 " << ( s2 > s1 ? "true" : "false") + << "\ns2 < s1 " << ( s2 < s1 ? "true" : "false") + << "\ns2 >= s1 " << ( s2 >= s1 ? "true" : "false"); + cout << "\n\ns3ǷΪ: "; + if (!s3){ + cout << "s3ǿմ"< +#include +#include +using namespace std; + +int main(int argc, char const *argv[]) +{ + string name[]={"","",""}; + double salary[]={1200,2000,1450}; + mapsal; + map::iterator p; + for(int i=0;i<3;i++){ + sal.insert(make_pair(name[i],salary[i])); + } + sal["tom"]=6156; + sal["bob"]=5999; + for(p=sal.begin();p!=sal.end();p++){ + cout<first<<"\t"<second<>person; + int flag=1; + for(p=sal.begin();p!=sal.end();p++) + if(p->first==person){ + cout<second<>person; + map::iterator it; + it = sal.find(person); + if(it!=sal.end()){ + cout<<"ҳɹ"<<(*it).first<<":"<<(*it).second<first<second< (){ + //ģ嶨 +} +``` +templateǶģĹؼ֣дһ<>еT1T2ģеclassʾIJ͡ +ģΪͲģʵģ庯ʱʱҪݵʵһͣintdouble֮ࡣ +ģIJгģT1T2 +2ʹúģע + ڶģʱtemplate뺯ģ嶨֮κ䡣 +```c++ +template +int x; //󣬲ڴλκ +T min(T a,T b){} +``` + ģжͲÿͲùؼclasstypename޶⣬ģлԳȷͲΪͲ +```c++ +template +T1 fx(T1 a, T 2 b, T3 c){} +``` +ڴʵʱͲT4ֻʹó + ҪclassؼclassһȻͬĸɣDzͬġclassʾTһͲκͣintfloatcharȣûstructenumclassԶ͡ + Ϊģе͹ؼclass׼C++?typenameΪģ͹ؼ֣ͬʱҲ֧ʹclass磬mintemplate дʽȫȼ۵ģ +```c++ +template +T min(T a,T b){} +``` + +3.ģʵ +- ʵʱ +ģʵڵģ庯ʱжԺģĵʱŻݵʵεľͣȷģͣô滻ģеģܹ͵ĺ룬ģ庯 +- ηͬIJʱֻڵ1νʵĺã +```c++ +int x=min(2,3); +int y=min(3,9); +int z=min(8.5); +``` +ֻڵ1εʱģ庯֮ͬ͵IJʱģ庯õ1ʵɵģ庯 + +- ʵķʽ + +**ʽʵ** + +ܹжģʱԶʵģΪģ庯 +```c++ +template T max (T, T); + +int i = max (1, 2); +float f = max (1.0, 2.0); +char ch = max (a, A); + +``` +ʽʵڵģ壬ʵǵʵ + +**ʾʵexplicit instantiation** +ʱ,жģͻֵ +Ҫʹضʵ +﷨ʽ:: +ģ<,,ֵ,> () +```c++ +template T max (T, T); + +int i = max (1, 2); +// error: data type cant be deduced +int i = max (1, 2); + +``` +4.ģػ +- ػԭ +ijЩ£ģͨ㷨ʺضijϣ͵ȣ +磺max +```c++ +char * cp = max (abcd, 1234); +ʵΪchar * max (char * a, char * b){return a > b ? a : b;} +``` +϶ģΪַıȽΪ +```c++ +char * max (char * a, char * b) +{ return strcmp(a, b)>0 ? a : b; } +``` +- ػ +νػģ岻ܴͣдģͬ⺯רŴЩ͡ +ģػĶʽ +template <> <ػ>() { + +} +˵ + template < >ģػĹؼ֣< >вҪκݣ + < >Ҫػ͡ + +5.˵ + ͬʱģػʱػȵã + ͬһУ˺ģػ⣬ͬͨC++ͨĵʵνʽתģ庯ػIJκʽת +6.˳ +ͬһоģͨʱƥ˳£ +ȫƥķģ庯 +ȫƥģ庯 +ݵķģ庯 +ģ +1.ģĸ +ģƽṹͳԱȫͬͲͬͨࡣ +ջ +˫ջ +```c++ +class doubleStack{ + private: + double data[size]; + +}; +``` +ַջ +```c++ +class charStack{ + private: + char data[size]; + +}; +``` +Щջ֮⣬ȫͬͿģʵ֡ + +2.ģ +```c++ +template +class { + // Ա붨 +} +``` +T1T2Ͳ +ģпжģͲͷͲ + +3.Ͳ + +Ͳָij־ͣڵģʱֻΪṩӦ͵ijֵͲƵģͨ͡ö͡ãԼ󡢺Աָ룬**øͣ˫ͣvoidΪͲ** + +ģУT1T2ͲT3ǷͲ +```c++ +template +``` +ʵʱΪT1T2ṩһͣΪT3ָһ10ģܱȷʵ + +4.ģijԱĶ +1ģⶨ壬﷨ +template <ģб> ֵ ģ<ģ>::Ա (б) { }; +2Ա壬볣ԱĶƣ +ģбġͱʶΪʹ +ģбġͨͳΪʹ +5.ģػ + +ػ +9-9ķػģͬΪijרдһģࡣ +ģػʽһػģ壬һػԱ +ػԱķ +```c++ +template <> ģ<ػ>::ػԱ(){ + // +} +``` + +```c++ +template<> void Array::Sort(){ + for(int i=0;i +using namespace std; + + +template +//ʵֽ +void sort(T &a, int n){ + for (int i=0;i +void display(T& a,int n) { + for(int i=0;i +#include +using namespace std; +template +T Max(T a,T b) { + return (a>b)?a:b; +} +//ػ +//template <> <ػ>() {} +template<>char * Max(char *a,char *b) { + return (strcmp(a,b)>=0)?a:b; +} +int main(){ + float c=5.1,d=3.2; + cout<<"2,3ֵǣ"< +using namespace std; +template +T Max(T a,T b) { + return (a>b)?a:b; +} +/* +C++ʵģĹУֻǼ򵥵ؽģ滻ɵʵεͣԴģ庯в͵κת +*/ +int main(){ + double a=2,b=3.4; + float c=5.1,d=3.2; + //ģʱв͵ǿת + cout<<"2, 3.2 ֵǣ"<('a',3)< +//עһ㣬maxminʹõʱͻдһд룬Ҫı亯ģֱ֣ʹstd::coutstd::endl +using namespace std; +/* +ҪclassؼclassһȻͬĸɣDzͬġ +classʾTһͲκͣintfloatcharȣûstructenumclassԶ͡ +*/ +template +T Min(T a,T b) { + return (aдʽȫȼ۵ģ +*/ +template +T myMin(T a, T b){ + return (a(2,3)< +using namespace std; + +template +class Stack{ + private: + T elem[MAXSIZE]; + int top; + public: + Stack(){top=0;}; + void push(T e); + T pop(); + bool empty(){ + if (top<=-1) + return 1; + else + return 0; + } + void setEmpty(){ + top=-1; + } + bool full(){ + if (top>=MAXSIZE-1) + { + return 1; + } + else + return 0; + } +}; +/* +ԭͣ +template <ģб> +ֵ ģ<ģ>::Ա (б){}; +*/ +template +void Stack::push(T e){ + if(full()){ + cout<<"ջԪˣ"; + return; + } + elem[++top]=e; +} + +template +T Stack::pop(){ + if(empty()){ + cout<<"ջѿգٵԪˣ"< iStack; + Stack cStack; + iStack.setEmpty(); + cStack.setEmpty(); + cout<<"-------intStack----\n"; + int i; + for(i=1;i<11;i++) + iStack.push(i); + for(i=1;i<11;i++) cout< +#include +using namespace std; +const int Size=5; +template +class Array{ + private: + T a[Size]; + public: + Array(){ + for(int i=0;i T& Array::operator[](int i){ + if(i<0||i>Size-1){ + cout<<"\n±Խ磡"< void Array::Sort(){ + int p; + for(int i=0;i ģ<ػ>::ػԱ(){} +template<> void Array::Sort(){ + int p; + for(int i=0;i a1; + Arrayb1; + a1[0]=1;a1[1]=23;a1[2]=6; + a1[3]=3; a1[4]=9; + a1.Sort(); + for(int i=0;i<5;i++) + cout< +using namespace std; +int main(){ + cout<<"1--befroe try block..."< +using namespace std; +const int MAX=3; +class Full{ + int a; + public: + Full(int i):a(i){} + int getValue(){return a;} +}; +class Empty{}; +class Stack{ + private: + int s[MAX]; + int top; + public: + Stack(){top=-1;} + void push(int a){ + if(top>=MAX-1) + throw Full(a); + s[++top]=a; + } + int pop(){ + if(top<0) + throw Empty(); + return s[top--]; + } +}; +int main(){ + Stack s; + try{ + s.push(10); + s.push(20); + s.push(30); + s.push(40); + } + catch(Full e){ + cout<<"Exception: Stack Full..."< +using namespace std; +int main(){ + cout<<"1--befroe try block..."< +using namespace std; +void temperature(int t) +{ + try{ + if(t==100) throw "е㣡"; + else if(t==0) throw "㣡"; + else cout<<"the temperature is OK..."< +using namespace std; +void temperature(int t) +{ + + if(t==100) throw "е㣡"; + else if(t==0) throw "㣡"; + else{cout<<"temperatore="< +using namespace std; + +void handler(int n)throw(int,char,double){ + if(n==1) throw n; + if(n==2) throw 'x'; + if(n==3) throw 1.1; +} +int main(){ + cout<<"Before handler..."< +using namespace std; +void Errhandler(int n)throw(){ + try{ + if(n==1) throw n; + if(n==2) throw "dx"; + if(n==3) throw 1.1; + } + catch(...){cout<<"catch an exception..."< +using namespace std; +class A{ + int a; +public: + A(int i=0):a(i){} + ~A(){cout<<"in A destructor..."< +using namespace std; +//ڲٴthrow쳣ʱ򣬺Ҫthrow() +void Errhandler(int n) +{ + try{ + if(n==1) throw n; + cout<<"all is ok..."< +using namespace std; +const int MAX=3; +class Full{}; //L1 ջʱ׳쳣 +class Empty{}; //L2 ջʱ׳쳣 +class Stack{ + private: + int s[MAX]; + int top; + public: + void push(int a); + int pop(); + Stack(){top=-1;} +}; +void Stack::push(int a){ + if(top>=MAX-1) + throw Full(); + s[++top]=a; +} +int Stack::pop(){ + if(top<0) throw Empty(); + return s[top--]; +} +int main(){ + Stack s; + try{ + s.push(10); s.push(20); s.push(30); + //s.push(40); //L5 ջ쳣 + cout<<"stack(0)= "< +using namespace std; +class BasicException{ + public: + char* Where(){return "BasicException...";} +}; +class FileSysException:public BasicException{ + public: + char *Where(){return "FileSysException...";} +}; +class FileNotFound:public FileSysException{ + public: + char *Where(){return "FileNotFound...";} +}; +class DiskNotFound:public FileSysException{ + public: + char *Where(){return "DiskNotFound...";} +}; +void main(){ + try{ + ..... // + throw FileSysException(); + } + catch(DiskNotFound p){cout< +#include +using namespace std; +struct Person{ //ְϢĽṹ + double salary; + char *name; +}; +class SalaryManaege{ + Person *employ; //ְϢ + int max; //±Ͻ + int n; //еʵְ +public: + SalaryManaege(int Max=0){ + max=Max; + n=0; + employ=new Person[max]; + } + //ǿֱڷֱֵʹ + double &operator[](char *Name) { //[] + Person *p; + for(p=employ;pname,Name)==0) + return p->salary; + // + p=employ + n++; + p->name=new char[strlen(Name)+1]; + strcpy(p->name,Name); + p->salary=0; + return p->salary; + } + + void display(){ + for(int i=0;i +#include +using namespace std; + + +template T compareMax(T t1,T t2){ + return t1>t2?t1:t2; +} + +template<> char* compareMax(char *s1,char *s2){ + return strcmp(s1,s2)>=0?s1:s2; +} +int main(int argc, char const *argv[]) +{ + cout< +char *sPtr; //ַָ +s="" +sPtr = new char[strlen(s)+1]; +strncpy(sPtr,s; \ No newline at end of file diff --git a/practical_exercises/key_exercises/字典插入与查找.cpp b/practical_exercises/key_exercises/字典插入与查找.cpp new file mode 100644 index 0000000..bf7f6b9 --- /dev/null +++ b/practical_exercises/key_exercises/字典插入与查找.cpp @@ -0,0 +1,78 @@ +#include +#include +#include +using namespace std; + + + +int main(int argc, char const *argv[]) +{ + map mp; + map::iterator iter; + const char key[3][20]={"img","system","ip"}; + const char value[3][20]={"d:/a.img","win7","193.68.6.3"}; + + //make_pair + for(int i=0;i<2;i++){ + mp.insert(make_pair(key[i],value[i])); + } + //pair + mp.insert(pair(key[2],value[2])); + + //뷽ʽ + mp["addr"]="й"; + //ȡԪ + //ѭȡԪ + for(iter=mp.begin();iter!=mp.end();iter++){ + cout<first<<"\t"<second<first,key1)==0){ + cout<first<<"ҳˣ"<<"ӦֵΪ"<second<first<<"keyҳˣ"<<"ӦvalueΪ"<second<first,drop_key)==0){ + cout<first<<"keyҳˣ"<<"ӦvalueΪ"<second<second,drop_value)==0){ + cout<second<<"valueҳˣ"<<"ӦkeyΪ"<first<first<<"\t"<second< +using namespace std; +//쳣׳ȥ +void fun(int x){ + try{ + if (x==0) + throw "쳣"; + }catch(...){ + cout<<"in fun"< +#include +using namespace std; + +/* + * ʱ + */ +class Clock +{ +private: + int Hour, Minute, Second; +public: + Clock(int h=0, int m=0, int s=0); + void ShowTime(); + Clock& operator ++(); + Clock operator ++(int); +}; + +/* + * ʱ๹캯 + */ +Clock::Clock(int h,int m, int s) +{ + if(h>=0 && h<24 && m>=0 && m<60 && s>=0 && s<60) + { + Hour = h; + Minute =m; + Second= s; + } + else + cout<<"ʱʽ!"<= 60) + { + Second = Second - 60; + Minute++; + if (Minute >= 60) + { + Minute = Minute - 60; + Hour++; + Hour = Hour % 24; + } + } + return *this; +} + +/* + * ʱһ루غ׺++ + */ +Clock Clock::operator ++(int) +{ + Clock old = *this; + ++(*this); + return old; +} + +/* + * + */ +int main() +{ + Clock myClock(23,59,59); + cout<<"ʼʾʱΪ:\t\t"; + myClock.ShowTime(); + + cout<<"ִmyClock++ʱΪ:\t"; + + //ִShowTime()myClock=23:59:59 + //ִmyClock++ʱmyClock=00:00:00 + (myClock++).ShowTime(); + + cout<<"ִ++myClockʱΪ:\t"; + + //ִ++myClockʱmyClock=00:00:01 + //ִShowTime()myClock=00:00:01 + (++myClock).ShowTime(); + system("pause"); +} \ No newline at end of file diff --git a/practical_exercises/key_exercises/类模板之栈.cpp b/practical_exercises/key_exercises/类模板之栈.cpp new file mode 100644 index 0000000..6d05646 --- /dev/null +++ b/practical_exercises/key_exercises/类模板之栈.cpp @@ -0,0 +1,63 @@ +#include + +using namespace std; + +template + +class Stack{ + public: + Stack(){} + void init(){ + top=-1; + } + bool isFull(){ + if(top>=MAXSIZE-1) + return true; + else + return false; + } + bool isEmpty(){ + if(top==-1) + return true; + else + return false; + } + void push(T e); + T pop(); + + private: + T elems[MAXSIZE]; + int top; +}; + +template void Stack::push(T e){ + if(!isFull()){ + elems[++top]=e; + } + else{ + cout<<"ջ벻ҪټԪأ"; + return; + } +} +template T Stack::pop(){ + if(!isEmpty()){ + return elems[top--]; + } + else{ + cout<<"ջѿգ벻ҪٵԪأ"; + return 0; + } +} + +int main(int argc, char const *argv[]) +{ + Stack s1; + s1.init(); + int i; + for(i=1;i<11;i++) + s1.push(i); + for(i=1;i<11;i++) cout< +#include +using namespace std; +#define MAXSIZE 5 +template +class Array{ + public: + Array(){ + for(int i=0;i T& Array::operator[](int i){ + if(i<0||i>MAXSIZE-1){ + cout<<"±Խ磡"< void Array::Sort(){ + int p,j; + for(int i=0;i void Array::Sort(){ + int p,j; + for(int i=0;i a1; + Arrayb1; + a1[0]=1;a1[1]=23;a1[2]=6; + a1[3]=3; a1[4]=9; + a1.Sort(); + for(int i=0;i<5;i++) + cout< +#include + +using namespace std; +class Employee{ + public: + Employee(const char *name,const char *id){ + strcpy(Name,name); + strcpy(Id,id); + } + char* getName(){ + return Name; + } + char* getId(){ + return Id; + } + void display(){ + cout<>week; + Manager m("С","11111111",week); + m.display(); + cout<<""; + int profit; + cin>>profit; + cout<<"ۼ"; + int x; + cin>>x; + SaleWorker s("С","222222",profit,x); + s.display(); + cout<<"빤Сʱ"; + int hour; + cin>>hour; + HourWorker h("С","333333",hour); + h.display(); + system("pause"); + return 0; +} diff --git a/practical_exercises/key_exercises/读写文件综合题.cpp b/practical_exercises/key_exercises/读写文件综合题.cpp new file mode 100644 index 0000000..fd3f7c7 --- /dev/null +++ b/practical_exercises/key_exercises/读写文件综合题.cpp @@ -0,0 +1,73 @@ + +#include +#include +#include +#include +using namespace std; + +class Person{ + public: + Person(){} + Person(char *name,char *id,int math,int chinese,int english){ + strcpy(Name,name); + strcpy(Id,id); + Math=math; + Chinese=chinese; + English=english; + Sum=Math+Chinese+English; + } + + void display(){ + cout<>Name; + cout<<"֤ţ"; + cin>>Id; + cout<<"ѧɼ"; + cin>>Math; + cout<<"뺺ɼ"; + cin>>Chinese; + cout<<"Ӣɼ"; + cin>>English; + Person per(Name,Id,Math,Chinese,English); + ioFile.write((char *)&per,sizeof(per)); + cout<<"Ƿ(Y/y) "; + cin>>ch; + }while(ch=='y'||ch=='Y'); + ioFile.close(); + ioFile.open("d://per.dat",ios::in); + Person p; + ioFile.read((char*)&p,sizeof(p)); + vector v; + vector::iterator vt; + while(!ioFile.eof()){ + v.push_back(p); + ioFile.read((char*)&p,sizeof(p)); + } + ioFile.close(); + cout<<"---------Ϣ-----------"< +#include +class Sales{ +private: + char name[10]; + char id[18]; + int age; +public: + Sales(char *Name,char *ID,int Age); + + friend Sales &operator<<(ostream &os,Sales &s); // + friend Sales &operator>>(istream &is,Sales &s); // +}; +Sales::Sales(char *Name,char *ID,int Age) { + strcpy(name,Name); + strcpy(id,ID); + age=Age; +} +Sales& operator<<(ostream &os,Sales &s) { + os<>(istream &is,Sales &s) { + cout<<"Ա֤ţ"<>s.name>>s.id>>s.age; + return s; +} +void main(){ + Sales s1("ſ","214198012111711",40); //L1 + cout<>s1; //L4 + cout< +using namespace std; +#include +class Sales{ +private: + char name[10]; + char id[18]; + int age; +public: + Sales(char *Name,char *ID,int Age); + friend Sales &operator<<(ostream &os,Sales &s); + friend Sales &operator>>(istream &is,Sales &s); + +}; + + +Sales::Sales(char *Name,char *ID,int Age){ + strcpy(name,Name); + strcpy(id,ID); + age=Age; +} + +Sales &operator<<(ostream&os,Sales &s){ + os<>(istream&is,Sales &s){ + cout<<"Ա֤䣺\n"; + is>>s.name>>s.id>>s.age; +} + +int main(int argc, char const *argv[]) +{ + Sales s("","15611",26); + cout<>s; + cout< +#include +using namespace std; + +int main(int argc, char const *argv[]) +{ + char s[20]="this is a string"; + double digit=-36.96656; + cout< +using namespace std; +class Circle{ +private: + double x,y,r; +public: + Circle(double x1,double y1,double r1){x=x1;y=y1;r=r1; } + operator int(){return int(r);} + operator double(){return 2*3.14*r;} + operator float(){return (float)3.14*r*r;} +}; +int main(){ + Circle c(2.3,3.4,2.5); + int r=c; //operator int()Circleתint + double length=c; //operator double()תdouble + float area=c; //operator float()Circleתfloat + double len=(double) c; //CirlceͶǿתdouble + cout< +using namespace std; +class Time{ +private: + int hh,mm,ss; +public: + Time(int h=0,int m=0,int s=0):hh(h),mm(m),ss(s){} + void operator()(int h,int m,int s) { + hh=h; + mm=m; + ss=s; + } + void ShowTime(){ + cout<