//第十章第一题
#ifndef GOLF_H_
#define GOLF_H_
#include<string>
#include<iostream>
using std::string;
class BankCount
{
private: string name;
string account;
unsigned long deposit;
public:
BankCount();
BankCount(const string &s1, const string &s2, unsigned long n);
~BankCount();
void show();
void input(unsigned long n);
void output(unsigned long n);
};
#endif
#include<iostream>
#include"golf.h"
using std::cin;
using std::cout;
using std::endl;
BankCount::BankCount()
{
cout << "Default constructor called\n";
}
BankCount::BankCount(const string &s1, const string &s2, unsigned long n)
{
cout << " constructor using "<<s1<<" called\n";
name = s1;
account = s2;
deposit = n;
}
BankCount::~BankCount()
{
cout << "Good bye!\n";
}
void BankCount::show()
{
cout << "Bank name:"<<name;
cout << " Bank account:" << account;
cout << " Bank deposit:" << deposit<<endl;
}
void BankCount::input(unsigned long n)
{
if (n<0)
cout << "Bad input"<< endl;
else
{
cout << "Your input is:" << n << endl;
deposit += n;
}
}
void BankCount::output(unsigned long n)
{
if (n<0)
cout << "Bad input" << endl;
else
{
if (deposit > n)
{
deposit -= n;
cout << "Your output is:" << n << endl;
}
else
cout << "Your account is not enough!:"<< endl;
}
}
#include<iostream>
#include<stdlib.h>
#include<string>
#include"golf.h"
//第十章
using std::string;
using std::cin;
using std::cout;
using std::endl;
int main()
{
{
BankCount b1("1:", "郑州银行", 1000);
b1.show();
BankCount b2 = BankCount("2:", "中原银行", 2000);
b2.show();
b1.input(2000);
b1.show();
b2.input(2000);
b2.show();
b1.output(4000);
b1.show();
}
system("pause");
return 0;
}
//第二题
#ifndef GOLF_H_
#define GOLF_H_
#include<string>
#include<iostream>
using std::string;
using std::cout;
class Person
{
private:
static const int LIMIT = 25;
string lname;
char fname[LIMIT];
public:
Person(){ lname = "", fname[0] = '\0'; cout << "Default constor!\n"; }
Person(const string &ln, const char *fn="Heryyou");
~Person();
void show()const;
void FormalShow() const;
};
#endif
#define _CRT_SECURE_NO_WARNINGS
#include<string>
#include<iostream>
#include"golf.h"
using std::string;
using std::cin;
using std::cout;
using std::endl;
Person::~Person()
{
cout << "Bye!" << endl;
}
Person::Person(const string &ln, const char *fn )
{
strncpy(fname,fn,LIMIT);
lname = ln;
cout << "No Default!" << endl;
}
void Person::show() const
{
cout << "Your name is(FL):" << fname << " " << lname << endl;
}
void Person::FormalShow() const
{
cout << "Your name is(LF):" << lname << " " << fname << endl;
}
#include<iostream>
#include<stdlib.h>
#include<string>
#include"golf.h"
//第十章
using std::string;
using std::cin;
using std::cout;
using std::endl;
int main()
{
{
Person one;
Person two("Simth");
Person three("Tomson", "wu");
one.show();
one.FormalShow();
two.show();
two.FormalShow();
three.show();
three.FormalShow();
}
system("pause");
return 0;
}
//第3题
#ifndef GOLF_H_
#define GOLF_H_
const int len = 40;
class golf
{
private:
char fullname[len];
int handicap;
public:
golf(const char* name="wqf", int hc=10);
~golf();
int setgolf();
void sethandicap(int hc);
void showgolf()const;
};
#endif
#include<iostream>
#include<string>
#include"golf.h"
using std::string;
using std::cin;
using std::cout;
using std::endl;
golf::golf(const char* name, int hc)
{
strcpy_s(fullname, name);
handicap = hc;
}
golf::~golf()
{
cout << "Bye!" << endl;
}
int golf::setgolf()
{
cout << "Please enter your name:";
string str;
getline(cin, str);
if (str == "\0")
return 0;
cout << "Please enter your hc:";
strcpy_s(fullname, str.c_str());
while (!(cin >> handicap))
{
cin.clear();
cout << "Please enter a integer:";
}
while (cin.get() != '\n')
continue;
*this = golf(fullname, handicap);
return 1;
}
void golf::sethandicap(int hc)
{
handicap = hc;
}
void golf::showgolf()const
{
cout << "golf: " << fullname << endl;
cout << "handicap " << handicap << endl;
}
#include<iostream>
#include"golf.h"
//第十章
int main()
{
using namespace std;
golf one;
golf two = {"hhj",11};
golf three;
three.setgolf();
one.showgolf();
two.showgolf();
three.showgolf();
one.sethandicap(3);
one.showgolf();
system("pause");
return 0;
}
//第四题
#ifndef GOLF_H_
#define GOLF_H_
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();
Sales();
void showSales()const;
};
}
#endif
#include<iostream>
#include"golf.h"
namespace SALES
{
using std::cin;
using std::cout;
using std::endl;
using namespace SALES;
Sales::Sales(const double ar[], int n)
{
unsigned int size = n < QUARTERS ? n : QUARTERS;
double sum=0;
max = min = ar[0];
for (unsigned int i = 0; i < size; i++)
{
sales[i] = ar[i];
sum += ar[i];
if (ar[i]>max)
max = ar[i];
else if (ar[i] < min)
min = ar[i];
}
average = sum / size;
}
Sales::~Sales()
{
cout << "Bye!" << endl;
}
Sales::Sales()
{
cout << "Please enter 4 sales:";
for (int i = 0; i < QUARTERS; i++)
{
cout << "sales" << i + 1 << ":";
cin >> sales[i];
}
*this = Sales(sales, QUARTERS);
double sum=0;
max = min = sales[0];
for (unsigned int i = 0; i < QUARTERS; i++)
{
sum += sales[i];
if (sales[i]>max)
max = sales[i];
else if (sales[i] < min)
min = sales[i];
}
average = sum / QUARTERS;
}
void Sales::showSales()const
{
cout << "Sales: ";
for (int i = 0; i < QUARTERS; i++)
{
cout << sales[i] << " ";
}
cout << endl;
cout << "aver:" << average << endl;
cout << "max:" << max << endl;
cout << "min:" << min << endl;
}
}
#include<iostream>
#include<stdlib.h>
#include<cstring>
#include"golf.h"
int main()
{
{
using namespace SALES;
double salesList[] = { 11.2, 11.3, 11.4, 11.5, 11.6 };
Sales s1 = Sales(salesList, sizeof(salesList) / sizeof(double));
s1.showSales();
Sales s2;
s2.showSales();
}
system("pause");
return 0;
}
第5题
#ifndef STACK_H_
#define STACK_H_
typedef struct customer
{
char fullname[35];
double payment;
}CU;
typedef customer Item;
class Stack
{
private:
enum{MAX=10};
Item items[MAX];
int top;
int total;
public:
Stack();
bool isempty()const;
bool isfull()const;
bool push(const Item &item);
bool pop(Item &item);
};
#endif
#include<iostream>
#include"栈.h"
using std::cout;
using std::endl;
Stack::Stack()
{
top = 0;
total = 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 += item.payment;
cout <<"#"<< item.fullname << endl;
cout << "total:" << total << endl;
return true;
}
else
return false;
}
#include<iostream>
#include<cctype>
#include"栈.h"
#include<string>
int main()
{
using namespace std;
Stack st;
char ch;
Item po;
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')
continue;
if (!isalpha(ch))
{
cout << '\a';
continue;
}
switch (ch)
{
case'a':
case'A':
cout << "Enter a fullname to add:";
cin.getline( po.fullname,35);
cout << "Enter a payment to add:";
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 is empty!" << endl;
else
{
st.pop(po);
}
break;
}
cout << "Please enter A to add a purchase order,\n"
<< "P to process a PO,or Q to quit\n";
}
cout << "Bye!";
return 0;
}
//第六题
#ifndef GOLF_H_
#define GOLF_H_
class Move
{
private:
double x;
double y;
public:
Move(double a=0, double b=0);
Move add(const Move & m)const;
void showMove()const;
void reset(double a = 0, double b = 0);
};
#endif
#include"golf.h"
#include<iostream>
using std::cin;
using std::cout;
using std::endl;
Move::Move(double a, double b)
{
x = a;
y = b;
}
Move Move::add(const Move & m)const
{
return Move(m.x + x, m.y + y);
}
void Move::showMove()const
{
cout << "x=" << x<<endl;
cout << "y=" << y << endl;
}
void Move::reset(double a, double b)
{
x = a;
y = b;
}
#include<iostream>
#include<stdlib.h>
#include<cstring>
#include"golf.h"
int main()
{
Move m1;
m1 = { 3, 5 };
Move m2(7, 8);
m1.showMove();
m2.showMove();
m1=m1.add(m2);
m1.showMove();
m1.reset();
m1.showMove();
system("pause");
return 0;
}
//第七题
#ifndef GOLF_H_
#define GOLF_H_
class Plorg
{
private:
enum{ Max = 20 };
char name[Max];
int CI;
public:
Plorg(char *wname="wqf", int wCI=10);
void showPlorg()const;
void setCI(int wCI);
};
#endif
#include<iostream>
#include"golf.h"
#include<string>
using std::cout;
using std::endl;
Plorg::Plorg(char *wname, int wCI)
{
strcpy_s(name, wname);
CI = wCI;
}
void Plorg::showPlorg()const
{
cout << "Name:" << name << endl;
cout << "CI:" << CI << endl;
}
void Plorg::setCI(int wCI)
{
CI = wCI;
}
#include<iostream>
#include<stdlib.h>
#include<cstring>
#include"golf.h"
int main()
{
Plorg p1;
Plorg p2("hhj",11);
p1.showPlorg();
p2.showPlorg();
p1.setCI(13);
p1.showPlorg();
system("pause");
return 0;
}
//第8题
#ifndef STACK_H_
#define STACK_H_
typedef int Item;
class List
{
private:
enum{ MAX = 5 };
Item items[MAX];
int top;
public:
List(const Item[]=NULL,int m=0);
bool isempty()const;
bool isfull()const;
bool push(const Item &item);
bool pop(Item &item);
void visit(void(*pf)(Item &m));
};
void show(Item &m);
void change(Item &m);
Item get();
#endif
#include<iostream>
#include"zhan.h"
using std::cout;
using std::endl;
using std::cin;
List::List(const Item[], int m)
{
top = 0;
}
bool List::isempty()const
{
return top == 0;
}
bool List::isfull()const
{
return top == MAX;
}
bool List::push(const Item &item)
{
if (top < MAX)
{
items[top++] = item;
return true;
}
else
return false;
}
bool List::pop(Item &item)
{
if (top>0)
{
item = items[--top];
return true;
}
else
return false;
}
void List::visit(void(*pf)(Item &m))
{
for (int i = 0; i < top; i++)
pf(items[i]);
}
void show(Item &m)
{
cout << m << ',';
}
void change(Item &m)
{
cout << "Please enter replace content:";
Item a;
cin >> a;
if (m = a)
{
cout << "Success!" << endl;
}
else
cout << "failure!" << endl;
}
Item get()
{
Item a;
cout << "Please enter your content:";
cin >> a;
while (!cin)
{
cin.sync();
cin.clear();
cout << "Bad input! enter again!";
cin >> a;
}
return a;
}
#include<iostream>
#include"zhan.h"
#include<string>
int main()
{
using namespace std;
List st;
if (st.isempty())
cout << "It's empty." << endl;
else
cout << "It's not empty." << endl;
Item a;
while (!st.isfull())
{
a = get();
if (st.push(a))
cout << "Input successful"<< endl;
else
cout << "Bad input!" << endl;
}
st.visit(show);
st.visit(change);
st.visit(show);
system("pause");
return 0;
}