【10.5】c++ primer plus 课后编程答案

来源:互联网 发布:网络购物狂欢节 编辑:程序博客网 时间:2024/06/05 03:47

C++ PRIMER PLUS 课后答案 
使用IDE为window7系统下的VS2010

/*user.h*/#ifndef USERSH_H_#define USERSH_H_#include <string>  struct customer{         char fullname[85];         double payment;}; typedef customer item; class Stack{private:         enum{max=5};         item items[max];         int top; public:         Stack();         ~Stack(){};         bool isempty() const;         bool isfull() const;         bool push(const item & item);         bool pop(item & item);};   #endif  


/*userfucntion.cpp*/#include "usersh.h"#include <iostream>using std::cout;using std::cin;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*/#include <iostream>#include <Windows.h>#include "usersh.h"#include <string>#include <cctype>using std::cout;using std::cin;double total=0.0;int main(){            Stack st;         char ch;         item pre;         cout<<"pleaseenter A to add a purchase order ,\n"                  <<"Pto 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': if(st.isfull())                           {                              cout<<"stackalrady full\n";                              break;                           }                           else                             cout<<"Enter pre information toadd:\n";                                                              cout<<"the full name:"                  ;                           cin>>pre.fullname;                           cout<<"thepayment:";                           cin>>pre.payment;                           st.push(pre);                           break;                  case'P':                  case'p':if (st.isempty())                            cout<<"stackalrady empty \n";                            else{                                  total+=pre.payment;                                  st.pop(pre);                                                                          cout<<"st#"<<pre.payment<<"POPPED\n";                                  }                            cout<<"\ntotal:"<<total<<'\n';                            break;                   }                  cout<<"pleaseenter A to add a purchase order ,\n"                          <<"Pto process a Po, or Q to quit.\n";         }         cout<<"BYE!";         system("pause");         return 0;}


原创粉丝点击