//10.1
//account.h
#pragma once
#include <string>
class Account
{
private:
std::string name_;
std::string account_;
double money_;
public:
Account();
Account(const std::string& na, const std::string& ac, double mo);
~Account();
void show() const;
void deposit(double mo);
void withdrawal(double mo);
};
//account.cpp
#include<iostream>
#include"account.h"
Account::Account()
{
std::cout << "dafault constructor called\n";
name_ = "no name";
account_ = "no account";
money_ = 0.0;
}
Account::Account(const std::string& na, const std::string& ac, double mo)
{
std::cout << "constructor using " << na << " called\n";
name_ = na;
account_ = ac;
if (mo < 0)
{
std::cout << "money can not to be negative; "
<< name_ << " money set to 0.\n";
money_ = 0.0;
}
else
money_ = mo;
}
Account::~Account()
{
std::cout << "bye," << name_ << " !\n";
}
void Account::show() const
{
std::cout << "name : " << name_<<"\n";
std::cout << "account: " << account_ << "\n";
std::cout << "money : " << money_ << "\n";
}
void Account::deposit(double mo)
{
if (mo < 0)
{
std::cout << "deposite can not to be negative. "
<< "Transaction is aborted.\n";
}
else
money_ += mo;
}
void Account::withdrawal(double mo)
{
if (mo < 0)
{
std::cout << "withdrawal can not to be negative. "
<< "Transaction is aborted.\n";
}
else if (mo > money_)
{
std::cout << "you can not withdrawal more than you have. "
<< "Transaction is aborted.\n";
}
else
money_ -= mo;
}
//myaccount.cpp
#include<iostream>
#include"account.h"
int main()
{
Account account1;
account1.show();
Account account2("ann", "20250330", 100);
account2.show();
account2.deposit(-10);
account2.deposit(100);
account2.show();
account2.withdrawal(1000);
account2.withdrawal(-100);
account2.withdrawal(100);
account2.show();
return 0;
}
//10.2
//person.h
#pragma once
#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 FormlShow() const;
};
//person.cpp
#include<iostream>
#include"person.h"
#include<cstring>
Person::Person(const std::string& ln, const char* fn)
{
lname = ln;
if (strlen(fn) > LIMIT - 1)
strcpy_s(fname, "less than 25 letter");
else
strcpy_s(fname,fn);
}
void Person::show() const
{
std::cout << lname << " " << fname<<"\n";
}
void Person::FormlShow() const
{
std:: cout<< fname << " " << lname<<"\n";
}
//myperson.cpp
#include"person.h"
int main()
{
Person one;
Person two("Smythecraft");
Person three("Dimwiddy", "Sam");
Person four("Ann", "1234567890123456789012345");
four.show();
one.show();
one.FormlShow();
two.show();
two.FormlShow();
three.show();
three.FormlShow();
return 0;
}
//10.3
//golf.h
#pragma once
#include<string>
class Golf
{
private:
std::string fullname_;
int handicap_;
public:
Golf();
Golf(const std::string & na,int hc);
~Golf();
void set_handicap(int hc);
void show() const;
void setgolf();
};
//golf.cpp
#include<iostream>
#include "golf.h"
Golf::Golf()
{
fullname_ = "no name";
handicap_ = 0;
}
Golf::Golf(const std::string& na, int hc)
{
fullname_ = na;
handicap_ = hc;
}
Golf::~Golf()
{
std::cout << "bye " << fullname_<<"\n";
}
void Golf::set_handicap(int hc)
{
handicap_ = hc;
}
void Golf::show() const
{
std::cout << "fullname: " << fullname_ << std::endl;
std::cout << "handicap: " << handicap_ << std::endl;
}
void Golf::setgolf()
{
std::string name;
int hc;
std::cout << "enter the name of golf player: ";
std::getline(std::cin, name);
std::cout << "enter handicap: ";
std::cin >> hc;
*this=Golf(name, hc);
}
//mygolf.cpp
#include <iostream>
#include "golf.h"
int main()
{
Golf one("one", 5);
one.show();
one.set_handicap(2);
one.show();
Golf two;
two.show();
Golf temp;
temp.setgolf();
temp.show();
return 0;
}
//10.4
//sales.h
#pragma once
namespace SALES
{
class Sales
{
private:
static const int QUARTERS = 4;
double sales[QUARTERS];
double average;
double max;
double min;
public:
Sales();
Sales(const double* ar, int n);
~Sales();
void setSales();
void show() const;
};
}
//sales.cpp
#include<iostream>
#include "sales.h"
namespace SALES
{
Sales::Sales()
{
for (int i = 0; i < QUARTERS; i++)
sales[i] = 0.0;
average = max = min = 0.0;
}
Sales::Sales(const double* ar, int n)
{
int count = 0;
for (int i = 0; i < n && i < QUARTERS; i++)
{
sales[i] = ar[i];
count++;
}
if (count < QUARTERS)
{
for (int i = count; i < QUARTERS; i++)
sales[i] = 0;
}
average = max = min = sales[0];
for (int i = 1; i < QUARTERS; i++)
{
average += sales[i];
max = max > sales[i] ? max : sales[i];
min = min < sales[i] ? min : sales[i];
}
average = average / QUARTERS;
}
Sales::~Sales()
{
std::cout << "bye!\n";
}
void Sales::setSales()
{
double ar[QUARTERS];
std::cout << "Please enter " << QUARTERS << " number:\n";
for (int i = 0; i < QUARTERS; i++)
{
std::cout << "number#" << i + 1 << " :";
std::cin >> ar[i];
}
*this = Sales(ar, QUARTERS);
}
void Sales::show() const
{
for (int i = 0; i < QUARTERS; i++)
std::cout << sales[i] << " ";
std::cout << "\n";
std::cout << "average=" << average << "\n";
std::cout << "max=" << max << "\n";
std::cout << "min=" << min << "\n";
}
}
//mysales.cpp
#include<iostream>
#include "sales.h"
int main()
{
SALES::Sales one;
one.show();
SALES::Sales two;
two.setSales();
two.show();
double money[4] = { 1.1,2.2,3.3,4.4 };
SALES::Sales three(money, 4);
three.show();
return 0;
}
//10.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.cpp
#include"stack.h"
#include<iostream>
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
false;
}
bool Stack::pop(Item& item)
{
if (top>0)
{
item = items[--top];
return true;
}
else
return false;
}
//stacker.cpp
#include"stack.h"
#include<iostream>
#include<cctype>
int main()
{
Stack st;
char ch;
struct customer temp;
static double sum = 0;
std:: cout << "please enter A to add a purchase order,\n"
<< "P to process a PO,or Q to quit.\n";
while (std::cin >> ch && toupper(ch) != 'Q')
{
while (std::cin.get() != '\n')
continue;
if (!isalpha(ch))
{
std::cout << '\a'<<"please enter A or P or Q";
continue;
}
switch (ch)
{
case 'A':
case 'a':std::cout << "Enter a struct to add:\n";
std::cout << "enter the fullname: ";
std::cin.get(temp.fullname, 35).get();
std::cout << "enter the payment: ";
std::cin >> temp.payment;
if (st.isfull())
std::cout << "stack already full\n";
else
st.push(temp);
break;
case 'P':
case 'p':if (st.isempty())
std::cout << "stack already empty\n";
else
{
st.pop(temp);
sum += temp.payment;
std::cout << "customer: " << temp.fullname << " popped\n"
<< "the sum of payment is " << sum << std::endl;
}
break;
}
std::cout << "\nplease enter A to add a purchase order,\n"
<< "P to process a PO,or Q to quit.\n";
}
std::cout << "bye\n";
return 0;
}
//10.6
//move.h
#pragma once
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);
};
//move.cpp
#include<iostream>
#include"move.h"
Move::Move(double a, double b)
{
x = a;
y = b;
}
void Move::showmove() const
{
std::cout << "x= " << x << " ""y= " << y<<"\n";
}
Move Move::add(const Move& m) const
{
double tempx, tempy;
tempx = x + m.x;
tempy = y + m.y;
Move temp = Move(tempx, tempy);
return temp;
}
void Move::reset(double a , double b )
{
x = a;
y = b;
}
//mymove.cpp
#include<iostream>
#include"move.h"
int main()
{
Move one;
one.showmove();
one.reset(1,4);
one.showmove();
Move two(5,10);
two.showmove();
Move three = one.add(two);
three.showmove();
return 0;
}
//10.7
//plorg.h
#pragma once
class Plorg
{
private:
enum{MAX=19};
char fullname[MAX];
int ci;
public:
Plorg();
Plorg(const char *str, int a);
~Plorg();
void reset(const char* str);
void fixci(int a);
void show();
};
//plorg.cpp
#include<iostream>
#include<cstring>
#include"plorg.h"
Plorg::Plorg()
{
strcpy_s(fullname,MAX,"Plorga");
ci = 0;
}
Plorg::Plorg(const char* str , int a)
{
strcpy_s(fullname, MAX, str);
ci = a;
}
Plorg::~Plorg()
{
}
void Plorg::reset(const char* str)
{
strcpy_s(fullname, MAX, str);
ci = 50;
}
void Plorg::fixci(int a)
{
ci = a;
}
void Plorg::show()
{
std::cout << "fullname: " << fullname << "\n";
std::cout << "CI : " << ci<< "\n";
}
//myplorg.cpp
#include<iostream>
#include"plorg.h"
int main()
{
Plorg one;
one.show();
Plorg two("i am sorry", 30);
two.show();
one.reset("you are welcome");
one.show();
two.fixci(100);
two.show();
return 0;
}
//10.8
//list.h
#pragma once
typedef double Item;
void show(Item& item);
class List
{
private:
enum { MAX = 10};
Item items[MAX];
int top;
public:
List();
bool push(const Item& item);
bool isempty() const;
bool isfull() const;
void visit(void (*pf) (Item&));
};
//list.cpp
#include<iostream>
#include"list.h"
void show(Item& item)
{
std::cout<<item<<" ";
}
List::List()
{
for (int i = 0; i < MAX; i++)
items[i] = 0;
top = 0;
}
bool List::push(const Item& item)
{
if (top < MAX)
{
items[top++] = item;
return true;
}
else
false;
}
bool List::isempty() const
{
return top == 0;
}
bool List::isfull() const
{
return top == MAX;
}
void List::visit(void (*pf) (Item&))
{
for (int i = 0; i < MAX; i++)
{
(*pf)(items[i]);
}
}
//mylist.cpp
include<iostream>
#include"list.h"
int main()
{
List list;
char ch;
Item temp;
std:: cout << "please enter A to add a object,\n"
<< "or Q to quit.\n";
while (std::cin >> ch && toupper(ch) != 'Q')
{
while (std::cin.get() != '\n')
continue;
if (!isalpha(ch))
{
std::cout << '\a'<<"please enter A or Q";
continue;
}
if(ch=='A'||ch=='a')
{
std::cout << "Enter a object to add: ";
std::cin >> temp;
if (list.isfull())
{
std::cout << "list already full\n";
break;
}
else
list.push(temp);
}
std::cout << "\nplease enter A to add a purchase order,\n"
<< "or Q to quit.\n";
}
list.visit(show);
std::cout << "bye\n";
return 0;
}