C++ primer plus 练习10



10.1

#include
using namespace std;
#include
class Bankaccount
{
 char name[60];
 char accountnumber[30];
 int  deposit;
public:
 Bankaccount ();
 Bankaccount(char *na, char*acnum, int dep);
 void show();
 void depositmoney(int money);
 void withdrawing_money(int money);
 ~Bankaccount(){};
};
Bankaccount::Bankaccount()
{
 strcpy_s(name ,60,"Wrong");
 strcpy_s(accountnumber, 30 ,"00000000");
 deposit = 0;
}
Bankaccount::Bankaccount(char*na, char*acnum, int dep)
{
 strcpy_s(name, strlen(na) + 1, na);
 strcpy_s(accountnumber, strlen(acnum) + 1, acnum);
 deposit = dep;
}

void Bankaccount::show()
{
 cout << "储户名字: " << name << endl;
 cout << "账号: " << accountnumber << endl;
 cout << "存款: " << deposit << "元" << endl;
}
void Bankaccount::depositmoney(int money)
{
 deposit += money;
 cout << "存入: " << money << "元" << endl;
 cout << "现在存款: " << deposit << "元" << endl;
}
void Bankaccount::withdrawing_money(int money)
{
 deposit -= money;
 cout << "取出:" << money << "元" << endl;
 cout << "现在存款: " << deposit << "元" << endl;
}

int main()
{
 cout << "------------------  Show B  ------------------\n";
 Bankaccount b;
 b.show();
 cout << "------------------  Show BC ------------------\n";
 Bankaccount bc("Lily Cheng", "13400000", 3500);
 bc.show();
 bc.depositmoney(2500);
 bc.withdrawing_money(1000);
 return 0;
}

 

10.2

#include
using namespace std;
#include
#include
class Person
{
 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::Person(const string &ln, const char * fn )//不要写成 const char *fn = "Heyyou",重定义
{
 lname = ln;
 strcpy_s(fname, LIMIT, fn);
}
void Person::show()const
{
 cout << fname << ", "<<  lname << endl;
}
void Person::FormalShow()const
{
 cout << lname << ", " << fname << endl;
}
int main()
{
 Person one;
 Person two("Smythecraft");
 Person three("Dimwiddy", "Sam");
 one.show();
 one.FormalShow();
 cout << endl;
 two.show();
 two.FormalShow();
 cout << endl;
    three.show();
three.FormalShow();
 
}

 

10.3

//golf.h
#ifndef GOLF_H_
#define GOLF_H_
const int Len = 40;
class golf
{
 char fullname[Len];
 int handicap;
public:
 golf(const char * name, int hc);
 int setgolf();
 void handicaprewrite( int hc);
 void showgolf()const;
};
#endif


//golf.cpp
#include"golf.h"
#include<iostream>
using namespace std;
#include<cstring>
golf::golf(const char * name, int hc)
{
 strcpy_s(fullname, strlen(name) + 1, name);
 handicap = hc;
}
int golf:: setgolf()
{

 cout << "Enter your name: \n";
 cin.getline(fullname, Len);
 cout << "Enter your handicap: \n";
 cin >> handicap;
 cin.get();
 if (fullname == "")  return 0;
 return 1;
}
void golf::handicaprewrite(int hc)
{
 handicap = hc;
}
void golf:: showgolf()const
{
 cout << "Name : " << fullname << endl;
 cout << "Handicap : " << handicap << endl;
}



//pe9 -1.cpp
#include<iostream>
#include"golf.h"
#include<cstring>
using namespace std;
int main()
{
 golf g("Lily Cheng",25);
 g.showgolf();
 int n = 4;
 for (int i = 0; i < n; i++)
 {
  g.setgolf();
  g.showgolf();
 }
}


10.4

//sale.h
#ifndef SALE_H_
#define SALE_H_
namespace SALES
{
 const int QUARTERS = 4;
 class Sales
 {
  double sales[QUARTERS];
  double average;
  double max;
  double min;
 public:
  ~Sales(){};
  Sales(const double ar[], int n);
  Sales();
  void showSales()const;
 };
}
#endif

//sales.cpp
#include"sale.h"
#include<iostream>
using namespace std;
namespace SALES
{
 Sales::Sales(const double ar[], int n)
 {
  int i;
  double total = 0;
  max = -999;
  min = 999;
  if (n > 4) n = 4;
  for (i = 0; i < n; i++)
  {
   sales[i] = ar[i];
   if (max < sales[i])  max = sales[i];
   if ( min >  sales[i])  min =  sales[i];
   total +=  sales[i];
  }
  if (n < 4)
  {
   for (; i < 4; i++)
     sales[i] = 0;
   if ( min > 0)  min = 0;
   if (max < 0) max = 0;
  }
  average = total / 4;
 }
 Sales::Sales()
 {
  int i;
  double total = 0;
  max = -999;
  min = 999;
  cout << "Sales : \n";
  for (i = 0; i < 4; i++)
  {
   cout << i << " : ";
   cin >>sales[i];
   if (sales[i]> max)
     max =  sales[i];
   if ( sales[i] <  min)
     min =  sales[i];
   total +=  sales[i];
  }
   average = total / 4;

 }
 void Sales::showSales()const
 {
  for (int i = 0; i < 4; i++)
   cout << sales[i] << endl;
  cout << "Average: " << average << endl;
  cout << "Max: " << max << endl;
  cout << "Min: " << min << endl;
 }
}


//main.cpp
#include<iostream>
using namespace std;
#include"sale.h"
int main()
{
 using namespace SALES;
 double ar[4] = { 3.3, 2.2, 1.1, 4.4 };
 double ar2[3] = { 2.2, 3.1, 1.1 };
 Sales s(ar, 4);
 s.showSales();
 Sales s2(ar2, 3);
 s2.showSales();
 Sales ss;
 ss.showSales();
 return 0;
}

10.6

#include<iostream>
using namespace std;
class Move
{
 double x;
 double y;
public:
 Move(double a = 0, double b = 0) :x(a), y(b){};
 void showmove()const;
 Move add(const Move&m)const ;
 void reset(double a = 0, double b = 0){
  x = a; y = b;
 }
};
void Move::showmove()const
{
 cout << "x = " << x << ", ";
 cout << "y = " << y << endl;
}
Move Move::add(const Move&m)const
{
 double xx;
 double yy;
 xx = m.x + x;
 yy = m.y + y;
 Move a(xx, yy);
 a.showmove();
 return a;

}

void main()
{
 Move a(5.5, 6.6);
 a.showmove();
 Move b(3.3, 4.4);
 b.showmove();
 a.add(b);
 a.reset();
 a.showmove();
 return;
}


10.7

#include<iostream>
using namespace std;
#include<cstring>
const int LIMIT = 19;
class Betelgeusean_plorg
{
 char plorg[LIMIT];
 int CI;
public:
 Betelgeusean_plorg(char*name = "PLORGA", int ci = 50);
 void SetCI(int ci);
 void showPlorg()const;
};
Betelgeusean_plorg::Betelgeusean_plorg(char*name, int ci)
{
 strcpy_s(plorg, LIMIT, name);
 CI = ci;
}
void Betelgeusean_plorg::showPlorg()const
{
 cout << "Plorg Name: " << plorg << endl;
 cout << "CI: " << CI << endl;
}
void Betelgeusean_plorg::SetCI(int ci)
{
 CI = ci;
}
int main()
{
 Betelgeusean_plorg p;
 p.showPlorg();
 p.SetCI(55);
 p.showPlorg();
 Betelgeusean_plorg pl("Hey you", 25);
 pl.showPlorg();
 return 0;
}

10.5

//stack.h
#ifndef STACK_H_
#define STACK_H_
struct customer
{
 char fullname[35];
 double payment;
};
typedef customer Item;
class Stack
{
 enum{MAX = 10};
 Item items[MAX];
 int top;
 static double total;
public:

 Stack();
 bool isempty()const;
 bool isfull()const;
 bool push(const Item & item);
 bool pop(Item & item);
};

#endif

//stack.cpp
#include"Stack.h"
#include<iostream>
double Stack::total = 0;//要在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 (top < MAX)
 {
  items[top++] = item;
  return true;
 }
 else return false;
}

bool Stack::pop(Item & item)
{
 if (top > 0)
 {
  item = items[--top];
  total += items[top].payment;
  std::cout << "Total: " << total << std::endl;
  return true;
 }
 else return false;
}

//stack.cpp
#include<iostream>
#include<cctype>
#include"Stack.h"
using namespace std;
int main()
{
 Stack st;
 char ch;
 customer po;
 cout << "Please Enter A to add a customer.\n"
  << "P to process a customer , or Q to quit.\n";
 while (cin >> ch && toupper(ch) != 'Q')
 {
  while (cin.get() != '\n')
   continue;
  if (!isalpha(ch))
  {
   cout << "\a";
   continue;
  }
  switch (ch)
  {
  case'A':
  case'a':cout << "Enter a customer's name to add: ";
   cin.getline(po.fullname, 35);
   cout << "Enter his\\her payment: ";
   cin >> po.payment;
   if (st.isfull())
    cout << "stack already full\n";
   else
    st.push(po);
   break;
  case'P':
  case'p':if (st.isempty())
   cout << "stack already empty\n";
    else{
     st.pop(po);
     cout << "customer # name: " << po.fullname << ",  payment: " << po.payment
      << " popped.\n";
    }break;
  }
  cout << "Please Enter A to add a customer.\n"
   << "P to process a customer , or Q to quit.\n";
 }
 cout << "Bye\n";
 return 0;
}

10.8
//List.h 一般模板定义和模板实现不分开,一般在头文件实现,做inline函数
#ifndef LIST_H_
#define LIST_H_
const int LIMIT = 10;
template < typename T>
class List
{
 T list[LIMIT];
 int count;
public:
 List();//创建空列表
 bool listadd(const T & a);
 bool isempty()const;
 bool isfull()const;
 void visit(void(*pf)(T & a));
 void show()const;
};
template<typename T>
void add_one(T &a);//随便一个函数
#endif

//List.cpp
#include<iostream>
#include"List.h"
template <typename T>//必须写出模板类,否则T无效 
List<T>::List()//模板类的定义必须有模板参数,不能只写List::List 
{
 list[0] = '\0';
 count = 0;
}
template<typename T>
bool List<T>::listadd(const T &a)
{
 if (isfull() == 0)
 {
  list[count++] = a;
  return true;
 }
 else return false;
}
template<typename T>
bool List<T>::isempty()const
{
 return count == 0;
}
template<typename T>
bool List<T>::isfull()const
{
 return count == LIMIT;
}
template<typename T>
void List<T>::visit(void(*pf)(T &a))
{
 for (int i = 0; i < count; i++)
  pf(list[i]);
}
template<typename T>
void List<T>::show()const
{
 for (int i = 0; i < count; i++)
  cout << list[i] << " ";
}
template<typename T>
void add_one(T&a)
{
 a = a*a;
}

//list实现.cpp
#include"List.h"
#include"List.cpp"//模板定义和模板实现分开时,需要弄这个
#include<iostream>
using namespace std;
int main()
{
 List<int> list;
 int i;
 cout << "empty? " << list.isempty() << endl;
 for (i = 0; i < 5; i++)
 {
  list.listadd((i+2) );
 }
 list.show();
 cout << endl;
 cout << "full? " << list.isfull() << endl;
 list.visit(add_one);
 list.show();
 return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值