?? str.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;
str=x.s;
return str.size() - y;
}
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 + -