C++ Primer Plus (第6版)编程练习 代码-----第十章

1.
bank.h

#ifndef bank_H_  
#define bank_H_  
#include"stdafx.h"


class bank{

 private:
	 char name[40];
	 char account[20];
	 double  money;
 public:
	 bank();
	 bank(const char* nm,const char*ac,double mo);
	 ~bank();
	 void show();
	 void input(double mo);
	 void output(double mo);

};
#endif


bank.cpp


#include"stdafx.h"

#include"bank.h"
#include



bank::bank()
 {
   strncpy(name,"no name",39);
   name[39]='\0';
   strncpy(account,"no account",19);
   account[19]='\0';
   money=0.0;
 
 }


 bank::bank(const char* nm,const char*ac,double mo)
 {
   strncpy(name,nm,39);
   name[39]='\0';
   strncpy(account,ac,19);
   account[19]='\0';
   money=mo;
 
 }

 bank::~bank()
 {
	 std::cout<<"Bye";
 }
void bank::show() 
{
	std::cout<
#include 
#include
#include "bank.h"


void main()
{
	{
	 bank bk("John Smith","JS0001",5000.0);
	 
	 bk.show();
	 bk.input(1200.0);
	 bk.show();
	 bk.output(200.0);
	  bk.show();
	}
	std::system("pause");
	
}



2.test.h

#ifndef TEST_H_  
#define TEST_H_  
#include"stdafx.h"
#include
using namespace std;
class Person{

 private:
	static const int LIMIT =25;
	string lname;
	char fname[LIMIT];
 public:
	 Person(){lname="";fname[0]='\0';};
	 Person(const string & ln,const char *fn="Heyyou");
	 void Show() const;
	 void FormalShow() const;
	 ~Person();
};
#endif

test.cpp


#include"stdafx.h"

#include"test.h"
#include

#include




Person::Person(const string & ln ,const char *fn)
 {
	 std::cout<<"\n";
	 std::cout<<"lname is :"<

main.cpp


#include "stdafx.h"
#include
#include 
#include
#include "test.h"


int main()
{
	
	{    Person one;
	    
	   
		
		one.Show();
		
		one.FormalShow();
	    Person two("Smythecraft");
		two.Show();
		
		two.FormalShow();
	    Person three("Dimwiddy","Sam");
		three.Show();
		
		three.FormalShow();
   
	std::system("pause");
 }
	return 0;
}

3.


test.h



#ifndef TEST_H_  
#define TEST_H_  
#include"stdafx.h"
#include
using namespace std;
const int Len=40;
class Golf{
	
private:
  char fullname[Len];  
      int handicap;  
public: 
	Golf();
	~Golf();
   void setgolf(const char*name,int hc);  
   int setgolf();  
   void Handicap(int hc);  
   void showgolf();  
};
#endif


test.cpp



#include"stdafx.h"

#include"test.h"
#include

#include




using namespace std;  
Golf::Golf()
{
}

Golf::~Golf()
{
}

void Golf::setgolf(const char*name,int hc)  
{  
          
     strcpy(fullname,name);  
      
    handicap=hc;  
}  
int Golf::setgolf()  
{  
      
    cout<<"Please enter the name: ";  
    cin.getline(fullname,40);  
      
      
  if(strlen(fullname)==0)  
      return 0;  
  else   
      cout<<"Please enter the handicap: ";  
      cin>>handicap;  
      return 1;  
}  
void Golf::Handicap(int hc)  
{  
    handicap=hc;  
}  
void Golf::showgolf()  
{  
   cout<<"the name is :     "<
#include 
#include
#include "test.h"

using namespace std;
int main()
{
	
	{    Golf ann;  
         ann.setgolf("Ann Birdfree",24);  
         ann.showgolf();  
         ann.Handicap(26);  
         ann.showgolf();  
         int i=0;   
         Golf gf[5] ;  
       int  test=gf[i].setgolf();  
        while((test==1)&&(i<4))  
       {  
            i++;  
           cin.get();  
           test=gf[i].setgolf();  
       }
  
  
        for(int j=0;j<=i;j++)  
       {  
          cout<<"the "<



4.test.h

#ifndef TEST_H_  
#define TEST_H_  
#include"stdafx.h"
#include
using namespace std;
namespace SALES  
{  
  
    const int QUARTERS =4;  
    class Sales  
    { 
	   private:
          double sales[QUARTERS];  
          double average;  
          double max;  
          double min;  
	   public :
		  Sales(const double ar[],int n);
		  Sales(); 
          void showSales();  
   };  
}  
#endif

test.cpp

#include"stdafx.h"

#include"test.h"
#include

#include




using namespace std;  
namespace SALES  
{  
  
      
    Sales::Sales(const double ar[],int n)  
    {  
        double sum=0;
		max=ar[0];
		min=ar[0];  
       for(int i=0;imax)  
                max=sales[i];  
            if(sales[i]>sales[i];    
            sum=sum+sales[i];  
        }  
       max=sales[0];
	   min=sales[0];  
       for(int i=0;i<4;i++)  
        {   
           if(sales[i]>max)  
                max=sales[i];  
              
            if(sales[i]
5.
Stack.h

#ifndef STACK_H_  
#define STACK_H_  

struct customer
{
	char fullname[35];
	double payment;
};

typedef customer Item;
class Stack
{
private:
	enum{MAX =10};
	Item items [MAX];
	int top;
public:
	Stack();
	bool isempty() const;
	bool isfull() const;
	bool push(const Item &item);
	bool pop(Item &item);

};


#endif


Stack.cpp

Stack::Stack()
{
   top=0;

}
bool Stack::isempty()const
{
	return top==0;
}

bool Stack::isfull()const
{

	return top==MAX;
}


bool Stack::push(const Item &item)
{
	if (top0)
	{
	  item =items[--top];
	  return true;
	}
	else
		return false;

}


main.cpp

using namespace std;
int main()
{
	
	Stack st;
	customer cs[10]={{"AA",100},{"BB",200},{"CC",300},{"DD",400},{"EE",500},{"FF",600},{"GG",700},{"HH",800},{"II",900},{"JJ",1000}};
  
    for(int i=0;i<=10;i++)  
    {  if(st.isfull())
	    {
	     cout<<"the stack is full!\n";
	    }
		else 
		{ 
        st.push(cs[i]);  
        cout<<"push the "<



6.

Move.h


class Move
{
private:
	   double x;
	   double y;
public:
	   Move (double a=0,double b=0);
	   void  showmove() const;
	   Move add(const Move &m);
	   void  reset(double a=0,double b=0);

};


Move.cpp

using namespace std;
 Move::Move (double a,double b)
{
   x=a;
   y=b;
}

void Move:: showmove() const
{
  cout<<"\nthe current x is :"<

7.
test.h
#ifndef TEST_H_  
#define TEST_H_  

class Plorg
{
private:
	   char name[19];
	   int ci;
public:
	  Plorg(char *ch="Plorg",int c=0);
	  void newplorg(char*ch);
	  void changeplorg(int n);
	  void showplorg()const;

};


#endif


test.cpp

#include"stdafx.h"

#include"test.h"
#include

#include
using namespace std;

Plorg::Plorg(char *ch,int c)
 {
   strcpy(name,ch);
	ci=c;
 
 }
void Plorg::newplorg(char*ch)
{
	cout<<"the new name !\n" ;
	strcpy(name,ch);
	ci=50;

}
void Plorg::changeplorg(int n)
{
	cout<<"change the ci !\n" ;
	ci=n;
}
 void Plorg::showplorg()const
 {
   cout<<"the Name is :"<
#include 
#include
#include "test.h"

using namespace std;
int main()
{
	Plorg pl("Test",10);
	pl.showplorg();
	pl.newplorg("ChangeTest");
	pl.showplorg();
	pl.changeplorg(20);
	pl.showplorg();
	
	
     system("pause");  
    return 0;  
}

10.8 








 
  
 
 

你可能感兴趣的:(C++,Primer,Plus,(第6版),C++,Primer,Plus,第6版,c++,vs2010,编程)