Цель курсовой работы достигнута. Были созданы тестируемые классы: базовый класс Строка, Строка-идентификатор, Десятичная строка. Тестируемые классы работают корректно


 

 

                  Список использованных источников         

 

 

1 Б.И. Березин. Начальный курс С и С++. – М.:Издательство Диалог-МИФИ,

2005 г. – 248 с.



Приложение

Файл decstr.h

#ifndef LABA_DECSTR__H

#define LABA_DECSTR__H

#include "stroka.h"

 

class decstr: public stroka

{

public:

decstr (int=0);

decstr (const char *);

decstr (const decstr &);

~decstr();

decstr & operator = (const decstr &);

friend decstr operator + (const decstr &, const decstr &);

friend decstr operator - (const decstr &, const decstr &);

friend decstr operator + (const decstr &, int);

friend decstr operator + (int, const decstr &);

friend decstr operator + (const decstr &, const char*);

friend decstr operator + (const char*, const decstr &);

};

 

#endif //LABA_DECSTR__H

Файл decstr.cpp

 

#include <iostream>

#include "decstr.h"

#include <stdlib.h>

#include <stdio.h>

using namespace std;

 

decstr:: decstr (int val): stroka (val)

{

cout << "decstr::decstr(int val): stroka (val)" << val << endl;

}

 

decstr::decstr (const char *dstr):stroka(dstr)

{

if (!((pCh[0] >= '1' && pCh[0] <= '9') || (pCh[0] == '-' && pCh[1] >='1' && pCh[1] <='9')) || pCh[0]=='+')

{

   cout << "bad symbol, pCh[0]=" << pCh[0] << endl;

   if (pCh)

       delete[] pCh;

   len = 0;

   pCh = new char[len + 1];

   pCh[0] = '\0';

   return;

}

cout << "decstr::decstr (const char *dstr):stroka(dstr)" << endl;

}

 

decstr:: decstr (const decstr &from): stroka (from)

{

cout << "decstr::decstr (const decstr &from): stroka (from)" << endl;

}

 

decstr:: ~decstr()

{

cout << "decstr::~decstr()" << endl;

}

 

decstr & decstr:: operator = (const decstr &dS)

{

if(&dS!=this)

{

   delete[] pCh;

   len = strlen(dS.pCh);

   pCh = new char[len + 1];

   strcpy(pCh,dS.pCh);

}

cout << "decstr & decstr:: operator = (const decstr &dS)" << endl;

return *this;

}

 

decstr operator + (const decstr &pobj1,const decstr &pobj2)

{

int num1, num2;

char *pChTmp;

decstr tmp (pobj1);

num1 = atoi(tmp.getstr());

num2 = atoi(pobj2.getstr());

if (tmp.len >= pobj2.len)

{

 

  int A = num1 + num2;

  int count = tmp.len + 1;

  int modul, AS, i = 0;

  AS = A;

  char *pMasCh = new char[count+1];

  pMasCh[count - 1] = '\0';

  while (i < count - 1)

      pMasCh[i++] = ' ';

  if (A < 0)

      A = -A;

  --i;

  while (A)

  {

      modul = A % 10;

      pMasCh[i] = modul + '0';

      A /= 10;

      --i;

  }

  if (AS < 0)

      pMasCh[i] = '-';

  pChTmp = pMasCh;

}

else

{

   int A = num1 + num2;

   int count = pobj2.len + 1;

   int modul, AS, i = 0;

   AS = A;

   char *pMasCh = new char[count+1];

   pMasCh[count - 1] = '\0';

   while (i < count - 1)

       pMasCh[i++] = ' ';

   if (A < 0)

       A = -A;

   --i;

   while (A)

   {

       modul = A % 10;

       pMasCh[i] = modul + '0';

       A /= 10;

       --i;

   }

   if (AS < 0)

       pMasCh[i] = '-';

   pChTmp = pMasCh;

}

if (tmp.pCh)

   delete[] tmp.pCh;

tmp.pCh = pChTmp;

tmp.len = strlen(pChTmp);

cout << pChTmp << endl;

cout << strlen(pChTmp) << endl;

cout << "decstr operator + (const decstr &pobj1,const decstr &pobj2)" << endl;

return tmp;

}

 

decstr operator - (const decstr &pobj1,const decstr &pobj2)

{

int num1, num2;

char *pChTmp;

decstr tmp (pobj1);

num1 = atoi(tmp.getstr());

num2 = atoi(pobj2.getstr());

if (tmp.len >= pobj2.len)

{

 

   int A = num1 - num2;

   int count = tmp.len + 1;

   int modul, AS, i = 0;

   AS = A;

   char *pMasCh = new char[count+1];

   pMasCh[count - 1] = '\0';

   while (i < count - 1)

       pMasCh[i++] = ' ';

   if (A < 0)

       A = -A;

   --i;

   while (A)

   {

       modul = A % 10;

       pMasCh[i] = modul + '0';

       A /= 10;

       --i;

   }

   if (AS < 0)

       pMasCh[i] = '-';

   pChTmp = pMasCh;

}

else

{

   int A = num1 - num2;

   int count = pobj2.len + 1;

   int modul, AS, i = 0;

   AS = A;

   char *pMasCh = new char[count+1];

   pMasCh[count - 1] = '\0';

   while (i < count - 1)

       pMasCh[i++] = ' ';

   if (A < 0)

       A = -A;

   --i;

   while (A)

   {

       modul = A % 10;

       pMasCh[i] = modul + '0';

       A /= 10;

       --i;

   }

   if (AS < 0)

      pMasCh[i] = '-';

   pChTmp = pMasCh;

}

if (tmp.pCh)

   delete[] tmp.pCh;

tmp.pCh = pChTmp;

tmp.len = strlen(pChTmp);

cout << pChTmp << endl;

cout << strlen(pChTmp) << endl;

cout << "decstr operator - (const decstr &pobj1,const decstr &pobj2)" << endl;

return tmp;

}

 

decstr operator + (const decstr &pobj1, int num2)

{

int num1;

char *pChTmp;

decstr tmp (pobj1);

num1 = atoi(tmp.getstr());

int numm2=num2;

int num2size=0;

while (numm2!=0)

{

   numm2/=10;

   num2size ++;

}

if (tmp.len >= num2size)

{

 

   int A = num1 + num2;

   int count = tmp.len + 1;

   int modul, AS, i = 0;

   AS = A;

   char *pMasCh = new char[count+1];

   pMasCh[count - 1] = '\0';

   while (i < count - 1)

       pMasCh[i++] = ' ';

   if (A < 0)

       A = -A;

   --i;

   while (A)

   {

       modul = A % 10;

       pMasCh[i] = modul + '0';

       A /= 10;

         --i;

   }

   if (AS < 0)

       pMasCh[i] = '-';

   pChTmp = pMasCh;

}

else

{

   int A = num1 + num2;

   int count = num2size + 1;

   int modul, AS, i = 0;

   AS = A;

   char *pMasCh = new char[count+1];

   pMasCh[count - 1] = '\0';

   while (i < count - 1)

       pMasCh[i++] = ' ';

   if (A < 0)

       A = -A;

   --i;

   while (A)

   {

       modul = A % 10;

       pMasCh[i] = modul + '0';

       A /= 10;

       --i;

   }

   if (AS < 0)

       pMasCh[i] = '-';

   pChTmp = pMasCh;

}

if (tmp.pCh)

   delete[] tmp.pCh;

tmp.pCh = pChTmp;

tmp.len = strlen(pChTmp);

cout << pChTmp << endl;

cout << strlen(pChTmp) << endl;

cout << "decstr operator + (const decstr &pobj1, int num2)" << endl;

return tmp;

}

 

decstr operator + (int num1, const decstr &pobj2)

{

int num2;

char *pChTmp;

decstr tmp (pobj2);

num2 = atoi(tmp.getstr());

int numm1=num1;

int num1size=0;

while (numm1!=0)

{

   numm1/=10;

   num1size ++;

}

if (tmp.len >= num1size)

{

 

   int A = num1 + num2;

   int count = tmp.len + 1;

   int modul, AS, i = 0;

   AS = A;

   char *pMasCh = new char[count+1];

   pMasCh[count - 1] = '\0';

   while (i < count - 1)

       pMasCh[i++] = ' ';

   if (A < 0)

       A = -A;

   --i;

   while (A)

   {

       modul = A % 10;

       pMasCh[i] = modul + '0';

       A /= 10;

       --i;

   }

   if (AS < 0)

       pMasCh[i] = '-';

   pChTmp = pMasCh;

}

else

{

   int A = num1 + num2;

   int count = num1size + 1;

   int modul, AS, i = 0;

   AS = A;

   char *pMasCh = new char[count+1];

   pMasCh[count - 1] = '\0';

   while (i < count - 1)

       pMasCh[i++] = ' ';

   if (A < 0)

       A = -A;

   --i;

   while (A)

     {

       modul = A % 10;

       pMasCh[i] = modul + '0';

       A /= 10;

       --i;

   }

   if (AS < 0)

       pMasCh[i] = '-';

   pChTmp = pMasCh;

}

if (tmp.pCh)

   delete[] tmp.pCh;

tmp.pCh = pChTmp;

tmp.len = strlen(pChTmp);

cout << pChTmp << endl;

cout << strlen(pChTmp) << endl;

cout << "decstr operator + (int num1, const decstr &pobj2)" << endl;

return tmp;

}

 

decstr operator + (const decstr &pobj1,const char* pobj2)

{

int num1,num2;

char *pChTmp;

decstr tmp (pobj1);

num1 = atoi(tmp.getstr());

num2 = atoi(pobj2);

if (tmp.len >= strlen(pobj2))

{

 

   int A = num1 + num2;

   int count = tmp.len + 1;

   int modul, AS, i = 0;

  AS = A;

   char *pMasCh = new char[count+1];

   pMasCh[count - 1] = '\0';

   while (i < count - 1)

       pMasCh[i++] = ' ';

   if (A < 0)

       A = -A;

   --i;

   while (A)

   {

       modul = A % 10;

       pMasCh[i] = modul + '0';

       A /= 10;

       --i;

   }

   if (AS < 0)

       pMasCh[i] = '-';

   pChTmp = pMasCh;

}

else

{

   int A = num1 + num2;

   int count = strlen(pobj2) + 1;

   int modul, AS, i = 0;

   AS = A;

   char *pMasCh = new char[count+1];

   pMasCh[count - 1] = '\0';

   while (i < count - 1)

       pMasCh[i++] = ' ';

   if (A < 0)

       A = -A;

   --i;

   while (A)

   {

       modul = A % 10;

       pMasCh[i] = modul + '0';

       A /= 10;

       --i;

   }

   if (AS < 0)

       pMasCh[i] = '-';

   pChTmp = pMasCh;

}

if (tmp.pCh)

   delete[] tmp.pCh;

tmp.pCh = pChTmp;

tmp.len = strlen(pChTmp);

cout << pChTmp << endl;

cout << strlen(pChTmp) << endl;

cout << "decstr operator + (const decstr &pobj1,const char* pobj2)" << endl;

return tmp;

}

 

decstr operator + (const char* pobj1,const decstr &pobj2)

{

int num1,num2;

char *pChTmp;

decstr tmp (pobj1);

num1 = atoi(tmp.getstr());

num2 = atoi(pobj2.getstr());

if (tmp.len >= pobj2.len)

{

 

   int A = num1 + num2;

   int count = tmp.len + 1;

   int modul, AS, i = 0;

   AS = A;

   char *pMasCh = new char[count+1];

   pMasCh[count - 1] = '\0';

   while (i < count - 1)

       pMasCh[i++] = ' ';

   if (A < 0)

       A = -A;

   --i;

   while (A)

   {

       modul = A % 10;

       pMasCh[i] = modul + '0';

       A /= 10;

       --i;

   }

   if (AS < 0)

       pMasCh[i] = '-';

   pChTmp = pMasCh;

}

else

{

   int A = num1 + num2;

   int count = pobj2.len + 1;

   int modul, AS, i = 0;

   AS = A;

   char *pMasCh = new char[count+1];

   pMasCh[count - 1] = '\0';

   while (i < count - 1)

       pMasCh[i++] = ' ';

   if (A < 0)

       A = -A;

   --i;

   while (A)

   {

       modul = A % 10;

       pMasCh[i] = modul + '0';

       A /= 10;

       --i;

   }

   if (AS < 0)

       pMasCh[i] = '-';

   pChTmp = pMasCh;

}

if (tmp.pCh)

   delete[] tmp.pCh;

tmp.pCh = pChTmp;

tmp.len = strlen(pChTmp);

cout << pChTmp << endl;

cout << strlen(pChTmp) << endl;

cout << "decstr operator + (const char* pobj1,const decstr &pobj2)" << endl;

return tmp;

}

Файл IdentStr.h

 

#ifndef LABA_IDENTSTR_H

#define LABA_IDENTSTR_H

#include "stroka.h"

 

class IdentStr: public stroka {

public:

IdentStr(int=0);

IdentStr(char ch);

IdentStr(const char *);

IdentStr(const IdentStr &);

~IdentStr();

IdentStr & operator = (const IdentStr &);

char & operator [] (int);

IdentStr operator ~ ();

friend IdentStr operator + (const IdentStr &, const IdentStr &);

friend IdentStr operator + (const IdentStr &, const char *);

friend IdentStr operator + (const char *, const IdentStr &);

};

 

 

#endif //LABA_IDENTSTR_H

Файл IdentStr.cpp

#include <iostream>

#include "IdentStr.h"

using namespace std;

 

IdentStr::IdentStr(int val):stroka(val)

{

cout << "IdentStr::IdentStr (int val):stroka (val),val=" << val << endl;

}

 

IdentStr::IdentStr(char ch):stroka(ch)

{

if (!((pCh[0] >= 'a' && pCh[0] <= 'z') || (ch >= 'A' && ch <= 'Z')))

{

   if (pCh) delete[] pCh;

   len = 0;

   pCh = new char[len + 1];

   pCh[0] = '\0';

   cout << "bad symbol" << endl;

}

cout << "IdentStr::IdentStr(char ch):stroka(ch)" << endl;

}

 

IdentStr::IdentStr(const char *str):stroka(str)

{

if ((pCh[0] >= '0' && pCh[0] <= '9') || (pCh[0] == ' '))

{

   cout << "bad symbol, pCh[0]=" << pCh[0] << endl;

   if (pCh) delete[] pCh;

   len = 0;

   pCh = new char[len + 1];

   pCh[0] = '\0';

   return;

}

for (int i=1; i<len;i++)

{

   if (!((pCh[i] >= 'a' && pCh[i] <= 'z') || (pCh[i] >= 'A' && pCh[i] <= 'Z') || (pCh[i] >= '0' && pCh[i] <= '9') || (pCh[i] == '_')))

   {

       cout << "bad symbol, pCh[i]=" << pCh[i] << endl;

       if (pCh) delete[] pCh;

       len = 0;

       pCh = new char[len + 1];

       pCh[0] = '\0';

       return;

   }

}

const char *keywords[] = { "auto", "break", "case", "char", "const", "continue", "default", "do", "double", "else", "enum", "extern", "float", "for", "goto", "if", "int", "long", "register", "return", "short", "signed", "sizeof", "static", "struct", "switch", "typedef", "union", "unsigned", "void", "volatile", "while", "asm", "bool", "catch", "class", "const_cast", "delete", "dynamic_cast", "explicit", "export", "false", "friend", "inline", "mutable", "namespace", "new", "operator", "private", "protected", "public", "reinterpret_cast", "static_cast", "template", "this", "throw", "true", "try", "typeid", "typename", "using", "virtual", "wchar_t", "_Bool", "_Complex", "_Imaginary", "restrict" };

for (int i=0; i<67; i++)

 {

   if (!strcmp(pCh, keywords[i]))

   {

       cout << "Error!!!" << pCh << endl;

       if (pCh) delete[] pCh;

       len = 0;

       pCh = new char[len + 1];

       pCh[0] = '\0';

       return;

   }

}

cout << "IdentStr::IdentStr(const char *):stroka(char *)" << endl;

}

 

