1. 程式人生 > >程式設計與演算法三第五週測驗finish

程式設計與演算法三第五週測驗finish

1:全面的MyString

#include <cstdlib>
#include <iostream>
using namespace std;
int strlen(const char * s) 
{	int i = 0;
	for(; s[i]; ++i);
	return i;
}
void strcpy(char * d,const char * s)
{
	int i = 0;
	for( i = 0; s[i]; ++i)
		d[i] = s[i];
	d[i] = 0;
		
}
int strcmp(const char * s1,const char * s2)
{
	for(int i = 0; s1[i] && s2[i] ; ++i) {
		if( s1[i] < s2[i] )
			return -1;
		else if( s1[i] > s2[i])
			return 1;
	}
	return 0;
}
void strcat(char * d,const char * s)
{
	int len = strlen(d);
	strcpy(d+len,s);
}
class MyString
{//補充程式碼
};


int CompareString( const void * e1, const void * e2)
{
	MyString * s1 = (MyString * ) e1;
	MyString * s2 = (MyString * ) e2;
	if( * s1 < *s2 )
	return -1;
	else if( *s1 == *s2)
	return 0;
	else if( *s1 > *s2 )
	return 1;
}
int main()
{
	MyString s1("abcd-"),s2,s3("efgh-"),s4(s1);
	MyString SArray[4] = {"big","me","about","take"};
	cout << "1. " << s1 << s2 << s3<< s4<< endl;
	s4 = s3;
	s3 = s1 + s3;
	cout << "2. " << s1 << endl;
	cout << "3. " << s2 << endl;
	cout << "4. " << s3 << endl;
	cout << "5. " << s4 << endl;
	cout << "6. " << s1[2] << endl;
	s2 = s1;
	s1 = "ijkl-";
	s1[2] = 'A' ;
	cout << "7. " << s2 << endl;
	cout << "8. " << s1 << endl;
	s1 += "mnop";
	cout << "9. " << s1 << endl;
	s4 = "qrst-" + s2;
	cout << "10. " << s4 << endl;
	s1 = s2 + s4 + " uvw " + "xyz";
	cout << "11. " << s1 << endl;
	qsort(SArray,4,sizeof(MyString),CompareString);
	for( int i = 0;i < 4;i ++ )
	cout << SArray[i] << endl;
	//s1的從下標0開始長度為4的子串
	cout << s1(0,4) << endl;
	//s1的從下標5開始長度為10的子串
	cout << s1(5,10) << endl;
	return 0;
}

輸入

輸出

1. abcd-efgh-abcd-
2. abcd-
3. 
4. abcd-efgh-
5. efgh-
6. c
7. abcd-
8. ijAl-
9. ijAl-mnop
10. qrst-abcd-
11. abcd-qrst-abcd- uvw xyz
about
big
me
take
abcd
qrst-abcd-

#include <cstdlib>
#include <iostream>
using namespace std;
int strlen(const char * s)
{
	int i = 0;
	for (; s[i]; ++i);
	return i;
}
void strcpy(char * d, const char * s)
{
	int i = 0;
	for (i = 0; s[i]; ++i)
		d[i] = s[i];
	d[i] = 0;

}
int strcmp(const char * s1, const char * s2)
{
	for (int i = 0; s1[i] && s2[i]; ++i) {
		if (s1[i] < s2[i])
			return -1;
		else if (s1[i] > s2[i])
			return 1;
	}
	return 0;
}
void strcat(char * d, const char * s)
{
	int len = strlen(d);
	strcpy(d + len, s);
}
class MyString
{
	// 在此處補充你的程式碼
	char *p;
public:
	MyString(const char* p1) {
		if (!p1) {
			p = NULL;
			return;
		}
		p = new char[strlen(p1) + 1];
		strcpy(p, p1);
	}
	MyString(const string &p1) {		
		p = new char[strlen(p1.c_str()) + 1];
		strcpy(p, p1.c_str());
	}
	MyString() { p = NULL; }
	MyString(const MyString &a) {
		if (this == &a) return;
		if (!a.p) {
			p = NULL;
			return;
		}
		p = new char[strlen(a.p) + 1];
		strcpy(p, a.p);
	}
	friend ostream& operator<<(ostream& o, MyString &a) {
		if(!a.p) return o;
		o << a.p;
		return o;
	}
	friend MyString operator+(const MyString& a,const MyString& b){
		MyString c; 
		c.p=new char[strlen(a.p)+strlen(b.p)+1];
		strcpy(c.p,a.p);
		strcat(c.p,b.p);
		return c;
	}
	char& operator[](int x){//這個好好琢磨下,char*不可以		
		return p[x];
	}
	MyString& operator+=(const string &p1){
		if(!p){
			p = new char[strlen(p1.c_str()) + 1];
		    strcpy(p, p1.c_str());	
			return *this;		
		}else{
			char *p2=new char[strlen(p1.c_str())+strlen(p)+1];
			strcpy(p2,p);
			strcat(p2,p1.c_str());
			delete []p;
			p=new char[strlen(p2)];
			strcpy(p,p2);
			return *this;
		}
	} 
	friend MyString operator+(const char* a,const MyString& b){
		MyString a1(a);
		MyString c=a1+b;
		return c;
	}
	bool operator<(MyString &b){
		if(strcmp(p,b.p)==-1){
			return true;
		}else
		return false;		
	} 
	bool operator==(MyString &b){
		if(strcmp(p,b.p)==0){
			return true;
		}else
		return false;
	} 
	int operator>(MyString &b){
		if(strcmp(p,b.p)){
			return true;
		}else
		return false;
	} 
	MyString& operator()(int s,int e){//注意這裡的引用 
	    MyString x;
	    x.p=new char[e-s+1];
	    int j=0;
	    for(int i=s;i<e;i++){
	    	x.p[j++]=p[i];
		}
		x.p[e-s]=0;
		return x;		
	}
};


int CompareString(const void * e1, const void * e2)
{
	MyString * s1 = (MyString *)e1;
	MyString * s2 = (MyString *)e2;
	if (*s1 < *s2)
		return -1;
	else if (*s1 == *s2)
		return 0;
	else if (*s1 > *s2)
		return 1;
}
int main()
{
	MyString s1("abcd-"), s2, s3("efgh-"), s4(s1);
	MyString SArray[4] = { "big","me","about","take" };
	cout << "1. " << s1 << s2 << s3 << s4 << endl;
	s4 = s3;
	s3 = s1 + s3;
	cout << "2. " << s1 << endl;
	cout << "3. " << s2 << endl;
	cout << "4. " << s3 << endl;
	cout << "5. " << s4 << endl;
	cout << "6. " << s1[2] << endl;
	s2 = s1;
	s1 = "ijkl-";
	s1[2] = 'A';
	cout << "7. " << s2 << endl;
	cout << "8. " << s1 << endl;
	s1 += "mnop";
	cout << "9. " << s1 << endl;
	s4 = "qrst-" + s2;
	cout << "10. " << s4 << endl;
	s1 = s2 + s4 + " uvw " + "xyz";
	cout << "11. " << s1 << endl;
	qsort(SArray, 4, sizeof(MyString), CompareString);
	for (int i = 0; i < 4; i++)
		cout << SArray[i] << endl;
	//s1的從下標0開始長度為4的子串
	cout << s1(0, 4) << endl;
	//s1的從下標5開始長度為10的子串
	cout << s1(5, 10) << endl;/**/
	getchar();
	return 0;
}

知識點:

語法:
const char *c_str();
c_str()函式返回一個指向正規C字串的指標, 內容與本string串相同.
這是為了與c語言相容,在c語言中沒有string類型,故必須通過string類物件的成員函式c_str()把string 物件轉換成c中的字串樣式。
注意:一定要使用strcpy()函式 等來操作方法c_str()返回的指標
比如:最好不要這樣:
char* c;
string s="1234";
c = s.c_str(); //c最後指向的內容是垃圾,因為s物件被析構,其內容被處理

應該這樣用:
char c[20];
string s="1234";
strcpy(c,s.c_str());
這樣才不會出錯,c_str()返回的是一個臨時指標,不能對其進行操作

再舉個例子
c_str() 以 char* 形式傳回 string 內含字串
如果一個函式要求char*引數,可以使用c_str()方法:
string s = "Hello World!";
printf("%s", s.c_str()); //輸出 "Hello World!"

2:繼承自串的MyString

描述

程式填空,輸出指定結果

#include <cstdlib>
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
class MyString:public string
{
//補充程式碼
};
int main()
{
	MyString s1("abcd-"),s2,s3("efgh-"),s4(s1);
	MyString SArray[4] = {"big","me","about","take"};
	cout << "1. " << s1 << s2 << s3<< s4<< endl;
	s4 = s3;
	s3 = s1 + s3;
	cout << "2. " << s1 << endl;
	cout << "3. " << s2 << endl;
	cout << "4. " << s3 << endl;
	cout << "5. " << s4 << endl;
	cout << "6. " << s1[2] << endl;
	s2 = s1;
	s1 = "ijkl-";
	s1[2] = 'A' ;
	cout << "7. " << s2 << endl;
	cout << "8. " << s1 << endl;
	s1 += "mnop";
	cout << "9. " << s1 << endl;
	s4 = "qrst-" + s2;
	cout << "10. " << s4 << endl;
	s1 = s2 + s4 + " uvw " + "xyz";
	cout << "11. " << s1 << endl;
        sort(SArray,SArray+4);
	for( int i = 0;i < 4;i ++ )
	cout << SArray[i] << endl;
	//s1的從下標0開始長度為4的子串
	cout << s1(0,4) << endl;
	//s1的從下標5開始長度為10的子串
	cout << s1(5,10) << endl;
	return 0;
}

輸入

輸出

1. abcd-efgh-abcd-
2. abcd-
3. 
4. abcd-efgh-
5. efgh-
6. c
7. abcd-
8. ijAl-
9. ijAl-mnop
10. qrst-abcd-
11. abcd- qrst-abcd- uvw xyz
關於


採取
abcd
qrst-abcd-

原理參考:

#include<iostream>
using namespace std;
class A{
	public:
		int x;
		A(int n):x(n){	}
		Print(){
			cout<<x;
		}
};
class B:public A{
	public:
	B(int n):A(n){	}
};
int main(){
	B b(10);
	b.Print();//B裡面沒有的函式,會自動呼叫A的 
}
#include <cstdlib>
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
class MyString :public string
{
	// 在此處補充你的程式碼
public:
	MyString() :string() {}
	MyString(const char *c) :string(c) {}
	MyString(const string &s) :string(s) {}//這一句是核心,"abcd-"這裡是string物件
	MyString operator()(int i, int j) {
		/*string b = string::substr(i, j);
		MyString a(b);//或者下面的寫法*/
		MyString a(string::substr(i, j));
		//or MyString a(this->substr(i, j));
		//or return MyString(substr(s, e));
		return a;
	}
};


int main()
{
	MyString s1("abcd-"), s2, s3("efgh-"), s4(s1);
	MyString SArray[4] = { "big","me","about","take" };
	cout << "1. " << s1 << s2 << s3 << s4 << endl;
	s4 = s3;
	s1 + s3;
	s3 = s1 + s3;
	cout << "2. " << s1 << endl;
	cout << "3. " << s2 << endl;
	cout << "4. " << s3 << endl;
	cout << "5. " << s4 << endl;
	cout << "6. " << s1[2] << endl;
	s2 = s1;
	s1 = "ijkl-";
	s1[2] = 'A' ;
	cout << "7. " << s2 << endl;
	cout << "8. " << s1 << endl;
	s1 += "mnop";
	cout << "9. " << s1 << endl;
	s4 = "qrst-" + s2;
	cout << "10. " << s4 << endl;
	s1 = s2 + s4 + " uvw " + "xyz";
	cout << "11. " << s1 << endl;
	sort(SArray,SArray+4);
	for( int i = 0;i < 4;i ++ )
	cout << SArray[i] << endl;
	//s1的從下標0開始長度為4的子串
	cout << s1(0,4) << endl;
	//s1的從下標5開始長度為10的子串
	cout << s1(5,10) << endl;
	getchar();
	return 0;
}