среда, 10 сентября 2008 г.

глава 9 упражнение 3

Рассмотрите следующее определение структуры:



struct customer {
char fullname[35];
double payment;
};


Напишите программу, которая добавляет и удаляет структуры из стека, описанного в объявлении класса. Каждый раз, когда удаляется запись о заказчике, его платеж прибавляется к текущей сумме, а сама текущая сумма выводится на экран. Примечание: нужно иметь возможность сохранять класс Stack неизменным; достаточно изменить объявление typedef так, чтобы Item имел тип customer, а не тип unsigned long. __________________________________________________________________________




golf.cpp




#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


golf.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;
}


golf.cpp




#include <cstdlib>
#include <iostream>
#include <cctype>
#include "stack.h"

using namespace std;

int main()
{
Stack st;
char c;
customer po;

cout << "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n";
cout << "+ vyberite nuzhnoe dejstvie: +\n";
cout << "+ a) dobavit v stek p)udalit iz steka +\n";
cout << "+ q)quit +\n";
cout << "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n";

while (cin >> c && toupper(c) != 'Q')
{
while (cin.get() != '\n')
continue;
if (!isalpha(c))
{
cout << '\a';
continue;
}
switch (c)
{
case 'A' :
case 'a' : if (st.isfull())
cout << "\n +++!!!! stack polnyj, operacija nevozmozhna\n\n";
else {
cout << "vvedite infu : \n";
cout << " fullname : ";
cin.getline(po.fullname, 35);
cout << " pay : ";
cin >> po.payment;
st.push(po);
}
break;
case 'P' :
case 'p' : if (st.isempty())
cout << "stack pustoj\n";
else {st.pop(po);
cout << "zapis : " << po.fullname << " poped \n";
};
break;
default : cout << "vy vveli ne vernyj simvol, povtorite vvod\n";
}
cout << "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n";
cout << "+ vyberite nuzhnoe dejstvie: +\n";
cout << "+ a) dobavit v stek p)udalit iz steka +\n";
cout << "+ q)quit +\n";
cout << "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n";
}
return 0;
}


ООП ... её мать...

Комментариев нет: