// 10.1
// bank.h
#ifndef BANK__H__
#define BANK__H__
class Bank
{
private:
enum{SIZE = 20 };
char bank_name[SIZE];
char account[SIZE];
double deposit;
public:
Bank();
Bank(const char* _name ,const char* _account,double _deposit = 0);
~Bank();
void showData() const;
void pushDeposit(double num);
void popDeposit(double num);
};
#endif
//Bank.cpp
#include <iostream>
#include <string>
#include "bank.h"
Bank::Bank()
{
strncpy(bank_name,"no name",7);
bank_name[7] = '\0';
strncpy(account,"null",4);
account[4] = '\0';
deposit = 0;
std::cout<<"use default constructor\n";
}
Bank::Bank(const char* _name,const char* _account,double _deposit)
{
strcpy(bank_name,_name);
strcpy(account,_account);
deposit = _deposit;
std::cout<<"use no_default constructor\n";
}
Bank::~Bank()
{
std::cout<<"Bye, "<<bank_name<<std::endl;
}
void Bank::showData() const
{
std::cout<<"The user bank_name is: "<<bank_name<<std::endl;
std::cout<<"The user account is: "<<account<<std::endl;
std::cout<<"The user deposit is: "<<deposit<<std::endl;
}
void Bank::pushDeposit(double num)
{
if(num>DBL_MAX)
std::cout<<"can't enter extra large number.\n";
else
deposit += num;
}
void Bank::popDeposit(double num)
{
if(num>deposit)
std::cout<<"the num more than the deposit.\n";
else
deposit -= num;
}
//main.cpp
#include <iostream>
#include "bank.h"
int main()
{
using namespace std;
Bank user_one = Bank("luopeng","mmdnxh",2000);
Bank user_two;
user_two.showData();
user_one.showData();
user_one.popDeposit(3000);
user_one.showData();
user_one.popDeposit(1999);
user_one.showData();
user_one.pushDeposit(200);
user_one.showData();
}
//10.2
//person.h
#ifndef PERSON__H__
#define PERSON__H__
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
//person.cpp
#include <iostream>
#include <string>
#include "person.h"
using std::cout;
using std::cin;
using std::endl;
using std::string;
Person::Person(const string & ln,const char* fn)
{
lname = ln;
strncpy(fname,fn,24);
fname[24]='\0';
}
void Person::show() const
{
cout<<"the man firstname is:"<<fname<<" ,and the lastname is"<<lname<<endl;
}
void Person::FormalShow() const
{
cout<<"the man lastname is:"<<lname<<" ,and the firstname is"<<fname<<endl;
}
//main.cpp
#include <iostream>
#include "person.h"
#include <string>
int main()
{
using namespace std;
Person one;
Person two("Smythecraft");
Person three("Dimwiddy","Sam");
one.show();
one.FormalShow();
two.show();
three.show();
}
//10.3
//golf.h
#ifndef GOLF__H__
#define GOLF__H__
class Golf
{
private:
static const int LEN = 40;
char fullname[LEN];
int handicap;
public:
Golf();
Golf(const char* name,int hc);
void setGolf(const char* name,int hc);
void newHandicap(int hc);
void showGolf() const;
};
#endif
//golf.cpp
#include <iostream>
#include "golf.h"
#include <string>
Golf::Golf()
{
strncpy(fullname,"null",4);
fullname[4]='\0';
handicap = 0;
}
Golf::Golf(const char* name,int hc)
{
strncpy(fullname,name,39);
fullname[39]='\0';
handicap = hc;
}
void Golf::setGolf(const char* name,int hc)
{
*this=Golf::Golf(name,hc);//将产生的临时对象赋给调用这个方法的对象
}
void Golf::newHandicap(int hc)
{
handicap = hc;
}
void Golf::showGolf() const
{
std::cout<<"The fullname is: "<<fullname<<" , and the handicap is : "<<handicap<<std::endl;
}
//main.cpp
#include <iostream>
#include "golf.h"
#include <string>
int main()
{
using namespace std;
Golf golf_one;
golf_one.showGolf();
Golf golf_two = Golf("mmdnxh",20);
golf_two.newHandicap(10);
golf_two.showGolf();
golf_two.setGolf("xiaoming",10);
golf_two.showGolf();
}
//10.4
//sale.h
#include <iostream>
#ifndef __SALE__H__
#define __SALE__H__
namespace SALES
{
class Sales
{
private:
static const int QUARTERS = 4;
double sales[QUARTERS];
double average;
double max;
double min;
public:
Sales(const double ar[],int n);
Sales();
void showSales() const;
};
}
#endif
//sale.cpp
#include <iostream>
#include "sale.h"
using std::cin;
using std::cout;
using std::endl;
using std::string;
namespace SALES
{
static double calcAverage(double arr[], int arrsize)
{
double sum = 0;
for(int i = 0;i<arrsize;i++)
sum+=arr[i];
return (sum/arrsize);
}
static double calcMax(double arr[], int arrsize)
{
int idMax = 0;
for(int i =1;i<arrsize;i++)
{
if(arr[i]>arr[idMax])
idMax = i;
}
return arr[idMax];
}
static double calcMin(double arr[], int arrsize)
{
int idMin = 0;
for(int i =1;i<arrsize;i++)
{
if(arr[i]<arr[idMin])
idMin = i;
}
return arr[idMin];
}
Sales::Sales(const double ar[],int n)
{
int times = n<QUARTERS?n:QUARTERS;
for(int i = 0;i<times;i++)
(*this).sales[i]=ar[i];
for(int i =times;i<QUARTERS;i++)
this->sales[i] = 0;
this->average = calcAverage(this->sales, times);
this->max = calcMax(this->sales, times);
this->min = calcMin(this->sales, times);
}
Sales::Sales()
{
cout << "Please enter " << QUARTERS << " sale record:";
int times = QUARTERS;
double arr[QUARTERS] = {0};
for (int i = 0;i < QUARTERS; i++) {
cin >> arr[i];
if (!cin) {
times = i; //表示输入异常
break;
}
}
*this = Sales::Sales(arr, QUARTERS); //先得到初始数据在进行操作
this->average = calcAverage(this->sales, times);
this->max = calcMax(this->sales, times);
this->min = calcMin(this->sales, times);
}
void Sales::showSales() const
{
cout << "sales: ";
for(int i = 0;i<QUARTERS;i++)
cout<<this->sales[i]<<" ";
cout << endl;
cout << "average: " <<this->average << endl;
cout << "max: " << this->max << endl;
cout << "min: " << this->min << endl;
}
}
//main.cpp
#include <iostream>
#include "sale.h"
using namespace std;
int main ()
{
using namespace SALES;
const double salesList[] = {2.24,234,53,23.6,6.4};
Sales salesCar(salesList, (sizeof(salesList)/sizeof(salesList[0]))); ;
salesCar.showSales();
Sales salesBus;
salesBus.showSales();
}
//10.5
//stack.h
// class definition for the stack ADT
#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();
bool pop();
};
#endif
//stack.cpp
// stack.cpp Stack member functions
#include "stack.h"
#include <iostream>
#include <string>
Stack::Stack()
{
top = 0;
}
bool Stack::isempty() const
{
return top == 0;
}
bool Stack::isfull() const
{
return top == MAX;
}
bool Stack::push()
{
if((*this).top<MAX)
{
std::cout<<"Please enter the fullname:";
std::string str;
std::cin>>str;
strncpy((*this).items[top].fullname,str.c_str(),sizeof(str)>34?34:sizeof(str));
(*this).items[top].fullname[34]='\0';
std::cout<<"Please enter the payment:";
while(!(std::cin>>(*this).items[top].payment))//判断是否输入数字
{
std::cin.clear();//将标志位从fail(1)改为0,再次打开输出检测
std::cout<<"Please enter the type of matching data:";
//while (std::cin.get() != '\n')//读取第一个字符后的多余的字符。检测到\n表示读取完毕,输入流为空。
// continue;
std::cin.sync();//将输入流清空,与上面等价
}
top++;
return true;
}
else
return false;
}
bool Stack::pop()
{
static double payment = 0.0;
if(top>0)
{
//*(this->items) = items[--top];
payment += (*this).items[--top].payment;
std::cout<<"the total quantity is "<<payment<<std::endl;
return true;
}
else
return false;
}
//main.cpp
//stacker.cpp -- testing the Stack class
#include <iostream>
#include "stack.h"
int main()
{
using namespace std;
Stack st;
char ch;
cout<<"Please enter A to add a purchase order.\n"
<<"P to process a PO , or Q to quit\n";
while(cin>>ch &&toupper(ch)!='Q')
{
while (cin.get() != '\n')//读取第一个字符后的多余的字符。检测到\n表示读取完毕,输入流为空。
continue;
if(!isalpha(ch))//判断第一个字符的合理性
{
cout<<'\a';
continue;
}
switch(ch)
{
case 'a':
case 'A':
if(st.isfull())
cout<<"stack already full\n";
else
{
cout<<"Enter a sturct member to add.\n ";
st.push();
cout<<"Add success!\n";
}
break;
case 'p':
case 'P':
if(st.isempty())
cout<<"stack already empty\n";
else{
st.pop();
}
break;//没有使用default
}
cout<<"Please enter A to add a purchase order.\n"
<<"P to process a PO , or Q to quit\n";
}
cout<<"Bye!\n";
return 0;
}
//10.6
//move.h
#ifndef MOVE__H__
#define 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) const;
void reset(double a = 0,double b =0);
};
#endif
//move.cpp
#include <iostream>
#include "move.h"
Move::Move(double a,double b)
{
this->x = a;
this->y = b;
}
void Move::showmove() const
{
std::cout<<"the x is "<<x<<" ,and the y is: "<<y<<std::endl;
}
Move Move::add(const Move & m) const
{
return Move::Move(m.x,m.y);
}
void Move::reset(double a , double b)
{
this->x = a;
this->y = b;
}
//main.cpp
#include <iostream>
#include "move.h"
int main()
{
using namespace std;
Move move_one(12,11);
move_one.showmove();
Move move_two(110.1,312);
Move move_three = move_one.add(move_two);
move_three.showmove();
move_three.reset();
move_three.showmove();
move_three.reset(123,121);
move_three.showmove();
}
//10.7
//plorg.h
#ifndef PLORG__H__
#define PLORG__H__
class Plorg
{
private:
char fullname[20];
int ci;
public:
Plorg(const char* name ="Plorga",int _ci =50);
void resetCi(int _ci);
void showPlorg();
};
#endif
//plorg.cpp
#include <iostream>
#include "plorg.h"
#include <string>
Plorg::Plorg(const char* name ,int _ci)
{
int len =(strlen(name)>19?19:strlen(name));
strncpy((*this).fullname,name,len);
(*this).fullname[len] = '\0';
(*this).ci = _ci;
}
void Plorg::resetCi(int _ci)
{
(*this).ci = _ci;
}
void Plorg::showPlorg()
{
std::cout<<"the fullname is: "<<(*this).fullname<<" ,and the ci is : "<<ci<<std::endl;
}
//main.cpp
#include <iostream>
#include "plorg.h"
#include <string>
int main()
{
Plorg plorg_one = Plorg();
Plorg plorg_two = Plorg("mmdnxh",20);
plorg_one.showPlorg();
plorg_two.showPlorg();
plorg_two.resetCi(12);
plorg_two.showPlorg();
}
//10.8
//list.h
#ifndef LIST__H__
#define LIST__H__
#include<iostream>
typedef double Item;
void showList(Item & l);
class List
{
private:
enum{MAX = 10};
Item items;
int top;
public:
List();
bool isempty() const;
bool isfull() const;
bool push();
void visit(void (*pf)(Item &));
};
#endif
list.cpp
// stack.cpp Stack member functions
#include "list.h"
#include <iostream>
#include <string>
void showList(Item & l)
{
std::cout<<"The list payment is:"<<l<<std::endl;
}
List::List()
{
top = 0;
}
bool List::isempty() const
{
return top == 0;
}
bool List::isfull() const
{
return top == MAX;
}
bool List::push()
{
if((*this).top<MAX)
{
std::cout<<"Please enter the payment:";
while(!(std::cin>>items))//判断是否输入数字
{
std::cin.clear();//将标志位从fail(1)改为0,再次打开输出检测
std::cout<<"Please enter the type of matching data:";
//while (std::cin.get() != '\n')//读取第一个字符后的多余的字符。检测到\n表示读取完毕,输入流为空。
// continue;
std::cin.sync();//将输入流清空,与上面等价
}
top++;
return true;
}
else
return false;
}
void List::visit(void (*pf)(Item &))
{
pf((*this).items);
}
//main.cpp
//stacker.cpp -- testing the Stack class
#include <iostream>
#include "list.h"
int main()
{
using namespace std;
List list;
char ch;
cout<<"Please enter A to add a purchase order.\n"
<<"v to show a PO , or Q to quit\n";
while(cin>>ch &&toupper(ch)!='Q')
{
while (cin.get() != '\n')//读取第一个字符后的多余的字符。检测到\n表示读取完毕,输入流为空。
continue;
if(!isalpha(ch))//判断第一个字符的合理性
{
cout<<'\a';
continue;
}
switch(ch)
{
case 'a':
case 'A':
if(list.isfull())
cout<<"list already full\n";
else
{
cout<<"Enter a sturct member to add.\n ";
list.push();
cout<<"Add success!\n";
}
break;
case 'v':
case 'V':
if(list.isempty())
cout<<"list already empty\n";
else{
double x = 20;
//pf = showList;
list.visit(showList);
}
break;//没有使用default
}
cout<<"Please enter A to add a purchase order.\n"
<<"v to show a PO , or Q to quit\n";
}
cout<<"Bye!\n";
return 0;
}