第一题
要求:创建一个银行账户类,要求可以初始化,显示账户,存取款。
类声明:
class Bank
{
private:
static const int MAX = 20;
char Name[MAX];
std::string Id;
int Money;
public:
Bank();
Bank( char name[], const std::string& na,int money = 0);
~Bank();
void show_bank() const;
void in_bank(int m);
void out_bank(int m);
};
实现方法:
Bank::Bank()
{
Id = "no name";
for (int i = 0; i < MAX; i++)
Name[i] = 0;
Money = 0;
show_bank();
}
Bank::Bank( char name[],const std::string& na, int money)
{
Id = na;
for (int i = 0; i < MAX; i++)
Name[i] = name[i];
if (money < 0)
{
std::cout << "Enter error, the money can't be negative.\n"
<< "Set it to zero.\n";
Money = 0;
}
else
Money = money;
show_bank();
}
Bank::~Bank()
{
show_bank();
std::cout << "Over the using " << Id << std::endl;
}
void Bank::show_bank() const
{
using std::cout;
using std::endl;
cout << "The Bank user : " << Id << endl
<< "The User ID : " << Name << endl
<< "The User total money : " << Money << endl;
}
void Bank::in_bank(int n)
{
using std::cout;
//using std::endl;
if (n < 0)
{
cout << "The Saving Money can't be negative.\n";
}
else
Money += n;
show_bank();
}
void Bank::out_bank(int n)
{
using std::cout;
//using std::endl;
if (n < 0)
{
cout << "The Withdraw Money can't be negative.\n";
}
else if (n > Money)
{
cout << "The Withdraw Money can't be greater than your Bank Money.\n";
}
else
Money -= n;
show_bank();
}
测试程序:
int work_1(){
const int MAX = 20;
char name[MAX] = "Mark Lude King";
Bank a(name, "19491001", 9999);
a.in_bank(10001);
a.out_bank(20001);
a.out_bank(20000);
return 0;
}
第二题
要求:创建一个类,了解字符数组与string字符串在类中使用的区别。
类声明:
class Person
{
private:
static const int LIMIT = 25;
std::string Iname;
char fname[LIMIT];
public:
Person();
Person(const std::string& ln, const char* fn = "Heyyou");
void Show() const;
void FormalShow() const;
};
实现方法:
Person::Person()
{
Iname = "Data Structure";
for (int i = 0; i < LIMIT-1; i++)
fname[i] = ' ';
fname[LIMIT - 1] = '\0';
}
Person::Person(const std::string& ln,const char * fn)
{
Iname = ln;
sizeof(fn);
int s;
//s = sizeof(fn) < LIMIT ? sizeof(fn) : LIMIT;
//for (int i = 0; i < s; i++)
// fname[i] = fn[i];
int i = 0;;
while (fn[i] != '\0')
i++;
if (i == 0)
{
std::cout << "Error.\nAnd set fname to NULL\n";
fname[0] = '\0';
}
else {
s = i < LIMIT-1 ? i : LIMIT;
for (int i = 0; i < s; i++)
fname[i] = fn[i];
fname[s] = '\0';//注意结尾
std::cout << "#测试#:sizeof是否包括\'\\0\' : " << fname[s-1] << std::endl;
}
}
void Person::Show()const
{
using std::cout;
using std::endl;
cout << Iname << endl;
cout << fname << endl;
}
void Person::FormalShow() const
{
using std::cout;
using std::endl;
cout << Iname << endl;
cout << fname << endl;
}
测试程序:
int work_2()
{
Person one;
Person two("Smythecraft");
Person three("Dimwiddy", "Sammer");
one.Show();one.FormalShow();
two.Show(); two.FormalShow();
three.Show(); three.FormalShow();
return 0;
}
第三题
要求:使用类形式来改写程序9.1,要求使用构造函数。
类声明:
class golf
{
private:
static const int Len = 40;
char fullname[Len];
int _handicap;
public:
golf();
void setgolf(golf& g, const char* name, int hc);
int setgolf(golf& g);
void handicap(golf& g, int hc);
void showgolf(const golf& g);
int name_is_null(golf& g)const;
};
实现方法:
golf::golf()
{
fullname[0] = '\0';
_handicap = 0;
}
void golf::setgolf(golf& g, const char* name, int hc)
{
int i;
for (i = 0; i < Len; i++)
if (name[i] != '\0')
fullname[i] = name[i];
else
break;
fullname[i] = '\0';
if (hc < 0)
{
std::cout << "the level can't be negative.\n"
<< "and set it to zero.\n";
_handicap = 0;
}
else
{
_handicap = hc;
}
}
int golf::setgolf(golf& g)
{
using std::cin;
static int num = 0;
std::cout << "enter the #" << ++num << " person 's name:\n";
int i;
for (i = 0; i < Len; i++)
{
fullname[i]=cin.get();
if (!cin || fullname[i] == '\n')
{
//cin.get();
break;
}
}
fullname[i] = '\0';
std::cout << "enter the level:\n";
int hc;
cin >> hc;
if (!cin)
{
std::cout << "enter error and set level to zero.\n";
cin.clear();
while (cin.get() != '\n')
continue;
}
else
{
cin.get();
}
if (!hc||hc < 0)
{
std::cout << "the level can't be negative.\n"
<< "and set it to zero.\n";
_handicap = 0;
}
else
{
_handicap = hc;
}
return 0;
}
void golf::handicap(golf& g, int hc)
{
if (hc < 0)
{
std::cout << "the level can't be negative.\n"
<< "and set it to zero.\n";
_handicap = 0;
}
else
{
_handicap = hc;
}
}
void golf::showgolf(const golf& g)
{
using std::cout;
using std::endl;
cout << "name : " << fullname << endl
<< "level : " << _handicap << endl;
}
int golf::name_is_null(golf& g) const
{
if (fullname[0]=='\0')
return 0;
else
return 1;
}
测试程序:
int work_3()
{
const int size = 4;
golf golfs[size];
for (int i = 0; i < size; i++)
{
golfs[i].setgolf(golfs[i]);
std::cout << golfs[i].name_is_null(golfs[i]) << std::endl;
if (golfs[i].name_is_null(golfs[i])==0)
break;
}
for (int i = 0; i < size; i++)
golfs[i].showgolf(golfs[i]);
std::cout << "Enter the person you wan't to change :\n";
int i;
std::cin >> i;
std::cout << "the level you wan't to use:\n";
int hc;
std::cin >> hc;
if (i<1 || i>size)
{
std::cout << "error\n";
}
else
{
golfs[i-1].handicap(golfs[i-1],hc);
}
for (int i = 0; i < size; i++)
golfs[i].showgolf(golfs[i]);
return 0;
}
第四题
要求:使用名称空间以及类改写程序9.4。
类声明:
namespace SALES
{
class Sales
{
private:
enum { QUARTERS = 4 };
double sales[QUARTERS];
double average;
double max;
double min;
double total;
void sum() { for (int i = 0; i < QUARTERS; i++) total += sales[i]; };
void Average() { average = total / QUARTERS; };
public:
Sales();
void setSales(Sales& s, const double ar[], int n);
void setSales(Sales& s);
void showSales(const Sales& s);
};
}
实现方法:
using namespace SALES;
Sales::Sales()
{
for (int i = 0; i < QUARTERS; i++)
sales[i] = 0.0;
average = max = min = total = 0.0;
}
void Sales::setSales(Sales& s, const double ar[], int n)
{
int i;
for (i = 0; i < n-1; i++)
{
min= ar[i] < ar[i + 1] ? ar[i] : ar[i + 1];
max = ar[i] > ar[i + 1] ? ar[i] : ar[i + 1];
}
for (i = 0; i < n; i++)
sales[i] = ar[i];
sum();
Average();
showSales(s);
}
void Sales::setSales(Sales& s)
{
using std::cout;
using std::endl;
static int count = 1;
double a[QUARTERS];
cout << "the number # " << ++count << "class :\n";
for (int i = 0; i < QUARTERS; i++)
{
std::cin >> a[i];
while (a[i] < 0)
{
cout << "enter error,enter again.\n";
std::cin >> a[i];
}
sales[i] = a[i];
}
setSales(s, sales, QUARTERS);
}
void Sales::showSales(const Sales& s)
{
using std::cout;
using std::endl;
static int a = 1;
cout << "the number # " << a++ << " class Sales: \n";
for (int i = 0; i < QUARTERS; i++)
cout << "the number #" << i + 1 << "sales: " << sales[i] << endl;
cout << "average : " << average << endl
<< "max : " << max << endl
<< "min : " << min << endl
<< "total : " << total << endl;
}
测试程序:
int work_4()
{
const int size = 4;
Sales s[size];
double ar[4] = { 321,345,456.4,357 };
s[0].setSales(s[0], ar, size);
for (int i = 1; i < size; i++)
s[i].setSales(s[i]);
return 0;
}
第五题
要求:在栈堆里添加或删除结构体,每次删除时,都将payment值写入到总数里。
类声明:
struct customer {
char fullname[35];
double payment;
};
typedef struct customer item;
struct List {
item* ph;
int length;
int maxsize;
};
class Stack
{
private:
static const int max = 5;
public:
void create_s(List&a);
void insert_s(List& a, int n);
void delete_s(List&a,int n);
void show_s(List& a)const;
};
实现方法:
void Stack::create_s(List&a)
{
a.ph= new item[sizeof(item) * max];
if (!a.ph)
exit(OVERFLOW);
a.length = 0;
a.maxsize = max * sizeof(item);
}
void Stack::insert_s(List& a,int n)
{
using std::cout;
using std::cin;
for (int i = 0; i < max; i++)
{
item Item;
Item.fullname;
cout << "enter the #" << i + 1 << " fullname:\n";
cin.get(Item.fullname, 35).get();
cout <<"enter the payment:\n";
cin >> Item.payment;
cin.get();
a.ph[i] = Item;
}
a.length = n;
show_s(a);
}
void Stack::delete_s(List& a, int n)
{
static double total = 0.0;
if (n<0 || n>a.length)
exit(EXIT_FAILURE);
if (n == a.length)
{
total += a.ph[n].payment;
for(int i=34;i>0;i--)
a.ph[n].fullname[i]='\0';
a.ph[n].payment = 0.0;
a.length --;
}
else
{
total += a.ph[n].payment;
a.ph[n] = a.ph[n + 1];
a.length --;
}
std::cout << "total = " << total<<std::endl;
}
void Stack::show_s(List& a)const
{
std::cout << a.length << std::endl;
std::cout << a.maxsize << std::endl;
for (int i = 0; i < max; i++)
{
std::cout << "fullname : " << a.ph[i].fullname << std::endl
<< "payment : " << a.ph[i].payment << std::endl;
}
}
测试程序:
int work_5()
{
Stack stack1;
List a;
stack1.create_s(a);
stack1.insert_s(a, 5);
for (int i = 0; i < 5; i++)
stack1.delete_s(a, i);
return 0;
}
第六题
要求:提供已给类的成员定义,以及修改错误处。
类声明:
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::Move(double a, double b)
{
x = a;
y = b;
}
void Move::showmove()const
{
std::cout << "x = " << x
<< ", y = " << y << std::endl;
}
Move Move::add(const Move& m)
{
x+= m.x;
y += m.y;
return *this;
}
void Move::reset(double a, double b)
{
x = a;
y = b;
}
测试程序:
int work_6()
{
Move a(5, 5);
Move b(1, 1);
a.showmove();
a.add(b);
a.showmove();
a.reset(5, 5);
a.showmove();
return 0;
}
第七题
要求:编写类,创造默认构造函数和构造函数。
类声明:
class plorg
{
private:
static const int max = 20;
char name_p[max];
int CI;
public:
plorg();
plorg(const char* a, int ci = 50);
void show_p(plorg& p)const;
};
typedef class plorg Plo;
实现方法:
Plo::plorg()
{
char name[] = "Plorga";
int i = 0;
do
{
name_p[i] = name[i];
i++;
} while (name[i] != '\0');
name_p[i] = '\0';
CI = 50;
}
Plo::plorg(const char* a, int ci)
{
int i = 0;
do {
name_p[i] = a[i];
i++;
} while (a[i] != '\0');
name_p[i] = '\0';
CI = ci;
}
void Plo::show_p(plorg& p)const
{
using std::cout;
using std::endl;
cout << "name : " << name_p << endl
<< "CI : " << CI << endl;
}
测试程序:
int work_7()
{
Plo ploa;
Plo plob("Myfriend", 99);
ploa.show_p(ploa);
plob.show_p(plob);
return 0;
}
第八题
要求:创建一个数组或链表,要求可以对数值进行增改查等方法。
类声明:
typedef int adt;
class LIST
{
private:
static const int max = 20;
adt _array[max];
int top;
public:
LIST();
void set_list(LIST& l);
bool is_full(LIST& l);
bool is_null(LIST& l);
void get_list( int n ,adt i);
void show_list(LIST& l, int n)const ;
};
实现方法:
LIST::LIST()
{
for (int i = 0; i < max; i++)
_array[i] = '\0';
top = 0;
}
void LIST::set_list(LIST& l)
{
using std::cin;
int i = 0;
cin >> _array[i];
while (i < max && cin)
{
i++;
cin >> _array[i];
}
if (!cin)
cin.get();
}
bool LIST::is_full(LIST& l)
{
return top == max;
}
bool LIST::is_null(LIST& l)
{
return top == 0;
}
void LIST::get_list(int n,adt i)
{
for (int i = max; i > n; i--)
_array[i] = _array[i - 1];
_array[n - 1] = i;
show_list(*this, n);
}
void LIST::show_list(LIST& l, int n)const
{
std::cout << "the elem # " << n << " is " << _array[n - 1] << std::endl;
}
测试程序:
int work_8()
{
LIST a;
std::cout << "is null?\n" << std::endl;
std::cout << a.is_null(a) << std::endl;
a.set_list(a);
std::cout << "is full ?\n" << std::endl;
std::cout << a.is_full(a) << std::endl;
a.get_list(5, 520);
return 0;
}