IdentStr::IdentStr (const IdentStr &from):stroka(from)

{

cout << "IdentStr::IdentStr(const IdentStr & from):stroka(from)" << endl;

}

IdentStr::~IdentStr()

{

cout << "IdentStr::~IdentStr()" << endl;

}

 

IdentStr & IdentStr:: operator = (const IdentStr &S)

{

if(&S!=this)

{

   delete[] pCh;

   len = strlen(S.pCh);

   pCh = new char[len + 1];

   strcpy(pCh,S.pCh);

}

cout << "IdentStr & IdentStr::operator=(const IdentStr &S)" << endl;

return *this;

}

 

IdentStr IdentStr::operator ~ ()

{

int i,j;

char tmp;

for (i=0,j=len-1;i<len/2;i++,j--)

{

   tmp = pCh[i];

   pCh[i] = pCh[j];

   pCh[j] = tmp;

}

cout << "IdentStr & IdentStr::operator~()" << endl;

return *this;

}

 

char & IdentStr::operator [](int index)

{

if ((index >= 0) && (index < len))

{

   cout << "char & IdentStr::operator[](int index)" << endl;

   return pCh[index];

}

return pCh[0];

}

 

IdentStr operator + (const IdentStr &obj1, const IdentStr &obj2)

{

IdentStr tmp(obj1.getlen()+obj2.getlen());

//strcpy (tmp.pCh,obj1.getstr());

int i=0,j=0;

while (tmp.pCh[i++]=obj1.pCh[j++]);

--i;

//strcat (tmp.pCh,obj2.getstr());

//int i = obj1.getlen();

j=0;

while (tmp.pCh[i++]=obj2.pCh[j++]);

cout << "IdentStr operator + (const IdentStr &obj1, const IdentStr &obj2)" << endl;

return tmp;

}

 

IdentStr operator + (const IdentStr &obj1, const char *obj2)

{

IdentStr tmp (obj1.getlen()+(int)strlen(obj2));

int i=0, j=0;

while (tmp.pCh[i++]=obj1.pCh[j++]);

--i;

j=0;

while (tmp.pCh[i++]= *obj2++);

cout << "IdentStr operator + (const IdentStr &obj1, const IdentStr &obj2)" << endl;

return tmp;

}

 

IdentStr operator + (const char *obj1,const IdentStr &obj2)

{

IdentStr tmp ((int)strlen(obj1)+obj2.getlen());

int i=0, j=0;

while (tmp.pCh[i++]= *obj1++);

--i;

j=0;

while (tmp.pCh[i++]=obj2.pCh[j++]);

cout << "IdentStr operator + (const IdentStr &obj1, const IdentStr &obj2)" << endl;

return tmp;

}

Файл Stroka.h

#ifndef LABA_STROKA_H

#define LABA_STROKA_H

 

class stroka{

protected:

int len;

char *pCh;

public:

stroka (int=0);

stroka (char ch);          //stroka obj('z');

stroka (const char *);     //stroka obj1("itmo");

stroka (const stroka &);   //stroka obj2=obj1;

~stroka();

char *getstr(void)const

{

   return pCh;

}

int getlen(void)const

{

   return len;

}

void show(void);

};

 

#endif //LABA_STROKA_H

Stroka.cpp

#include <iostream>

#include "stroka.h"

using namespace std;

 

stroka::stroka(int val):len(val),pCh(new char[len+1])

/*{

 * len= val;

 * pCh= new char [len+1]};     //др вид записи

 * }*/

{

if(val==0)pCh[0]='0';

cout << "stroka::stroka (int val)" << endl;

}

 

stroka::stroka(char ch):len(1),pCh(new char[len+1])

{

pCh[0]=ch;

pCh[1]='\0';

cout << "stroka::stroka(char ch)" << endl;

}

 

