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

📄 class str.cpp

📁 完善字符串类的运算符功能 1、 利用运算符重载完善字符串类的运算符功能。如: 串+整数;整数+串;浮点数+串;串-串;串/串 等; 2、 编制一个带菜单的小系统
💻 CPP
字号:
// class str.cpp : Defines the entry point for the console application.
//

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

class ex_string
{	
private:
	char* s;
public:
	
	ex_string(char* string)
	{
		s=string;
	}
	
	
	//字符串对整型操作 ×3
	friend int operator +(ex_string x,int y)
	{	string str;
				str=x.s;
				return str.size()+y;
	}
/*	
	friend int operator -(ex_string x,int y)
	{	
		string str;
		int i=0;
		i=str.size();
				str=x.s;
				return y;
	}
*/	
	int friend operator - (ex_string a)
	{

		return 0;
	}
	friend int operator *(ex_string x,int y)
	{	string str;
				str=x.s;
				return str.size()*y;
	}
	
	friend int operator /(ex_string x,int y)
	{	string str;
				str=x.s;
				return str.size()/y;
	}
	
	//字符串对字符串操作  ×3
	friend int operator +(ex_string x,ex_string y)
	{
		string str1;
		string str2;
		str1=x.s;
		str2=y.s;
		return str1.size()+str2.size();
	}
	
	/*friend int operator -(ex_string x,ex_string y)
	{
	string str1;
	string str2;
				str1=x.s;
				str2=y.s;
				return str1.size()-str2.size();
	}*/
	
	friend int operator *(ex_string x,ex_string y)
	{
		string str1;
		string str2;
		str1=x.s;
		str2=y.s;
		return str1.size()*str2.size();
	}
	
	friend int operator /(ex_string x,ex_string y)
	{
		string str1;
		string str2;
		str1=x.s;
		str2=y.s;
		return str1.size()/str2.size();
	}
	
	//字符串对字符串赋值 ×1
	friend void operator ==(ex_string x,ex_string y)
	{	
		string str1;
		string str2;
		str1=x.s;
		str2=y.s;
		cout<<str1.assign(str2)<<endl<<endl<<endl;
		
	}
	
	//字符串对字符串连接 ×1
	friend void operator +=(ex_string x,ex_string y)
	{	
		string str1;
		string str2;
		str1=x.s;
		str2=y.s;
		cout<<str1.append(str2)<<endl;
		
		
	}
	//字符串对字符串交换  ×1
	friend void operator *=(ex_string x,ex_string y)
	{	
		
		string str1;
		string str2;
		str1=x.s;
		str2=y.s;
		cout<<"s1:"<<str1<<endl;
		cout<<"s2:"<<str2<<endl;
		str1.swap(str2);
		cout<<"now use swap function"<<endl;
		cout<<"s1:"<<str1<<endl;
		cout<<"s2:"<<str2<<endl;
		
		
	}
	
	
	
	
};

int main(int argc, char* argv[])
{	
	char* s1=new char[100];
	char* s2=new char[100];
	//int t;
	ex_string a(s1);
	ex_string b(s2);
	
	
	cout<<"input str1"<<endl;
	cin>>s1;
	/*cout<<"input int t"<<endl;
	cin>>t;
	cout<<endl<<a+t<<endl;
						//cout<<a-t<<endl;
	cout<<a*t<<endl;
	cout<<a/t<<endl<<endl<<endl<<endl<<endl;*/
	cout<<"now input str2"<<endl;
	cin>>s2;
	/*cout<<a+b<<endl;
					//cout<<a-b<<endl;
	cout<<a*b<<endl;
	cout<<a/b<<endl;*/
	a+=b;
	a*=b;
	
	
	
return 0;
}

⌨️ 快捷键说明

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