#ifndef BANK_H_
#define BANK_H_
#include <string>
class Bank
{
private :
std:: string m_name;
std:: string m_id;
double m_deposit;
public :
Bank ( ) ;
Bank ( std:: string name, std:: string id, double deposit) ;
void show ( ) const ;
void save ( double num) ;
void withdraw ( double num) ;
} ;
#endif
#include <iostream>
#include "bank.h"
Bank:: Bank ( ) { }
Bank:: Bank ( std:: string name, std:: string id, double deposit)
{
m_name = name;
m_id = id;
m_deposit = deposit;
}
void Bank:: show ( ) const
{
std:: cout << "[" << m_name << "]\n"
<< " ID: " << m_id << '\t' << "Deposit: " << m_deposit;
}
void Bank:: save ( double num)
{
if ( num< 0 )
std:: cout << "You can't save money less than zero! "
<< "Transaction is aborted.\n" ;
else
{
m_deposit + = num;
std:: cout << "Deposit successfully!\n" ;
}
}
void Bank:: withdraw ( double num)
{
if ( num> m_deposit)
std:: cout << "You can't withdraw money more than you have! "
<< "Transaction is aborted.\n" ;
else
{
m_deposit - = num;
std:: cout << "Withdraw successfully!\n" ;
}
}
#include <iostream>
#include <cctype>
#include "bank.h"
int main ( )
{
using namespace std;
double in_money;
double out_money;
char ch;
Bank* account = new Bank{ "李四" , "361" , 40000 } ;
account- > show ( ) ;
cout << "\nPlease enter S to save money, W to withdraw money, 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 'S' :
case 's' :
cout << "Please enter the money you want to save: " ;
cin >> in_money;
account- > save ( in_money) ;
break ;
case 'W' :
case 'w' :
cout << "Please enter the money you want to withdraw: " ;
cin >> out_money;
account- > withdraw ( out_money) ;
break ;
}
account- > show ( ) ;
cout << "\nPlease enter S to save money, W to withdraw money, or Q to quit:\n" ;
}
cout << "Bye!\n" ;
return 0 ;
}
#ifndef PERSON_H_
#define 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" ) ;
void Show ( ) const ;
void FormalShow ( ) const ;
} ;
#endif
#include <iostream>
#include <cstring>
#include "person.h"
Person:: Person ( const std:: string & ln, const char * fn)
{
lname = ln;
strcpy ( fname, fn) ;
}
void Person:: Show ( ) const
{
std:: cout << "LastName: " << lname;
}
void Person:: FormalShow ( ) const
{
std:: cout << "\nFirstName: " << fname;
}
#include <iostream>
#include "person.h"
int main ( )
{
using std:: cout;
using std:: endl;
Person one;
Person two ( "Smythecraft" ) ;
Person three{ "Dimwiddy" , "Sam" } ;
one. Show ( ) ;
one. FormalShow ( ) ;
cout << endl;
two. Show ( ) ;
two. FormalShow ( ) ;
cout << endl;
three. Show ( ) ;
three. FormalShow ( ) ;
}
#ifndef GOLF_H
#define GOLF_H
#include <cstring>
static const int Len = 40 ;
class Golf
{
private :
char fullname[ 40 ] ;
int handicap;
public :
Golf ( ) ;
Golf ( const char * name, int hc)
{
strcpy ( fullname, name) ;
handicap = hc;
}
int setGolf ( ) ;
int setGolf ( const char * name, int hc) ;
int setHandicap ( const int hc) ;
void showGolf ( ) const ;
} ;
#endif
#include <iostream>
#include "Golf.h"
Golf:: Golf ( ) { }
int Golf:: setGolf ( )
{
using std:: cout;
using std:: cin;
cout << "Enter the fullname and handicap:\n" ;
if ( cin. getline ( fullname, Len) && fullname[ 0 ] != '\0' )
{
cin >> handicap;
cin. get ( ) ;
return 1 ;
}
else
return 0 ;
}
int Golf:: setGolf ( const char * name, int hc)
{
Golf g = Golf ( name, hc) ;
* this = g;
if ( fullname[ 0 ] != '\0' )
return 1 ;
else
return 0 ;
}
int Golf:: setHandicap ( const int hc)
{
handicap = hc;
}
void Golf:: showGolf ( ) const
{
using std:: cout;
cout << "Fullname[" << fullname << "]\t"
<< "Handicap: " << handicap << "\n" ;
}
#include <iostream>
#include "Golf.h"
const int Size= 3 ;
int main ( )
{
using std:: cout;
using std:: cin;
Golf anny[ Size] ;
Golf ann[ Size] ;
char * name = new char [ Len] ;
int hc, i= 0 ;
for ( i= 0 ; i< Size; i++ )
{
cout << "Enter the fullname and handicap:\n" ;
cin. getline ( name, Len) ;
if ( name[ 0 ] == '\0' )
{
hc= 0 ;
anny[ i] . setGolf ( name, hc) ;
cout << "Empty!\n" ;
continue ;
}
else
{
cin >> hc;
anny[ i] . setGolf ( name, hc) ;
cin. get ( ) ;
}
}
delete [ ] name;
cout << "________________________\n" ;
i= 0 ;
while ( i< Size && ann[ i] . setGolf ( ) )
++ i;
while ( i++ < Size)
ann[ i] . setGolf ( "" , 0 ) ;
cout << "\nAll the players:\n" ;
for ( i= 0 ; i< Size; i++ )
{
anny[ i] . showGolf ( ) ;
}
for ( i= 0 ; i< Size; i++ )
{
ann[ i] . showGolf ( ) ;
}
}
#include <iostream>
namespace SALES
{
static const int QUARTERS = 4 ;
class Sales
{
private :
double sales[ QUARTERS] ;
double m_Average;
double m_Max;
double m_Min;
public :
Sales ( ) ;
Sales ( const double ar[ ] , int n) ;
void setSales ( ) ;
void showSales ( ) const ;
} ;
}
#include "names.h"
namespace SALES
{
Sales:: Sales ( )
{
for ( int i= 0 ; i< QUARTERS; i++ )
sales[ i] = 0.0 ;
m_Average = 0.0 ;
m_Max = 0.0 ;
m_Min = 0.0 ;
}
Sales:: Sales ( cosnt double ar[ ] , int n)
{
int times = QUARTERS> n ? n: QUARTERS;
double arMax= ar[ 0 ] , arMin= ar[ 0 ] ;
double total= 0.0 ;
for ( int i= 0 ; i< times; i++ )
{
sales[ i] = ar[ i] ;
total + = sales[ i] ;
if ( arMax< ar[ i] )
arMax = ar[ i] ;
if ( arMin> ar[ i] )
arMin = ar[ i] ;
}
m_Max = arMax;
m_Min = arMin;
m_Average = total/ times;
if ( n< QUARTERS)
{
for ( int i= n; i< QUARTERS; i++ )
sales[ i] = 0.0 ;
}
}
void Sales:: setSales ( )
{
int size;
double total= 0.0 ;
cout << "Enter the array size: " ;
cin >> size;
double * ar = new double [ size] ;
cout << "Enter " << size << " double array:\n" ;
for ( int i= 0 ; i< size; i++ )
cin >> ar[ i] ;
std:: cout << "Enter the array size(size<5): " ;
std:: cin >> size;
std:: cout << "Enter " << size << " array:\n" ;
for ( int i= 0 ; i< size; i++ )
{
std:: cin >> sales[ i] ;
total + = sales[ i] ;
}
double max = sales[ 0 ] ;
double min = sales[ 0 ] ;
for ( int i= 1 ; i< size; i++ )
{
if ( max< sales[ i] )
max = sales[ i] ;
if ( min> sales[ i] )
min = sales[ i] ;
}
m_Max = max;
m_Min = min;
m_Average = total/ size;
if ( size< QUARTERS)
{
for ( int i= size; i< QUARTERS; i++ )
sales[ i] = 0.0 ;
}
}
void Sales:: showSales ( ) const
{
using std:: cout;
using std:: endl;
for ( int i= 0 ; i< QUARTERS; i++ )
cout << "[" << i+ 1 << "] " << sales[ i] << endl;
cout << "MAX: " << m_Max << endl
<< "MIN: " << m_Min << endl
<< "AVERAGE: " << m_Average << endl;
}
}
#include "names.h"
int main ( )
{
using namespace SALES;
using namespace std;
Sales* s1 = new Sales;
Sales* s2 = new Sales;
s1- > setSales ( ) ;
s1- > showSales ( ) ;
s2- > setSales ( ) ;
s2- > showSales ( ) ;
return 0 ;
}
#ifndef STACK_H_
#define STACK_H_
static double total= 0 ;
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) ;
double getTotal ( ) { return total; }
} ;
#endif
#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)
{
total + = item. payment;
items[ top++ ] = item;
std:: cout << " Total: " << getTotal ( ) << '\n' ;
return true ;
}
else
return false ;
}
bool Stack:: pop ( Item & item)
{
if ( top> 0 )
{
total + = item. payment;
item = items[ -- top] ;
std:: cout << " Total: " << getTotal ( ) << '\n' ;
return true ;
}
else
return false ;
}
#include <iostream>
#include <cctype>
#include "stack.h"
int main ( )
{
using namespace std;
Stack st;
Item item;
char ch;
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 the fullname to add:" ;
cin. getline ( item. fullname, 35 ) ;
cout << "Enter the payment to add:" ;
cin >> item. payment;
if ( st. isFull ( ) )
cout << "stack already full\n" ;
else
st. push ( item) ;
break ;
case 'P' :
case 'p' :
if ( st. isEmpty ( ) )
cout << "stack already empty\n" ;
else
{
st. pop ( item) ;
cout << "PO #" << item. fullname << " popped\n" ;
}
break ;
}
cout << "Please enter A to add a purchase order,\n"
<< "p to process a PO, or Q to quit.\n" ;
}
cout << "Bye.\n" ;
return 0 ;
}
#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
#include <iostream>
#include "move.h"
Move:: Move ( double a, double b)
{
x = a;
y = b;
}
void Move:: showMove ( ) const
{
std:: cout << "The x = " << x
<< ", and y = " << 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 = a;
y = b;
}
#include <iostream>
#include "move.h"
int main ( )
{
using std:: cout;
using std:: cin;
double a, b;
Move* m1 = new Move ( ) ;
cout << "Enter two numbers: " ;
cin >> a >> b;
Move m2 ( a, b) ;
m1- > showMove ( ) ;
m1- > add ( m2) ;
m1- > showMove ( ) ;
m2. showMove ( ) ;
m1- > reset ( a, b) ;
m1- > showMove ( ) ;
return 0 ;
}
#ifndef PLORG_H_
#define PLORG_H_
const int size= 19 ;
class Plorg
{
private :
char name[ size] ;
int CI;
public :
Plorg ( int ci= 50 ) ;
void setCI ( int ci) ;
void showPlorg ( ) const ;
} ;
#endif
#include <iostream>
#include <cstring>
#include "plorg.h"
Plorg:: Plorg ( int ci)
{
strcpy ( name, "Plorga" ) ;
CI = ci;
}
void Plorg:: setCI ( int ci)
{
CI = ci;
}
void Plorg:: showPlorg ( ) const
{
std:: cout << name << ":\n" << " CI:" << CI << '\n' ;
}
#include <iostream>
#include "plorg.h"
int main ( )
{
using std:: cin;
int ci;
cin >> ci;
Plorg* p1 = new Plorg ( ) ;
p1- > setCI ( ci) ;
p1- > showPlorg ( ) ;
Plorg p2 ( ci) ;
p2. showPlorg ( ) ;
p2. setCI ( 80 ) ;
p2. showPlorg ( ) ;
return 0 ;
}
第八题看见链表啥的,直接学链表去了,这里附上一个刚写成的单链表的实现的链接。至于题目嘛,嘿嘿,先欠着这是链表的链接: