C++ 程序设计(谭浩强)教材例题程序

C++程序设计题解与上机指导

教材例题程序

第一章
Ch1-1
#include 
using namespace std;
int main()
{
 cout<<"This is a C++ program.";
 return 0;
}

Ch1-2
#include 
using namespace std;
int main()
{
 int a,b, sum;
 cin>>a>>b;
 sum=a+b;
 cout<<"a+b="<
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="<
using namespace std;
int main()
{ int max(int x,int y);
  int a,b,c;
  cin>>a>>b;
  c=max(a,b);
  cout<<"max="<y) z=x;
  else z=y;
  return(z);
}

Ch1-4
#include 
using namespace std;
class Student
{
 private:
  int num;
  int score;
 public:
  void setdata()
  {
   cin>>num;
   cin>>score;
  }
  void display()
  {
   cout<<"num="<
#include 
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="<>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<
using namespace std;
int main()
{
  char ch;
  cin>>ch;
  ch=(ch>='A' && ch<='Z')?(ch+32):ch;
  cout<
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<
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="<
using namespace std;
int main()
{
 int i=1,sum=0;
 while (i<=100)
  {sum=sum+i;
   i++;
  }
 cout<<"sum="<
using namespace std;
int main()
{int i=1,sum=0;
 do 
  { sum=sum+i;
    i++;
   }while (i<=100);
 cout<<"sum="<
#include 
#include 
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="<
#include 
using namespace std;
int main()
{long f1,f2;
 int i;
 f1=f2=1;
 for(i=1;i<=20;i++)
   {cout<
#include 
#include 
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<
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<
using namespace std;
void printstar(void)
{
 cout<<"******************************"<
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="<
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="<
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="<a) a=b;                          
 if(c>a) a=c;
 return a;
}

Ch4-5
#include 
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="<>d1>>d2>>d3;
 d=max(d1,d2,d3);
 cout<<"d_max="<>g1>>g2>>g3;
 g=max(g1,g2,g3);
 cout<<"g_max="<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 
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)="<>x1>>x2;
   f1=f(x1);
   f2=f(x2);
  }while(f1*f2>=0);
 x=root(x1,x2);
 cout<0)
    {y1=y;
     x1=x;
    }
   else
    x2=x;
  }while(fabs(y)>=0.000001);
 return x;
}

Ch4-10
#include 
using namespace std;
int age(int);
int main()
 {cout<
using namespace std;
long fac(int);
int main()
 {int n;
  long y;
  cout<<"please input an integer:";
  cin>>n;
  y=fac(n);
  cout<
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<
using namespace std;
int fac(int);
int main()
 {
  int i;
  for(i=1;i<=5;i++)
   cout<
using namespace std;
int max(int,int);
int main()
 {extern int a,b;
  cout<y?x:y;
  return z;
 }

Ch4-15-1
//file1.cpp
#include 
using namespace std;
int main()
 { extern int max(int,int);
   int a,b;
   cin>>a>>b;
   cout<y?x:y;
 return z;
 }

Ch4-16
#include 
using namespace std;
#define RUN
int main()
{int x=1,y=2,z=3;
 #ifndef RUN
  cout<<"x="<
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<
#include 
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<
using namespace std;
int main()
{
  int a[10];
  int i,j,t;
  cout<<"input 10 numbers :"<>a[i];
  cout<a[i+1])
	   {t=a[i];a[i]=a[i+1];a[i+1]=t;}
  cout<<"the sorted numbers :"< 
using namespace std;
int main()
{
 int a[2][3]={{1,2,3},{4,5,6}};
 int b[3][2],i,j;
 cout<<"array a:"<
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="<
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) return x;
  else return max;
}

Ch5-7
#include 
using namespace std;
int main()
{void select_sort(int array[],int n);             
 int a[10],i;
 cout<<"enter the originl array:"<>a[i];
 cout< 
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) max=array[i][j];
 return max;
}

Ch5-9
#include 
using namespace std;
int main()
{char diamond[][5]={{' ',' ','*'},{' ','*',' ','*'},{'*',' ',' ',' ','*'},{' ','*',' ','*'},{' ',' ','*'}};
 int i,j;
 for (i=0;i<5;i++)
   {for (j=0;j<5;j++)
     cout<
#include 
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;i0) strcpy(string,str[i]);
  cout<
#include 
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
#include 
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>name[i]>>num[i];}
}

void search(string find_name)
{int i;
 bool flag=false;
 for(i=0;i
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<
using namespace std;
int main()
{
  int *p1,*p2,*p,a,b;
  cin>>a>>b;
  p1=&a;
  p2=&b;
  if(a
using namespace std;
int main()
{
  int a[10];
  int i;
  for(i=0;i<10;i++)
    cin>>a[i];
  cout<
using namespace std;
int main()
{
  int a[10];
  int i;
  for(i=0;i<10;i++)
    cin>>*(a+i);
  cout<
using namespace std;
int main()
{
  int a[10];
  int i,*p=a ;
  for(i=0;i<10;i++)
    cin>>*(p+i);
  cout<
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:"<>a[i];
 cout<
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
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)<
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<
using namespace std;
int main()
{ char str[]="I love CHINA!";
  cout<
#include 
using namespace std;
int main()
{ string str="I love CHINA!";
  cout<
using namespace std;
int main()
{ char *str="I love CHINA!";
  cout<
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: "<
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="<y) z=x;
 else z=y;
 return(z);
}

Ch6-14-2
#include 
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="<y) z=x;
 else z=y;
 return(z);
}

Ch6-15-1
#include 
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;i0) 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
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;i0) 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
using namespace std;
int main()
{ char **p;
  char *name[]={"BASIC","FORTRAN","C++","PASCAL","COBOL"};
  p=name+2;
  cout<<*p<
#include 
using namespace std;
int main()
{
 int a=10;
 int &b=a;
 a=a*a;
 cout<
using namespace std;
int main()
{void swap(int,int);
 int i=3;
 int j=5;
 swap(i,j);
 cout<
using namespace std;
int main()
{void swap(int *,int *);
 int i=3,j=5;
 swap(&i,&j);
 cout<
using namespace std;
int main()
{void swap(int &,int &);
 int i=3,j=5;
 swap(i,j);
 cout<<"i="<>a>>b>>c;
 a1=a;b1=b;c1=c;
 sort(a1,b1,c1);
 cout<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 
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<
#include 
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<
#include 
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<
#include 
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<
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<num<<"  "<score<next;
   } while(p!=NULL);
 return 0;
}

Ch7-5-1
#include 
#include 
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<
#include 
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<num<<" "<name<<" "<score[0]<<" "
     <score[1]<<" "<score[2]<
#include 
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<
#include 
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<name<num<sex<
#include 
#include 
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<
#include 
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<
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<
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<>t2.hour;
  cin>>t2.minute;
  cin>>t2.sec;
  cout<
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<
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<
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<
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="<
#include "student.h"
int main()
{Student stud;
 stud.display();
 return 0;
}

Student
//student.cpp                            这是源文件,在此文件中进行函数的定义
#include 
#include "student.h"                    //不要漏写此行
void Student::display( )                 //在类外定义display类函数
{cout<<"num:"<
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<
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<
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<
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 "<
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 "<
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 "<
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 "<
#include 
using namespace std;
class Student
 {public:
   Student(int n,string nam,char s)
    {num=n;
     name=nam;
     sex=s;
     cout<<"Constructor called."<
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 "<
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<get_time();
  void (Time::*p3)();
  p3=&Time::get_time;
  (t1.*p3)();
  return 0;
}

Ch9-8
#include 
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<
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 "<
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 "<
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<
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
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<
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<
using namespace std;
template
class Compare
 {public:
   Compare(numtype a,numtype b)
    {x=a;y=b;}
   numtype max()
    {return (x>y)?x:y;}
   numtype min()
    {return (x cmp1(3,7);
 cout< cmp2(45.78,93.6);
 cout< cmp3('a','A');
 cout<
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<<"("<
#include 
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<(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)<
#include 
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<(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)<
#include 
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<(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)<
#include 
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<(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)<
#include 
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<(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<
#include 
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<(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<
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<=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 
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<=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           
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<<"("<
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<<"("<
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<<"("<=0) output<<"+";
 output<> (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="<> (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="<
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<
#include 
using namespace std;
class Student
{public:
  void get_value()
   {cin>>num>>name>>sex;}
  void display( )
    {cout<<"num: "<>age>>addr;}
   void display_1()
    {  //cout<<"num: "<
#include 
using namespace std;
class Student
{public:
  void display( )
    {cout<<"num: "<
#include 
using namespace std;
class Student                        //声明基类
{public:                             //基类公用成员                
  void display( );
 protected :                         //基类保护成员
    int num;
    string name;
    char sex;
};

void Student::display( )
   {cout<<"num: "<
#include
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: "<
#include 
using namespace std;
class Student                              //声明基类
 {public:                                  //公用部分
   Student(int n,string nam)              //基类构造函数
    {num=n;
     name=nam;
    }
   void display()                           //输出基类数据成员
    {cout<<"num:"<
#include
using namespace std;
class Student                              //声明基类
 {public:                                  //公用部分
   Student(int n, string nam )            //基类构造函数
    {num=n;
     name=nam;
    }
   void display()                           //输出基类数据成员
    {cout<<"num:"<
#include 
using namespace std;
class Teacher                              //声明Teacher(教师)类
 {public:                                  //公用部分
   Teacher(string nam,int a,string t)      //构造函数
    {name=nam;
     age=a;
     title=t;}
   void display()                          //输出教师有关数据
     {cout<<"name:"<
#include 
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:"<
//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<<"["<
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<<"["<
//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<<"["<
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<<"["<
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<<"["<
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<<"["<
#include 
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:"<display();
  pt=&grad1;
  pt->display();
  return 0;
 }

Ch12-2-2
#include 
#include 
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:"<display();
  pt=&grad1;
  pt->display();
  return 0;
 }

Ch12-3
#include 
using namespace std;
class Point
{public:
  Point(){}
  ~Point(){cout<<"executing Point destructor"<
using namespace std;
class Point
{public:
  Point(){}
  virtual ~Point(){cout<<"executing Point destructor"<
//声明抽象基类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<<"["<shapeName();                                //动态关联
 cout<<"x="<area()
     <<"\nvolume="<volume()<<"\n\n";

 pt=&cylinder;                                   //指针指向Cylinder类对象
 pt->shapeName();                                //动态关联
 cout<<"x="<area()
     <<"\nvolume="<volume()<<"\n\n";
 return 0;
}

Ch12-4
#include 
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<<"["<shapeName();                                //动态关联
 cout<<"x="<area()
     <<"\nvolume="<volume()<<"\n\n";

 pt=&cylinder;                                   //指针指向Cylinder类对象
 pt->shapeName();                                //动态关联
 cout<<"x="<area()
     <<"\nvolume="<volume()<<"\n\n";
 return 0;
}

第十三章

Ch13-1
#include 
#include 
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!"<>a;
 cout<<"dec:"<=0;i--)
  cout.put(*(a+i));
 cout.put('\n');
 return 0;
}

Ch13-4-2
#include 
int main()
{char *a="BASIC";
 for(int i=4;i>=0;i--)
  putchar(*(a+i));
 putchar('\n');
 return 0;
}

Ch13-5
#include 
using namespace std;
int main()
{float grade;
 cout<<"enter grade:";
 while(cin>>grade)
  {if(grade>=85) cout<
using namespace std;
int main()
{char c;
 cout<<"enter a sentence:"<
using namespace std;
int main()
{char c;
 cout<<"enter a sentence:"<
using namespace std;
int main()
{char ch[20];
 cout<<"enter a sentence:"<
using namespace std;
int main()
{char ch[20];
 cout<<"enter a sentence:"<>ch;
 cout<<"The string read with cin is:"<
using namespace std;
int main()
{char c;
 while(!cin.eof())
  if((c=cin.get())!=' ')
     cout.put(c);
 return 0;
}

Ch13-9
#include 
using namespace std;
int main()
{char c[20];
 int ch;
 cout<<"please enter a sentence."<
using namespace std;
int main()
{char ch[20];
 cin.get(ch,20,'/');
 cout<<"The first part is:"<
using namespace std;
int main()
{char ch[20];
 cin.get(ch,20,'/');
 cout<<"The first part is:"<
using namespace std;
int main()
{int a[10];
 ofstream outfile("f1.dat");
 if(!outfile)
  {cerr<<"open error!"<>a[i];
   outfile<
using namespace std;
int main()
{int a[10],max,i,order;
 ifstream infile("f1.dat",ios::in); 
 if(!infile)
  {cerr<<"open error!"<>a[i];
    cout<max)
     {max=a[i];
      order=i;
     }
 cout<<"max="<
using namespace std;
void save_to_file()
{ofstream outfile("f2.dat");
 if(!outfile)
  {cerr<<"open f2.dat error!"<=65 && c[i]<=90||c[i]>=97 && c[i]<=122)
    {outfile.put(c[i]);
     cout<=97 && ch<=122)
     ch=ch-32;
   outfile.put(ch);
   cout<
using namespace std;
void display_file(char *filename)
{ifstream infile(filename,ios::in);
 if(!infile)
  {cerr<<"open error!"<
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!"<
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!"<
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!"<
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!"<
#include 
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<
#include 
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:"<>a[i];
 cout<<"array a:";
 for(i=0;i<10;i++)
   cout<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<
#include 
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<>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 
#include 
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<>a>>b>>c;}
  }
 catch(double)
  {cout<<"a="<
using namespace std;
int main()
{void f1();
 try
  {f1();}
 catch(double)
  {cout<<"OK0!"<
using namespace std;
int main()
{void f1();
 try
  {f1();}
 catch(double)
  {cout<<"OK0!"<
using namespace std;
int main()
{void f1();
 try
  {f1();}
 catch(double)
  {cout<<"OK0!"<
#include 
using namespace std;
class Student
 {public:
   Student(int n,string nam)
    {cout<<"construtot-"<
using namespace std;
#include "header1.h"
int main()
 {Student stud1(101,"Wang",18);
  stud1.get_data();
  cout<
using namespace std;
#include "header1.h"
#include "header2.h"
int main()
 {Student stud1(101,"Wang",18);
  stud1.get_data();
  cout<
#include "header11.h"
#include "header22.h"
int main()
 {Ns1::Student stud1(101,"Wang",18);
  stud1.get_data();
  cout<

你可能感兴趣的:(C++ 程序设计(谭浩强)教材例题程序)