Problem B: 字符串類(II)
阿新 • • 發佈:2018-06-02
char s del detail const a+b ID 一個空格 -- status
Submit: 6103 Solved: 2687
[Submit][Status][Web Board]
Problem B: 字符串類(II)
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 6103 Solved: 2687
[Submit][Status][Web Board]
Description
封裝一個字符串類,用於存儲字符串和處理的相關功能,支持以下操作:
1. STR::STR()構造方法:創建一個空的字符串對象。
2. STR::STR(const char *)構造方法:創建一個字符串對象,串的內容由參數給出。
3. STR::length()方法:返回字符串的長度。
4. STR::putline()方法:輸出串的內容,並換行。
5. 運算符“+”和“+=”,表示兩個字符串的連接運算,規則為:
c = a + b 表示串c中的字符是a和b的連接:“a+b”的結果是一個新的字符串,串a和串b的內容不變。
a += b 表示串a中的字符是a和b的連接:串b中的內容不變
-----------------------------------------------------------------------------
你設計一個字符串類STR,使得main()函數能夠正確運行。
函數調用格式見append.cc。
append.cc中已給出main()函數。
-----------------------------------------------------------------------------
Invalid Word(禁用單詞)錯誤:“string”、“vector”等被禁用。
Input
輸入有若幹行,每行一個字符串。
Output
每組測試數據對應輸出一行,包含兩部分內容,首先是一個整數,表示輸入串的長度,然後是輸入的字符串,兩者用一個空格分開。格式見sample。
Sample Input
A 123456789Sample Output
12 Hello World! 0 12 Hello World! 12 Hello World! 12 Hello World! 10 A123456789 1 A 9 123456789 10 123456789A 1 A
HINT
Append Code
append.cc,int main() { STR e; STR h("Hello World!"); STR he = e + h; cout << he.length() << " "; he.putline(); cout << e.length() << " "; e.putline(); cout << h.length() << " "; h.putline(); e += h; cout << e.length() << " "; e.putline(); cout << h.length() << " "; h.putline(); char s1[100001], s2[100001]; while(gets(s1) != NULL && gets(s2) != NULL) { STR str1(s1), str2(s2); STR str = str1 + str2; cout << str.length() << " "; str.putline(); cout << str1.length() << " "; str1.putline(); cout << str2.length() << " "; str2.putline(); str2 += str1; cout << str2.length() << " "; str2.putline(); cout << str1.length() << " "; str1.putline(); } }
#include <iostream> #include <iomanip> #include <cstdlib> #include <cstdio> using namespace std; int getlength(const char *arr) { int l=0; for(int i=0; arr[i]!=‘\0‘; i++) l++; return l; } void copystring(char *arr, const char *brr) { int l=getlength(brr); for(int i=0; i<l; i++) arr[i]=brr[i]; arr[l]=‘\0‘; } class STR { public : char *arr; int l; int ll; STR():arr(NULL),l(0),ll(0){} STR(const char *brr) { l=getlength(brr); ll=l*2; arr=new char[ll]; copystring(arr,brr); } int length() { return l; } void putline() { for(int i=0; i<l; i++) cout<<arr[i]; cout<<endl; } friend STR operator+(const STR &p1, const STR &p2) { STR temp; temp.l=p1.l+p2.l; temp.ll=temp.l*2; temp.arr=new char[temp.ll]; int i; for(i=0; i<p1.l; i++) temp.arr[i]=p1.arr[i]; for(int j=i; j<i+p2.l; j++) { temp.arr[j]=p2.arr[j-i]; } temp.arr[temp.l]=‘\0‘; return temp; } STR operator+=(const STR &p2) { int length=l+p2.l; ll=length*2; char *brr; brr=new char[ll]; int i; for(i=0; i<l; i++) { brr[i]=arr[i]; } for(int j=l; j<p2.l+l; j++) { brr[j]=p2.arr[j-l]; } brr[length]=‘\0‘; delete []arr; arr=brr; //delete []brr; return *this; } ~STR() { //delete []arr;// 不能析構!!!!! //在+= 運算中如果析構了就沒法調用使用了,就會亂碼!!!! } }; int main() { STR e; STR h("Hello World!"); STR he = e + h; cout << he.length() << " "; he.putline(); cout << e.length() << " "; e.putline(); cout << h.length() << " "; h.putline(); e += h; cout << e.length() << " "; e.putline(); cout << h.length() << " "; h.putline(); char s1[100001], s2[100001]; while(gets(s1) != NULL && gets(s2) != NULL) { STR str1(s1), str2(s2); STR str = str1 + str2; cout << str.length() << " "; str.putline(); cout << str1.length() << " "; str1.putline(); cout << str2.length() << " "; str2.putline(); str2 += str1; cout << str2.length() << " "; str2.putline(); cout << str1.length() << " "; str1.putline(); } }
Problem B: 字符串類(II)