10.1
//header file
#ifndef C__10_hpp
#define C__10_hpp
#include <stdio.h>
#include <string>
class Bank
{
private:
std::string name;
std::string num;
double balance;
public:
Bank(const std::string & str,const std::string &arr,double k);
void show() const;
void add(double k);
void withdraw(double k);
};
#endif /* C__10_hpp */
//defination
#include "C++10.hpp"
#include <iostream>
Bank::Bank(const std::string &str,const std::string& ptr,double k)
{
name=str;
num=ptr;
balance=k;
}
void Bank::show() const
{
using namespace std;
cout<<"Account name: "<<name<<endl;
cout<<"Account number: "<<num<<endl;
cout<<"Balance: "<<balance<<endl;
}
void Bank::add(double k)
{
balance+=k;
}
void Bank::withdraw(double k)
{
balance-=k;
}
//main
#include <iostream>
#include "C++10.hpp"
int main() {
Bank ban{"MR.Hao","1111111",910000};
ban.show();
ban.add(1);
ban.show();
ban.withdraw(100000);
ban.show();
return 0;
}
10.2
//header file
#ifndef C__10_hpp
#define C__10_hpp
#include <stdio.h>
#include <string>
class Person{
private:
static const int LIMIT = 25;
std::string lname;
char fname[LIMIT];
public:
Person(){lname="";fname[0]='\0';};
Person(const std::string &ln,const char * fn = "Heyyou");
void show() const;
void Formalshow()const;
};
#endif /* C__10_hpp */
//defination
#include "C++10.hpp"
#include <cstring>
#include <iostream>
Person::Person(const std::string &ln,const char * fn)
{
lname=ln;
strncpy(fname,fn,LIMIT-1);
fname[LIMIT-1]='\0';
}
void Person::show() const
{
using std::cout;
using std::endl;
cout<<"First name:"<<fname<<endl;
cout<<"Last name:"<<lname<<endl;
}
void Person::Formalshow()const
{
using std::cout;
using std::endl;
cout<<"Last name:"<<lname<<endl;
cout<<"First name:"<<fname<<endl;
}
//main
#include <iostream>
#include "C++10.hpp"
int main() {
using namespace std;
Person one;
one.show();
cout<<endl;
Person two("Smythecraft");
two.show();
cout<<endl;
Person three("Dimwiddy","Sam");
three.show();
cout<<endl;
three.Formalshow();
cout<<endl;
return 0;
}
10.3
//header file
#ifndef C__10_hpp
#define C__10_hpp
#include <stdio.h>
class Golf
{
private:
static const int Len =40;
char fullname[Len];
int cap;
public:
Golf(const char *ar="wulala",int k=0);
void setgolf();
void handicap(int hc);
void showgolf()const;
};
#endif /* C__10_hpp */
//defination
#include "C++10.hpp"
#include <iostream>
#include <cstring>
void Golf::setgolf()
{
using std::cout;
using std::endl;
using std::cin;
cout<<"Enter fullname ( empty string to quit):";
cin.get((*this).fullname,Len);//换行符留在输入流中,不能用cin.getline()函数
char next;
if(cin)
{
cin.get(next);
while(next!='\n')
cin.get(next);
cout<<"Enter handicap: ";
(cin>>(*this).cap).get();
}
}
void Golf::handicap(int hc)
{
cap=hc;
}
void Golf::showgolf()const
{
std::cout<<"Name: "<<fullname<<"\nHandicap: "<<cap<<std::endl;
}
Golf::Golf(const char *ar,int k)
{
strncpy(fullname,ar,6);//不能用fullname=ar,因为数组不能相互赋值
cap=k;
}
//main
#include <iostream>
#include "C++10.hpp"
int main() {
using namespace std;
Golf go;
go.showgolf();
go.setgolf();
go.showgolf();
go.handicap(20);
go.showgolf();
return 0;
}
10.4
//header file
#ifndef C__10_hpp
#define C__10_hpp
#include <stdio.h>
namespace SALES
{
class Sales
{
static const int QUARTERS = 4;
double sales[QUARTERS];
double average;
double max;
double min;
public:
Sales(const double ar[]={},int n=0);
void setSales();
void showSales()const;
};
}
#endif /* C__10_hpp */
//definationg
#include "C++10.hpp"
#include <iostream>
namespace SALES
{
Sales::Sales(const double ar[],int n)
{
double total=0;
int i;
for(i=0;i<n&&i<4;++i)
{
sales[i]=ar[i];
total+=sales[i];
}
average=total/i;
max=sales[0];
min=sales[0];
for(int k=1;k<i;++k)
{
max=(max>sales[k])?max:sales[k];
min=(min<sales[k])?min:sales[k];
}
if(n<4)
for(int k=n;k<4;++k)
sales[k]=0;
}
void Sales::setSales()
{
using std:: cout;
using std:: cin;
double arr[QUARTERS];
cout<<"Enter sales of 4 quarters:";
for(int i=0;i<QUARTERS;++i)
cin>>arr[i];
Sales(arr, 4);
}
void Sales::showSales()const
{
using std:: cout;
using std:: cin;
using std::endl;
cout<<"Sales of 4 quarters: ";
for(int i=0;i<QUARTERS;++i)
cout<<sales[i]<<" ";
cout<<"\nAverage is: "<<average<<endl;
cout<<"Max: "<<max<<endl;
cout<<"Min: "<<min<<endl;
}
}
//main
#include <iostream>
#include "C++10.hpp"
int main() {
using namespace SALES;
double ar[4]={11.2,3.4,2.6,3.7};
Sales sa;
sa.showSales();
sa=Sales(ar,4);
sa.showSales();
return 0;
}
10.5
//header file
#ifndef C__10_hpp
#define C__10_hpp
#include <stdio.h>
struct customer
{
char fullname[35];
double payment;
};
typedef customer CUS;
static double total=0.0;
class Stack{
private:
static const int LIMIT = 10;
CUS array[LIMIT];
int top;
double total;
public:
Stack();
bool isempty();
bool isfull();
bool add(CUS &);
bool remove();
void show()const;
};
#endif /* C__10_hpp */
//defination
#include "C++10.hpp"
#include <iostream>
Stack::Stack(){
top=0;
total=0;
}
bool Stack::isempty()
{
return top==0;
}
bool Stack::isfull()
{
return top==LIMIT;
}
bool Stack::add(CUS & k)
{
if(top<LIMIT)
{
array[top++]=k;
return true;
}
else
{
std::cout<<"The stack is full!\n";
return false;
}
}
bool Stack::remove()
{
if(top>0)
{
total+=array[--top].payment;
return true;
}
else
return false;
}
void Stack::show()const
{
std::cout<<total<<" removed.\n";
}
//main
#include <iostream>
#include <cctype>
#include "C++10.hpp"
int main() {
using namespace std;
CUS po;
Stack s;
char ch;
cout<<"Please enter A to add a purchase order,\n"
<<"R to remove a member,or Q to quit.\n";
while(cin>>ch && toupper(ch)!='Q')
{
while(cin.get()!='\n')
continue;
if(!isalpha(ch))
{
cout<<"\aEnter A,R or Q.\n";
continue;
}
switch(toupper(ch))
{
case 'A':
cout<<"Enter fullname and his or her payment:\n";
cin.getline(po.fullname,35);
(cin>>po.payment).get();
s.add(po);
s.show();
break;
case 'R':
if(s.isempty())
cout<<"Stack is already empty.\n";
else
{
s.remove();
s.show();
}
break;
}
cout<<"Please enter A to add a purchase order,\n"
<<"R to remove a member,or Q to quit.\n";
}
return 0;
}
10.6
//header file
#ifndef C__10_hpp
#define C__10_hpp
#include <stdio.h>
class Move
{
private:
double x;
double y;
public:
Move(double a = 0,double b = 0);
void showmove()const;
Move add(const Move & m)const;
void reset(double a = 0,double b = 0);
};
#endif /* C__10_hpp */
//defination
#include "C++10.hpp"
#include <iostream>
Move::Move(double a,double b)
{
x=a;
y=b;
}
void Move::showmove()const
{
std::cout<<"x = "<<x<<std::endl;
std::cout<<"y = "<<y<<std::endl;
}
Move Move::add(const Move & m)const
{
Move mo;
mo.x=(*this).x+m.x;
mo.y=(*this).y+m.y;
return mo;
}
void Move::reset(double a,double b)
{
x=a;
y=b;
}
//main
#include <iostream>
#include "C++10.hpp"
int main() {
using namespace std;
Move a=Move(11,23);
Move b=Move(12,20);
Move c;
a.showmove();
c=a.add(b);
c.showmove();
c.reset();
c.showmove();
return 0;
}
10.7
//header file
#ifndef C__10_hpp
#define C__10_hpp
#include <stdio.h>
#include <string>
class Bete
{
private:
std::string fullname;
int ci;
public:
Bete(std::string name="Plorga",int a=50);
void change(int k);
void show()const;
};
#endif /* C__10_hpp */
//defination
#include "C++10.hpp"
#include <iostream>
Bete::Bete(std::string name,int k)
{
fullname=name;
ci=k;
}
void Bete::change(int k)
{
ci+=k;
}
void Bete::show()const
{
std::cout<<"Name: "<<fullname<<std::endl;
std::cout<<"CI: "<<ci<<std::endl;
}
//main
#include <iostream>
#include "C++10.hpp"
int main() {
Bete be;
be.show();
be=Bete("wula wula",20);
be.show();
be.change(102);
be.show();
return 0;
}
10.8
//header file
#ifndef C__10_hpp
#define C__10_hpp
#include <stdio.h>
class List
{
private:
static const int Len =10;
int num[Len];
int top;
public:
List();
void add(int);
bool isfull();
bool isempty();
void visit(void (*ptr)(int &));
};
#endif /* C__10_hpp */
//defination
#include "C++10.hpp"
#include <iostream>
List::List()
{
top=0;
}
void List::add(int k)
{
if(top<Len)
num[top++]=k;
else
std::cout<<"List is full.\n";
}
bool List::isfull()
{
return top==Len;
}
bool List::isempty()
{
return top==0;
}
void List::visit(void (*ptr)(int & k))
{
for(int i=0;i<top;++i)
ptr(num[i]);
}
//main
#include <iostream>
#include "C++10.hpp"
void sub(int&);
int main() {
using namespace std;
List l;
if(l.isempty())
std::cout<<"List is empty.\n";
l.add(10);
l.add(11);
l.add(13);
if(l.isfull())
std::cout<<"List is full.\n";
l.visit(sub);
return 0;
}
void sub(int & k)
{
std::cout<<k*k<<std::endl;
}
/*end*/
C++ Primer plus 第6版 第10章编程答案
最新推荐文章于 2024-10-17 21:38:17 发布