⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 strings.cpp

📁 C++语言对字符串对象进行各种操作的子程序
💻 CPP
字号:
// strings.cpp : Defines the entry point for the console application.


#include "stdafx.h"
#include "iostream.h"
#include "string.h"
class string
{
	char *ptr;
	int len;
public:
	string(char *s)
	{
	    ptr=new char[strlen(s)+1];
		strcpy(ptr,s);
		len=strlen(ptr);
	}
	string()
	{
		ptr=NULL;
		len=0;
	}
	~string()
	{
	    delete[] ptr;
	}
    void show()
	{
		cout<<"--The string is :"<<ptr<<" --The length is:"<<len<<endl;
	}
	string &operator=(const string &);
	friend string &operator+(string &a,string &b);
	friend string &max(string &a,string &b);
	friend string &min(string &a,string &b);
    friend string &up_sort(string &a); 
    friend string &down_sort(string &a);
    friend void count(string &a);
};

string &string::operator=(const string &a)
	{
		if (this==&a) return *this;
        delete[] ptr;
		ptr=new char[strlen(a.ptr)+1];
		strcpy(ptr,a.ptr);
		len=a.len;
		return *this;
	}
string &operator+ (string &a,string &b)
	{
	    extern string temp;
	    temp.ptr=new char[strlen(a.ptr)+strlen(b.ptr)+1];
		strcpy(temp.ptr,a.ptr);
		strcat(temp.ptr,b.ptr);		
    	temp.len=a.len+b.len;        
		return temp;
	}
string &max(string &a,string &b)
{
    extern string temp;
	cout<<"The max:"<<endl;
	if(strcmp(a.ptr,b.ptr)==1)
	{strcpy(temp.ptr,a.ptr);
	 temp.len=a.len;
	 return temp;
	}
	else if(strcmp(a.ptr,b.ptr)==-1) 
	{
		strcpy(temp.ptr,b.ptr);
		temp.len=b.len;
	return temp;
	}
	else 
	{
		cout<<"The two strings are epual!"<<endl;
        strcpy(temp.ptr,a.ptr);
	    temp.len=a.len;
	    return temp;
	}
}
string &min(string &a,string &b)
{
    extern string temp;
	cout<<"The min:"<<endl;
	if(strcmp(a.ptr,b.ptr)==-1)
	{strcpy(temp.ptr,a.ptr);
	 temp.len=a.len;
	 return temp;
	}
	else if(strcmp(a.ptr,b.ptr)==1) 
	{
		strcpy(temp.ptr,b.ptr);
		temp.len=b.len;
	return temp;
	}
	else 
	{
		cout<<"The two strings are epual!"<<endl;
        strcpy(temp.ptr,a.ptr);
	    temp.len=a.len;
	    return temp;
	}
}
string &up_sort(string &a)
{
	int i,j,k;
	char q;
	extern string temp;
	temp=a;
	for(i=0;i<temp.len;i++)
	{
		k=i;
	    for(j=i+1;j<temp.len;j++)
			if(temp.ptr[j]<temp.ptr[k])
				k=j;
        if(k!=i)
		{
			q=temp.ptr[i];
			temp.ptr[i]=temp.ptr[k];
			temp.ptr[k]=q;
		}
	}
	cout<<"The sorted string is:"<<temp.ptr<<endl;
	return temp;
}
string &down_sort(string &a)
{
	int i,j,k;
	char q;
	extern string temp;
	temp=a;
	for(i=0;i<temp.len;i++)
	{
		k=i;
	    for(j=i+1;j<temp.len;j++)
			if(temp.ptr[j]>temp.ptr[k])
				k=j;
        if(k!=i)
		{
			q=temp.ptr[i];
			temp.ptr[i]=temp.ptr[k];
			temp.ptr[k]=q;
		}
	}
	cout<<"The sorted string is:"<<temp.ptr<<endl;
	return temp;
}
void count(string &a)
{
    char *p1;
	char y[100];
    int j,t,done;
	int cou[100];
    for (j=0;j<a.len;j++)
	{
		cou[j]=0;
		y[j]=NULL;
	}
	p1=a.ptr;t=1;
	y[0]=p1[0];
	for (;*p1!='\0';p1++)
	{
        done=1;
		for(j=0;j<t;j++)
		{
			if(*p1==y[j])
			{
				cou[j]++;
                done=0;
			}
		}
		if(done)
		{
			y[t]=*p1;
			cou[t]++;
			t++;
		}
	}
	cout<<"The result is:"<<endl;
	for (j=0;j<t;j++)
	{
		cout<<y[j]<<'-'<<cou[j]<<endl;
	}
}
string temp;
void main()
{
    char a1[]="qrurttiuo",a2[]="bacjfsahwwruucab",a3[]="toiovhdfk";
	string b1(a1),b2(a2),b3(a3),b4;
    b1=b2;
	b1.show();
    b4=b2+b3;
    b4.show();
	b4=max(b2,b3);
	b4.show();
	b4=min(b2,b3);
	b4.show();
    b4=up_sort(b2);
	b4.show();
    b4=down_sort(b2);
	b4.show();
    count(b2);    
}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -