📄 test1_3.cc
字号:
//-------------------------------------------------------//MSS sample program #1: 'A simple and buggy String class'//-------------------------------------------------------/****************************************************************************This is sample program #1 for the MSS package. Read the file 'test1.txt'for more info. Note that this program is delibelately buggy!****************************************************************************/#include <stdlib.h>#include <iostream.h>#include <mss.h> //Let's invite MSS to this party ;)/* DECLARATION *//* ----------- */class String{ char *str;public: String(); String(char *); ~String(); unsigned Length(); bool is_equal_to(String);};/* IMPLEMENTATION *//* -------------- *///CONSTRUCTORString::String(){ str=NULL;}//CONSTRUCTOR 2String::String(char *strpar){ unsigned length=0; char *aux1,*aux2=strpar; //count the length of the source string while(*aux2!=0) { length++; //add one more aux2++; //step to the next char } //allocate space for the string aux1=str=new char [length+1]; //copy the string until \0 found. aux2=strpar; while(*aux2!=0) *aux1++=*aux2++; //add the \0 terminator *aux1=0;}//DESTRUCTORString::~String(){ delete [] str;}//Returns the lenght of the string not counting the \0 terminatorunsigned String::Length(){ unsigned length=0; char *aux=str; //count until '\0'(end of string) found. if(aux!=NULL) while(*aux!=0) { length++; //add one more aux++; //step to the next char } return length;}//returns 'true' if strings are equalbool String::is_equal_to(String strpar){ bool equal=false; char *aux1=str, *aux2=strpar.str; //compare only if same length if(this->Length()==strpar.Length()) while(*aux1!=0 && (equal=(*aux1++==*aux2++))); return equal;}/* MAIN PROGRAM *//*--------------*/int main(){ MSS_LOG_MSG("DECLARATIONS ZONE"); //To mark the decl. zone in the logfile String str1; String str2("Hello Underworld!"); String str3("Hello Underworld!"); MSS_LOG_MSG("'IF' ZONE"); //To mark the 'if' zone in the log file if(str2.is_equal_to(str3)) cout<<"equal"<<endl; else cout<<"different"<<endl; MSS_LOG_MSG("'LENGTH' ZONE."); //To mark the 'length' zone in the log file cout<<"String length is: "<<str3.Length()<<endl; MSS_LOG_MSG("PROGRAM END ZONE"); //To mark the end of the code in the logfile}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -