Advertisement
Guest User

Untitled

a guest
Apr 8th, 2020
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.16 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5.  
  6.  
  7.  
  8. class DNA{
  9.     private:
  10.     struct lanac{
  11.        
  12.         int pos;
  13.         string lanac;
  14.     };
  15.    
  16.     vector<lanac> molekula;
  17.     int size;
  18.    
  19.    
  20.     public:
  21.     void print(std::ostream& o) const{
  22.         for(auto it=molekula.begin(); it!=molekula.end(); ++it){
  23.             o << it->pos << "-" << it->pos+(it->lanac).size()<< " " << it->lanac << "\n";
  24.         }
  25.  
  26.         o << "\n";
  27.     }
  28.    
  29.     string toString() const {
  30.        
  31.                  string str;
  32.        
  33.          
  34.          for(auto i=0; i<molekula.size(); ++i){
  35.                          if(i>=1){
  36.              if( (molekula[i].pos-1) > (molekula[i-1].pos+molekula[i-1].lanac.size()) ) str+=string(((molekula[i].pos) - (molekula[i-1].pos+molekula[i-1].lanac.size())),'x');
  37.              }
  38.              str+=molekula[i].lanac;
  39.  
  40.          }
  41.          
  42.          return str;
  43.        
  44.     }
  45.    
  46.      void print(std::ostream& o, int p, int len) const{
  47.          
  48.         string str=toString();
  49.          
  50.          o << p << "-" << len << " ";
  51.          for(auto j=p; j<len; ++j)
  52.             if(str[j]!='x') o << str[j];
  53.          
  54.          
  55.            
  56.     }
  57.    
  58.    
  59.    
  60.  
  61.      void insert(int pos, std::string l){
  62.          
  63.         // molekula.push_back(lanac{pos, l});
  64.          
  65.       string stringToChange = toString();
  66.  
  67.        int size = stringToChange.size();
  68.         molekula.clear();
  69.          
  70.          
  71.          if(pos >= size) {
  72.              stringToChange += string((pos-stringToChange.size()), 'x');
  73.              stringToChange += l;
  74.          } else {
  75.              for (int i = pos, j = 0; j < l.size(); ++i, ++j) {
  76.                  if (i < size) {
  77.                      stringToChange[i] = l[j];
  78.                  } else {
  79.                      stringToChange.push_back(l[j]);
  80.                  }
  81.              }
  82.          }
  83.          
  84.  
  85.          toMolekula(stringToChange);
  86.      }
  87.      
  88.      void toMolekula(string& lanacToChange) {
  89.          for (int i = 0; i < lanacToChange.size(); ++i) {
  90.           if (lanacToChange[i] != 'x') {
  91.                 string noviLanac = "";
  92.                 int position = i;
  93.                 while(lanacToChange[i] != 'x' && i < lanacToChange.size()) {
  94.                     noviLanac += lanacToChange[i++];
  95.                 }
  96.                 molekula.push_back(lanac{position, noviLanac});
  97.             }
  98.         }
  99.      }
  100.      
  101.      
  102.     int remove(int pos, int len) {
  103.         string lanacToChange = toString();
  104.         if (pos >= lanacToChange.size()) {
  105.             return 0;
  106.         }
  107.        
  108.         int ret_number = 0;
  109.         for (int i = pos; i <= len; ++i) {
  110.             if (lanacToChange[i] != 'x') {
  111.                 lanacToChange[i] = 'x';
  112.                 ++ret_number;
  113.             }
  114.         }
  115.        
  116.         molekula.clear();
  117.        
  118.         toMolekula(lanacToChange);
  119.          
  120.         return ret_number;
  121.     }
  122. };
  123.  
  124.  
  125. int main(){
  126.    
  127.     DNA mol;
  128.    
  129.     mol.insert(0,"ATCG");
  130.     mol.insert(8, "TGCA");
  131.     mol.print(std::cout);
  132.     mol.insert(10, "TTTT");
  133.     mol.remove(1, 2);
  134.     mol.print(cout);
  135.    
  136.     return 0;
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement