11.考研-数据结构-串的基本操作

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Suyebiubiu/article/details/80993735
// ConsoleApplication1.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include<iostream>
using namespace std;

//串的变长分配存储表示

typedef struct {
	char *ch;//指向动态分配存储区首地址的字符指针
	int length;//这个串的长度length
}Str;

/*
 *1.串的赋值操作
 *	串的赋值不能使用等号,因为串是一个数组
 *  该方法是将一个常量字符串赋值给str
 *	赋值成功返回1,否则返回0
 */
int strassign(Str& str,char* ch) {
	if (str.ch) {//str的ch首个指针
		///free(str.ch);//释放原来字符串的空间
	}
	int len = 0;
	char *c=ch;
	while (*c) {//确定一下这个字符串的长度len
		len++;
		c++;
	}
	if (len==0) {//如果字符串的长度为0
		str.ch = NULL;
		str.length = 0;
		return 0;
	}
	else {//如果这个字符串的长度不是0 的话
		str.ch = (char*)malloc(sizeof(char)*(len+1));
		//取了len+1是为了多分配一个空间存放“\0”字符
		if (str.ch==NULL) {
			return 0;
		}
		else {
			c = ch;
			for (int i = 0; i <= len;i++,c++) {
				str.ch[i] = *c;
				//这个循环中使用<=是为了将ch最后的“\0”复制到新串中作为结束的标记
			}
			str.length = len;
			return 1;
		}
	}
}
/*
	2.取出字符串的长度
*/
int strlength(Str str) {
	return str.length;
}

/*
	3.串的比较大小的操作
		规则:从第一个字符开始比较,比较ASCII码 如果相等比较下一位
			  在没有比较出大小的情况下,先结束的为较小串,两个串同时
				结束则返回两个串相等
*/
int strcompare(Str s1,Str s2) {
	for (int i = 0; i < s1.length&&i < s2.length;i++) {
		if (s1.ch[i]!=s2.ch[i]) {
			return s1.ch[i] - s2.ch[i];
		}
	}
	return s1.length - s2.length;
}

/*
	4.串的连接操作
	将两个串首位相接合并成一个新的字符串的操作
*/
int concat(Str& str ,Str str1,Str str2) {
	str.ch = (char*)malloc(sizeof(char)* (str1.length + str2.length + 1));

	int i = 0;
	while (i<str1.length) {
		str.ch[i] = str1.ch[i];
		i++;
	}
	int j = 0;
	while (j<=str2.length) {
		//这里使用<=是为了将str2最后一个“\0”一起赋值
		str.ch[i + j] = str2.ch[j];
		j++;
	}
	str.length = str1.length + str2.length;
	return 1;
}

/*
	5.求子串的操作
	从给定串中某一个位置开始到某一个位置结束的串的
*/
int subStr(Str& substr,Str str,int pos,int len) {
	if (pos<0||pos>=str.length||len<0||len>=str.length) {
		return 0;
	}

	if (len==0) {
		substr.ch = NULL;
		substr.length = 0;
		return 1;
	}
	else {
		substr.ch = (char*)malloc(sizeof(char)*(len + 1));
		int i = pos;
		int j = 0;
		while (i<pos+len) {
			substr.ch[j] = str.ch[i];
			i++;
			j++;
		}
		substr.ch[j]='\0';
		substr.length = len;
		return 1;
	}

}


int main()
{
	Str str;
	char ch[] = "abcdefg";
	//1.字符串的赋值操作
	int result = strassign(str, ch);
	cout << "打印赋值结果:" << result <<"该字符串str的长度是:"<<str.length<< endl;
	for (int i = 0; i < str.length;i++) {
		cout << str.ch[i];
	}
	cout << endl;

	//2.取出该字符串的长度
	int str_len = strlength(str);
	cout << "串的长度是:" << str_len << endl;

	//3.比较字符串的大小
	char ch2[] = "abcdef";
	Str str2;
	strassign(str2, ch2);
	int reCom = strcompare(str,str2);//比较str和str2的大小关系
	if (reCom<0) {
		cout << "第二个串大于第一个串" << endl;
	}
	else if(reCom==0) {
		cout << "两个串是相等的串" << endl;
	}
	else {
		cout << "第一个串大于第二个串" << endl;
	}
	//4.串的连接操作
	Str newstr;
	concat(newstr, str, str2);
	cout << "连接str和str2" << endl;
	for (int i = 0; i < newstr.length; i++) {
		cout << newstr.ch[i];
	}
	cout << endl;

	//5.求子串的操作
	Str substr;
	subStr(substr, newstr, 1, 3);
	//截取
	cout << "newstr中从1位置截取len为3" << endl;
	for (int i = 0; i < substr.length; i++) {
		cout << substr.ch[i];
	}
	cout << endl;
	//防止程序一闪而过
	int k;
	cin >> k;
    return 0;
}


猜你喜欢

转载自blog.csdn.net/Suyebiubiu/article/details/80993735