• 软件测试技术
  • 软件测试博客
  • 软件测试视频
  • 开源软件测试技术
  • 软件测试论坛
  • 软件测试沙龙
  • 软件测试资料下载
  • 软件测试杂志
  • 软件测试人才招聘
    暂时没有公告

字号: | 推荐给好友 上一篇 | 下一篇

习作!

发布: 2007-7-01 20:40 | 作者: admin | 来源: | 查看: 11次 | 进入软件测试论坛讨论

领测软件测试网

C++这个动东东以前看了始终没有写过代码,昨天看了今天花了一天时间,调了一个小类,让大家指点指点:

/*********************** tstring.h **********************************

#ifndef TSTRING_H
#define TSTRING_H

#define NULL    0
#define TRUE    1
#define FALSE    0

typedef long BOOL;

typedef unsigned long DWORD;

class TSTRING
{
public:

 TSTRING(const char * pStr = NULL);
 ~TSTRING();

 void SetNewString(const char * pStr = NULL);
 char * GetString(void);
 DWORD GetStringLen(void);

 void Print(void);
 
 /**********************************************************
 **There are some side effects in assignment(=) overloading
 ** with plus(+) overloading case(TSTRING &, TSTRING &).
 ***********************************************************/
  & operator = (const char * pStr);
 
 friend TSTRING & operator + (TSTRING & oStr, const char * pStr);
 friend TSTRING & operator + (const char * pStr, TSTRING & oStr);
 friend TSTRING & operator + (TSTRING & oStr1, TSTRING & oStr2);

protected:

 BOOL bNewAlloc; // Indicate whether memory is allocated
 char * pHeader; // Indicate the first character of the string
 DWORD dLen;    // Indicate the number of characters

private:
 
 char cNull;    // Represent the string when it is null.
};

#endif

/******************* tstring.cpp **************************************

#include <malloc.h>
#include <string.h>
#include <iostream>

using namespace std;

#include "tstring.h"

TSTRING :: TSTRING(const char * pStr)
{
 if (pStr == NULL) {
  dLen = 0;

  // Set pHeaer to point a null string
  cNull = 0;
  pHeader = &cNull;

  bNewAlloc = FALSE;
 }
 else {
  dLen = strlen(pStr);
  pHeader = (char *) malloc(dLen + 1);
  strcpy(pHeader, pStr);
  
  bNewAlloc = TRUE;
 }
}

TSTRING :: ~TSTRING()
{
 // If the memory used by the string is allocated originally
 // by compiler, MUST NOT free it, or cause a exception!
 if (bNewAlloc)
  free(pHeader);
 
}

void TSTRING :: SetNewString(const char * pStr)
{
 long lLen = strlen(pStr);

 // If the space is exactly enough, NO NEED to allocate memory.
 if (dLen != lLen) {
  if (bNewAlloc)
   free(pHeader);
  pHeader = (char *) malloc(lLen + 1);
  dLen = lLen;
  bNewAlloc = TRUE;
 }
 strcpy(pHeader, pStr);
}

char * TSTRING :: GetString(void)
{
 return pHeader;
}

DWORD TSTRING :: GetStringLen(void)
{
 return dLen;
}

void TSTRING :: Print(void)
{
 cout << pHeader;
}

/*
TSTRING & TSTRING :: operator = (const char * pStr)
{
 if (bNewAlloc)
  free(pHeader);
 dLen = strlen(pStr);
 pHeader = (char *) malloc(dLen + 1);
 strcpy(pHeader, pStr);
 
 return * this;
}
*/

TSTRING & operator + (TSTRING & oStr, const char * pStr)
{
 TSTRING *pNewStr = new TSTRING;
 
 long lLen;
 
 lLen = oStr.dLen + strlen(pStr);
 pNewStr->pHeader = (char *) malloc(lLen + 1);
 
 strcpy(pNewStr->pHeader, oStr.pHeader);
 strcpy(pNewStr->pHeader + oStr.dLen, pStr);
 
 pNewStr->bNewAlloc = TRUE;
 pNewStr->dLen = lLen;

 return * pNewStr;
}

TSTRING & operator + (const char * pStr, TSTRING & oStr)
{
 
 TSTRING *pNewStr = new TSTRING;
 
 long lLen;
 
 lLen = strlen(pStr);
 pNewStr->pHeader = (char *) malloc(lLen + oStr.dLen + 1);
 
 strcpy(pNewStr->pHeader, pStr);
 strcpy(pNewStr->pHeader + lLen, oStr.pHeader);
 
 pNewStr->bNewAlloc = TRUE;
 pNewStr->dLen = lLen + oStr.dLen;

 return * pNewStr;
}

TSTRING & operator + (TSTRING & oStr1, TSTRING & oStr2)
{
 
 TSTRING *pNewStr = new TSTRING;
 
 pNewStr->dLen = oStr1.dLen + oStr2.dLen;
 pNewStr->pHeader = (char *) malloc(pNewStr->dLen + 1);
 
 strcpy(pNewStr->pHeader, oStr1.pHeader);
 strcpy(pNewStr->pHeader + oStr1.dLen, oStr2.pHeader);
 
 pNewStr->bNewAlloc = TRUE;

 return * pNewStr;
}

/************************* learn.cpp (including main entry function) ************

#include "tstring.h"

int main(int argc, char * argv[])
{
 
 TSTRING str1("Original string!\n");
 str1.Print();

 str1.SetNewString("New string!");
 str1.Print();

 TSTRING & str2 = str1;
 
 str2 = "pre_" + str1 + "_post!";
 str2.Print();

 return 0;
}


延伸阅读

文章来源于领测软件测试网 https://www.ltesting.net/


关于领测软件测试网 | 领测软件测试网合作伙伴 | 广告服务 | 投稿指南 | 联系我们 | 网站地图 | 友情链接
版权所有(C) 2003-2010 TestAge(领测软件测试网)|领测国际科技(北京)有限公司|软件测试工程师培训网 All Rights Reserved
北京市海淀区中关村南大街9号北京理工科技大厦1402室 京ICP备2023014753号-2
技术支持和业务联系:info@testage.com.cn 电话:010-51297073

软件测试 | 领测国际ISTQBISTQB官网TMMiTMMi认证国际软件测试工程师认证领测软件测试网