读书人

C++基础解析三十八

发布时间: 2008-12-19 10:21:24 作者: liuhuituzi

#define strmax 100

  #include

  #include

  class string

  {

  char line[100];

  int x,y;

  public:

  string(char *);

  void print();

  void turn();

  int strlength();

  void substring(unsigned,unsigned);

  void insert(int,char *);

  void index(char *);

  void replace(char *,char *);

  void strempty();

  string operator (string s);

  string operator + (string s);

  void operator < (string s1);//判断字符窜长度

  void operator (string s1);//字符窜相等

  void operator ! (string s1);//字符窜不相等

  char operator [] (int pos);

  };

  string::string(char *n)

  {

  strcpy(line,n);

  }

  void string::print()

  {

  cout<< line;

  }

  void string::turn()

  {

  int i,lenstrlen(line);

  for(i0;i

  {

  if(line[i]>97&&line[i]<122)

  {

  line[i]-32;

  cout<< line[i];

  }

  else

  {

  cout<< line[i];

  }

  }

  }

  int string::strlength()

  {

  return strlen(line);

  }

  void string::substring(unsigned pos,unsigned len)

  {

  if(pos > strlen(line))

  cout<<"Error!";

  else

  {

  if((strlen(line)-pos) < len)

  lenstrlen(line)-pos;

  cout<<"The substring is:";

  unsigned ipos+len;

  for(pos;pos

  {

  cout<< line[pos-1];

  }

  }

  }

  void string::insert(int pos,char *p)

  {

  char p1[64];

  char p2[64];

  strcpy(p1,p);

  cout<<"The line which will be insert is:"<

  int lstrlen (p1);

  int m0;

  int c0;

  for(int bpos-1;b<64;b++,c++)

  {

  p2[c]line[b];

  }

  for(int apos-1;a

  {



  line[a]p1[m];

  }

  strcat(line,p2);

  cout<

  }

  void string::index(char *n)

  {

  char *p;

  char n1[100];

  char n2[100];

  strcpy(n1,line);

  strcpy(n2,n);

  pstrstr(n1,n2);

  int l;

  lstrlen(n1);

  cout<<"The words you want to find is:"<

  if(p)

  {

  strcpy(n2,p);

  for(int i0;i

  {

  if(n1[i]n2[0])

  {

  cout<

  }

  }

  void string::replace(char *q,char *p)//q被替换串,考试,大提示p替换串

  {

  char p1[100];

  char p2[100];

  char p3[100];

  strcpy(p1,line);

  strcpy(p2,p);

  strcpy(p3,q);

  pstrstr(p1,p3);

  if(p)

  {

  int i,j;

  int lpstrlen(p);

  int lp1strlen(p1);

  int lp2strlen(p2);

  int lp3strlen(p3);

  cout<<"The sourse string is : "<

void string::strempty()

  {

  int istrlen(line);

  if(i0)

  {

  cout<<"The string is empty !";

  }

  else

  {

  cout<<"The string is not empty !";

  }

  }

  string string::operator (string s1)

  {

  strcpy(line,s1.line);

  return *this;

  }

  string string::operator + (string s1)

  {

  strcat(line,s1.line);

  return *this;

  }

  void string::operator < (string s1)

  {

  int lstrlen(s1.line);

  int mstrlen(line);

  if(m

  {

  cout<<"less and equal";

  }

  else

  {

  cout<<"not less and equal";

  }

  }

  void string::operator ! (string s1)

  {

  char n1[100];

  strcpy(n1,s1.line);

  int l;

  lstrlen(line);

  int m;

  mstrlen(n1);

  if(l!m)



  cout<<"judge not equal correct";

  else

  {

  for(int i0;i

  {

  if(line[i]n1[i])

  {

  if(il-1)

  cout<<"judge not equal error";

  }

  else

  {

  cout<<"judge not equal correct";

  break;

  }

  }

  }

  }

  void string::operator (string s1)

  {

  char n1[100];

  strcpy(n1,s1.line);

  int m;

  mstrlen(n1);

  int l;

  lstrlen(this->line);

  if(l!m)

  {

  cout<<"judge equal error";

  }

  else

  {

  for(int i0;i

  {

  if(line[i]n1[i])

  {

  if(il-1)

  cout<<"judge equal correct";

  }

  else

  {

  cout<<"judge equal error";

  break;

  }

  }

  }

  }

  char string::operator [](int pos)

  {

  int ipos;

  int lstrlen(line);

  if(i>-1&&i

  {

  cout<<"The word at "<

  cout<<"s.print():"<

  s.print();

  cout<

  s.turn();

  cout<

  s.substring(4,12);

  cout<

  s.insert(5,"abcdefgh");

  cout<

  s.index("LINE");

  cout<

  s.index("xyz");

  cout<

  s.replace("abc","DingTao");

  cout<

  s.replace("req","dakfj");

  cout<

  s.strempty();

  string s1("abc"),s2("def"),s3("ghi"),s4("abcdefg"),s5("2008");

  cout<

  cout<<"after s1s1+s2 , s1";

  s1s1+s2;

  s1.print();

  cout<

  s1

  cout<

  s2s4;

  cout<

  s2!s3;

  cout<

  s5[3];

  }




3COME考试频道为您精心整理,希望对您有所帮助,更多信息在http://www.reader8.net/exam/

读书人网 >复习指导

热点推荐