//1
//bank1.h
#pragma once
#include <string>
class bank
{
private :
std::string name;
std::string account;
double asset;
public:
bank(); //default constructor
bank(const std::string & na, const std::string & acc, double a);
~bank(); //destructor
void push(double n);
void pop(double n);
void show();
};
//ban1.cpp
#include<iostream>
#include"bank1.h"
bank::bank()
{
name = "no name";
account = "no account";
asset = 0.0;
}
bank::bank(const std::string & na, const std::string &acc, double n)
{
name = na;
account = acc;
asset = n;
}
void bank::push(double n)
{
asset += n;
std::cout << "Pushing money...Done!\n";
}
void bank::pop(double n)
{
int temp = asset-n;
if (temp>0)
asset = temp;
else
asset = asset;
std::cout << "there is no enough money to pop!Failed!\n";
}
void bank::show()
{
std::cout << "the name in this bank: " << name<<"\n";
std::cout << "the account in the bank: " << account << "\n";
std::cout << "the asset in this bank now is: " << asset << "\n";
}
bank::~bank()
{
}
//main.cpp
#include<iostream>
#include<string>
#include"bank1.h"
int main()
{
std::cout << "please input the info of the bank account.\n";
std::cout << "the name in this bank is: ";
std::string na;
getline(std::cin, na);
std::cout << "the account registrated in the bank is:\n ";
std::string account;
getline(std::cin, account);
std::cout << "the asset in this bank is: ";
double n;
std::cin >> n;
std::cout << "the default constructor make:\n ";
bank b1;
b1.show();
bank b2 = bank(na, account, n);
b2.show();
int pus;
std::cout << "how much do you want to push: ";
std::cin >> pus;
b2.push(pus);
std::cout << "after push: ";
b2.show();
std::cout << "how much do you want to pop: ";
int pop;
std::cin >> pop;
b2.pop(pop);
std::cout << "after pop: ";
b2.show();
system("pause");
return 0;
}
//2**************************************************
//person.h
#include <string>
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");
~person();
void show() const;
void formalshow() const;
};
//person.cpp
#include<iostream>
#include"Person.h"
person::person(const std::string & ln, const char * fn)
{
lname = ln;
strcpy_s(fname, strlen(fn) + 1, fn);
}
person::~person()
{
}
void person::show() const
{
std::cout << "the fname is: " << fname<< " \n " ;
std::cout << "the lname is: " << lname<< " \n " ;
}
void person::formalshow() const
{
std::cout << "the lname is: " << lname << " \n ";
std::cout << "the fname is: " << fname << " \n ";
}
//main.cpp
#include<iostream>
#include<string>
#include"Person.h"
const int limits = 25;
int main()
{
std::string lname;
std::cout << "please input the lname: ";
std::cin >> lname;
std::cin.get();
char fname[limits];
std::cout << "please input the fname: ";
std::cin.get(fname, limits);
person p1;
p1.show();
p1 = person(lname, fname);
p1.show();
p1.formalshow();
person p2("Smythecraft");
p2.show();
person p3("Dimwiddy", "Sam");
p3.show();
system("pause");
return 0;
}
//3**************************************************
//golf.h
#include <string>
class golf
{
private :
static const int Len = 40;
char fullname[Len];
int handicap;
public:
golf() { fullname[0] = '\0'; handicap = 0; };
~golf();
golf & setgo(const char *name, int hc);
void handi(int hc);
void show();
};
//golf.cpp
#include<iostream>
#include"golf.h"
golf & golf::setgo(const char * name, int hc)
{
strcpy_s(fullname, strlen(name) + 1, name);
handicap = hc;
return *this;
}
void golf::handi(int hc)
{
handicap = hc;
std::cout << "the handicap is updated.\n";
}
void golf::show()
{
std::cout << "the fullname is: " << fullname<<"\n";
std::cout << "the handicap now is: " << handicap<<"\n";
}
golf::~golf()
{
}
//main.cpp
#include"golf.h"
const int limits = 40;
int main()
{
char ch[40];
int hc;
std::cout << "please input the fullname: ";
std::cin.get(ch, 40).get();
std::cout << "please input the handicap: ";
std::cin >> hc;
golf g;
golf *p = &g;
p=&p->setgo(ch, hc);
p->show();
system("pause");
return 0;
}
//4**************************************************
//sales.h
namespace SALES
{
class sale {
private:
static const int QUAETERS = 4;
double sales[QUAETERS];
double average;
double max;
double min;
public:
sale() { {0; }; 0; 0; 0; };
~sale();
void setsales(const double ar[], int n=4);
void showsales();
template <class T>
int arrlen(T& array){return (sizeof(array) / sizeof(array[0]));}
};
}
//sales.cpp
#include<iostream>
#include"sales.h"
SALES::sale::~sale()
{
}
void SALES::sale::setsales(const double ar[], int n)
{
double sum = 0;
max = -99999999;
min = 99999999;
for (int i = 0; i < n; ++i)
{
sales[i] = ar[i];
sum += sales[i];
max = ar[i] > max ? ar[i] : max;
min = ar[i] < min ? ar[i] : min;
}
average = sum / n;
}
void SALES::sale::showsales()
{
std::cout << "*****the info*****\n";
std::cout << "the sales:" << std::endl;
for (int i = 0; i < arrlen(sales); ++i)
std::cout << sales[i] << "\t\n";
std::cout << "the average: " << average << "\n";
std::cout << "the min: " << min << "\n";
std::cout << "the max: " << max << "\n";
}
//main.cpp
#include<string>
#include"sales.h"
int main()
{
double a[4] = { 1,3,2,4 };
SALES::sale s;
s.setsales(a);
s.showsales();
system("pause");
return 0;
}
//5*******************************************
//stack.h
#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(const Item &item);
bool pop(Item & item);
};
#endif // !STACK_H_
//sales.cpp
#include<iostream>
#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<string>
#include"stack.h"
void get_customer(customer & cu);
int main()
{
using namespace std;
Stack st;
customer temp;
double payments = 0;
char ch;
cout << "Please enter A to add a customer,\n"
<< "P to process a customer, and Q to quit.\n";
while (cin >> ch && (ch = toupper(ch)) != 'Q')
{
while (cin.get() != '\n')
continue;
if (!isalpha(ch))
{
cout << '\a';
continue;
}
switch (ch)
{
case 'a':
case 'A': if (st.isfull())
cout << "stack already full\n";
else
{
get_customer(temp);
st.push(temp);
}
break;
case 'p':
case 'P': if (st.isempty())
cout << "stack already empty\n";
else {
st.pop(temp);
payments += temp.payment;
cout << temp.fullname << " processed. ";
cout << "Payments now total $"
<< payments << "\n";
}
break;
}
cout << "Please enter A to add a customer,\n"
<< "P to process a customer, and Q to quit.\n";
}
cout << "Done!\n";
system("pause");
return 0;
}
void get_customer(customer & cu)
{
using namespace std;
cout << "please input customer name: ";
cin.getline(cu.fullname, 35);
cout << "please input customer payment: ";
cin >> cu.payment;
while (cin.get() != '\n')
continue;
}
//6****************************************
//move.h
#ifndef MOVE_H_
#define MOVE_H_
class Move
{
private:
double x;
double y;
public:
Move(double a = 0, double b = 0);
~Move();
void showmove() const;
Move add(const Move &m)const;
void reset(double a = 0, double b = 0);
};
#endif // !STACK_H_
//move.cpp
#include<iostream>
#include"move.h"
Move::Move(double a, double b)
{
x = a;
y = b;
}
Move::~Move()
{
}
void Move::showmove() const
{
std::cout << "Now x is: " << x << '\n';
std::cout << "Now y is: " << y << '\n';
}
Move Move::add(const Move & m) const
{
return Move(x + m.x, y + m.y);
}
void Move::reset(double a, double b)
{
x = 0;
y = 0;
}
//main.cpp
#include<iostream>
#include<string>
#include"move.h"
int main()
{
using namespace std;
Move m1 = {5,4};
std::cout << "now m1 info is:\n";
m1.showmove();
Move m = { 1,1 };
std::cout << "now m info is:\n";
m.showmove();
Move *p = &m;
p = &p->add(m1);
std::cout << "now m1+m info is:\n";
p->showmove();
p->reset();
std::cout << "reset the data:\n";
p->showmove();
system("pause");
return 0;
}
//7*****************************************
//plorga.h
#ifndef PLORG_H_
#define PLORG_H_
class plorg
{
private:
static const int max = 19;
char name[max];
int CI;
public:
plorg(char *na = "vincent",int ci =50);
~plorg();
void set(int ci);
void show();
void update(char *na, int ci);
};
#endif // !STACK_H_
//plorga.cpp
#include<iostream>
#include<cstring>
#include<string>
#include"plorg.h"
plorg::plorg(char * na, int ci)
{
strcpy_s(name, strlen(na) + 1, na);
CI = ci;
}
plorg::~plorg()
{
}
void plorg::set(int ci)
{
CI = ci;
}
void plorg::show()
{
std::cout << "the name is: " << name<<"\n";
std::cout << "the CI Value is: " << CI << "\n";
}
void plorg::update(char * na, int ci)
{
strcpy_s(name, strlen(na) + 1, na);
CI = ci;
}
//main.cpp
#include<iostream>
#include<string>
#include"plorg.h"
int main()
{
char name[19];
int ci;
plorg p1 = plorg();
p1.show();
std::cout << "please input the name: ";
std::cin.get(name, 19).get();
std::cout << "please input the CI value: ";
std::cin >> ci;
std::cout << "update the CI value: \n";
p1.set(ci);
p1.show();
std::cout << "after reset:\n ";
p1.update(name, ci);
p1.show();
system("pause");
return 0;
}
//8
//list.h
#ifndef LIST_H_
#define LIST_H_
struct songs
{
std::string songname;
int year;
};
typedef struct songs Item;
class list
{
private:
static const int len = 10;
Item items[len];
int count;
public:
list(void);
~list();
bool isempty(void);
bool isfull(void);
int itemcount();
bool add(Item item);
void visit(void(*p)(Item item));
};
#endif // !LIST_H_
//list.cpp
#include<iostream>
#include<cstring>
#include<string>
#include"list.h"
list::list(void)
{
count = 0;
}
list::~list()
{
}
bool list::isempty(void)
{
if (count == 0)
return true;
else
return false;
}
bool list::isfull(void)
{
if (count == len)
return true;
else
return false;
}
int list::itemcount()
{
return count;
}
bool list::add(Item item)
{
if (isfull())
return false;
else
items[count++] = item;
return true;
}
void list::visit(void(*p)(Item item))
{
for (int i = 0; i < count; ++i)
(*p)(items[i]);
}
//main.cpp
#include <iostream>
#include <cstdlib>
#include <string>
#include "list.h"
void show(Item item);
int main(void)
{
using namespace std;
list m;
Item s;
cout << "please input the songs you like: ";
while (getline(cin, s.songname) && s.songname != "\0")
{
cout << "please input the songdate,just the year like XXXX: ";
cin >> s.year;
while (cin.get() != '\n')
continue;
if (m.isfull())
{
cout << "you just filled the list.Done.\n";
break;
}
cout << "Enter next song please.(empty line to quit): ";
}
if (m.isempty())
cout << "No songs been entered!";
else
{
cout << "the songs you just input is as belows:\n";
m.visit(show);
}
system("pause");
return 0;
}
void show(Item item)
{
std::cout << "the song is " << item.songname << "\n";
std::cout << "its open year is " << item.year << "\n";
}
C++ Primer Plus 第六版 第十章 练习题参考答案
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...