1.
account.h
#ifndef ACCOUNT_H_
#define ACCOUNT_H_
#include <iostream>
#include <string>
class BankAccount
{
private:
std::string name_;
std::string acctnum_;
double balance_;
public:
BankAccount(const std::string& client,
const std::string& num, double bal = 0.0);
BankAccount();
~BankAccount();
void show() const;
void deposit(double cash);
void withdraw(double cash);
};
#endif
account.cpp
#include "account.h"
BankAccount::BankAccount(const std::string& client,
const std::string& num, double bal )
{
name_ = client;
acctnum_ = num;
balance_ = bal;
}
BankAccount::BankAccount()
{
name_ = "";
acctnum_ = "";
balance_ = 0.0;
}
BankAccount::~BankAccount()
{}
void BankAccount::show() const
{
std::cout << "储户姓名: " << name_ << std::endl;
std::cout << "储户帐号: " << acctnum_ << std::endl;
std::cout << "存款: " << balance_ << std::endl;
}
void BankAccount::deposit(double cash)
{
if (cash < 0.0)
std::cout << "你木钱了" << std::endl;
else
balance_ += cash;
}
void BankAccount::withdraw(double cash)
{
if (cash > balance_)
std::cout << "你木钱了" << std::endl;
else
balance_ -= cash;
}
main.cpp
#include "account.h"
int main()
{
BankAccount a = BankAccount("nigezazhong", "1966666", 0);
a.show();
a.deposit(100);
a.show();
a.withdraw(10);
a.show();
return 0;
}
2.
person.h
#ifndef PERSON_H_
#define PERSON_H_
#include <iostream>
#include <string>
#include <cstring>
class Person
{
private:
static const int LIMIT = 25;
std::string lname_;
char fname_[LIMIT];
public:
Person() { lname_ = ""; fname_[0] = '\0'; }
Person(const std::string& lname, const char* fname = "Heyyou");
void show() const;
void formal_show() const;
};
#endif
person.cpp
#include "person.h"
Person::Person(const std::string& lname, const char* fname)
{
lname_ = lname;
strcpy_s(fname_, fname);
}
void Person::show() const
{
std::cout << fname_ << " " << lname_ << std::endl;
}
void Person::formal_show() const
{
std::cout << lname_ << ", " << fname_ << std::endl;
}
main.cpp
#include "person.h"
int main()
{
Person one;
Person two("Symthecraft");
Person three("Dimwiddy", "Sam");
three.show();
three.formal_show();
return 0;
}
3.
golf.h
#ifndef GOLF_H_
#define GOLF_H_
#include <iostream>
class Golf {
private:
static const int LEN = 40;
char fullname[LEN];
int handicap;
public:
Golf(const char* name, int hc);
Golf();
~Golf();
const Golf& setgolf( Golf & g);
void showgolf() const;
};
#endif
golf.cpp
#include <cstring>
#include <iostream>
#include "golf.h"
Golf::Golf()
{
strcpy_s(fullname, "Noname");
handicap = 0;
}
Golf::~Golf()
{}
Golf::Golf(const char* name, int hc)
{
strcpy_s(fullname, name);
handicap = hc;
}
const Golf& Golf::setgolf( Golf& g)
{
std::cout << "输入姓名: ";
std::cin.getline(g.fullname, 40);
strcpy_s(fullname, g.fullname);
std::cout << "输入等级: ";
std::cin >> g.handicap;
handicap= g.handicap;
return *this;
}
void Golf::showgolf() const
{
std::cout << "姓名: " << fullname << std::endl;
std::cout << "等级: " << handicap << std::endl;
}
main.cpp
#include "golf.h"
int main()
{
Golf g1 = Golf("grfnb", 5);
g1.showgolf();
Golf g2;
g2.showgolf();
g2.setgolf(g1);
g2.showgolf();
return 0;
}
4.
sales.h
#ifndef SALES_H_
#define SALES_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 setSales();
void showSales();
};
}
#endif
sales.cpp
#include <iostream>
#include "sales.h"
namespace SALES
{
Sales::Sales(const double ar[], int n)
{
for (int i = 0; i < QUARTERS; ++i)
{
if (i < n)
sales[i] = ar[i];
else
sales[i] = 0.0;
}
double sum = 0.0;
double max_temp = sales[0], min_temp = sales[0];
for (int i = 0; i < QUARTERS; ++i)
{
double cur = sales[i];
if (cur > max_temp)
max_temp = cur;
if (cur < min_temp)
min_temp = cur;
sum += cur;
}
average = sum / (float)QUARTERS;
max = max_temp;
min = min_temp;
}
Sales::Sales()
{
for (int i = 0; i < QUARTERS; ++i)
sales[i] = 0.0;
average = 0.0;
max = 0.0;
min = 0.0;
}
void Sales::setSales()
{
using std::cin;
using std::cout;
using std::endl;
cout << "Enter sales:" << endl;
for (int i = 0; i < QUARTERS; ++i)
{
cout << "sales[" << i << "]: ";
cin >> sales[i];
}
double sum = 0.0;
double max_temp = sales[0], min_temp = sales[0];
for (int i = 0; i < QUARTERS; ++i)
{
double cur = sales[i];
if (cur > max_temp)
max_temp = cur;
if (cur < min_temp)
min_temp = cur;
sum += cur;
}
average = sum / (float)QUARTERS;
max = max_temp;
min = min_temp;
}
void Sales::showSales()
{
using std::cout;
using std::endl;
cout << "sales: ";
for (int i = 0; i < QUARTERS; ++i)
cout << sales[i] << " ";
cout << endl;
cout << "average: " << average << endl;
cout << "max: " << max << endl;
cout << "min: " << min << endl;
}
}
main.cpp
#include "sales.h"
int main(void)
{
using SALES::Sales;
double vals[3] = { 10, 20, 30 };
Sales s = Sales(vals, 3);
s.showSales();
Sales t;
t.setSales();
t.showSales();
return 0;
}
5.
stack.h
#ifndef STACK_H_
#define STACK_H_
struct customer {
char fullname[35];
double payment;
};
typedef customer Item;
class Stack {
private:
static const int MAX = 10;
Item items[MAX];
int top;
public:
Stack();
bool isempty() const;
bool isfull() const;
bool push(const Item& item);
bool pop(Item& item);
};
#endif
stack.cpp
#include "stack.h"
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];
return true;
}
else
return false;
}
main.cpp
#include <iostream>
#include <cctype>
#include "stack.h"
int main()
{
using namespace std;
Stack st;
char ch;
Item item;
double total = 0.0;
cout << "Please enter A to add an customer" << endl;
cout << "p to pop a customer, Q to quit." << endl;
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 customer name: ";
cin.getline(item.fullname, 35);
cout << "Enter payment: ";
cin >> item.payment;
if (st.isfull())
cout << "stack is full" << endl;
else
st.push(item);
break;
case 'p':
case 'P':
if (st.isempty())
cout << "stack is empty" << endl;
else
{
st.pop(item);
total += item.payment;
cout << "total = " << total << endl;
}
break;
}
cout << "Please enter A to add an customer" << endl;
cout << "p to pop a customer, Q to quit." << endl;
}
cout << "Bye!" << endl;
return 0;
}
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 "move.h"
#include <iostream>
Move::Move(double a, double b)
{
x = a;
y = b;
}
void Move::showmove() const
{
std::cout << "(" << x << ", " << y << ")" << std::endl;
}
Move Move::add(const Move& m) const
{
Move n = Move(x + m.x, y + m.y);
return n;
}
void Move::reset(double a, double b)
{
x = a;
y = b;
}
main.cpp
#include "move.h"
int main(void)
{
Move m1;
m1.showmove();
m1.reset(2, 3);
m1.showmove();
Move m2 = Move(3, 1);
m2.showmove();
Move m3;
m3 = m2.add(m1);
m3.showmove();
return 0;
}
7.
plorg.h
#ifndef PLORG_H_
#define PLORG_H_
class Plorg {
private:
static const int LEN = 20;
char name[LEN];
int ci;
public:
Plorg();
void reset(const int& c);
void show() const;
};
#endif
plorg.cpp
#include "plorg.h"
#include <iostream>
#include <cstring>
Plorg::Plorg()
{
strcpy_s(name, "Plorg");
ci = 50;
}
void Plorg::reset(const int& c)
{
ci = c;
}
void Plorg::show() const
{
std::cout << "name: " << name << std::endl;
std::cout << "CI: " << ci << std::endl;
}
main.cpp
#include "plorg.h"
int main()
{
Plorg p;
p.show();
p.reset(30);
p.show();
return 0;
}
8.
list.h
#ifndef LIST_H_
#define LIST_H_
#include <string>
const int MAX = 10;
struct people {
std::string name;
int age;
};
typedef people Item;
class List {
private:
Item items[MAX];
int count;
public:
List();
bool isempty() const;
bool isfull() const;
int itemcount() const;
bool additem(const Item& item);
void visit(void (*pf)(Item&));
};
#endif
list.cpp
#include "list.h"
#include <iostream>
List::List()
{
count = 0;
}
bool List::isempty() const
{
return count == 0;
}
bool List::isfull() const
{
return count == MAX;
}
int List::itemcount() const
{
return count;
}
bool List::additem(const Item& item)
{
if (isfull())
{
std::cout << "已经满了" << std::endl;
return false;
}
else
{
items[count++] = item;
return 1;
}
}
void List::visit(void (*pf)(Item&))
{
for (int i = 0; i < count; i++)
(*pf)(items[i]);
}
main.cpp
#include <iostream>
#include "list.h"
void addage(Item& item);
int main()
{
List l;
Item i = { "wnnmmb", 24 };
Item i2 = { "hhhh", 20 };
l.additem(i);
l.additem(i2);
std::string str;
if (l.isempty() == 1)
str = "Yes!";
if (l.isempty() == 0)
str = "No!";
if (l.isfull() == 1)
str = "Yes!";
if (l.isfull() == 0)
str = "No!";
std::cout << "isFull? " << str << std::endl;
int n = l.itemcount();
std::cout << n << " items in list" << std::endl;
l.visit(addage);
return 0;
}
void addage(Item& item)
{
item.age += 1;
std::cout << item.age << std::endl;
}