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

參考解答


#include "name.h"
#include <iostream>
using namespace std;
 
Name *setName(const char *name)
{
  int i,j,k,m;
  enum {without,have}dot;
  Name *someone = new Name;
 
  dot=without;
  for(i=0;name[i]!=0;i++)
  {
    if(name[i]==',')
      dot=have;
  }
 
  if(dot==without)
  {
    for(i=0;name[i]!=' ';i++)
      someone->lastname[i]=name[i];
    someone->lastname[i]=0;
 
    for(k=0,j=i+1;name[j]!=0;j++,k++)
      someone->firstname[k]=name[j];
 
    someone->firstname[k]=name[j];
  }
  else
  {  
    for(i=0;name[i]!=' ';i++)
      ;
    for(m=0;m<i-1;m++)
      someone->firstname[m]=name[m];
    someone->firstname[m]='\0';
 
    for(k=0,j=i+1;name[j]!=0;j++,k++)
    {
      someone->lastname[k]=name[j];
    }
    someone->lastname[k]=name[j];
  }
 
  return someone;
}

using namespace std;
#include "initial.h"
#include <iostream>
#include <cstring>
 
char *initial(Name *n)
{
  enum{without,have}dash;
  int i,tmp,tmp_dash;
  char *abridge=new char [LenFN+LenLN+1];
  dash=without;
 
  for(i=0;n->firstname[i]!=0;i++)
    abridge[i]=n->firstname[i];
  abridge[i]=',';
  abridge[i+1]=' ';
  tmp=i+2;
 
  for(i=0;n->lastname[i]!=0;i++)
    if(n->lastname[i]=='-')
    {
      dash=have;
      tmp_dash=i;
    }
 
  abridge[tmp]=n->lastname[0];
    abridge[tmp+1]='.';
 
 
  if(dash)
  {
    abridge[tmp+2]='-' ;
    abridge[tmp+3]=n->lastname[tmp_dash+1];
    abridge[tmp+4]='.';
    abridge[tmp+5]='\0';
  }
  return abridge;
}