⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 stringcl.cc

📁 this is a lp0 compilator new
💻 CC
字号:
//// *****************************************************************// *// *	Author 		: Gerald ( Jerry ) Carter// *			  cartegw@eng.auburn.edu// *	Filename	: stringcl.cc// *	Date Created	: 940401// *	Last update	: 970219// *// *	Description// *	-----------// *	This file contains all the method defintion for the string// *	class from "stringcl.h".  Each method is documented individually.// *// *****************************************************************//// INCLUDE FILES#include <iostream.h>         // cout, cin, cerr, etc...#include <string.h>           // strcmp(), strdup(), etc...#include <stdlib.h>           // exit()#include "logic.h"            // boolean typedefs#include "stringcl.h"         // string class declaration// Initialize static data membersconst string string::empty_string = string ("");// CONSTRUCTORS// Initializes the text to a string of 'set_char' of length 'size'string::string (char set_char, int size){   int       i = 0;   if (size > MAX_BUF_SIZE)      cerr << "String to long for buffer.  Unable to create string object.\n";   else   {      if ((text = new char[size+1]) == NULL)      {	 cerr << "Insufficient memory.\n";	 exit (-2);      }      for (; i<size; ++i)	 text[i] = set_char;      text[size+1] = '\0';      length = size;   }}// Initializes 'this' to another stringstring::string (const string& instring){   text = strdup (instring.text);   length = instring.length;}// Initializes the text to a null terminated string given by 'char_text'string::string (char* char_text){   int    i = 0;   if (strlen(char_text) > MAX_BUF_SIZE)      cerr << "String to long for buffer.  Unable to create string object.\n";   else   {      length = strlen(char_text);      if ((text = new char[length+1]) == NULL)      {	 cerr << "Insufficient memory.\n";	 exit (-2);      }      for (; i<length; ++i)	 text[i] = *(char_text++);      text[length] = '\0';   }}// Initializes the string to NULLstring::string (void){   length = 0;   text = 0;}// OVERLOADED OPERATORS// Add character to end of textstring string::operator+ (char add_char){   char    temp[MAX_BUF_SIZE];   int     i = 0;   for (; i<length; ++i)      temp[i] = text[i];   temp[length] = add_char;   temp[length+1] = '\0';   ++length;   return string(temp);}// Catenates two stringsstring string::operator+ (const string& add_string){   int     i = 0;   int     j = 0;   char    temp[(MAX_BUF_SIZE*2)+1];   for (; i<length; ++i)      temp[i] = text[i];   while (j<add_string.length)      temp[i++] = add_string.text[j++];   temp[i] = '\0';      return string(temp);}// Delete all instances of the given character from the stringstring string::operator- (char delete_char){   char   temp[MAX_BUF_SIZE];   int    i = 0;   int    index = 0;      for (; i<length; ++i)      if (text[i] != delete_char)	 temp[index++] = text[i];   temp[index] = '\0';      return string(temp);}// Delete character in the position indicated by the integer given//    position ranges 0..length-1string string::operator- (int delete_pos){   int   i = 0;   char  temp[MAX_BUF_SIZE];      if ((delete_pos<0) OR (delete_pos>length))   {      cerr << "Position to delete not within range.\n";      cerr << "      Position..." << delete_pos << "\n";      cerr << "    " << text << "\n";      return *this;   }      while (i<delete_pos)   {      temp[i] = text[i];      ++i;   }   ++delete_pos;   while (delete_pos<length)      temp[i++] = text[delete_pos++];   temp[i] = '\0';   return string(temp);}// Assignment operatorstring& string::operator= (const string& item){   int     i = 0;      if (text != NULL)      delete []text;   length = item.length;   if ((text = new char[length+1]) == NULL)   {      cerr << "Insufficient memory.\n";      exit (-2);   }   for (; i<length; ++i)      text[i] = item.text[i];   text[length] = '\0';   return *this;}// Simple ASCII comparison of the two string.text`sint string::operator< (const string& item) const{   int    i = 0;   int    limit;   if (length<item.length)      limit = length;   else      limit = item.length;   for (; i<limit; ++i)      if (text[i]<item.text[i])	 return TRUE;      else if (text[i]>item.text[i])	 return FALSE;   if (length < item.length)      return TRUE;   else      return FALSE;}// Simple ASCII comparison of the two string.text`sint string::operator> (const string& item)  const{   int    i = 0;   int    limit;   if (length<item.length)      limit = length;   else      limit = item.length;   for (; i<limit; ++i)      if (text[i]>item.text[i])	 return TRUE;      else if (text[i]<item.text[i])	 return FALSE;   if (length > item.length)      return TRUE;   else      return FALSE;}// Simple ASCII comparison of the two string.text`sint string::operator== (const string& item) const{   int    i = 0;   if (length!=item.length)      return FALSE;   for (; i<length; ++i)      if (text[i]!=item.text[i])	 return FALSE;   return TRUE;}// Simple ASCII comparison of the two string.text`sint string::operator!= (const string& item) const{   int    i = 0;   if (length!=item.length)      return TRUE;   for (; i<length; ++i)      if (text[i]!=item.text[i])	 return TRUE;   return FALSE;}// Divide the string into 2 strings at the first occurence of the character//   given.  Returns the first half excluding the characted divided atstring string::operator/ (const char split_char){   char      temp[MAX_BUF_SIZE];   char      *point = text;   int       i = 0;   while ((*point!=split_char) AND (*point!='\0') AND (i<MAX_BUF_SIZE))       temp[i++] = *(point++);   temp[i] = '\0';   return string(temp);}// Diviide the string into 2 strings at the first occurence of the character//   given.  Returns the second half of the string including the character//   divided atstring string::operator% (const char split_char){   char         temp[MAX_BUF_SIZE];   char         *point = text;   int          i = 0;      while ((*point!=split_char) AND (*point!='\0'))     ++point;   while ((*point!='\0') AND (i<MAX_BUF_SIZE))      temp[i++] = *point++;   temp[i] = '\0';   return string(temp);}// Subsrcipt operator that works identically to the subscript operator on arrayschar& string::operator[] (int index) const{   int        i = 0;   char       *point = text;   if (NOT ((0<=index) AND (i<length)))   {      // cerr << "Invalid index reference to string object.\n";      // cerr << text << "\n";      return *text;   }   while (i!=index)   {      ++point;      ++i;   }   return *point;}// print the stringostream& operator<< (ostream& S, const string& item){   int      index = 0;     for (; index<item.length; ++index)      S << item.text[index];   return S; }// Get from operator that reads a character string into the string objectistream& operator>> (istream& S, string& item){   char     buf[MAX_BUF_SIZE];   int      i = 0;   if (item.text != NULL)      delete []item.text;   S.getline (buf, MAX_BUF_SIZE-1, '\n');   item.length = strlen(buf);   if ((item.text = new char[item.length+1]) == NULL)   {      cerr << "Insufficient memory.\n";      exit (-2);   }   for (; i<item.length; ++i)      item.text[i] = buf[i];   item.text[item.length] = '\0';   return S;}/****** end if stringcl.cpp *******************************************************************************************************************/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -