教材例題程序
第一章
Ch1-1
#include <iostream>
using namespace std;
int main()
{
cout<<"This is a C++ program.";
return 0;
}
Ch1-2
#include <iostream>
using namespace std;
int main()
{
int a,b, sum;
cin>>a>>b;
sum=a+b;
cout<<"a+b="<<sum<<endl;
return 0;
}
Ch1-3-1
#include <iostream>
using namespace std;
int max(int x,int y)
{ int z;
if(x>y) z=x;
else z=y;
return(z);
}
int main()
{ int a,b,m;
cin>>a>>b;
m=max(a,b);
cout<<"max="<<m<<endl;
return 0;
}
Ch1-3-2
#include <iostream>
using namespace std;
int main()
{ int max(int x,int y);
int a,b,c;
cin>>a>>b;
c=max(a,b);
cout<<"max="<<c<<endl;
return 0;
}
int max(int x,int y)
{
int z;
if(x>y) z=x;
else z=y;
return(z);
}
Ch1-4
#include <iostream>
using namespace std;
class Student
{
private:
int num;
int score;
public:
void setdata()
{
cin>>num;
cin>>score;
}
void display()
{
cout<<"num="<<num<<endl;
cout<<"score="<<score<<endl;
};
};
Student stud1,stud2;
int main()
{
stud1.setdata();
stud2.setdata();
stud1.display();
stud2.display();
return 0;
}
第二章
Ch2-1
#include <iostream>
using namespace std;
int main ( )
{int i,j;
i='A';
j='B';
cout<<i<<' '<<j<<'\n';
return 0;
}
Ch2-2
#include <iostream>
using namespace std;
int main()
{char c1,c2;
c1='a';
c2='b';
c1=c1-32;
c2=c2-32;
cout<<c1<<' '<<c2<<endl;
return 0;
}
Ch2-3
#include <iostream>
#define PRICE 30
using namespace std;
int main ( )
{int num,total;
num=10;
total=num * PRICE;
cout<<"total="<<total<<endl;
return 0;
}
Ch2-4
#include <iostream>
using namespace std;
int main()
{ float x;
int i;
x=3.6;
i=(int)x;
cout<<"x="<<x<<",i="<<i<<endl;
return 0;
}
Ch2-5
#include <iostream>
using namespace std;
int main()
{ unsigned short a;
short int b=-1;
a=b;
cout<<"a="<<a<<endl;
return 0;
}
第三章
Ch3-1
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
double a=123.456,b=3.14159,c=-3214.67;
cout<<setiosflags(ios::fixed)<<setiosflags(ios::right)<<setprecision(2);
cout<<setw(10)<<a<<endl;
cout<<setw(10)<<b<<endl;
cout<<setw(10)<<c<<endl;
return 0;
}
Ch3-2
#include <iostream>
using namespace std;
int main()
{char a,b,c;
a='B';b='O';c='y';
putchar(a);putchar(b);putchar(c);putchar(10);
putchar(66);putchar(79);putchar(89);putchar(10);
return 0;
}
Ch3-3
#include <iostream>
using namespace std;
int main()
{char c;
c=getchar();
putchar(c+32);
putchar('\n');
return 0;
}
Ch3-3-2
#include <iostream>
using namespace std;
int main()
{putchar(getchar()+32);putchar('\n');
return 0;
}
Ch3-4
#include <iostream>
using namespace std;
int main()
{int a;
float b;
char c;
scanf("%d %c %f",&a,&c,&b);
printf("a=%d,b=%f,c=%c\n",a,b,c);
return 0;
}
Ch3-5
#include <iostream>
#include <cmath>
using namespace std;
int main()
{float a,b,c,x1,x2;
cin>>a>>b>>c;
x1=(-b+sqrt(b*b-4*a*c))/(2*a);
x2=(-b-sqrt(b*b-4*a*c))/(2*a);
cout<<"x1="<<x1<<endl;
cout<<"x2="<<x2<<endl;
return 0;
}
Ch3-6
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
int main()
{
double a,b,c;
cout<<"please enter a,b,c:";
cin>>a>>b>>c;
if (a+b>c && b+c>a && c+a>b)
{
double s,area;
s=(a+b+c)/2;
area=sqrt(s*(s-a)*(s-b)*(s-c));
cout<<setiosflags(ios::fixed)<<setprecision(4);
cout<<"area="<<area<<endl;
}
else cout<<"it is not a trilateral!"<<endl;
return 0;
}
Ch3-7
#include <iostream>
using namespace std;
int main()
{
char ch;
cin>>ch;
ch=(ch>='A' && ch<='Z')?(ch+32):ch;
cout<<ch<<endl;
return 0;
}
Ch3-8
#include <iostream>
using namespace std;
int main()
{
int year;
bool leap;
cout<<"please enter year:";
cin>>year;
if (year%4==0)
{if(year%100==0)
{if (year%400==0)
leap=true;
else leap=false;}
else
leap=true;}
else
leap=false;
if (leap)
cout<<year<<" is ";
else
cout<<year<<" is not ";
cout<<" a leap year."<<endl;
return 0;
}
Ch3-9
#include <iostream>
using namespace std;
int main()
{int c,s;
float p,w,d,f;
cout<<"please enter p,w,s:";
cin>>p>>w>>s;
if(s>=3000) c=12;
else c=s/250;
switch (c)
{ case 0:d=0;break;
case 1:d=2;break;
case 2:
case 3:d=5;break;
case 4:
case 5:
case 6:
case 7:d=8;break;
case 8:
case 9:
case 10:
case 11:d=10;break;
case 12:d=15;break;
}
f=p*w*s*(1-d/100.0);
cout<<"freight="<<f<<endl;
return 0;
}
Ch3-10
#include <iostream>
using namespace std;
int main()
{
int i=1,sum=0;
while (i<=100)
{sum=sum+i;
i++;
}
cout<<"sum="<<sum<<endl;
return 0;
}
Ch3-11
#include <iostream>
using namespace std;
int main()
{int i=1,sum=0;
do
{ sum=sum+i;
i++;
}while (i<=100);
cout<<"sum="<<sum<<endl;
return 0;
}
Ch3-12
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main()
{int s=1;
double n=1,t=1,pi=0;
while((fabs(t)) > 1e-7)
{pi=pi+t;
n=n+2;
s=-s;
t=s/n;
}
pi=pi*4;
cout<<"pi="<<setiosflags(ios::fixed)<<setprecision(6)<<pi<<endl;
return 0;
}
Ch3-13
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{long f1,f2;
int i;
f1=f2=1;
for(i=1;i<=20;i++)
{cout<<setw(12)<<f1<<setw(12)<<f2;
if(i%2==0) cout<<endl;
f1=f1+f2;
f2=f2+f1;
}
return 0;
}
Ch3-14
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
int main()
{int m,k,i,n=0;
bool prime;
for(m=101;m<=200;m=m+2)
{prime=true;
k=int(sqrt(m));
for(i=2;i<=k;i++)
if(m%i==0) {prime=false;break;}
if (prime)
{cout<<setw(5)<<m;
n=n+1;
}
if(n%10==0) cout<<endl;
}
cout<<endl;
return 0;
}
Ch3-15
#include <iostream>
using namespace std;
int main()
{char c;
while ((c=getchar())!='\n')
{if((c>='a' && c<='z') || (c>='A' && c<='Z'))
{c=c+4;
if(c>'Z' && c<='Z'+4 || c>'z') c=c-26;
}
cout<<c;
}
cout<<endl;
return 0;
}
第四章
Ch4-1
#include <iostream>
using namespace std;
void printstar(void)
{
cout<<"******************************"<<endl;
}
void print_message(void)
{
cout<<" Welcome to C++!"<<endl;
}
int main(void)
{
printstar();
print_message();
printstar();
return 0;
}
Ch4-2
#include <iostream>
using namespace std;
int max(int x,int y)
{int z;
z=x>y?x:y;
return(z);
}
int main()
{int a,b,c;
cout<<"please enter two integer numbers:";
cin>>a>>b;
c=max(a,b);
cout<<"max="<<c<<endl;
return 0;
}
Ch4-3
#include <iostream>
using namespace std;
int main()
{float add(float,float);
float a,b,c;
cout<<"please enter a,b:";
cin>>a>>b;
c=add(a,b);
cout<<"sum="<<c<<endl;
return 0;
}
float add(float x,float y)
{float z;
z=x+y;
return (z);
}
Ch4-4
#include <iostream>
using namespace std;
inline int max(int,int, int);
int main()
{
int i=10,j=20,k=30,m;
m=max(i,j,k);
cout<<"max="<<m<<endl;
return 0;
}
inline int max(int a,int b,int c)
{if(b>a) a=b;
if(c>a) a=c;
return a;
}
Ch4-5
#include <iostream>
using namespace std;
int main()
{int max(int a,int b,int c);
double max(double a,double b,double c);
long max(long a,long b,long c);
int i1,i2,i3,i;
cin>>i1>>i2>>i3;
i=max(i1,i2,i3);
cout<<"i_max="<<i<<endl;
double d1,d2,d3,d;
cin>>d1>>d2>>d3;
d=max(d1,d2,d3);
cout<<"d_max="<<d<<endl;
long g1,g2,g3,g;
cin>>g1>>g2>>g3;
g=max(g1,g2,g3);
cout<<"g_max="<<g<<endl;
return 0;
}
int max(int a,int b,int c)
{if(b>a) a=b;
if(c>a) a=c;
return a;
}
double max(double a,double b,double c)
{if(b>a) a=b;
if(c>a) a=c;
return a;
}
long max(long a,long b,long c)
{if(b>a) a=b;
if(c>a) a=c;
return a;
}
Ch4-6
#include <iostream>
using namespace std;
int main()
{int max(int a,int b,int c);
int max(int a,int b);
int a=8,b=-12,c=7;
cout<<"max(a,b,c)="<<max(a,b,c)<<endl;
cout<<"max(a,b)="<<max(a,b)<<endl;
return 0;
}
int max(int a,int b,int c)
{if(b>a) a=b;
if(c>a) a=c;
return a;
}
int max(int a,int b)
{if(a>b) return a;
else return b;
}
Ch4-7
#include <iostream>
using namespace std;
template <typename T>
T max(T a,T b,T c)
{if(b>a) a=b;
if(c>a) a=c;
return a;
}
int main()
{int i1=185,i2=-76,i3=567,i;
double d1=56.87,d2=90.23,d3=-3214.76,d;
long g1=67845,g2=-912456,g3=673456,g;
i=max(i1,i2,i3);
d=max(d1,d2,d3);
g=max(g1,g2,g3);
cout<<"i_max="<<i<<endl;
cout<<"d_max="<<d<<endl;
cout<<"g_max="<<g<<endl;
return 0;
}
Ch4-8
#include <iostream>
using namespace std;
int main()
{int max(int a,int b,int c=0);
int a,b,c;
cin>>a>>b>>c;
cout<<"max(a,b,c)="<<max(a,b,c)<<endl;
cout<<"max(a,b)="<<max(a,b)<<endl;
return 0;
}
int max(int a,int b,int c)
{if(b>a) a=b;
if(c>a) a=c;
return a;
}
Ch4-9
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
double root(double,double);
double f(double);
double xpoint(double,double);
int main()
{double x1,x2,f1,f2,x;
do
{
cout<<"input x1,x2:";
cin>>x1>>x2;
f1=f(x1);
f2=f(x2);
}while(f1*f2>=0);
x=root(x1,x2);
cout<<setiosflags(ios::fixed)<<setprecision(7);
cout<<"A root of equation is "<<x<<endl;
return 0;
}
double f(double x)
{
double y;
y=x*x*x-5*x*x+16*x-80;
return y;
}
double xpoint(double x1,double x2)
{
double y;
y=(x1*f(x2)-x2*f(x1))/(f(x2)-f(x1));
return y;
}
double root(double x1,double x2)
{double x,y,y1;
y1=f(x1);
do
{
x=xpoint(x1,x2);
y=f(x);
if (y*y1>0)
{y1=y;
x1=x;
}
else
x2=x;
}while(fabs(y)>=0.000001);
return x;
}
Ch4-10
#include <iostream>
using namespace std;
int age(int);
int main()
{cout<<age(5)<<endl;
return 0;
}
int age(int n)
{
int c;
if(n==1) c=10;
else c=age(n-1)+2;
return c;
}
Ch4-11
#include <iostream>
using namespace std;
long fac(int);
int main()
{int n;
long y;
cout<<"please input an integer:";
cin>>n;
y=fac(n);
cout<<n<<"!="<<y<<endl;
return 0;
}
long fac(int n)
{
long f;
if(n<0)
{cout<<"n<0,data error!"<<endl;
f=-1;
}
else if (n==0||n==1) f=1;
else f=fac(n-1)*n;
return f;
}
Ch4-12
#include <iostream>
using namespace std;
int f(int a)
{auto int b=0;
static int c=3;
b=b+1;
c=c+1;
return a+b+c;
}
int main()
{int a=2,i;
for(i=0;i<3;i++)
cout<<f(a)<<" ";
cout<<endl;
return 0;
}
Ch4-13
#include <iostream>
using namespace std;
int fac(int);
int main()
{
int i;
for(i=1;i<=5;i++)
cout<<i<<"!="<<fac(i)<<endl;
return 0;
}
int fac(int n)
{static int f=1;
f=f*n;
return f;
}
Ch4-14
#include <iostream>
using namespace std;
int max(int,int);
int main()
{extern int a,b;
cout<<max(a,b)<<endl;
return 0;
}
int a=15,b=-7;
int max(int x,int y)
{int z;
z=x>y?x:y;
return z;
}
Ch4-15-1
//file1.cpp
#include <iostream>
using namespace std;
int main()
{ extern int max(int,int);
int a,b;
cin>>a>>b;
cout<<max(a,b)<<endl;
return 0;
}
Ch4-15-2
//file2.cpp
int max(int x,int y)
{
int z;
z=x>y?x:y;
return z;
}
Ch4-16
#include <iostream>
using namespace std;
#define RUN
int main()
{int x=1,y=2,z=3;
#ifndef RUN
cout<<"x="<<x<<",y="<<y<<",z="<<z<<endl;
#endif
cout<<x*y*z<<endl;
return 0;
}
第五章
Ch5-1
#include <iostream>
using namespace std;
int main()
{int i,a[10];
for (i=0;i<=9;i++)
a[i]=i;
for (i=9;i>=0;i--)
cout<<a[i]<<" ";
cout<<endl;
return 0;
}
Ch5-2
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int i;
int f[20]={1,1};
for(i=2;i<20;i++)
f[i]=f[i-2]+f[i-1];
for(i=0;i<20;i++)
{
if(i%5==0) cout<<endl;
cout<<setw(8)<<f[i];
}
cout<<endl;
return 0;
}
Ch5-3
#include <iostream>
using namespace std;
int main()
{
int a[10];
int i,j,t;
cout<<"input 10 numbers :"<<endl;
for (i=0;i<10;i++)
cin>>a[i];
cout<<endl;
for (j=0;j<9;j++)
for(i=0;i<9-j;i++)
if (a[i]>a[i+1])
{t=a[i];a[i]=a[i+1];a[i+1]=t;}
cout<<"the sorted numbers :"<<endl;
for(i=0;i<10;i++)
cout<<a[i]<<" ";
cout<<endl;
return 0;
}
Ch5-4
#include <iostream>
using namespace std;
int main()
{
int a[2][3]={{1,2,3},{4,5,6}};
int b[3][2],i,j;
cout<<"array a:"<<endl;
for (i=0;i<=1;i++)
{
for (j=0;j<=2;j++)
{ cout<<a[i][j]<<" ";
b[j][i]=a[i][j];
}
cout<<endl;
}
cout<<"array b:"<<endl;
for (i=0;i<=2;i++)
{
for(j=0;j<=1;j++)
cout<<b[i][j]<<" ";
cout<<endl;
}
return 0;
}
Ch5-5
#include <iostream>
using namespace std;
int main()
{ int i,j,row=0,colum=0,max;
int a[3][4]={{5,12,23,56},{19,28,37,46},{-12,-34,6,8}};
max=a[0][0];
for (i=0;i<=2;i++)
for (j=0;j<=3;j++)
if (a[i][j]>max)
{max=a[i][j];
row=i;
colum=j;
}
cout<<"max="<<max<<",row="<<row<<",colum="<<colum<<endl;
return 0;
}
Ch5-6
#include <iostream>
using namespace std;
int main()
{ int max_value(int x,int max);
int i,j,row=0,colum=0,max;
int a[3][4]={{5,12,23,56},{19,28,37,46},{-12,-34,6,8}};
max=a[0][0];
for (i=0;i<=2;i++)
for (j=0;j<=3;j++)
{
max=max_value(a[i][j],max);
if(max==a[i][j])
{ row=i;
colum=j;
}
}
cout<<"max="<<max<<",row="<<row<<",colum="<<colum<<endl;
return 0;
}
int max_value(int x,int max)
{ if(x>max) return x;
else return max;
}
Ch5-7
#include <iostream>
using namespace std;
int main()
{void select_sort(int array[],int n);
int a[10],i;
cout<<"enter the originl array:"<<endl;
for(i=0;i<10;i++)
cin>>a[i];
cout<<endl;
select_sort(a,10);
cout<<"the sorted array:"<<endl;
for(i=0;i<10;i++)
cout<<a[i]<<" ";
cout<<endl;
return 0;
}
void select_sort(int array[],int n)
{int i,j,k,t;
for(i=0;i<n-1;i++)
{k=i;
for(j=i+1;j<n;j++)
if(array[j]<array[k]) k=j;
t=array[k];array[k]=array[i];array[i]=t;
}
}
Ch5-8
#include <iostream>
using namespace std;
int main()
{int max_value(int array[][4]);
int a[3][4]={{11,32,45,67},{22,44,66,88},{15,72,43,37}};
cout<<"max value is "<<max_value(a)<<endl;
return 0;
}
int max_value(int array[][4])
{int i,j,max;
max=array[0][0];
for( i=0;i<3;i++)
for(j=0;j<4;j++)
if(array[i][j]>max) max=array[i][j];
return max;
}
Ch5-9
#include <iostream>
using namespace std;
int main()
{char diamond[][5]={{' ',' ','*'},{' ','*',' ','*'},{'*',' ',' ',' ','*'},{' ','*',' ','*'},{' ',' ','*'}};
int i,j;
for (i=0;i<5;i++)
{for (j=0;j<5;j++)
cout<<diamond[i][j];
cout<<endl;
}
return 0;
}
Ch5-10
#include <iostream>
#include <string>
using namespace std;
int main()
{ void max_string(char str[][30],int i);
int i;
char country_name[3][30];
for(i=0;i<3;i++)
cin>>country_name[i];
max_string(country_name,3);
return 0;
}
void max_string(char str[][30],int n)
{
int i;
char string[30];
strcpy(string,str[0]);
for(i=0;i<n;i++)
if(strcmp(str[i],string)>0) strcpy(string,str[i]);
cout<<endl<<"the largest string is∶"<<string<<endl;
}
Ch5-11
#include <iostream>
#include <string>
using namespace std;
int main()
{void swap(string,string);
string string1,string2,string3,temp;
cout<<"please input three strings:";
cin>>string1>>string2>>string3;
if(string2>string3) {temp=string2;string2=string3;string3=temp;}
if(string1<string2) cout<<string1<<" "<<string2<<" "<<string3<<endl;
else if(string1<string3) cout<<string2<<" "<<string1<<" "<<string3<<endl;
else cout<<string2<<" "<<string3<<" "<<string1<<endl;
return 0;
}
Ch5-12
#include <iostream>
#include <string>
using namespace std;
string name[50],num[50];
int n;
int main()
{void input_data();
void search(string find_name);
string find_name;
cout<<"please input number of this class:";
cin>>n;
input_data();
cout<<"please input name you want find:";
cin>>find_name;
search(find_name);
return 0;
}
void input_data()
{
int i;
for (i=0;i<n;i++)
{cout<<"input name and number of student "<<i+1<<":";
cin>>name[i]>>num[i];}
}
void search(string find_name)
{int i;
bool flag=false;
for(i=0;i<n;i++)
if(name[i]==find_name)
{cout<<name[i]<<" "<<num[i]<<endl;flag=true;break;}
if(flag==false) cout<<"can't find this name";
}
第六章
Ch6-1
#include <iostream>
using namespace std;
int main()
{int a,b;
int *pointer_1,*pointer_2;
a=100;b=10;
pointer_1=&a; //把變量a的地址賦給pointer_1
pointer_2=&b; //把變量b的地址賦給pointer_2
cout<<a<<" "<<b<<endl;
cout<<*pointer_1<<" "<<*pointer_2<<endl;
return 0;
}
Ch6-2
#include <iostream>
using namespace std;
int main()
{
int *p1,*p2,*p,a,b;
cin>>a>>b;
p1=&a;
p2=&b;
if(a<b)
{p=p1;p1=p2;p2=p;}
cout<<"a="<<a<<" b="<<b<<endl;
cout<<"max="<<*p1<<" min="<<*p2<<endl;
return 0;
}
Ch6-3-1
#include <iostream>
using namespace std;
int main()
{ void swap(int *p1,int *p2);
int *pointer_1,*pointer_2,a,b;
cin>>a>>b;
pointer_1=&a;
pointer_2=&b;
if(a<b) swap(pointer_1,pointer_2);
cout<<"max="<<a<<" min="<<b<<endl;
return 0;
}
void swap(int *p1,int *p2)
{ int temp;
temp=*p1;
*p1=*p2;
*p2=temp;
}
Ch6-3-2
#include <iostream>
using namespace std;
int main()
{ void swap(int *p1,int *p2);
int *pointer_1,*pointer_2,a,b;
cin>>a>>b;
pointer_1=&a;
pointer_2=&b;
if(a<b) swap(pointer_1,pointer_2);
cout<<"max="<<a<<" min="<<b<<endl;
return 0;
}
void swap(int *p1,int *p2)
{ int *temp;
temp=p1;
p1=p2;
p2=temp;
}
Ch6-4
#include <iostream>
using namespace std;
int main()
{ void exchange(int *,int *,int *);
int a,b,c,*p1,*p2,*p3;
cin>>a>>b>>c;
p1=&a;p2=&b;p3=&c;
exchange(p1,p2,p3);
cout<<a<<" "<<b<<" "<<c<<endl;
return 0;
}
void exchange(int *q1,int *q2,int *q3)
{void swap(int *,int *);
if(*q1<*q2) swap(q1,q2);
if(*q1<*q3) swap(q1,q3);
if(*q2<*q3) swap(q2,q3);
}
void swap(int *pt1,int *pt2)
{int temp;
temp=*pt1;
*pt1=*pt2;
*pt2=temp;
}
Ch6-5-1
#include <iostream>
using namespace std;
int main()
{
int a[10];
int i;
for(i=0;i<10;i++)
cin>>a[i];
cout<<endl;
for(i=0;i<10;i++)
cout<<a[i]<<" ";
cout<<endl;
return 0;
}
Ch6-5-2
#include <iostream>
using namespace std;
int main()
{
int a[10];
int i;
for(i=0;i<10;i++)
cin>>*(a+i);
cout<<endl;
for(i=0;i<10;i++)
cout<<*(a+i)<<" ";
cout<<endl;
return 0;
}
Ch6-5-3
#include <iostream>
using namespace std;
int main()
{
int a[10];
int i,*p=a ;
for(i=0;i<10;i++)
cin>>*(p+i);
cout<<endl;
for(p=a;p<(a+10);p++)
cout<<*p<<" ";
cout<<endl;
return 0;
}
Ch6-6
#include <iostream>
using namespace std;
int main()
{void select_sort(int *p,int n); //函數聲明
int a[10],i;
double c=3.6,*q=&c;
cout<<"enter the originl array:"<<endl;
for(i=0;i<10;i++) //輸入10個數
cin>>a[i];
cout<<endl;
select_sort(a,10); //函數調用,數組名作實參
cout<<"the sorted array:"<<endl;
for(i=0;i<10;i++) //輸出10已排好序的數
cout<<a[i]<<" ";
cout<<endl;
return 0;
}
void select_sort(int *p,int n)
{int i,j,k,t;
for(i=0;i<n-1;i++)
{k=i;
for(j=i+1;j<n;j++)
if(*(p+j)<*(p+k)) k=j;
t=*(p+k);*(p+k)=*(p+i);*(p+i)=t;
}
}
Ch6-7
#include <iostream>
using namespace std;
int main()
{ int a[3][4]={1,3,5,7,9,11,13,15,17,19,21,23};
int *p;
for(p=a[0];p<a[0]+12;p++)
cout<<*p<<" ";
cout<<endl;
return 0;
}
Ch6-8
#include <iostream>
using namespace std;
int main()
{ int a[3][4]={1,3,5,7,9,11,13,15,17,19,21,23};
int (*p)[4],i,j;
cin>>i>>j;
p=a;
cout<<*(*(p+i)+j)<<endl;
return 0;
}
Ch6-9
#include <iostream>
using namespace std;
int main()
{ void output(int (*p)[4]);
int a[3][4]={1,3,5,7,9,11,13,15,17,19,21,23};
output(a);
return 0;
}
void output(int (*p)[4])
{ int i,j;
for(i=0;i<3;i++)
for(j=0;j<4;j++)
cout<<*(*(p+i)+j)<<" ";
cout<<endl;
}
Ch6-10
#include <iostream>
using namespace std;
int main()
{ char str[]="I love CHINA!";
cout<<str<<endl;
return 0;
}
Ch6-11
#include <string>
#include <iostream>
using namespace std;
int main()
{ string str="I love CHINA!";
cout<<str<<endl;
return 0;
}
Ch6-12
#include <iostream>
using namespace std;
int main()
{ char *str="I love CHINA!";
cout<<str<<endl;
return 0;
}
Ch6-13
#include <iostream>
using namespace std;
int main()
{ char str1[]="I love CHINA!",str2[20],*p1,*p2;
p1=str1;p2=str2;
for(;*p1!='\0';p1++,p2++)
*p2=*p1;
*p2='\0';
p1=str1;p2=str2;
cout<<"str1 is: "<<p1<<endl;
cout<<"str2 is: "<<p2<<endl;
return 0;
}
Ch6-14-1
#include <iostream>
using namespace std;
int main()
{int max(int x,int y); //函數聲明
int a,b,m;
cin>>a>>b;
m=max(a,b); //調用函數max,求出最大值,賦予m
cout<<"max="<<m<<endl;
return 0;
}
int max(int x,int y)
{int z;
if(x>y) z=x;
else z=y;
return(z);
}
Ch6-14-2
#include <iostream>
using namespace std;
int main()
{int max(int x,int y);
int (*p)(int,int);
int a,b,m;
p=max;
cin>>a>>b;
m=p(a,b);
cout<<"max="<<m<<endl;
return 0;
}
int max(int x,int y)
{int z;
if(x>y) z=x;
else z=y;
return(z);
}
Ch6-15-1
#include <iostream>
using namespace std;
int main()
{ void sort(char *name[],int n);
void print(char *name[],int n);
char *name[]={"BASIC","FORTRAN","C++","PASCAL","COBOL"};
int n=5;
sort(name,n);
print(name,n);
return 0;
}
void sort(char *name[],int n)
{char *temp;
int i,j,k;
for(i=0;i<n-1;i++)
{k=i;
for(j=i+1;j<n;j++)
if(strcmp(name[k],name[j])>0) k=j;
if(k!=i)
{ temp=name[i];name[i]=name[k];name[k]=temp;}
}
}
void print(char *name[],int n)
{int i;
for(i=0;i<n;i++)
cout<<name[i]<<endl;
}
Ch6-15-2
#include <iostream>
using namespace std;
int main()
{ void sort(char *name[],int n);
void print(char *name[],int n);
char *name[]={"BASIC","FORTRAN","C++","PASCAL","COBOL"};
int n=5;
sort(name,n);
print(name,n);
return 0;
}
void sort(char *name[],int n)
{char *temp;
int i,j,k;
for(i=0;i<n-1;i++)
{k=i;
for(j=i+1;j<n;j++)
if(strcmp(name[k],name[j])>0) k=j;
if(k!=i)
{ temp=name[i];name[i]=name[k];name[k]=temp;}
}
}
void print(char *name[],int n)
{int i=0;
char *p;
p=name[0];
while(i<n)
{p=*(name+i++);
cout<<p<<endl;
}
}
Ch6-16
#include <iostream>
using namespace std;
int main()
{ char **p;
char *name[]={"BASIC","FORTRAN","C++","PASCAL","COBOL"};
p=name+2;
cout<<*p<<endl;
cout<<**p<<endl;
return 0;
}
Ch6-17
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int a=10;
int &b=a;
a=a*a;
cout<<a<<setw(6)<<b<<endl;
b=b/5;
cout<<b<<setw(6)<<a<<endl;
return 0;
}
Ch6-18
#include <iostream>
using namespace std;
int main()
{void swap(int,int);
int i=3;
int j=5;
swap(i,j);
cout<<i<<" "<<j<<endl;
return 0;
}
void swap(int a,int b)
{int temp;
temp=a;
a=b;
b=temp;
}
Ch6-19
#include <iostream>
using namespace std;
int main()
{void swap(int *,int *);
int i=3,j=5;
swap(&i,&j);
cout<<i<<" "<<j<<endl;
return 0;
}
void swap(int *p1,int *p2)
{int temp;
temp=*p1;
*p1=*p2;
*p2=temp;
}
Ch6-20
#include <iostream>
using namespace std;
int main()
{void swap(int &,int &);
int i=3,j=5;
swap(i,j);
cout<<"i="<<i<<" "<<"j="<<j<<endl;
return 0;
}
void swap(int &a,int &b)
{int temp;
temp=a;
a=b;
b=temp;
}
Ch6-21
#include <iostream>
using namespace std;
int main()
{void sort(int &,int &,int &);
int a,b,c,a1,b1,c1;
cout<<"Please enter 3 integers:";
cin>>a>>b>>c;
a1=a;b1=b;c1=c;
sort(a1,b1,c1);
cout<<a<<" "<<b<<" "<<c<<" in sorted order is ";
cout<<a1<<" "<<b1<<" "<<c1<<endl;
return 0;
}
void sort(int &i,int &j,int &k)
{ void change(int &,int &);
if (i>j) change(i,j);
if (i>k) change(i,k);
if (j>k) change(j,k);
}
void change(int &x,int &y)
{ int temp;
temp=x;
x=y;
y=temp;
}
第七章
Ch7-1
#include <iostream>
using namespace std;
struct Date
{int month;
int day;
int year;
};
struct Student
{int num;
char name[20];
char sex;
Date birthday;
float score;
}student1,student2={10002,"Wang Li",'f',5,23,1982,89.5};
int main()
{student1=student2;
cout<<student1.num<<endl;
cout<<student1.name<<endl;
cout<<student1.sex<<endl;
cout<<student1.birthday.month<<'/'<<student1.birthday.day<<'/';
cout<<student1.birthday.year<<endl;
cout<<student1.score<<endl;
return 0;
}
Ch7-2-1
#include <iostream>
#include <string>
using namespace std;
struct Person
{ char name[20];
int count;
};
int main()
{Person leader[3]={"Li",0,"Zhang",0,"Fun",0};
int i,j;
char leader_name[20];
for(i=0;i<10;i++)
{cin>>leader_name;
for(j=0;j<3;j++)
if(strcmp(leader_name,leader[j].name)==0) leader[j].count++;
}
cout<<endl;
for(i=0;i<3;i++)
{cout<<leader[i].name<<":"<<leader[i].count<<endl;}
return 0;
}
Ch7-2-2
#include <iostream>
#include <string>
using namespace std;
struct Person
{ string name;
int count;
};
int main()
{Person leader[3]={"Li",0,"Zhang",0,"Fun",0};
int i,j;
string leader_name;
for(i=0;i<3;i++)
{cin>>leader_name;
for(j=0;j<10;j++)
if(leader_name==leader[j].name) leader[j].count++;
}
cout<<endl;
for(i=0;i<3;i++)
{cout<<leader[i].name<<":"<<leader[i].count<<endl;}
return 0;
}
Ch7-3
#include <iostream>
#include <string>
using namespace std;
int main()
{struct student
{int num;
string name;
char sex;
float score;
};
student stu;
student *p=&stu;
stu.num=10301;
stu.name="Wang Fun";
stu.sex='f';
stu.score=89.5;
cout<<stu.num<<" "<<stu.name<<" "<<stu.sex<<" "<<stu.score<<endl;
cout<<(*p).num<<" "<<(*p).name<<" "<<(*p).sex<<" "<<(*p).score<<endl;
return 0;
}
Ch7-4
#define NULL 0
#include <iostream>
using namespace std;
struct Student
{long num;
float score;
struct Student *next;
};
int main()
{Student a,b,c,*head,*p;
a.num=31001; a.score=89.5;
b.num=31003; b.score=90;
c.num=31007; c.score=85;
head=&a;
a.next=&b;
b.next=&c;
c.next=NULL;
p=head;
do
{cout<<p->num<<" "<<p->score<<endl;
p=p->next;
} while(p!=NULL);
return 0;
}
Ch7-5-1
#include <iostream>
#include <string>
using namespace std;
struct Student
{int num;
string name;
float score[3];
};
int main()
{void print(Student);
Student stu;
stu.num=12345;
stu.name="Li Fung";
stu.score[0]=67.5;
stu.score[1]=89;
stu.score[2]=78.5;
print(stu);
return 0;
}
void print(Student stu)
{cout<<stu.num<<" "<<stu.name<<" "<<stu.score[0]<<" "
<<stu.score[1]<<" "<<stu.score[2]<<endl;
}
Ch7-5-2
#include <iostream>
#include <string>
using namespace std;
struct Student
{int num;
string name;
float score[3];
}stu={12345,"Li Fung",67.5,89,78.5};
int main()
{void print(Student *);
Student *pt=&stu;
print(pt);
return 0;
}
void print(Student *p)
{cout<<p->num<<" "<<p->name<<" "<<p->score[0]<<" "
<<p->score[1]<<" "<<p->score[2]<<endl;
}
Ch7-5-3
#include <iostream>
#include <string>
using namespace std;
struct Student
{int num;
string name;
float score[3];
}stu={12345,"Li Fung",67.5,89,78.5};
int main()
{void print(Student &);
print(stu);
return 0;
}
void print(Student &stud)
{cout<<stud.num<<" "<<stud.name<<" "<<stud.score[0]<<" "<<stud.score[1]<<" "<<stud.score[2]<<endl;
}
Ch7-6
#include <iostream>
#include <string>
using namespace std;
struct Student
{string name;
int num;
char sex;
};
int main ( )
{
Student *p;
p=new Student;
p->name="Wang Fun";
p->num=10123;
p->sex='m';
cout<<p->name<<endl<<p->num<<endl<<p->sex<<endl;
delete p;
return 0;
}
Ch7-7
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
struct S
{int num;
string name;
char sex;
char job;
union
{int grade;
char position[10];
}category;
}person[2];
int main()
{ int i;
for(i=0;i<2;i++)
{cin>>person[i].num>>person[i].name>>person[i].sex>>person[i].job;
if(person[i].job=='s') cin>>person[i].category.grade;
else if (person[i].job=='t') cin>>person[i].category.position;
else cout<<"input error!";
}
cout<<endl<<"No. Name sex job grade/position"<<endl;
for(i=0;i<2;i++)
{if (person[i].job=='s')
cout<<person[i].num<<setw(6)<<person[i].name<<" "<<person[i].sex
<<" "<<person[i].job<<setw(10)<<person[i].category.grade<<endl;
else
cout<<person[i].num<<setw(6)<<person[i].name<<" "<<person[i].sex
<<" "<<person[i].job<<setw(10)<<person[i].category.position<<endl;
}
return 0;
}
Ch7-8
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{enum color {red,yellow,blue,white,black};
color pri;
int i,j,k,n=0,loop;
for (i=red;i<=black;i++)
for (j=red;j<=black;j++)
if (i!=j)
{ for (k=red;k<=black;k++)
if ((k!=i) && (k!=j))
{n=n+1;
cout<<n;
for (loop=1;loop<=3;loop++)
{switch (loop)
{case 1: pri=color(i);break;
case 2: pri=color(j);break;
case 3: pri=color(k);break;
default:break;
}
switch (pri)
{case red: cout<<setw(8)<<"red"; break;
case yellow: cout<<setw(8)<<"yellow"; break;
case blue: cout<<setw(8)<<"blue"; break;
case white: cout<<setw(8)<<"white"; break;
case black: cout<<setw(8)<<"black"; break;
default : break;
}
}
cout<<endl;
}
}
cout<<"total:"<<n<<endl;
return 0;
}
第八章
Ch8-1
#include <iostream>
using namespace std;
class Time
{public:
int hour;
int minute;
int sec;
};
int main()
{ Time t1;
Time &t2=t1;
cin>>t2.hour;
cin>>t2.minute;
cin>>t1.sec;
cout<<t1.hour<<":"<<t1.minute<<":"<<t2.sec<<endl;
}
Ch8-2-a
#include <iostream>
using namespace std;
class Time
{public:
int hour;
int minute;
int sec;
};
int main()
{Time t1;
cin>>t1.hour;
cin>>t1.minute;
cin>>t1.sec;
cout<<t1.hour<<":"<<t1.minute<<":"<<t1.sec<<endl;
Time t2;
cin>>t2.hour;
cin>>t2.minute;
cin>>t2.sec;
cout<<t2.hour<<":"<<t2.minute<<":"<<t2.sec<<endl;
return 0;
}
Ch8-2-b
#include <iostream>
using namespace std;
class Time
{public:
int hour;
int minute;
int sec;
};
int main()
{
void set_time(Time&) ;
void show_time(Time&);
Time t1;
set_time(t1);
show_time(t1);
Time t2;
set_time(t2);
show_time(t2);
return 0;
}
void set_time(Time& t)
{
cin>>t.hour;
cin>>t.minute;
cin>>t.sec;
}
void show_time(Time& t)
{
cout<<t.hour<<":"<<t.minute<<":"<<t.sec<<endl;
}
Ch8-2-c
#include <iostream>
using namespace std;
class Time
{public:
int hour;
int minute;
int sec;
};
int main()
{
void set_time(Time&,int hour=0,int minute=0,int sec=0);
void show_time(Time&);
Time t1;
set_time(t1,12,23,34);
show_time(t1);
Time t2;
set_time(t2);
show_time(t2);
return 0;
}
void set_time(Time& t,int hour=9,int minute=30,int sec=0)
{
t.hour=hour;
t.minute=minute;
t.sec=sec;
}
void show_time(Time& t)
{
cout<<t.hour<<":"<<t.minute<<":"<<t.sec<<endl;
}
Ch8-3
#include <iostream>
using namespace std;
class Time
{public:
void set_time() ;
void show_time();
private:
int hour;
int minute;
int sec;
};
int main()
{
Time t1;
t1.set_time();
t1.show_time();
Time t2;
t2.set_time();
t2.show_time();
return 0;
}
void Time::set_time()
{
cin>>hour;
cin>>minute;
cin>>sec;
}
void Time::show_time()
{
cout<<hour<<":"<<minute<<":"<<sec<<endl;
}
Ch8-4
#include <iostream>
using namespace std;
class Array_max
{public:
void set_value();
void max_value();
void show_value();
private:
int array[10];
int max;
};
void Array_max::set_value()
{ int i;
for (i=0;i<10;i++)
cin>>array[i];
}
void Array_max::max_value()
{int i;
max=array[0];
for (i=1;i<10;i++)
if(array[i]>max) max=array[i];
}
void Array_max::show_value()
{cout<<"max="<<max;
}
int main()
{Array_max arrmax;
arrmax.set_value();
arrmax.max_value();
arrmax.show_value();
return 0;
}
Main
//main.cpp
#include <iostream>
#include "student.h"
int main()
{Student stud;
stud.display();
return 0;
}
Student
//student.cpp 這是源文件,在此文件中進行函數的定義
#include <iostream>
#include "student.h" //不要漏寫此行
void Student::display( ) //在類外定義display類函數
{cout<<"num:"<<num<<endl;
cout<<"name:"<<name<<endl;
cout<<"sex:"<<sex<<endl;
}
第九章
Ch9-1
#include <iostream>
using namespace std;
class Time
{public:
Time()
{hour=0;
minute=0;
sec=0;
}
void set_time();
void show_time();
private:
int hour;
int minute;
int sec;
};
void Time::set_time()
{cin>>hour;
cin>>minute;
cin>>sec;
}
void Time::show_time()
{
cout<<hour<<":"<<minute<<":"<<sec<<endl;
}
int main()
{
Time t1;
t1.show_time();
Time t2;
t2.show_time();
return 0;
}
Ch9-1-1
#include <iostream>
using namespace std;
class Time
{public:
Time()
{hour=0;
minute=0;
sec=0;
}
void set_time();
void show_time();
private:
int hour;
int minute;
int sec;
};
int main()
{
Time t1;
t1.set_time();
t1.show_time();
Time t2;
t2.show_time();
return 0;
}
void Time::set_time()
{cin>>hour;
cin>>minute;
cin>>sec;
}
void Time::show_time()
{
cout<<hour<<":"<<minute<<":"<<sec<<endl;
}
Ch9-1-2
#include <iostream>
using namespace std;
class Time
{public:
Time();
void show_time();
private:
int hour;
int minute;
int sec;
};
Time::Time()
{
hour=0;
minute=0;
sec=0;
}
int main()
{
Time t1;
t1.show_time();
Time t2;
t2.show_time();
return 0;
}
void Time::show_time()
{
cout<<hour<<":"<<minute<<":"<<sec<<endl;
}
Ch9-2
#include <iostream>
using namespace std;
class Box
{public:
Box(int,int,int);
int volume();
private:
int height;
int width;
int length;
};
Box::Box(int h,int w,int len)
{height=h;
width=w;
length=len;
}
int Box::volume()
{return(height*width*length);
}
int main()
{
Box box1(12,25,30);
cout<<"The volume of box1 is "<<box1.volume()<<endl;
Box box2(15,30,21);
cout<<"The volume of box2 is "<<box2.volume()<<endl;
return 0;
}
Ch9-3
#include <iostream>
using namespace std;
class Box
{public:
Box();
Box(int h,int w ,int len):height(h),width(w),length(len){}
int volume();
private:
int height;
int width;
int length;
};
Box::Box()
{height=10;
width=10;
length=10;
}
int Box::volume()
{return(height*width*length);
}
int main()
{
Box box1;
cout<<"The volume of box1 is "<<box1.volume()<<endl;
Box box2(15,30,25);
cout<<"The volume of box2 is "<<box2.volume()<<endl;
return 0;
}
Ch9-4
#include <iostream>
using namespace std;
class Box
{public:
Box(int w=10,int h=10,int len=10);
int volume();
private:
int height;
int width;
int length;
};
Box::Box(int w,int h,int len)
{height=h;
width=w;
length=len;
}
int Box::volume()
{return(height*width*length);
}
int main()
{
Box box1;
cout<<"The volume of box1 is "<<box1.volume()<<endl;
Box box2(15);
cout<<"The volume of box2 is "<<box2.volume()<<endl;
Box box3(15,30);
cout<<"The volume of box3 is "<<box3.volume()<<endl;
Box box4(15,30,20);
cout<<"The volume of box4 is "<<box4.volume()<<endl;
return 0;
}
Ch9-4-2
#include <iostream>
using namespace std;
class Box
{public:
Box(int=10,int=10,int=10);
int volume();
private:
int height;
int width;
int length;
};
Box::Box(int h,int w,int len):height(h),width(w),length(len)
{ }
int Box::volume()
{return(height*width*length);
}
int main()
{
Box box1;
cout<<"The volume of box1 is "<<box1.volume()<<endl;
Box box2(15);
cout<<"The volume of box2 is "<<box2.volume()<<endl;
Box box3(15,30);
cout<<"The volume of box3 is "<<box3.volume()<<endl;
Box box4(15,30,20);
cout<<"The volume of box4 is "<<box4.volume()<<endl;
return 0;
}
Ch9-5
#include <iostream>
#include <string>
using namespace std;
class Student
{public:
Student(int n,string nam,char s)
{num=n;
name=nam;
sex=s;
cout<<"Constructor called."<<endl;
}
~Student()
{cout<<"Destructor called."<<endl;}
void display()
{cout<<"num:"<<num<<endl;
cout<<"name:"<<name<<endl;
cout<<"sex:"<<sex<<endl<<endl;
}
private:
int num;
string name;
char sex;
};
int main()
{Student stud1(10010,"Wang_li",'f');
stud1.display();
Student stud2(10011,"Zhang_fun",'m');
stud2.display();
return 0;
}
Ch9-6
#include <iostream>
using namespace std;
class Box
{public:
Box(int h=10,int w=12,int len=15):height(h),width(w),length(len){ }
int volume();
private:
int height;
int width;
int length;
};
int Box::volume()
{return(height*width*length);
}
int main()
{
Box a[3]={
Box(10,12,15),
Box(15,18,20),
Box(16,20,26)
};
cout<<"volume of a[0] is "<<a[0].volume()<<endl;
cout<<"volume of a[0] is "<<a[1].volume()<<endl;
cout<<"volume of a[0] is "<<a[2].volume()<<endl;
return 0;
}
Ch9-7
#include <iostream>
using namespace std;
class Time
{public:
Time(int,int,int);
int hour;
int minute;
int sec;
void get_time();
};
Time::Time(int h,int m,int s)
{hour=h;
minute=m;
sec=s;
}
void Time::get_time()
{cout<<hour<<":"<<minute<<":"<<sec<<endl;}
int main()
{Time t1(10,13,56);
int *p1=&t1.hour;
cout<<*p1<<endl;
t1.get_time();
Time *p2=&t1;
p2->get_time();
void (Time::*p3)();
p3=&Time::get_time;
(t1.*p3)();
return 0;
}
Ch9-8
#include <iostream>
using namespace std;
class Time
{public:
Time(int,int,int);
int hour;
int minute;
int sec;
};
Time::Time(int h,int m,int s)
{hour=h;
minute=m;
sec=s;
}
void fun(Time &t)
{t.hour=18;}
int main()
{Time t1(10,13,56);
fun(t1);
cout<<t1.hour<<endl;
return 0;
}
Ch9-9
#include <iostream>
using namespace std;
class Box
{public:
Box(int=10,int=10,int=10);
int volume();
private:
int height;
int width;
int length;
};
Box::Box(int h,int w,int len)
{height=h;
width=w;
length=len;
}
int Box::volume()
{return(height*width*length);
}
int main()
{
Box box1(15,30,25),box2;
cout<<"The volume of box1 is "<<box1.volume()<<endl;
box2=box1;
cout<<"The volume of box2 is "<<box2.volume()<<endl;
return 0;
}
Ch9-9-2
#include <iostream>
using namespace std;
class Box
{public:
Box(int=10,int=10,int=10);
int volume();
private:
int height;
int width;
int length;
};
Box::Box(int h,int w,int len)
{height=h;
width=w;
length=len;
}
int Box::volume()
{return(height*width*length);
}
int main()
{Box box1(15,30,25);
cout<<"The volume of box1 is "<<box1.volume()<<endl;
Box box2=box1,box3=box2;
cout<<"The volume of box2 is "<<box2.volume()<<endl;
cout<<"The volume of box3 is "<<box3.volume()<<endl;
return 0;
}
Ch9-10
#include <iostream>
using namespace std;
class Box
{public:
Box(int,int);
int volume();
static int height;
int width;
int length;
};
Box::Box(int w,int len)
{
width=w;
length=len;
}
int Box::volume()
{return(height*width*length);
}
int Box::height=10;
int main()
{
Box a(15,20),b(20,30);
cout<<a.height<<endl;
cout<<b.height<<endl;
cout<<Box::height<<endl;
cout<<a.volume()<<endl;
return 0;
}
Ch9-11
#include <iostream>
using namespace std;
class Student
{public:
Student(int,int,int);
void total();
static float average();
private:
int num;
int age;
float score;
static float sum;
static int count;
};
Student::Student(int m,int a,int s)
{num=m;
age=a;
score=s;
}
void Student::total()
{
sum+=score;
count++;
}
float Student::average()
{ return(sum/count);
}
float Student::sum=0;
int Student::count=0;
int main()
{
Student stud[3]={
Student(1001,18,70),
Student(1002,19,79),
Student(1005,20,98)
};
int n;
cout<<"please input the number of students:";
cin>>n;
for(int i=0;i<n;i++)
stud[i].total();
cout<<"The average score of "<<n<<" students is "<<stud[0].average()<<endl;
return 0;
}
Ch9-12
#include <iostream>
using namespace std;
class Time
{public:
Time(int,int,int);
friend void display(Time &);
private:
int hour;
int minute;
int sec;
};
Time::Time(int h,int m,int s)
{hour=h;
minute=m;
sec=s;
}
void display(Time &t)
{
cout<<t.hour<<":"<<t.minute<<":"<<t.sec<<endl;
}
int main()
{
Time t1(10,13,56);
display(t1);
return 0;
}
Ch9-13
#include <iostream>
using namespace std;
class Date;
class Time
{public:
Time(int,int,int);
void display(const Date&);
private:
int hour;
int minute;
int sec;
};
class Date
{public:
Date(int,int,int);
friend void Time::display(const Date &);
private:
int month;
int day;
int year;
};
Time::Time(int h,int m,int s)
{hour=h;
minute=m;
sec=s;
}
void Time::display(const Date &da)
{cout<<da.month<<"/"<<da.day<<"/"<<da.year<<endl;
cout<<hour<<":"<<minute<<":"<<sec<<endl;
}
Date::Date(int m,int d,int y)
{month=m;
day=d;
year=y;
}
int main()
{Time t1(10,13,56);
Date d1(12,25,2004);
t1.display(d1);
return 0;
}
Ch9-14
#include <iostream>
using namespace std;
template<class numtype>
class Compare
{public:
Compare(numtype a,numtype b)
{x=a;y=b;}
numtype max()
{return (x>y)?x:y;}
numtype min()
{return (x<y)?x:y;}
private:
numtype x,y;
};
int main()
{Compare<int> cmp1(3,7);
cout<<cmp1.max()<<" is the Maximum of two inteder numbers."<<endl;
cout<<cmp1.min()<<" is the Minimum of two inteder numbers."<<endl<<endl;
Compare<float> cmp2(45.78,93.6);
cout<<cmp2.max()<<" is the Maximum of two float numbers."<<endl;
cout<<cmp2.min()<<" is the Minimum of two float numbers."<<endl<<endl;
Compare<char> cmp3('a','A');
cout<<cmp3.max()<<" is the Maximum of two characters."<<endl;
cout<<cmp3.min()<<" is the Minimum of two characters."<<endl;
return 0;
}
第十章
Ch10-1
#include <iostream>
using namespace std;
class Complex
{public:
Complex(){real=0;imag=0;}
Complex(double r,double i){real=r;imag=i;}
Complex complex_add(Complex &c2);
void display();
private:
double real;
double imag;
};
Complex Complex::complex_add(Complex &c2)
{return Complex(real+c2.real,imag+c2.imag);}
void Complex::display()
{cout<<"("<<real<<","<<imag<<"i)"<<endl;}
int main()
{Complex c1(3,4),c2(5,-10),c3;
c3=c1.complex_add(c2);
cout<<"c1="; c1.display();
cout<<"c2="; c2.display();
cout<<"c1+c2="; c3.display();
return 0;
}
Ch10-2
#include <iostream>
using namespace std;
class Complex
{public:
Complex(){real=0;imag=0;}
Complex(double r,double i){real=r;imag=i;}
Complex operator + (Complex &c2);
void display();
private:
double real;
double imag;
};
Complex Complex::operator + (Complex &c2)
{Complex c;
c.real=real+c2.real;
c.imag=imag+c2.imag;
return c;}
void Complex::display()
{cout<<"("<<real<<","<<imag<<"i)"<<endl;}
int main()
{Complex c1(3,4),c2(5,-10),c3;
c3=c1+c2;
cout<<"c1=";c1.display();
cout<<"c2=";c2.display();
cout<<"c1+c2=";c3.display();
return 0;
}
Ch10-2-2
#include <iostream>
using namespace std;
class Complex
{public:
Complex(){real=0;imag=0;}
Complex(double r,double i){real=r;imag=i;}
Complex operator + (Complex &c2);
void display();
private:
double real;
double imag;
};
Complex Complex::operator + (Complex &c2)
{return Complex(real+c2.real,imag+c2.imag);}
void Complex::display()
{cout<<"("<<real<<","<<imag<<"i)"<<endl;}
int main()
{Complex c1(3,4),c2(5,-10),c3;
c3=c1+c2;
cout<<"c1="; c1.display();
cout<<"c2="; c2.display();
cout<<"c1+c2="; c3.display();
return 0;
}
Ch10-3
#include <iostream>
using namespace std;
class Complex
{public:
Complex(){real=0;imag=0;}
Complex(double r){real=r;imag=0;}
Complex(double r,double i){real=r;imag=i;}
friend Complex operator+ (Complex &c1,Complex &c2);
void display();
private:
double real;
double imag;
};
Complex operator+ (Complex &c1,Complex &c2)
{return Complex(c1.real+c2.real, c1.imag+c2.imag);}
void Complex::display()
{cout<<"("<<real<<","<<imag<<"i)"<<endl;}
int main()
{Complex c1(3,4),c2(5,-10),c3;
c3=c1+c2;
cout<<"c1="; c1.display();
cout<<"c2="; c2.display();
cout<<"c1+c2="; c3.display();
return 0;
}
Ch10-4-1
#include <iostream>
using namespace std;
class String //String 是用戶自己指定的類名
{public:
String(){p=NULL;}
String(char *str);
void display();
private:
char *p;
};
String::String(char *str)
{p=str;}
void String::display()
{cout<<p;}
int main()
{String string1("Hello"),string2("Book");
string1.display();
cout<<endl;
string2.display();
return 0;
}
Ch10-4-2(vc)
//本程序適用於VC++ 6.0
#include <iostream.h>
#include <string.h>
class String
{public:
String(){p=NULL;}
String(char *str);
friend bool operator>(String &string1,String &string2);
friend bool operator<(String &string1,String &string2);
friend bool operator==(String &string1,String &string2);
void display();
private:
char *p;
};
String::String(char *str)
{p=str;}
void String::display()
{cout<<p;}
bool operator>(String &string1,String &string2)
{if(strcmp(string1.p,string2.p)>0)
return true;
else return false;
}
int main()
{String string1("Hello"),string2("Book");
cout<<(string1>string2)<<endl;
return 0;
}
Ch10-4-2
#include <iostream>
#include <cstring>
using namespace std;
class String
{public:
String(){p=NULL;}
String(char *str);
friend bool operator>(String &string1,String &string2);
friend bool operator<(String &string1,String &string2);
friend bool operator==(String &string1,String &string2);
void display();
private:
char *p;
};
String::String(char *str)
{p=str;}
void String::display()
{cout<<p;}
bool operator>(String &string1,String &string2)
{if(strcmp(string1.p,string2.p)>0)
return true;
else return false;
}
int main()
{String string1("Hello"),string2("Book");
cout<<(string1>string2)<<endl;
return 0;
}
Ch10-4-3(vc)
//本程序適用於VC++ 6.0
#include <iostream.h>
#include <string.h>
class String
{public:
String(){p=NULL;}
String(char *str);
friend bool operator>(String &string1,String &string2);
friend bool operator<(String &string1,String &string2);
friend bool operator==(String &string1,String &string2);
void display();
private:
char *p;
};
String::String(char *str)
{p=str;}
void String::display()
{cout<<p;}
bool operator>(String &string1,String &string2)
{if(strcmp(string1.p,string2.p)>0)
return true;
else
return false;
}
bool operator<(String &string1,String &string2)
{if(strcmp(string1.p,string2.p)<0)
return true;
else
return false;
}
bool operator==(String &string1,String &string2)
{if(strcmp(string1.p,string2.p)==0)
return true;
else
return false;
}
int main()
{String string1("Hello"),string2("Book"),string3("Computer");
cout<<(string1>string2)<<endl;
cout<<(string1<string3)<<endl;
cout<<(string1==string2)<<endl;
return 0;
}
Ch10-4-3
#include <iostream>
#include <string>
using namespace std;
class String
{public:
String(){p=NULL;}
String(char *str);
friend bool operator>(String &string1,String &string2);
friend bool operator<(String &string1,String &string2);
friend bool operator==(String &string1,String &string2);
void display();
private:
char *p;
};
String::String(char *str)
{p=str;}
void String::display()
{cout<<p;}
bool operator>(String &string1,String &string2)
{if(strcmp(string1.p,string2.p)>0)
return true;
else
return false;
}
bool operator<(String &string1,String &string2)
{if(strcmp(string1.p,string2.p)<0)
return true;
else
return false;
}
bool operator==(String &string1,String &string2)
{if(strcmp(string1.p,string2.p)==0)
return true;
else
return false;
}
int main()
{String string1("Hello"),string2("Book"),string3("Computer");
cout<<(string1>string2)<<endl;
cout<<(string1<string3)<<endl;
cout<<(string1==string2)<<endl;
return 0;
}
Ch10-4-4(vc)
//本程序適用於VC++ 6.0
#include <iostream.h>
#include <string.h>
class String
{public:
String(){p=NULL;}
String(char *str);
friend bool operator>(String &string1,String &string2);
friend bool operator<(String &string1,String &string2);
friend bool operator==(String &string1,String &string2);
void display();
private:
char *p;
};
String::String(char *str)
{p=str;
}
void String::display()
{cout<<p;}
bool operator>(String &string1,String &string2)
{if(strcmp(string1.p,string2.p)>0)
return true;
else
return false;
}
bool operator<(String &string1,String &string2)
{if(strcmp(string1.p,string2.p)<0)
return true;
else
return false;
}
bool operator==(String &string1,String &string2)
{if(strcmp(string1.p,string2.p)==0)
return true;
else
return false;
}
void compare(String &string1,String &string2)
{if(operator>(string1,string2)==1)
{string1.display();cout<<">";string2.display();}
else
if(operator<(string1,string2)==1)
{string1.display();cout<<"<";string2.display();}
else
if(operator==(string1,string2)==1)
{string1.display();cout<<"=";string2.display();}
cout<<endl;
}
int main()
{String string1("Hello"),string2("Book"),string3("Computer"),string4("Hello");
compare(string1,string2);
compare(string2,string3);
compare(string1,string4);
return 0;
}
Ch10-4-4
#include <iostream>
#include <cstring>
using namespace std;
class String
{public:
String(){p=NULL;}
String(char *str);
friend bool operator>(String &string1,String &string2);
friend bool operator<(String &string1,String &string2);
friend bool operator==(String &string1,String &string2);
void display();
private:
char *p;
};
String::String(char *str)
{p=str;
}
void String::display()
{cout<<p;}
bool operator>(String &string1,String &string2)
{if(strcmp(string1.p,string2.p)>0)
return true;
else
return false;
}
bool operator<(String &string1,String &string2)
{if(strcmp(string1.p,string2.p)<0)
return true;
else
return false;
}
bool operator==(String &string1,String &string2)
{if(strcmp(string1.p,string2.p)==0)
return true;
else
return false;
}
void compare(String &string1,String &string2)
{if(operator>(string1,string2)==1)
{string1.display();cout<<">";string2.display();}
else
if(operator<(string1,string2)==1)
{string1.display();cout<<"<";string2.display();}
else
if(operator==(string1,string2)==1)
{string1.display();cout<<"=";string2.display();}
cout<<endl;
}
int main()
{String string1("Hello"),string2("Book"),string3("Computer"),string4("Hello");
compare(string1,string2);
compare(string2,string3);
compare(string1,string4);
return 0;
}
Ch10-5
#include <iostream>
using namespace std;
class Time
{public:
Time(){minute=0;sec=0;}
Time(int m,int s):minute(m),sec(s){}
Time operator++();
void display(){cout<<minute<<":"<<sec<<endl;}
private:
int minute;
int sec;
};
Time Time::operator++()
{if(++sec>=60)
{sec-=60;
++minute;}
return *this;
}
int main()
{Time time1(34,0);
for (int i=0;i<61;i++)
{++time1;
time1.display();}
return 0;
}
Ch10-6
#include <iostream>
using namespace std;
class Time
{public:
Time(){minute=0;sec=0;}
Time(int m,int s):minute(m),sec(s){}
Time operator++();
Time operator++(int);
void display(){cout<<minute<<":"<<sec<<endl;}
private:
int minute;
int sec;
};
Time Time::operator++()
{if(++sec>=60)
{sec-=60;
++minute;}
return *this;
}
Time Time::operator++(int)
{Time temp(*this);
sec++;
if(sec>=60)
{sec-=60;
++minute;}
return temp;
}
int main()
{Time time1(34,59),time2;
cout<<" time1 : ";
time1.display();
++time1;
cout<<"++time1: ";
time1.display();
time2=time1++;
cout<<"time1++: ";
time1.display();
cout<<" time2 : ";
time2.display();
return 0;
}
Ch10-7(vc)
//本程序適用於VC++ 6.0
#include <iostream.h>
class Complex
{public:
Complex(){real=0;imag=0;}
Complex(double r,double i){real=r;imag=i;}
Complex operator + (Complex &c2);
friend ostream& operator << (ostream&,Complex&);
private:
double real;
double imag;
};
Complex Complex::operator + (Complex &c2)
{return Complex(real+c2.real,imag+c2.imag);}
ostream& operator << (ostream& output,Complex& c)
{output<<"("<<c.real<<"+"<<c.imag<<"i)"<<endl;
return output;
}
int main()
{Complex c1(2,4),c2(6,10),c3;
c3=c1+c2;
cout<<c3;
return 0;
}
Ch10-7
#include <iostream>
using namespace std;
class Complex
{public:
Complex(){real=0;imag=0;}
Complex(double r,double i){real=r;imag=i;}
Complex operator + (Complex &c2);
friend ostream& operator << (ostream&,Complex&);
private:
double real;
double imag;
};
Complex Complex::operator + (Complex &c2)
{return Complex(real+c2.real,imag+c2.imag);}
ostream& operator << (ostream& output,Complex& c)
{output<<"("<<c.real<<"+"<<c.imag<<"i)"<<endl;
return output;
}
int main()
{Complex c1(2,4),c2(6,10),c3;
c3=c1+c2;
cout<<c3;
return 0;
}
Ch10-8(vc)
//本程序適用於VC++ 6.0
#include <iostream.h>
class Complex
{public:
friend ostream& operator << (ostream&,Complex&);
friend istream& operator >> (istream&,Complex&);
private:
double real;
double imag;
};
ostream& operator << (ostream& output,Complex& c)
{output<<"("<<c.real;
if(c.imag>=0) output<<"+";
output<<c.imag<<"i)";
return output;
}
istream& operator >> (istream& input,Complex& c)
{cout<<"input real part and imaginary part of complex number:";
input>>c.real>>c.imag;
return input;
}
int main()
{Complex c1,c2;
cin>>c1>>c2;
cout<<"c1="<<c1<<endl;
cout<<"c2="<<c2<<endl;
return 0;
}
Ch10-8
#include <iostream>
using namespace std;
class Complex
{public:
friend ostream& operator << (ostream&,Complex&);
friend istream& operator >> (istream&,Complex&);
private:
double real;
double imag;
};
ostream& operator << (ostream& output,Complex& c)
{output<<"("<<c.real<<"+"<<c.imag<<"i)";
return output;
}
istream& operator >> (istream& input,Complex& c)
{cout<<"input real part and imaginary part of complex number:";
input>>c.real>>c.imag;
return input;
}
int main()
{Complex c1,c2;
cin>>c1>>c2;
cout<<"c1="<<c1<<endl;
cout<<"c2="<<c2<<endl;
return 0;
}
Ch10-9
#include <iostream>
using namespace std;
class Complex
{public:
Complex(){real=0;imag=0;}
Complex(double r,double i){real=r;imag=i;}
operator double() {return real;}
private:
double real;
double imag;
};
int main()
{Complex c1(3,4),c2(5,-10),c3;
double d;
d=2.5+c1;
cout<<d<<endl;
return 0;
}
Ch10-10
#include <iostream>
using namespace std;
class Complex
{public:
Complex(){real=0;imag=0;}
Complex(double r){real=r;imag=0;}
Complex(double r,double i){real=r;imag=i;}
friend Complex operator + (Complex c1,Complex c2);
void display();
private:
double real;
double imag;
};
Complex operator + (Complex c1,Complex c2)
{return Complex(c1.real+c2.real, c1.imag+c2.imag);}
void Complex::display()
{cout<<"("<<real<<"+"<<imag<<"i)"<<endl;}
int main()
{Complex c1(3,4),c2(5,-10),c3;
c3=c1+2.5;
c3.display();
return 0;
}
Ch10-10-2
#include <iostream>
using namespace std;
class Complex
{public:
Complex(){real=0;imag=0;}
Complex(double r){real=r;imag=0;}
Complex(double r,double i){real=r;imag=i;}
operator double(){cout<<return real;}
friend Complex operator + (Complex c1,Complex c2);
void display();
private:
double real;
double imag;
};
Complex operator + (Complex c1,Complex c2)
{return Complex(c1.real+c2.real, c1.imag+c2.imag);}
void Complex::display()
{cout<<"("<<real<<","<<imag<<"i)"<<endl;}
int main()
{Complex c1(3,4),c2(5,-10),c3;
c3=c1+2.5;
c3.display();
return 0;
}
第十一章
Ch11-1
#include <iostream>
#include <string>
using namespace std;
class Student
{public:
void get_value()
{cin>>num>>name>>sex;}
void display( )
{cout<<"num: "<<num<<endl;
cout<<"name: "<<name<<endl;
cout<<"sex: "<<sex<<endl;}
private :
int num;
string name;
char sex;
};
class Student1: public Student
{public:
void get_value_1()
{cin>>age>>addr;}
void display_1()
{ //cout<<"num: "<<num<<endl; //企圖引用基類的私有成員,錯誤
//cout<<"name: "<<name<<endl; //企圖引用基類的私有成員,錯誤
//cout<<"sex: "<<sex<<endl; //企圖引用基類的私有成員,錯誤
cout<<"age: "<<age<<endl; //引用派生類的私有成員,正確
cout<<"address: "<<addr<<endl;} //引用派生類的私有成員,正確
private:
int age;
string addr;
};
int main()
{Student1 stud1;
stud1.get_value();
stud1.get_value_1();
stud1.display();
stud1.display_1();
return 0;
}
Ch11-2
#include <iostream>
#include <string>
using namespace std;
class Student
{public:
void display( )
{cout<<"num: "<<num<<endl;
cout<<"name: "<<name<<endl;
cout<<"sex: "<<sex<<endl;}
private :
int num;
string name;
char sex;
};
class Student1: private Student
{public:
void display_1()
{display();
cout<<"age: "<<age<<endl; //引用派生類的私有成員,正確
cout<<"address: "<<addr<<endl;} //引用派生類的私有成員,正確
private:
int age;
string addr;
};
int main()
{Student1 stud1;
stud1.display_1();
return 0;
}
Ch11-3
#include <iostream>
#include <string>
using namespace std;
class Student //聲明基類
{public: //基類公用成員
void display( );
protected : //基類保護成員
int num;
string name;
char sex;
};
void Student::display( )
{cout<<"num: "<<num<<endl;
cout<<"name: "<<name<<endl;
cout<<"sex: "<<sex<<endl;
}
class Student1: protected Student //用protected繼承方式聲明一個派生類
{public:
void display1( );
private:
int age;
string addr;
};
void Student1::display1( )
{cout<<"num: "<<num<<endl; //引用基類的保護成員,合法
cout<<"name: "<<name<<endl; //引用基類的保護成員,合法
cout<<"sex: "<<sex<<endl; //引用基類的保護成員,合法
cout<<"age: "<<age<<endl; //引用派生類的私有成員,合法
cout<<"address: "<<addr<<endl; //引用派生類的私有成員,合法
int main( )
{Student1 stud1; //stud2是派生類student2的對象
stud1.display1( ); //display是派生類中的公用成員函數
return 0;
}
Ch11-5
#include <iostream>
#include<string>
using namespace std;
class Student //聲明基類
{public: //公用部分
Student(int n,string nam,char s ) //基類構造函數
{num=n;
name=nam;
sex=s; }
~Student( ) { }
protected: //保護部分
int num;
string name;
char sex ; //基類析構函數
};
class Student1: public Student //聲明公用派生類student
{public:
Student1(int n,string nam,char s,int a,char ad[]) : Student(n,nam,s),age(a)
//派生類構造函數
{age=a; //在函數體中只對派生類新增的數據成員初始化
addr=ad;
}
void show( )
{cout<<"num: "<<num<<endl;
cout<<"name: "<<name<<endl;
cout<<"sex: "<<sex<<endl;
cout<<"age: "<<age<<endl;
cout<<"address: "<<addr<<endl<<endl;
}
~Student1( ) { } //派生類析構函數
private: //派生類的私有部分
int age;
string addr;
};
int main( )
{Student1 stud1(10010,"Wang-li",'f',19,"115 Beijing Road,Shanghai");
Student1 stud2(10011,"Zhang-fun",'m',21,"213 Shanghai Road,Beijing");
stud1.show( ); //輸出第一個學生的數據
stud2.show( ); //輸出第二個學生的數據
return 0;
}
Ch11-6
#include <iostream>
#include <string>
using namespace std;
class Student //聲明基類
{public: //公用部分
Student(int n,string nam) //基類構造函數
{num=n;
name=nam;
}
void display() //輸出基類數據成員
{cout<<"num:"<<num<<endl<<"name:"<<name<<endl;}
protected: //保護部分
int num;
string name;
};
class Student1: public Student //用public繼承方式聲明派生類student
{public:
Student1(int n,string nam,int n1,string nam1,int a,string ad)
:Student(n,nam),monitor(n1,nam1) //派生類構造函數
{age=a; //在此處只對派生類新增的數據成員初始化
addr=ad;
}
void show( )
{cout<<"This student is:"<<endl;
display(); //輸出num和name
cout<<"age: "<<age<<endl;
cout<<"address: "<<addr<<endl<<endl;
}
void show_monitor() //輸出子對象的數據成員
{cout<<endl<<"Class monitor is:"<<endl;
monitor.display(); //調用基類成員函數
}
private: //派生類的私有數據
Student monitor; //定義子對象(班長)
int age;
string addr;
};
int main( )
{Student1 stud1(10010,"Wang-li",10001,"Li-sun",19,"115 Beijing Road,Shanghai");
stud1.show( ); //輸出第一個學生的數據
stud1.show_monitor(); //輸出子對象的數據
return 0;
}
Ch11-7
#include <iostream>
#include<string>
using namespace std;
class Student //聲明基類
{public: //公用部分
Student(int n, string nam ) //基類構造函數
{num=n;
name=nam;
}
void display() //輸出基類數據成員
{cout<<"num:"<<num<<endl;
cout<<"name:"<<name<<endl;
}
protected: //保護部分
int num; //基類有兩個數據成員
string name;
};
class Student1: public Student //聲明公用派生類Student1
{public:
Student1(int n,char nam[10],int a):Student(n,nam) //派生類構造函數
{age=a; } //在此處只對派生類新增的數據成員初始化
void show( ) //輸出num,name和age
{display(); //輸出num和name
cout<<"age: "<<age<<endl;
}
private: //派生類的私有數據
int age; //增加一個數據成員
};
class Student2:public Student1 //聲明間接公用派生類student2
{public:
//下面是間接派生類構造函數
Student2(int n, string nam,int a,int s):Student1(n,nam,a)
{score=s;}
void show_all() //輸出全部數據成員
{show(); //輸出num和name
cout<<"score:"<<score<<endl; //輸出age
}
private:
int score; //增加一個數據成員
};
int main( )
{Student2 stud(10010,"Li",17,89);
stud.show_all( ); //輸出學生的全部數據
return 0;
}
Ch11-8
#include <iostream>
#include <string>
using namespace std;
class Teacher //聲明Teacher(教師)類
{public: //公用部分
Teacher(string nam,int a,string t) //構造函數
{name=nam;
age=a;
title=t;}
void display() //輸出教師有關數據
{cout<<"name:"<<name<<endl;
cout<<"age"<<age<<endl;
cout<<"title:"<<title<<endl;
}
protected: //保護部分
string name;
int age;
string title; //職稱
};
class Student //聲明類Student(學生)
{public:
Student(string nam,char s,float sco)
{name1=nam;
sex=s;
score=sco;} //構造函數
void display1() //輸出學生有關數據
{cout<<"name:"<<name1<<endl;
cout<<"sex:"<<sex<<endl;
cout<<"score:"<<score<<endl;
}
protected: //保護部分
string name1;
char sex;
float score; //成績
};
class Graduate:public Teacher,public Student //聲明多重繼承的派生類Graduate
{public:
Graduate(string nam,int a,char s,string t,float sco,float w):
Teacher(nam,a,t),Student(nam,s,sco),wage(w) {}
void show( ) //輸出人員的有關數據
{cout<<"name:"<<name<<endl;
cout<<"age:"<<age<<endl;
cout<<"sex:"<<sex<<endl;
cout<<"score:"<<score<<endl;
cout<<"title:"<<title<<endl;
cout<<"wages:"<<wage<<endl;
}
private:
float wage; //工資
};
int main( )
{Graduate grad1("Wang-li",24,'f',"assistant",89.5,1234.5);
grad1.show( );
return 0;
}
Ch11-9
#include <iostream>
#include <string>
using namespace std;
//定義公共基類Person
class Person
{public:
Person(char *nam,char s,int a) //構造函數
{strcpy(name,nam);sex=s;age=a;}
protected: //保護成員
char name[20];
char sex;
int age;
};
//定義類Teacher
class Teacher:virtual public Person //聲明Person為公用繼承的虛基類
{public:
Teacher(char *nam,char s,int a,char *t):Person(nam,s,a) //構造函數
{strcpy(title,t);
}
protected: //保護成員
char title[10]; //職稱
};
//定義類Student
class Student:virtual public Person //聲明Person為公用繼承的虛基類
{public:
Student(char *nam,char s,int a,float sco): //構造函數
Person(nam,s,a),score(sco){} //初始化表
protected: //保護成員
float score; //成績
};
//定義多重繼承的派生類Graduate
class Graduate:public Teacher,public Student //聲明Teacher和Student類為公用繼承的直接基類
{public:
Graduate(char *nam,char s,int a,char *t,float sco,float w): //構造函數
Person(nam,s,a),Teacher(nam,s,a,t),Student(nam,s,a,sco),wage(w){} //初始化表
void show( ) //輸出研究生的有關數據
{cout<<"name:"<<name<<endl;
cout<<"age:"<<age<<endl;
cout<<"sex:"<<sex<<endl;
cout<<"score:"<<score<<endl;
cout<<"title:"<<title<<endl;
cout<<"wages:"<<wage<<endl;
}
private:
float wage; //工資
};
int main( )
{Graduate grad1("Wang-li",'f',24,"assistant",89.5,1234.5);
grad1.show( );
return 0;
}
第十二章
Ch12-1-1(vc)
#include <iostream.h>
//using namespace std;
//聲明類Point
class Point
{public:
Point(float=0,float=0);
void setPoint(float,float);
float getX() const {return x;}
float getY() const {return y;}
friend ostream & operator<<(ostream &,const Point &);
protected:
float x,y;
};
//定義Point類的成員函數
//Point的構造函數
Point::Point(float a,float b)
{x=a;y=b;}
//設置x和y的坐標值
void Point::setPoint(float a,float b)
{x=a;y=b;}
//輸出點的坐標
ostream & operator<<(ostream &output,const Point &p)
{output<<"["<<p.x<<","<<p.y<<"]"<<endl;
return output;
}
int main()
{Point p(3.5,6.4);
cout<<"x="<<p.getX()<<",y="<<p.getY()<<endl;
p.setPoint(8.5,6.8);
cout<<"p(new):"<<p<<endl;
return 0;
}
Ch12-1-1
#include <iostream>
using namespace std;
//聲明類Point
class Point
{public:
Point(float=0,float=0);
void setPoint(float,float);
float getX() const {return x;}
float getY() const {return y;}
friend ostream & operator<<(ostream &,const Point &);
protected:
float x,y;
};
//定義Point類的成員函數
//Point的構造函數
Point::Point(float a,float b)
{x=a;y=b;}
//設置x和y的坐標值
void Point::setPoint(float a,float b)
{x=a;y=b;}
//輸出點的坐標
ostream & operator<<(ostream &output,const Point &p)
{output<<"["<<p.x<<","<<p.y<<"]"<<endl;
return output;
}
int main()
{Point p(3.5,6.4);
cout<<"x="<<p.getX()<<",y="<<p.getY()<<endl;
p.setPoint(8.5,6.8);
cout<<"p(new):"<<p<<endl;
return 0;
}
Ch12-1-2(vc)
#include <iostream.h>
//using namespace std;
//聲明類Point
class Point
{public:
Point(float=0,float=0);
void setPoint(float,float);
float getX() const {return x;}
float getY() const {return y;}
friend ostream & operator<<(ostream &,const Point &);
protected:
float x,y;
};
//定義Point類的成員函數
//Point的構造函數
Point::Point(float a,float b)
{x=a;y=b;}
//設置x和y的坐標值
void Point::setPoint(float a,float b)
{x=a;y=b;}
//輸出點的坐標
ostream & operator<<(ostream &output,const Point &p)
{output<<"["<<p.x<<","<<p.y<<"]"<<endl;
return output;
}
class Circle:public Point
{public:
Circle(float x=0,float y=0,float r=0);
void setRadius(float);
float getRadius() const;
float area () const;
friend ostream &operator<<(ostream &,const Circle &);
private:
float radius;
};
Circle::Circle(float a,float b,float r):Point(a,b),radius(r){}
void Circle::setRadius(float r)
{radius=r;}
float Circle::getRadius() const {return radius;}
float Circle::area() const
{return 3.14159*radius*radius;}
ostream &operator<<(ostream &output,const Circle &c)
{output<<"Center=["<<c.x<<","<<c.y<<"], Radius="<<c.radius<<", area="<<c.area()<<endl;
return output;
}
int main()
{Circle c(3.5,6.4,5.2);
cout<<"original circle:\nx="<<c.getX()<<", y="<<c.getY()<<", r="<<c.getRadius()
<<", area="<<c.area()<<endl;
c.setRadius(7.5);
c.setPoint(5,5);
cout<<"new circle:\n"<<c;
Point &pRef=c;
cout<<"pRef:"<<pRef;
return 0;
}
Ch12-1-2
#include <iostream>
using namespace std;
//聲明類Point
class Point
{public:
Point(float=0,float=0);
void setPoint(float,float);
float getX() const {return x;}
float getY() const {return y;}
friend ostream & operator<<(ostream &,const Point &);
protected:
float x,y;
};
//定義Point類的成員函數
//Point的構造函數
Point::Point(float a,float b)
{x=a;y=b;}
//設置x和y的坐標值
void Point::setPoint(float a,float b)
{x=a;y=b;}
//輸出點的坐標
ostream & operator<<(ostream &output,const Point &p)
{output<<"["<<p.x<<","<<p.y<<"]"<<endl;
return output;
}
class Circle:public Point
{public:
Circle(float x=0,float y=0,float r=0);
void setRadius(float);
float getRadius() const;
float area () const;
friend ostream &operator<<(ostream &,const Circle &);
private:
float radius;
};
Circle::Circle(float a,float b,float r):Point(a,b),radius(r){}
void Circle::setRadius(float r)
{radius=r;}
float Circle::getRadius() const {return radius;}
float Circle::area() const
{return 3.14159*radius*radius;}
ostream &operator<<(ostream &output,const Circle &c)
{output<<"Center=["<<c.x<<","<<c.y<<"], Radius="<<c.radius<<", area="<<c.area()<<endl;
return output;
}
int main()
{Circle c(3.5,6.4,5.2);
cout<<"original circle:\nx="<<c.getX()<<", y="<<c.getY()<<", r="<<c.getRadius()
<<", area="<<c.area()<<endl;
c.setRadius(7.5);
c.setPoint(5,5);
cout<<"new circle:\n"<<c;
Point &pRef=c;
cout<<"pRef:"<<pRef;
return 0;
}
Ch12-1-3(vc)
#include <iostream.h>
class Point
{public:
Point(float=0,float=0);
void setPoint(float,float);
float getX() const {return x;}
float getY() const {return y;}
friend ostream & operator<<(ostream &,const Point &);
protected:
float x,y;
};
Point::Point(float a,float b)
{x=a;y=b;}
void Point::setPoint(float a,float b)
{x=a;y=b;}
ostream & operator<<(ostream &output,const Point &p)
{output<<"["<<p.x<<","<<p.y<<"]"<<endl;
return output;
}
class Circle:public Point
{public:
Circle(float x=0,float y=0,float r=0);
void setRadius(float);
float getRadius() const;
float area () const;
friend ostream &operator<<(ostream &,const Circle &);
protected:
float radius;
};
Circle::Circle(float a,float b,float r):Point(a,b),radius(r){}
void Circle::setRadius(float r)
{radius=r;}
float Circle::getRadius() const {return radius;}
float Circle::area() const
{return 3.14159*radius*radius;}
ostream &operator<<(ostream &output,const Circle &c)
{output<<"Center=["<<c.x<<","<<c.y<<"], r="<<c.radius<<", area="<<c.area()<<endl;
return output;
}
class Cylinder:public Circle
{public:
Cylinder (float x=0,float y=0,float r=0,float h=0);
void setHeight(float);
float getHeight() const;
float area() const;
float volume() const;
friend ostream& operator<<(ostream&,const Cylinder&);
protected:
float height;
};
Cylinder::Cylinder(float a,float b,float r,float h)
:Circle(a,b,r),height(h){}
void Cylinder::setHeight(float h){height=h;}
float Cylinder::getHeight() const {return height;}
float Cylinder::area() const
{ return 2*Circle::area()+2*3.14159*radius*height;}
float Cylinder::volume() const
{return Circle::area()*height;}
ostream &operator<<(ostream &output,const Cylinder& cy)
{output<<"Center=["<<cy.x<<","<<cy.y<<"], r="<<cy.radius<<", h="<<cy.height
<<"\narea="<<cy.area()<<", volume="<<cy.volume()<<endl;
return output;
}
int main()
{Cylinder cy1(3.5,6.4,5.2,10);
cout<<"\noriginal cylinder:\nx="<<cy1.getX()<<", y="<<cy1.getY()<<", r="
<<cy1.getRadius()<<", h="<<cy1.getHeight()<<"\narea="<<cy1.area()
<<", volume="<<cy1.volume()<<endl;
cy1.setHeight(15);
cy1.setRadius(7.5);
cy1.setPoint(5,5);
cout<<"\nnew cylinder:\n"<<cy1;
Point &pRef=cy1;
cout<<"\npRef as a point:"<<pRef;
Circle &cRef=cy1;
cout<<"\ncRef as a Circle:"<<cRef;
return 0;
}
Ch12-1-3
#include <iostream>
using namespace std;
class Point
{public:
Point(float=0,float=0);
void setPoint(float,float);
float getX() const {return x;}
float getY() const {return y;}
friend ostream & operator<<(ostream &,const Point &);
protected:
float x,y;
};
Point::Point(float a,float b)
{x=a;y=b;}
void Point::setPoint(float a,float b)
{x=a;y=b;}
ostream & operator<<(ostream &output,const Point &p)
{output<<"["<<p.x<<","<<p.y<<"]"<<endl;
return output;
}
class Circle:public Point
{public:
Circle(float x=0,float y=0,float r=0);
void setRadius(float);
float getRadius() const;
float area () const;
friend ostream &operator<<(ostream &,const Circle &);
protected:
float radius;
};
Circle::Circle(float a,float b,float r):Point(a,b),radius(r){}
void Circle::setRadius(float r)
{radius=r;}
float Circle::getRadius() const {return radius;}
float Circle::area() const
{return 3.14159*radius*radius;}
ostream &operator<<(ostream &output,const Circle &c)
{output<<"Center=["<<c.x<<","<<c.y<<"], r="<<c.radius<<", area="<<c.area()<<endl;
return output;
}
class Cylinder:public Circle
{public:
Cylinder (float x=0,float y=0,float r=0,float h=0);
void setHeight(float);
float getHeight() const;
float area() const;
float volume() const;
friend ostream& operator<<(ostream&,const Cylinder&);
protected:
float height;
};
Cylinder::Cylinder(float a,float b,float r,float h)
:Circle(a,b,r),height(h){}
void Cylinder::setHeight(float h){height=h;}
float Cylinder::getHeight() const {return height;}
float Cylinder::area() const
{ return 2*Circle::area()+2*3.14159*radius*height;}
float Cylinder::volume() const
{return Circle::area()*height;}
ostream &operator<<(ostream &output,const Cylinder& cy)
{output<<"Center=["<<cy.x<<","<<cy.y<<"], r="<<cy.radius<<", h="<<cy.height
<<"\narea="<<cy.area()<<", volume="<<cy.volume()<<endl;
return output;
}
int main()
{Cylinder cy1(3.5,6.4,5.2,10);
cout<<"\noriginal cylinder:\nx="<<cy1.getX()<<", y="<<cy1.getY()<<", r="
<<cy1.getRadius()<<", h="<<cy1.getHeight()<<"\narea="<<cy1.area()
<<", volume="<<cy1.volume()<<endl;
cy1.setHeight(15);
cy1.setRadius(7.5);
cy1.setPoint(5,5);
cout<<"\nnew cylinder:\n"<<cy1;
Point &pRef=cy1;
cout<<"\npRef as a point:"<<pRef;
Circle &cRef=cy1;
cout<<"\ncRef as a Circle:"<<cRef;
return 0;
}
Ch12-2
#include <iostream>
#include <string>
using namespace std;
class Student
{public:
Student(int,string,float);
void display();
protected:
int num;
string name;
float score;
};
Student::Student(int n,string nam,float s)
{num=n;name=nam;score=s;}
void Student::display()
{cout<<"num:"<<num<<"\nname:"<<name<<"\nscore:"<<score<<"\n\n";}
class Graduate:public Student
{public:
Graduate(int,string,float,float);
void display();
private:
float pay;
};
void Graduate::display()
{cout<<"num:"<<num<<"\nname:"<<name<<"\nscore:"<<score<<"\npay="<<pay<<endl;}
Graduate::Graduate(int n,string nam,float s,float p):Student(n,nam,s),pay(p){}
int main()
{Student stud1(1001,"Li",87.5);
Graduate grad1(2001,"Wang",98.5,563.5);
Student *pt=&stud1;
pt->display();
pt=&grad1;
pt->display();
return 0;
}
Ch12-2-2
#include <iostream>
#include <string>
using namespace std;
class Student
{public:
Student(int,string,float);
virtual void display();
protected:
int num;
string name;
float score;
};
Student::Student(int n,string nam,float s)
{num=n;name=nam;score=s;}
void Student::display()
{cout<<"num:"<<num<<"\nname:"<<name<<"\nscore:"<<score<<"\n\n";}
class Graduate:public Student
{public:
Graduate(int,string,float,float);
void display();
private:
float pay;
};
void Graduate::display()
{cout<<"num:"<<num<<"\nname:"<<name<<"\nscore:"<<score<<"\npay="<<pay<<endl;}
Graduate::Graduate(int n,string nam,float s,float p):Student(n,nam,s),pay(p){}
int main()
{Student stud1(1001,"Li",87.5);
Graduate grad1(2001,"Wang",98.5,1200);
Student *pt=&stud1;
pt->display();
pt=&grad1;
pt->display();
return 0;
}
Ch12-3
#include <iostream>
using namespace std;
class Point
{public:
Point(){}
~Point(){cout<<"executing Point destructor"<<endl;}
};
class Circle:public Point
{public:
Circle(){}
~Circle(){cout<<"executing Circle destructor"<<endl;}
private:
int radus;
};
int main()
{Point *p=new Circle;
delete p;
return 0;
}
Ch12-3-2
#include <iostream>
using namespace std;
class Point
{public:
Point(){}
virtual ~Point(){cout<<"executing Point destructor"<<endl;}
};
class Circle:public Point
{public:
Circle(){}
~Circle(){cout<<"executing Circle destructor"<<endl;}
private:
int radus;
};
void main()
{Point *p=new Circle;
delete p;
}
Ch12-4(vc)
#include <iostream.h>
//聲明抽象基類Shape
class Shape
{public:
virtual float area() const {return 0.0;} //虛函數
virtual float volume() const {return 0.0;} //虛函數
virtual void shapeName() const =0; //純虛函數
};
//聲明Point類
class Point:public Shape //Point是Shape的公用派生類
{public:
Point(float=0,float=0);
void setPoint(float,float);
float getX() const {return x;}
float getY() const {return y;}
virtual void shapeName() const {cout<<"Point:";} //對純虛函數進行定義
friend ostream & operator<<(ostream &,const Point &);
protected:
float x,y;
};
Point::Point(float a,float b)
{x=a;y=b;}
void Point::setPoint(float a,float b)
{x=a;y=b;}
ostream & operator<<(ostream &output,const Point &p)
{output<<"["<<p.x<<","<<p.y<<"]";
return output;
}
//聲明Circle類
class Circle:public Point
{public:
Circle(float x=0,float y=0,float r=0);
void setRadius(float);
float getRadius() const;
virtual float area() const;
virtual void shapeName() const {cout<<"Circle:";} //對純虛函數進行再定義
friend ostream &operator<<(ostream &,const Circle &);
protected:
float radius;
};
Circle::Circle(float a,float b,float r):Point(a,b),radius(r){}
void Circle::setRadius(float r)
{radius=r;}
float Circle::getRadius() const {return radius;}
float Circle::area() const
{return 3.14159*radius*radius;}
ostream &operator<<(ostream &output,const Circle &c)
{output<<"["<<c.x<<","<<c.y<<"], r="<<c.radius;
return output;
}
//聲明Cylinder類
class Cylinder:public Circle
{public:
Cylinder (float x=0,float y=0,float r=0,float h=0);
void setHeight(float);
float getHeight() const;
virtual float area() const;
virtual float volume() const;
virtual void shapeName() const {cout<<"Cylinder:";} //對純虛函數進行再定義
friend ostream& operator<<(ostream&,const Cylinder&);
protected:
float height;
};
Cylinder::Cylinder(float a,float b,float r,float h)
:Circle(a,b,r),height(h){}
void Cylinder::setHeight(float h){height=h;}
float Cylinder::getHeight() const {return height;}
float Cylinder::area() const
{ return 2*Circle::area()+2*3.14159*radius*height;}
float Cylinder::volume() const
{return Circle::area()*height;}
ostream &operator<<(ostream &output,const Cylinder& cy)
{output<<"["<<cy.x<<","<<cy.y<<"], r="<<cy.radius<<", h="<<cy.height;
return output;
}
int main()
{Point point(3.2,4.5); //建立Point類對象point
Circle circle(2.4,12,5.6); //建立Circle類對象circle
Cylinder cylinder(3.5,6.4,5.2,10.5); //建立Cylinder類對象cylinder
point.shapeName(); //靜態關聯
cout<<point<<endl;
circle.shapeName(); //靜態關聯
cout<<circle<<endl;
cylinder.shapeName(); //靜態關聯
cout<<cylinder<<endl<<endl;
Shape *pt; //定義基類指針
pt=&point; //指針指向Point類對象
pt->shapeName(); //動態關聯
cout<<"x="<<point.getX()<<",y="<<point.getY()<<"\narea="<<pt->area()
<<"\nvolume="<<pt->volume()<<"\n\n";
pt=&circle; //指針指向Circle類對象
pt->shapeName(); //動態關聯
cout<<"x="<<circle.getX()<<",y="<<circle.getY()<<"\narea="<<pt->area()
<<"\nvolume="<<pt->volume()<<"\n\n";
pt=&cylinder; //指針指向Cylinder類對象
pt->shapeName(); //動態關聯
cout<<"x="<<cylinder.getX()<<",y="<<cylinder.getY()<<"\narea="<<pt->area()
<<"\nvolume="<<pt->volume()<<"\n\n";
return 0;
}
Ch12-4
#include <iostream>
using namespace std;
//聲明抽象基類Shape
class Shape
{public:
virtual float area() const {return 0.0;} //虛函數
virtual float volume() const {return 0.0;} //虛函數
virtual void shapeName() const =0; //純虛函數
};
//聲明Point類
class Point:public Shape //Point是Shape的公用派生類
{public:
Point(float=0,float=0);
void setPoint(float,float);
float getX() const {return x;}
float getY() const {return y;}
virtual void shapeName() const {cout<<"Point:";} //對純虛函數進行定義
friend ostream & operator<<(ostream &,const Point &);
protected:
float x,y;
};
Point::Point(float a,float b)
{x=a;y=b;}
void Point::setPoint(float a,float b)
{x=a;y=b;}
ostream & operator<<(ostream &output,const Point &p)
{output<<"["<<p.x<<","<<p.y<<"]";
return output;
}
//聲明Circle類
class Circle:public Point
{public:
Circle(float x=0,float y=0,float r=0);
void setRadius(float);
float getRadius() const;
virtual float area() const;
virtual void shapeName() const {cout<<"Circle:";} //對純虛函數進行再定義
friend ostream &operator<<(ostream &,const Circle &);
protected:
float radius;
};
Circle::Circle(float a,float b,float r):Point(a,b),radius(r){}
void Circle::setRadius(float r)
{radius=r;}
float Circle::getRadius() const {return radius;}
float Circle::area() const
{return 3.14159*radius*radius;}
ostream &operator<<(ostream &output,const Circle &c)
{output<<"["<<c.x<<","<<c.y<<"], r="<<c.radius;
return output;
}
//聲明Cylinder類
class Cylinder:public Circle
{public:
Cylinder (float x=0,float y=0,float r=0,float h=0);
void setHeight(float);
float getHeight() const;
virtual float area() const;
virtual float volume() const;
virtual void shapeName() const {cout<<"Cylinder:";} //對純虛函數進行再定義
friend ostream& operator<<(ostream&,const Cylinder&);
protected:
float height;
};
Cylinder::Cylinder(float a,float b,float r,float h)
:Circle(a,b,r),height(h){}
void Cylinder::setHeight(float h){height=h;}
float Cylinder::getHeight() const {return height;}
float Cylinder::area() const
{ return 2*Circle::area()+2*3.14159*radius*height;}
float Cylinder::volume() const
{return Circle::area()*height;}
ostream &operator<<(ostream &output,const Cylinder& cy)
{output<<"["<<cy.x<<","<<cy.y<<"], r="<<cy.radius<<", h="<<cy.height;
return output;
}
int main()
{Point point(3.2,4.5); //建立Point類對象point
Circle circle(2.4,12,5.6); //建立Circle類對象circle
Cylinder cylinder(3.5,6.4,5.2,10.5); //建立Cylinder類對象cylinder
point.shapeName(); //靜態關聯
cout<<point<<endl;
circle.shapeName(); //靜態關聯
cout<<circle<<endl;
cylinder.shapeName(); //靜態關聯
cout<<cylinder<<endl<<endl;
Shape *pt; //定義基類指針
pt=&point; //指針指向Point類對象
pt->shapeName(); //動態關聯
cout<<"x="<<point.getX()<<",y="<<point.getY()<<"\narea="<<pt->area()
<<"\nvolume="<<pt->volume()<<"\n\n";
pt=&circle; //指針指向Circle類對象
pt->shapeName(); //動態關聯
cout<<"x="<<circle.getX()<<",y="<<circle.getY()<<"\narea="<<pt->area()
<<"\nvolume="<<pt->volume()<<"\n\n";
pt=&cylinder; //指針指向Cylinder類對象
pt->shapeName(); //動態關聯
cout<<"x="<<cylinder.getX()<<",y="<<cylinder.getY()<<"\narea="<<pt->area()
<<"\nvolume="<<pt->volume()<<"\n\n";
return 0;
}
第十三章
Ch13-1
#include <iostream>
#include <math.h>
using namespace std;
int main()
{float a,b,c,disc;
cout<<"please input a,b,c:";
cin>>a>>b>>c;
if (a==0)
cerr<<"a is equal to zero,error!"<<endl;
else
if ((disc=b*b-4*a*c)<0)
cerr<<"disc=b*b-4*a*c<0"<<endl;
else
{cout<<"x1="<<(-b+sqrt(disc))/(2*a)<<endl;
cout<<"x2="<<(-b-sqrt(disc))/(2*a)<<endl;
}
return 0;
}
Ch13-2
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{int a;
cout<<"input a:";
cin>>a;
cout<<"dec:"<<dec<<a<<endl;
cout<<"hex:"<<hex<<a<<endl;
cout<<"oct:"<<setbase(8)<<a<<endl;
char *pt="China";
cout<<setw(10)<<pt<<endl;
cout<<setfill('*')<<setw(10)<<pt<<endl;
double pi=22.0/7.0;
cout<<setiosflags(ios::scientific)<<setprecision(8);
cout<<"pi="<<pi<<endl;
cout<<"pi="<<setprecision(4)<<pi<<endl;
cout<<"pi="<<setiosflags(ios::fixed)<<pi<<endl;
return 0;
}
Ch13-3
#include <iostream>
using namespace std;
int main()
{int a=21;
cout.setf(ios::showbase);
cout<<"dec:"<<a<<endl;
cout.unsetf(ios::dec);
cout.setf(ios::hex);
cout<<"hex:"<<a<<endl;
cout.unsetf(ios::hex);
cout.setf(ios::oct);
cout<<"oct:"<<a<<endl;
char *pt="China";
cout.width(10);
cout<<pt<<endl;
cout.width(10);
cout.fill('*');
cout<<pt<<endl;
double pi=22.0/7.0;
cout.setf(ios::scientific);
cout<<"pi=";
cout.width(14);
cout<<pi<<endl;
cout.unsetf(ios::scientific);
cout.setf(ios::fixed);
cout.width(12);
cout.setf(ios::showpos);
cout.setf(ios::internal);
cout.precision(6);
cout<<pi<<endl;
return 0;
}
Ch13-4
#include <iostream>
using namespace std;
int main()
{char *a="BASIC";
for(int i=4;i>=0;i--)
cout.put(*(a+i));
cout.put('\n');
return 0;
}
Ch13-4-2
#include <iostream>
int main()
{char *a="BASIC";
for(int i=4;i>=0;i--)
putchar(*(a+i));
putchar('\n');
return 0;
}
Ch13-5
#include <iostream>
using namespace std;
int main()
{float grade;
cout<<"enter grade:";
while(cin>>grade)
{if(grade>=85) cout<<grade<<" GOOD!"<<endl;
if(grade<60) cout<<grade<<" fail!"<<endl;
cout<<"enter grade:";
}
cout<<"The end."<<endl;
return 0;
}
Ch13-6
#include <iostream>
using namespace std;
int main()
{char c;
cout<<"enter a sentence:"<<endl;
while((c=cin.get())!=EOF)
cout.put(c);
return 0;
}
Ch13-6-2
#include <iostream>
using namespace std;
int main()
{char c;
cout<<"enter a sentence:"<<endl;
while(cin.get(c))
{cout.put(c);
}
cout<<"end"<<endl;
return 0;
}
Ch13-6-3
#include <iostream>
using namespace std;
int main()
{char ch[20];
cout<<"enter a sentence:"<<endl;
cin.get(ch,10,'\n');
cout<<ch<<endl;
return 0;
}
Ch13-7
#include <iostream>
using namespace std;
int main()
{char ch[20];
cout<<"enter a sentence:"<<endl;
cin>>ch;
cout<<"The string read with cin is:"<<ch<<endl;
cin.getline(ch,20,'/');
cout<<"The second part is:"<<ch<<endl;
cin.getline(ch,20);
cout<<"The third part is:"<<ch<<endl;
return 0;
}
Ch13-8
#include <iostream>
using namespace std;
int main()
{char c;
while(!cin.eof())
if((c=cin.get())!=' ')
cout.put(c);
return 0;
}
Ch13-9
#include <iostream>
using namespace std;
int main()
{char c[20];
int ch;
cout<<"please enter a sentence."<<endl;
cin.getline(c,15,'/');
cout<<"The first part is:"<<c<<endl;
ch=cin.peek();
cout<<"The next character(ASCII code) is:"<<ch<<endl;
cin.putback(c[0]);
cin.getline(c,15,'/');
cout<<"The second part is:"<<c<<endl;
return 0;
}
Ch13-10
#include <iostream>
using namespace std;
int main()
{char ch[20];
cin.get(ch,20,'/');
cout<<"The first part is:"<<ch<<endl;
cin.get(ch,20,'/');
cout<<"The second part is:"<<ch<<endl;
return 0;
}
Ch13-10-2
#include <iostream>
using namespace std;
int main()
{char ch[20];
cin.get(ch,20,'/');
cout<<"The first part is:"<<ch<<endl;
cin.ignore();
cin.get(ch,20,'/');
cout<<"The second part is:"<<ch<<endl;
return 0;
}
Ch13-11
#include <fstream>
using namespace std;
int main()
{int a[10];
ofstream outfile("f1.dat");
if(!outfile)
{cerr<<"open error!"<<endl;
exit(1);
}
cout<<"enter 10 integer numbers:"<<endl;
for(int i=0;i<10;i++)
{cin>>a[i];
outfile<<a[i]<<" ";}
outfile.close();
return 0;
}
Ch13-12
#include <fstream>
using namespace std;
int main()
{int a[10],max,i,order;
ifstream infile("f1.dat",ios::in);
if(!infile)
{cerr<<"open error!"<<endl;
exit(1);
}
for(i=0;i<10;i++)
{infile>>a[i];
cout<<a[i]<<" ";}
cout<<endl;
max=a[0];
order=0;
for(i=1;i<10;i++)
if(a[i]>max)
{max=a[i];
order=i;
}
cout<<"max="<<max<<endl<<"order="<<order<<endl;
infile.close();
return 0;
}
Ch13-13
#include <fstream>
using namespace std;
void save_to_file()
{ofstream outfile("f2.dat");
if(!outfile)
{cerr<<"open f2.dat error!"<<endl;
exit(1);
}
char c[80];
cin.getline(c,80);
for(int i=0;c[i];i++)
if(c[i]>=65 && c[i]<=90||c[i]>=97 && c[i]<=122)
{outfile.put(c[i]);
cout<<c[i];}
cout<<endl;
outfile.close();
}
void get_from_file()
{char ch;
ifstream infile("f2.dat",ios::in);
if(!infile)
{cerr<<"open f2.dat error!"<<endl;
exit(1);
}
ofstream outfile("f3.dat");
if(!outfile)
{cerr<<"open f3.dat error!"<<endl;
exit(1);
}
while(infile.get(ch))
{if(ch>=97 && ch<=122)
ch=ch-32;
outfile.put(ch);
cout<<ch;
}
cout<<endl;
infile.close();
outfile.close();
}
int main()
{save_to_file();
get_from_file();
return 0;
}
Ch13-13-2
#include <fstream>
using namespace std;
void display_file(char *filename)
{ifstream infile(filename,ios::in);
if(!infile)
{cerr<<"open error!"<<endl;
exit(1);}
char ch;
while(infile.get(ch))
cout.put(ch);
cout<<endl;
infile.close();
}
int main()
{display_file("f3.dat");
return 0;
}
Ch13-14
#include <fstream>
using namespace std;
struct student
{char name[20];
int num;
int age;
char sex;
};
int main()
{student stud[3]={"Li",1001,18,'f',"Fun",1002,19,'m',"Wang",1004,17,'f'};
ofstream outfile("stud.dat",ios::binary);
if(!outfile)
{cerr<<"open error!"<<endl;
abort();
}
for(int i=0;i<3;i++)
outfile.write((char *)&stud[i],sizeof(stud[i]));
outfile.close();
return 0;
}
Ch13-14-2
#include <fstream>
using namespace std;
struct student
{char name[20];
int num;
int age;
char sex;
};
int main()
{student stud[3]={"Li",1001,18,'f',"Fun",1002,19,'m',"Wang",1004,17,'f'};
ofstream outfile("stud.dat",ios::binary);
if(!outfile)
{cerr<<"open error!"<<endl;
abort();
}
outfile.write((char *)&stud[0],sizeof(stud));
outfile.close();
return 0;
}
Ch13-15
#include <fstream>
using namespace std;
struct student
{char name[20];
int num;
int age;
char sex;
};
int main()
{student stud[3];
int i;
ifstream infile("stud.dat",ios::binary);
if(!infile)
{cerr<<"open error!"<<endl;
abort();
}
for(i=0;i<3;i++)
infile.read((char*)&stud[i],sizeof(stud[i]));
infile.close();
for(i=0;i<3;i++)
{cout<<"NO."<<i+1<<endl;
cout<<"name:"<<stud[i].name<<endl;
cout<<"num:"<<stud[i].num<<endl;;
cout<<"age:"<<stud[i].age<<endl;
cout<<"sex:"<<stud[i].sex<<endl<<endl;
}
return 0;
}
Ch13-16
#include <fstream>
using namespace std;
struct student
{int num;
char name[20];
float score;
};
int main()
{int i;
student stud[5]={1001,"Li",85,1002,"Fun",97.5,1004,"Wang",54,
1006,"Tan",76.5,1010,"ling",96};
fstream iofile("stud.dat",ios::in|ios::out|ios::binary);
if(!iofile)
{cerr<<"open error!"<<endl;
abort();
}
for(i=0;i<5;i++)
iofile.write((char *)&stud[i],sizeof(stud[i]));
student stud1[5];
for(i=0;i<5;i=i+2)
{iofile.seekg(i*sizeof(stud[i]),ios::beg);
iofile.read((char *)&stud1[i/2],sizeof(stud1[i]));
cout<<stud1[i/2].num<<" "<<stud1[i/2].name<<" "<<stud1[i/2].score<<endl;
}
cout<<endl;
stud[2].num=1012;
strcpy(stud[2].name,"Wu");
stud[2].score=60;
iofile.seekp(2*sizeof(stud[0]),ios::beg);
iofile.write((char *)&stud[2],sizeof(stud[2]));
iofile.seekg(0,ios::beg);
for(i=0;i<5;i++)
{iofile.read((char *)&stud[i],sizeof(stud[i]));
cout<<stud[i].num<<" "<<stud[i].name<<" "<<stud[i].score<<endl;
}
iofile.close();
return 0;
}
Ch13-17
#include <strstream>
#include <iostream>
using namespace std;
struct student
{int num;
char name[20];
float score;
};
int main()
{student stud[3]={1001,"Li",78,1002,"Wang",89.5,1004,"Fun",90};
char c[50];
ostrstream strout(c,30);
for(int i=0;i<3;i++)
strout<<stud[i].num<<stud[i].name<<stud[i].score;
strout<<ends;
cout<<"array c:"<<endl<<c<<endl;
return 0;
}
Ch13-18
#include <strstream>
#include <iostream>
using namespace std;
int main()
{char c[50]="12 34 65 -23 -32 33 61 99 321 32";
int a[10],i,j,t;
cout<<"array c:"<<c<<endl;
istrstream strin(c,sizeof(c));
for(i=0;i<10;i++)
strin>>a[i];
cout<<"array a:";
for(i=0;i<10;i++)
cout<<a[i]<<" ";
cout<<endl;
for(i=0;i<9;i++)
for(j=0;j<9-i;j++)
if(a[j]>a[j+1])
{t=a[j];a[j]=a[j+1];a[j+1]=t;}
ostrstream strout(c,sizeof(c));
for(i=0;i<10;i++)
strout<<a[i]<<" ";
strout<<ends;
cout<<"array c:"<<c<<endl;
return 0;
}
第十四章
Ch14-1-1
#include <iostream>
#include <cmath>
using namespace std;
int main()
{double triangle(double,double,double);
double a,b,c;
cin>>a>>b>>c;
while(a>0 && b>0 && c>0)
{cout<<triangle(a,b,c)<<endl;
cin>>a>>b>>c;
}
return 0;
}
double triangle(double a,double b,double c)
{double area;
double s=(a+b+c)/2;
area=sqrt(s*(s-a)*(s-b)*(s-c));
return area;
}
Ch14-1-2
#include <iostream>
#include <cmath>
using namespace std;
int main()
{double triangle(double,double,double);
double a,b,c;
cin>>a>>b>>c;
try
{while(a>0 && b>0 && c>0)
{cout<<triangle(a,b,c)<<endl;
cin>>a>>b>>c;}
}
catch(double)
{cout<<"a="<<a<<",b="<<b<<",c="<<c<<",that is not a traingle!"<<endl;}
cout<<"end"<<endl;
return 0;
}
double triangle(double a,double b,double c)
{double s=(a+b+c)/2;
if (a+b<=c||b+c<=a||c+a<=b) throw a;
return sqrt(s*(s-a)*(s-b)*(s-c));
}
Ch14-2-1
#include <iostream>
using namespace std;
int main()
{void f1();
try
{f1();}
catch(double)
{cout<<"OK0!"<<endl;}
cout<<"end0"<<endl;
return 0;
}
void f1()
{void f2();
try
{f2();}
catch(char)
{cout<<"OK1!";}
cout<<"end1"<<endl;
}
void f2()
{void f3();
try
{f3();}
catch(int)
{cout<<"Ok2!"<<endl;}
cout<<"end2"<<endl;
}
void f3()
{double a=0;
try
{throw a;}
catch(float)
{cout<<"OK3!"<<endl;}
cout<<"end3"<<endl;
}
Ch14-2-2
#include <iostream>
using namespace std;
int main()
{void f1();
try
{f1();}
catch(double)
{cout<<"OK0!"<<endl;}
cout<<"end0"<<endl;
return 0;
}
void f1()
{void f2();
try
{f2();}
catch(char)
{cout<<"OK1!";}
cout<<"end1"<<endl;
}
void f2()
{void f3();
try
{f3();}
catch(int)
{cout<<"Ok2!"<<endl;}
cout<<"end2"<<endl;
}
void f3()
{double a=0;
try
{throw a;}
catch(double)
{cout<<"OK3!"<<endl;}
cout<<"end3"<<endl;
}
Ch14-2-3
#include <iostream>
using namespace std;
int main()
{void f1();
try
{f1();}
catch(double)
{cout<<"OK0!"<<endl;}
cout<<"end0"<<endl;
return 0;
}
void f1()
{void f2();
try
{f2();}
catch(char)
{cout<<"OK1!";}
cout<<"end1"<<endl;
}
void f2()
{void f3();
try
{f3();}
catch(int)
{cout<<"Ok2!"<<endl;}
cout<<"end2"<<endl;
}
void f3()
{double a=0;
try
{throw a;}
catch(double)
{cout<<"OK3!"<<endl;throw;}
cout<<"end3"<<endl;
}
Ch14-3
#include <iostream>
#include <string>
using namespace std;
class Student
{public:
Student(int n,string nam)
{cout<<"construtot-"<<n<<endl;
num=n;name=nam;}
~Student(){cout<<"destructor-"<<num<<endl;}
void get_data();
private:
int num;
string name;
};
void Student::get_data()
{if(num==0) throw num;
else cout<<num<<" "<<name<<endl;
cout<<"in get_data()"<<endl;
}
void fun()
{Student stud1(1101,"tan");
stud1.get_data();
Student stud2(0,"Li");
stud2.get_data();
}
int main()
{cout<<"main begin"<<endl;
cout<<"call fun()"<<endl;
try
{fun();}
catch(int n)
{cout<<"num="<<n<<",error!"<<endl;}
cout<<"main end"<<endl;
return 0;
}
Ch14-4-1
#include <iostream>
using namespace std;
#include "header1.h"
int main()
{Student stud1(101,"Wang",18);
stud1.get_data();
cout<<fun(5,3)<<endl;
return 0;
}
Ch14-4-2
#include <iostream>
using namespace std;
#include "header1.h"
#include "header2.h"
int main()
{Student stud1(101,"Wang",18);
stud1.get_data();
cout<<fun(5,3)<<endl;
return 0;
}
Ch14-5
//main file
#include <iostream>
#include "header11.h"
#include "header22.h"
int main()
{Ns1::Student stud1(101,"Wang",18);
stud1.get_data();
cout<<Ns1::fun(5,3)<<endl;
Ns2::Student stud2(102,"Li",'f');
stud2.get_data();
cout<<Ns2::fun(5,3)<<endl;
return 0;
}