Целью этого проекта является создание собственного модульного теста и использование c-строк.CPP c-string Slice and Unit Test
Проект: Секвенирование ДНК Strand
Вот пример из двух нитей ДНК, которые соответствуют при перекрытии:
Strand 1: ACGGACATAGTCATT
Strand 2: CATAGTCATTTCATG
Combined: ACGGACATAGTCATTTCATG
Я в недоумении, когда я пытаюсь реализовать следующее:
Strand
должен иметь способ Strand substrand(size_t i, size_t j) const
, который возвращает копию нити, которая начинается с позиции i
и заканчивается в позиции j - 1
. Подумайте «Python slice.»
обновление: Я недавно ответил на свой вопрос и обновил void test_substrand()
и Strand Strand::substrand(size_t start, size_t end) const
. Я получаю ошибку сегментации.
Вот мой код до сих пор с именем файла под его телом.
void test_substrand()
{
Strand s1;
s1.setStrand("Hellow World");
s1.substrand(1, 4);
std::cout << "Substrand: " << s1.getStrand() << std::endl;
}
int main()
{
test_substrand();
return 0;
}
test.cpp
#ifndef _STRAND_H_
#define _STRAND_H_
#include <cstdlib>
#include <string>
#include <string>
class Strand
{
public:
Strand();
Strand(const char *src);
~Strand();
Strand & operator=(const Strand &rhs);
const char *getStrand() const;
void setStrand(const char *strand);
Strand & operator+=(const Strand &rhs);
Strand operator+(const Strand &rhs) const;
Strand substrand(size_t start, size_t end) const;
protected:
char *mStrand;
};
#endif /* _STRAND_H_ */
/* Local Variables: */
/* mode:c++ */
/* End: */
Strand.h
#include "Strand.h"
#include <cstring>
#include <string>
// default constructor
Strand::Strand()
: mStrand(0)
{
}
// copy constructor
Strand::Strand(const char *src)
: mStrand(0)
{
*this = src;
}
// destructor
Strand::~Strand()
{
if(mStrand != 0)
{
delete [] mStrand;
mStrand = 0;
}
}
// assignment operator
Strand &Strand::operator=(const Strand &rhs)
{
setStrand(rhs.mStrand);
return *this;
}
// Get Strand
const char *Strand::getStrand() const
{
return mStrand;
}
// Set Strand
void Strand::setStrand(const char *strand)
{
if(mStrand)
{
delete [] mStrand;
mStrand = 0;
}
if(strand)
{
mStrand = new char [std::strlen(strand) + 1];
std::strcpy(mStrand, strand);
}
}
size_t Strand::size() const
{
return mBases;
}
// operator +=
Strand &Strand::operator+=(const Strand &rhs)
{
char *new_strand;
new_strand = new char [std::strlen(mStrand) + std::strlen(rhs.mStrand) + 1];
strcat(strcpy(new_strand, mStrand), rhs.mStrand);
setStrand(new_strand);
delete [] new_strand;
return *this;
}
// operator +
Strand Strand::operator+(const Strand &rhs) const
{
Strand value;
value = *this;
value += rhs;
return value;
}
// substrand "python" slice
Strand Strand::substrand(size_t start, size_t end) const
{
size_t k, i, size;
size = end - start;
char *new_strand;
new_strand = new char [size +1];
for (k = start, i = 0; k < end; k++, i++)
{
new_strand[i] = mStrand[k];
}
new_strand[k] = 0;
Strand setStrand(new_strand);
delete [] new_strand;
new_strand = 0;
return setStrand;
}
Strand.cpp
Я должен упомянуть, что я проверил другие методы и оставил их. –
Поскольку ваша цепочка - это только массив символов, почему бы вам не использовать 'std :: string :: substr()' для реализации 'substrand'? – GMichael
@GMichael, я не могу сказать, что знал о 'string :: substr()', но я пойду проверю документацию и попробую реализовать. –