C++ 學習筆記6

來源:互聯網
上載者:User

 

Create a parametric string type. The basic type is to act as container class that contains a class T object. In the prototype case, the object is a char. The normal end-of-string sentinel is 0. The standard behavior should model the functions found in the
string library. The class definition could parameterize the sentinel as well. Such a type exists in the standard library string.

 

建立一個參數化字串類型,它是一個包含對象class T的容器類。在字串原型中,對象是char類型,字串結束標誌是0。實現該參數化型別時可模仿string庫,類定義時也可以將字串結束標誌進行參數化。

 

 

//本程式用VCSP6編譯通過

#include<iostream>
#include<cassert>
#include<string>
using namespace std;

const int max_size = 100;

template<typename T>
class my_string
{
public:
     my_string(const my_string& str)//define constructor
  {
  len = str.len;
  end = str.end;
  for(int i = 0; i < len; i++)
  {
   s[i] = str.s[i];
  }
  };  // constructor function 1
 
 
  my_string()
  {
  len = 0;
  end = 0;
  s[0] = end;
  };
 
 
  my_string(const T * str,const T End):end(End)
  {
  len = 0;
  while(str[len] != end)
  {
   s[len] = str[len++];
  }
  s[len] = end;
  } ; // constructor function 2
 
 
 
 
 
  ~my_string() {} ; // destructor function

 
  my_string& operator=(const my_string& str) // '=' -> overloading,assignment operations
  { 
  len = str.len;
  end = str.end;
  
  for(int i = 0; i < len; i++)
  {
   s[i] = str.s[i];
  }
  
  return *this;
  };
 
  T operator[](int index) // overloading of '[]'
  {  
  return s[index];
  };

  //define the friend functions
  friend my_string& operator+(const my_string& str1,const my_string& str2);  //the overloading of '+'
 
 
  friend bool operator==(const my_string& str1,const my_string& str2);  // judge if two strings are equal

 
  int size()
  { 
  return len;
  };
 
 
  bool empty()
  { 
  return len == 0;
  };
 
  void print()
  {
   for(int i=0;i<len;i++)
    cout<<s[i];

   cout<<endl;

  };
 
 

private:
 int len; //record the length of the string
    T end; //the ends of a string
 T s[max_size];  // s stores the string
};

template <typename T>
bool operator==(const my_string<T>& str1,const my_string<T>& str2)
{
 if(str1.len == str2.len)
 {
  for(int i = 0; i < len; i++)
  {
   if(str1.s[i] != str2.s[i])
    return 0;
  }
  return 1;
 }
 return 0;
}

template <typename T>
my_string<T> operator+(const my_string<T>& str1,const my_string<T>& str2)  //the overloading of '+'

 my_string newmy_string;
 newmy_string.len = str1.len + str2.len - 1;
 
 for(int i = 0; i < str1.len; i++)
 {
  newmy_string.s[i] = str1.s[i];
 }
 
 for(int j = 0; j <= str2.len; j++)
 {
  newmy_string.s[i+j] = str2.s[j];
 }
 
 return newmy_string;
}

int main()
{  
    char a;
 int a1;
 double a2;
 char type1[10];
 int type;
    char up[100];
 int flag1=0,flag2=0,flag3=0;
 int number1[100], num;
 double number2[100];
 
 cout << "我實現了 +,==,=,[] ,empty(),size()這六個功能,歡迎使用" << endl;
 cout<<"********************************************************"<<endl;
 cout<<"請輸入您需要的類型(char int or double):"<<endl;
 cin>>type1;
 if(strcmp(type1,"char")==0)
  flag1=1;
 if(strcmp(type1,"int")==0)
  flag2=1;
 if(strcmp(type1,"double")==0)
  flag3=1;
 cout<<"請輸入結束標誌:(對應您輸入的類型)"<<endl;
 if(flag1==1)
  cin>>a;
 if(flag2==1)
  cin>>a1;
 if(flag3==1)
  cin>>a2;
 cout<<"*******************************************************"<<endl;

 cout<<"請輸入資料:"<<endl;
 cout<<"如果您選擇char 型,輸入一行資料,以對應結束標誌結束"<<endl;
 cout<<"如果是其他類型,請輸入數的個數"<<endl;
  if(flag1==1)
  { 
   cin>>up;
  
  }
  if(flag2==1)
  {
  
   cin>>num;
   for (int i = 0; i < num; i++)
   {
    cout << "請輸入第" << i+1 << "個數" << endl;
    cin >> number1[i];
   }
   number1[num] = a1;
   
  }
  if(flag3==1)
  {  
   cin>>num;
   for(int i=0;i<num;i++)
   {
    cout << "請輸入第" << i+1 << "個數" << endl;
    cin >> number2[i];
   }
   number2[num]=a2;
  }
my_string<char> temp1(up, a);
my_string<int>  temp2(number1, a1);
 my_string<double> temp3(number2, a2);
  cout<<"****************************************************"<<endl;
  cout<<"1.列印整行資料:"<<endl;
  cout<<"2.求輸入資料量的大小:"<<endl;
  cout<<"0.結束操作:"<<endl;
  cin>>type;
  while(type!=0)
  {
   switch(type)
   {case 1:
  
   {
    if(flag1==1)
    temp1.print();
  
    if(flag2==1)
    temp2.print();
  
    if(flag3==1)
    temp3.print();
   }
   break;
   case 2:
    {
     if(flag1==1)
      cout<<temp1.size()<<endl;
     if(flag2==1)
      cout<<temp2.size()<<endl;
     if(flag3==1)
      cout<<temp3.size()<<endl;
    }
    break;

 

   }
   cin>>type;
  }

 

 

 return 0;
}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.