程序设计与算法三第五周测验

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));
		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;
}

猜你喜欢

转载自blog.csdn.net/qq_31647835/article/details/81265313