stroka::stroka(const char *s):len(strlen(s)),pCh(new char[len+1])

{

strcpy(pCh,s);     //strcy_s(pCh,len+1,s);

cout << "stroka::stroka(const char* s)" << endl;

}

 

stroka::stroka(const stroka &from):len(strlen(from.pCh)),pCh(new char[from.len+1])

{

strcpy(pCh,from.pCh);

//strcy_s(pCh,from.len+1,from.pCh);

cout << "stroka::stroka(const stroka & from)" << endl;

}

stroka::~stroka()

{

if (pCh)

   delete[] pCh;

cout << "stroka::~stroka()" << endl;

}

void stroka::show (void)

{

cout << "len = " << len << endl;

cout << "pCh = " << pCh << endl;

}                     

Файл main.cpp

 

#include <iostream>

#include "stroka.h"

#include "IdentStr.h"

#include "decstr.h"

using namespace std;

extern "C" char* __cdecl fun1(int);

 

int main()

{

stroka obj1(0);

stroka obj2(3);

obj2.show();

stroka obj3('s');

obj3.show();

stroka obj4("fibonacci");

obj4.show();

stroka obj5=obj1;

obj5.show();

 

IdentStr obj1_(5);

IdentStr obj2_('c');

IdentStr obj3_("iTMO");

IdentStr obj4_(0);

IdentStr obj5_("auto");

IdentStr obj6_(" 12");

IdentStr obj7_("1PBKS");

IdentStr obj8_("SLOG");

obj1_=obj2_;

obj3_[2];

obj3_=obj3_+obj8_;

obj3_.show();

obj8_=obj8_+"HELLO";

obj8_.show();

obj7_="GOOD"+obj3_;

obj7_.show();

 

decstr _obj1(2);

decstr _obj2("a421");

decstr _obj3("123");

decstr _obj4(" 1");

decstr _obj5=_obj3;

decstr _obj6(0);

_obj6=_obj2;

_obj3=_obj3+_obj5;

_obj4=_obj3-_obj5;

decstr _obj7("-55");

_obj6=_obj3-_obj7;

_obj4=_obj3+_obj7;

_obj4=_obj4+123;

_obj6=12+_obj6;

_obj4="456"+_obj4;

_obj6=_obj6+"333";

 

char* k = fun1(9090);

cout << k << endl;

 

return 0;

}

 

Asm.asm

.586
.model flat, stdcall
public fun1
_data segment
strmas db 11 dup(?)
dec1 dd 10
_data ends
_text segment
fun1 proc par1:dword
lea ebx, strmas
push ebx
mov ecx, 11
metka1:
mov byte ptr[ebx],'_'
inc ebx
loop metka1
mov eax, par1
push eax
or eax,eax
jns metka2
neg eax
metka2:
xor edx, edx
div dec1
add dx, '0'
dec ebx
mov byte ptr[ebx],dl
;inc ecx
or eax, eax
jnz metka2
pop eax
or eax,eax
jns metka3
dec ebx
mov byte ptr[ebx], '_'
;inc ecx
metka3:
pop ebx
mov eax,ebx
ret
fun1 endp
_text ends
end

.586

.model flat, stdcall

public fun1

_data segment

strmas db 11 dup(?)

dec1 dd 10

_data ends

_text segment

fun1 proc par1:dword

lea ebx, strmas

push ebx

mov ecx, 11

metka1:

mov byte ptr[ebx],'_'

inc ebx

loop metka1

mov eax, par1

push eax

or eax,eax

jns metka2

neg eax

metka2:

xor edx, edx

div dec1

add dx, '0'

dec ebx

mov byte ptr[ebx],dl

;inc ecx

or eax, eax

jnz metka2

pop eax

or eax,eax

jns metka3

dec ebx

mov byte ptr[ebx], '_'

;inc ecx

metka3:

pop ebx

mov eax,ebx

ret

fun1 endp

_text ends

end












































Понравилась статья? Добавь ее в закладку (CTRL+D) и не забудь поделиться с друзьями:  



double arrow
Сейчас читают про: