國立屏東商業技術學院 資訊工程系 物件導向程式設計

參考解答


using namespace std;
#include <iostream>
#include <cstring>
#include "name.h"
 
enum Level{Silver,Gold};
 
union Preferent{
  float Gold;
  int Silver;
};
 
struct data
{
  Name *name;
  char tp[10+1];
  Level level;
  Preferent benefit;
};
 
int main(int argc,char *argv[])
{
  enum {without,have}search;
  data *person=new data [5];
  char name[LenFN+LenLN+1];
  char tmp;
  char lv[10],input[10];
  int i,j,k;
  for(i=0;i<5;i++)
  {
 
      cin.getline(name,LenFN+LenLN+1);
      person[i].name=setName(name);
      cin.getline(person[i].tp,11);
      cin.getline(lv,10);
      if(strcmp(lv,"Gold")==0)
      {
        person[i].level=Gold;
        cin >>person[i].benefit.Gold;
        cin.get();
      }
      else if(strcmp(lv,"Silver")==0)
      {
        person[i].level=Silver;
        cin >>person[i].benefit.Silver;
        cin.get();
      }
  }
 
  search=without;
  while(strcmp(input,"Quit")!=0)
  {
    cin.getline(input,10);
    cout << "Input: " << input;
    for(i=0;i<5;i++)
    {
      if(strcmp(input,(person[i].name)->firstname)==0)
      {
        if(search==without)
          cout << endl;
        search=have;
        cout << (person[i].name)->lastname ;
        cout << " (" << person[i].tp << ") ";
        cout << (person[i].level==0? "S/":"G/");
        cout << (person[i].level==0? (person[i].benefit.Silver) :(person[i].benefit.Gold)) << endl;
      }
    }
    if(search==without&&strcmp(input,"Quit")!=0)
      cout << endl<< "Member not found!" << endl;
    search=without;
    cout << endl;
  }
  cout << "Bye" <<endl;
 
  return 0;
}