1、第一章5:#include using namespace std;int main()coutusing namespace std;int main()int a,b,c;a=10;b=23;c=a+b;coutusing namespace std;int main()int a,b,c;int f(int x,int y,int z);cinabc;c=f(a,b,c);coutusing namespace std;int main()int a,b,c;cinab;c=a+b;coutusing namespace std;int main()int add(int x,int y
2、);int a,b,c;cinab;c=add(a,b);coutusing namespace std;int main()void sort(int x,int y,int z);int x,y,z;cinxyz;sort(x,y,z);return 0;void sort(int x, int y, int z)int temp;if (xy) temp=x;x=y;y=temp; / 内 3 个语句的作用是将 x 和 y 的值互换)if (zusing namespace std;int main()int max(int a,int b,int c=0);int a,b,c;cina
3、bc;couta) a=b;if(ca) a=c;return a;12:#include using namespace std;int main() void change(int ,int );int a,b;cinab;if(ausing namespace std;int main()void sort(int int a,b,c,a1,b1,c1;coutabc;a1=a;b1=b;c1=c;sort(a1,b1,c1);coutj) change(i,j);if (ik) change(i,k);if (jk) change(j,k);void change(int temp=x
4、;x=y;y=temp;14:#include #include using namespace std;int main() string s1=“week“,s2=“end“;cout#include using namespace std;int main() string str;int i,n;char temp;coutstr;n=str.size();for(i=0;i#include using namespace std;int main() int i;string str5=“BASIC“,“C“,“FORTRAN“,“C+“,“PASCAL“;void sort(str
5、ing );sort(str);coutsi+1)t=si;si=si+1;si+1=t;17: #include #include using namespace std;int main() long c5=10100,-123567, 1198783,-165654, 3456;int a5=1,9,0,23,-45;float b5=2.4, 7.6, 5.5, 6.6, -2.3 ;void sort(int );void sort(float );void sort(long );sort(a);sort(b);sort(c);return 0;void sort(int a)in
6、t i,j,t;for (j=0;jai+1)t=ai;ai=ai+1;ai+1=t;coutai+1)t=ai;ai=ai+1;ai+1=t;coutai+1)t=ai;ai=ai+1;ai+1=t;cout#include using namespace std;template void sort(T a) int i,j,min;T t;for(i=0;iaj) min=j;t=ai; ai=amin; amin=t; coutusing namespace std;class Timepublic:void set_time();void show_time();private: /
7、成员改为公用的int hour;int minute;int sec;void Time:set_time() /在 main 函数之前定义cinhour;cinminute;cinsec;void Time:show_time() /在 main 函数之前定义coutusing namespace std;class Timepublic:void set_time(void)cinhour;cinminute;cinsec;void show_time(void)coutusing namespace std;class Timepublic:void set_time(void);voi
8、d show_time(void);private:int hour;int minute;int sec;void Time:set_time(void)cinhour;cinminute;cinsec;void Time:show_time(void)coutusing namespace std;#include “xt2-4.h“int main()Student stud;stud.set_value();stud.display();return 0;/xt2-4-2.cpp(即 student.cpp)#include “xt2-4.h“ /在此文件中进行函数的定义#includ
9、e using namespace std; /不要漏写此行void Student:display( ) coutnum;cinname;cinsex;5:/xt2-5-1.cpp(file1.cpp)#include #include “xt2-5.h“int main()Array_max arrmax;arrmax.set_value();arrmax.max_value();arrmax.show_value();return 0;/xt2-5-2.cpp(arraymax.cpp)#include using namespace std;#include “xt2-5.h“void
10、 Array_max:set_value() int i;for (i=0;iarrayi;void Array_max:max_value()int i;max=array0;for (i=1;imax) max=arrayi;void Array_max:show_value()coutusing namespace std;class Boxpublic:void get_value();float volume();void display();public:float lengh;float width;float height;void Box:get_value() coutle
11、ngh;cinwidth;cinheight;float Box:volume() return(lengh*width*height);void Box:display() coutusing namespace std;class Boxpublic:void get_value();void volume();void display();public:float lengh;float width;float height;float vol;void Box:get_value() coutlengh;cinwidth;cinheight;void Box:volume() vol=
12、lengh*width*height;void Box:display() coutusing namespace std;class Datepublic:Date(int,int,int);Date(int,int);Date(int);Date();void display();private:int month;int day;int year;Date:Date(int m,int d,int y):month(m),day(d),year(y) Date:Date(int m,int d):month(m),day(d)year=2005;Date:Date(int m):mont
13、h(m)day=1;year=2005;Date:Date()month=1;day=1;year=2005;void Date:display()coutusing namespace std;class Datepublic:Date(int=1,int=1,int=2005);void display();private:int month;int day;int year;Date:Date(int m,int d,int y):month(m),day(d),year(y) void Date:display()coutusing namespace std;class Studen
14、tpublic:Student(int n,float s):num(n),score(s)void display();private:int num;float score;void Student:display()coutdisplay();return 0;5:#include using namespace std;class Studentpublic:Student(int n,float s):num(n),score(s)int num;float score;void main()Student stud5=Student(101,78.5),Student(102,85
15、.5),Student(103,98.5),Student(104,100.0),Student(105,95.5);void max(Student* );Student *p=max(p);void max(Student *arr)float max_score=arr0.score;int k=0;for(int i=1;imax_score) max_score=arri.score;k=i;coutusing namespace std;class Studentpublic:Student(int n,float s):num(n),score(s)void change(int
16、 n,float s) num=n;score=s;void display()coutusing namespace std;class Studentpublic:Student(int n,float s):num(n),score(s)void change(int n,float s) num=n;score=s;void display() coutusing namespace std;class Studentpublic:Student(int n,float s):num(n),score(s)void change(int n,float s) const num=n;s
17、core=s;void display() const coutusing namespace std;class Studentpublic:Student(int n,float s):num(n),score(s)void change(int n,float s) num=n;score=s;void display() coutdisplay();p-change(101,80.5);p-display();return 0;8:#include using namespace std;class Studentpublic:Student(int n,float s):num(n)
18、,score(s)void change(int n,float s) num=n;score=s;void display() coutusing namespace std;class Productpublic:Product(int n,int q,float p):num(n),quantity(q),price(p);void total();static float average();static void display();private:int num;int quantity;float price;static float discount;static float
19、sum;static int n;void Product:total()float rate=1.0;if(quantity10) rate=0.98*rate;sum=sum+quantity*price*rate*(1-discount);n=n+quantity;void Product:display()coutusing namespace std;class Date;class Timepublic:Time(int,int,int);friend void display(const Date private:int hour;int minute;int sec;Time:
20、Time(int h,int m,int s)hour=h;minute=m;sec=s;class Datepublic:Date(int,int,int);friend void display(const Date private:int month;int day;int year;Date:Date(int m,int d,int y)month=m;day=d;year=y;void display(const Date class Time;class Datepublic:Date(int,int,int);friend Time;private:int month;int d
21、ay;int year;Date:Date(int m,int d,int y):month(m),day(d),year(y) class Timepublic:Time(int,int,int);void display(const Date private:int hour;int minute;int sec;Time:Time(int h,int m,int s):hour(h),minute(m),sec(s) void Time:display(const Date templateclass Comparepublic:Compare(numtype a,numtype b);
22、numtype max();numtype min();private:numtype x,y;template Compare:Compare(numtype a,numtype b)x=a;y=b;template numtype Compare:max()return (xy)?x:y;template numtype Compare:min()return (x cmp1(3,7);cout cmp2(45.78,93.6);cout cmp3(a,A);coutusing namespace std;class Complexpublic:Complex()real=0;imag=0
23、;Complex(double r,double i)real=r;imag=i;double get_real();double get_imag();void display();private:double real;double imag;double Complex:get_real()return real;double Complex:get_imag()return imag;void Complex:display()coutusing namespace std;class Complexpublic:Complex()real=0;imag=0;Complex(doubl
24、e r,double i)real=r;imag=i;Complex operator+(Complex Complex operator-(Complex Complex operator*(Complex Complex operator/(Complex void display();private:double real;double imag;Complex Complex:operator+(Complex c.real=real+c2.real;c.imag=imag+c2.imag;return c;Complex Complex:operator-(Complex c.rea
25、l=real-c2.real;c.imag=imag-c2.imag;return c;Complex Complex:operator*(Complex c.real=real*c2.real-imag*c2.imag;c.imag=imag*c2.real+real*c2.imag;return c;Complex Complex:operator/(Complex c.real=(real*c2.real+imag*c2.imag)/(c2.real*c2.real+c2.imag*c2.imag);c.imag=(imag*c2.real-real*c2.imag)/(c2.real*
26、c2.real+c2.imag*c2.imag);return c;void Complex:display()cout /用 VC+时改为 #include using namespace std; /用 VC+时为取消此行class Complexpublic:Complex()real=0;imag=0;Complex(double r,double i)real=r;imag=i;Complex operator+(Complex Complex operator+(int friend Complex operator+(intvoid display();private:doubl
27、e real;double imag;Complex Complex:operator+(Complex Complex Complex:operator+(int void Complex:display()coutusing namespace std;class Matrix /定义 Matrix 类public:Matrix(); /默认构造函数friend Matrix operator+(Matrix /重载运算符“+”void input(); /输入数据函数void display(); /输出数据函数private:int mat23;Matrix:Matrix() /定义构
28、造函数for(int i=0;imatij;void Matrix:display() /定义输出数据函数for (int i=0;i/using namespace std;class Matrixpublic:Matrix();friend Matrix operator+(Matrix friend ostreamprivate:int mat23;Matrix:Matrix()for(int i=0;i(istream return in;ostreamcinb;coutusing namespace std;class Complexpublic:Complex()real=0;im
29、ag=0;Complex(double r)real=r;imag=0;Complex(double r,double i)real=r;imag=i;operator double()return real;void display();private:double real;double imag;void Complex:display()coutusing namespace std;class Studentpublic:Student(int,char,char,float);int get_num()return num;char * get_name()return name;char get_sex()return sex;void display()coutusing namespace std;class Student