📄 selsolutions5.txt
字号:
void BankAccount::show(void) const
{
using std::cout;
using std:: endl;
cout << "Client: " << name << endl;
cout << "Account Number: " << acctnum << endl;
cout << "Balance: " << balance << endl;
}
void BankAccount::deposit(double cash)
{
if (cash >= 0)
balance += cash;
else
std::cout << "Illegal transaction attempted";
}
void BankAccount::withdraw(double cash)
{
if (cash < 0)
std::cout << "Illegal transaction attempted";
else if (cash <= balance)
balance -=cash;
else
std::cout << "Request denied due to insufficient funds.\n";
}
// sample use
int main()
{
BankAccount bird;
BankAccount frog("Kermit", "croak322", 123.00);
bird.show();
frog.show();
bird = BankAccount("Chipper", "peep8282", 214.00);
bird.show();
frog.deposit(20);
frog.show();
frog.withdraw(4000);
frog.show();
frog.withdraw(50);
frog.show();
}
PE10-4
// pe10-4.h
#ifndef SALES__
#define SALES__
namespace SALES
{
const int QUARTERS = 4;
class Sales
{
private:
double sales[QUARTERS];
double average;
double max;
double min;
public:
// default constructor
Sales();
// copies the lesser of 4 or n items from the array ar
// to the sales member and computes and stores the
// average, maximum, and minimum values of the entered items;
// remaining elements of sales, if any, set to 0
Sales(const double ar[], int n);
// gathers sales for 4 quarters interactively, stores them
// in the sales member of object and computes and stores the
// average, maximum, and minumum values
void setSales();
// display all information in object
void showSales();
};
}
#endif
// pe10-4a.cpp
#include <iostream>
#include "pe10-4.h"
int main()
{
using SALES::Sales;
double vals[3] = {2000, 3000, 5000};
Sales forFiji(vals, 3);
forFiji.showSales();
Sales red;
red.showSales();
red.setSales();
red.showSales();
return 0;
}
// pe10-4b.cpp
#include <iostream>
#include "pe10-4.h"
namespace SALES
{
using std::cin;
using std::cout;
using std::endl;
Sales::Sales(const double ar[], int n)
{
if (n < 0)
n = 0;
int limit = n < QUARTERS ? n : QUARTERS;
double total = 0;
min = 0;
max = 0;
average = 0;
if (limit > 0)
min = max = ar[0];
int i;
for (i = 0; i < limit; i++)
{
sales[i] = ar[i];
total += ar[i];
if (ar[i] > max)
max = ar[i];
else if (ar[i] < min)
min = ar[i];
}
for (i = limit; i < QUARTERS; i++)
sales[i] = 0;
if (limit > 0)
average = total / limit;
}
Sales::Sales()
{
min = 0;
max = 0;
average = 0;
for (int i = 0; i < QUARTERS; i++)
sales[i] =0;
}
void Sales::setSales()
{
double sa[QUARTERS];
int i;
for (i = 0; i < QUARTERS; i++)
{
cout << "Enter sales for quarter " << i + 1 << ": ";
cin >> sa[i];
}
// create temporary object, copy to invoking object
*this = Sales(sa, QUARTERS);
}
void Sales::showSales()
{
cout << "Sales:\n";
for (int i = 0; i < QUARTERS; i++)
cout << "Quarter " << i + 1 << ": $"
<< sales[i] << endl;
cout << "Average: $" << average << endl;
cout << "Minimum: $" << min << endl;
cout << "Maximum: $" << max << endl;
}
}
PE 10-5
// pe10stack.h -- class definition for the stack ADT
// for use with pe10-5.cpp
#ifndef _STACK_H_
#define _STACK_H_
struct customer {
char fullname[35];
double payment;
};
typedef customer Item;
class Stack
{
private:
enum {MAX = 10}; // constant specific to class
Item items[MAX]; // holds stack items
int top; // index for top stack item
public:
Stack();
bool isempty() const;
bool isfull() const;
// push() returns false if stack already is full, true otherwise
bool push(const Item & item); // add item to stack
// pop() returns false if stack already is empty, true otherwise
bool pop(Item & item); // pop top into item
};
#endif
// pe10stack.cpp -- Stack member functions
// for use with pe10-5.cpp
// exactly the same as stack.cpp in the text
#include "pe10stack.h"
Stack::Stack() // create an empty 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;
}
// pe10-5.cpp
#include <iostream>
#include <cctype>
#include "pe10stack.h" // modified to define customer structure
// link with pe10stack.cpp
void get_customer(customer & cu);
int main(void)
{
using namespace std;
Stack st; // create a stack of customer structures
customer temp;
double payments = 0;
char c;
cout << "Please enter A to add a customer,\n"
<< "P to process a customer, and Q to quit.\n";
while (cin >> c && (c = toupper(c)) != 'Q')
{
while (cin.get() != '\n')
continue;
if (c != 'A' && c != 'P')
{
cout << "Please respond with A, P, or Q: ";
continue;
}
switch (c)
{
case 'A' : if (st.isfull())
cout << "stack already full\n";
else
{
get_customer(temp);
st.push(temp);
}
break;
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;
default : cout << "Whoops! Programming error!\n";
}
cout << "Please enter A to add a customer,\n"
<< "P to process a customer, and Q to quit.\n";
}
cout << "Done!\n";
return 0;
}
void get_customer(customer & cu)
{
using namespace std;
cout << "Enter customer name: ";
cin.getline(cu.fullname,35);
cout << "Enter customer payment: ";
cin >> cu.payment;
while (cin.get() != '\n')
continue;
}
PE 10-8
// pe10-8arr.h -- header file for a simple list class
#ifndef SIMPLEST_
#define SIMPLEST_
// program-specific declarations
const int TSIZE = 45; // size of array to hold title
struct film
{
char title[TSIZE];
int rating;
};
// general type definitions
typedef struct film Item;
const int MAXLIST = 10;
class simplist
{
private:
Item items[MAXLIST];
int count;
public:
simplist(void);
bool isempty(void);
bool isfull(void);
int itemcount();
bool additem(Item item);
void transverse( void (*pfun)(Item item));
};
#endif
// pe10-8arr.cpp -- functions supporting simple list operations
#include "pe10-8arr.h"
simplist::simplist(void)
{
count = 0;
}
bool simplist::isempty(void)
{
return count == 0;
}
bool simplist::isfull(void)
{
return count == MAXLIST;
}
int simplist::itemcount()
{
return count;
}
bool simplist::additem(Item item)
{
if (count == MAXLIST)
return false;
else
items[count++] = item;
return true;
}
void simplist::transverse( void (*pfun)(Item item))
{
for (int i = 0; i < count; i++)
(*pfun)(items[i]);
}
// pe10-8.cpp -- using a class definition
#include <iostream>
#include <cstdlib> // prototype for exit()
#include "pe10-8arr.h" // simple list class declaration
// array version
void showmovies(Item item); // to be used by transverse()
int main(void)
{
using namespace std;
simplist movies; // creates an empty list
Item temp;
if (movies.isfull()) // invokes isfull() member function
{
cout << "No more room in list! Bye!\n";
exit(1);
}
cout << "Enter first movie title:\n";
while (cin.getline(temp.title,TSIZE) && temp.title[0] != '\0')
{
cout << "Enter your rating <0-10>: ";
cin >> temp.rating;
while(cin.get() != '\n')
continue;
if (movies.additem(temp) == false)
{
cout << "List already is full!\n";
break;
}
if (movies.isfull())
{
cout << "You have filled the list.\n";
break;
}
cout << "Enter next movie title (empty line to stop):\n";
}
if (movies.isempty())
cout << "No data entered. ";
else
{
cout << "Here is the movie list:\n";
movies.transverse(showmovies);
}
cout << "Bye!\n";
return 0;
}
void showmovies(Item item)
{
std::cout << "Movie: " << item.title << " Rating: "
<< item.rating << std::endl;
}
Chapter 11
PE 11-2
// pe11-2.h -- Vector class with <<, mode state
// modified implementation
#ifndef MODVECTOR_H_
#define MODVECTOR_H_
#include <iostream>
namespace VECTOR
{
using std::ostream;
class Vector
{
private:
double x; // horizontal value
double y; // vertical value
char mode; // 'r' = rectangular, 'p' = polar
// private methods for setting values
void set_mag();
void set_ang();
void set_x(double, double);
void set_y(double, double);
public:
Vector();
Vector(double n1, double n2, char form = 'r');
void set(double n1, double n2, char form = 'r');
~Vector();
double xval() const {return x;} // report x value
double yval() const {return y;} // report y value
double magval() const; // report magnitude
double angval() const; // report angle
void polar_mode(); // set mode to 'p'
void rect_mode(); // set mode to 'r'
// operator overloading
Vector operator+(const Vector & b) const;
Vector operator-(const Vector & b) const;
Vector operator-() const;
Vector operator*(double n) const;
// friends
friend Vector operator*(double n, const Vector & a);
friend ostream & operator<<(ostream & os, const Vector & v);
};
} // end namespace VECTOR
#endif
// pe11-2.cpp -- modified methods for Vector class
#include <cmath>
#include "pe11-2.h" // includes <iostream>
using std::sqrt;
using std::sin;
using std::cos;
using std::atan2;
using std::cout;
namespace VECTOR
{
const double Rad_to_deg = 57.2957795130823;
// private methods
// calculates magnitude from x and y
// set x from polar coordinate
void Vector::set_x(double mag, double ang)
{
x = mag * cos(ang);
}
// set y from polar coordinate
void Vector::set_y(double mag, double ang)
{
y = mag * sin(ang);
}
// public methods
Vector::Vector() // default constructor
{
x = y = 0.0;
mode = 'r';
}
// construct vector from rectangular coordinates if form is r
// (the default) or else from polar coordinates if form is p
Vector::Vector(double n1, double n2, char form)
{
mode = form;
if (form == 'r')
{
x = n1;
y = n2;
}
else if (form == 'p')
{
set_x(n1, n2 / Rad_to_deg);
set_y(n1, n2 / Rad_to_deg);
}
else
{
cout << "Incorrect 3rd argument to Vector() -- ";
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -