//CTString.h文件
#ifndef CTString_H_INCLUDED
#define CTString_H_INCLUDED
#include <iostream>
using namespace std;
class CTString
{
public:
CTString();
CTString(const char * ptr_s);
CTString(const CTString & refer_str);//拷贝构造函数
int mylength();//计算字符串的长度
~CTString();
friend istream & operator>>(istream & is, CTString & refer_str);
friend ostream & operator<<(ostream & os, const CTString & refer_str);
CTString & operator=(const CTString & refer_str);
void mygetline();//对输入按行读取
char & operator[](int n);
friend CTString operator+(const CTString & refer_str_1, const CTString & refer_str_2);
CTString & operator+=(const CTString & refer_str);
friend bool operator==(const CTString & refer_str_1, const CTString & refer_str_2);
friend bool operator!=(const CTString & refer_str_1, const CTString & refer_str_2);
CTString mysubstr(int pos, int posn);//计算一个字符串的子串,pos为起始位置,从0开始,posn为希望求得的子串的长度
friend void mystrswap(CTString & str1, CTString & str2);//交换两个字符串
int myfind(char ch, int pos = 0);//从pos开始查找字符ch在当前字符串中的位置,pos从0开始,返回值也从0开始
CTString & myerase(int pos, int posn);//从pos开始删除当前字符串中的字符,pos从0开始,posn表示希望删除的字符数量,返回删除后字符串的引用,也即当前字符串
CTString & myinsert(int pos, const CTString & refer_str);//将refer_str代表的字符串插入到当前字符串中,pos表示插入的位置,0 <= pos <= mystrlen((*this).ptr_s)
CTString & myinsert(int pos, const CTString & refer_str, int posn);//将refer_str代表的字符串插入到当前字符串中,pos表示插入的位置,0 <= pos <= mystrlen((*this).ptr_s),0 <= posn <= mystrlen(refer_str.ptr_s)
private:
char * ptr_s;
};
int mystrlen(const char * ptr_s);//计算字符串的长度
void mystrcpy(char * ptr_s_1, const char * ptr_s_2);//将ptr_s_2指向的内容复制到ptr_s_1所指向的空间,但前提后者空间足够
int mystrcmp(const char * ptr_s_1, const char * ptr_s_2);//比较ptr_s_1指向的内容和ptr_s_2指向的内容,若两者相同返回0,前者大返回1,前者小返回-1
void mystrcat(char * ptr_s_1, const char * ptr_s_2);//将ptr_s_2指向的内容连接到ptr_s_1指向的内容的后面,但需保证ptr_s_1所指向的空间足够,并且ptr_s_1所指向的空间必须经过初始化,即包含有意义的字符串结束符\0
#endif // CTString_H_INCLUDED
//为了重载>>和<<这两个运算符并实现mygetline()函数,还是得在CTString.h中包含iostream。
//CTString.cpp文件
#include "CTString.h"
CTString::CTString()
{
ptr_s = new char[1];
*ptr_s = '\0';
}
CTString::CTString(const char * ptr_s)
{
this->ptr_s = new char[mystrlen(ptr_s)+1];
mystrcpy(this->ptr_s, ptr_s);
}
CTString::CTString(const CTString & refer_str)
{
ptr_s = new char[mystrlen(refer_str.ptr_s)+1];
mystrcpy(ptr_s, refer_str.ptr_s);
}
int CTString::mylength()
{
return mystrlen(ptr_s);
}
CTString::~CTString()
{
delete [] ptr_s;
}
istream & operator>>(istream & is, CTString & refer_str)
{
delete [] refer_str.ptr_s;
char ch_tmp[1000];// 申请一块在一般情况下足够大的内存
is >> ch_tmp;
refer_str.ptr_s = new char[mystrlen(ch_tmp)+1];
mystrcpy(refer_str.ptr_s, ch_tmp);
return is;
}
ostream & operator<<(ostream & os, const CTString & refer_str)
{
os << refer_str.ptr_s;
return os;
}
CTString & CTString::operator=(const CTString & refer_str)
{
if(this == &refer_str)
return *this;
else
{
delete [] ptr_s;
ptr_s = new char[mystrlen(refer_str.ptr_s)+1];
mystrcpy(ptr_s, refer_str.ptr_s);
return *this;
}
}
void CTString::mygetline()
{
delete [] ptr_s;
char ch_tmp[1000];// 申请一块在一般情况下足够大的内存
cin.getline(ch_tmp, 1000);
ptr_s = new char[mystrlen(ch_tmp)+1];
mystrcpy(ptr_s, ch_tmp);
}
char & CTString::operator[](int n)
{
return *(ptr_s+n);
}
CTString operator+(const CTString & refer_str_1, const CTString & refer_str_2)
{
CTString str_tmp;
str_tmp.ptr_s = new char[mystrlen(refer_str_1.ptr_s)+mystrlen(refer_str_2.ptr_s)+1];
mystrcpy(str_tmp.ptr_s, refer_str_1.ptr_s);
mystrcpy(str_tmp.ptr_s+mystrlen(refer_str_1.ptr_s), refer_str_2.ptr_s);
return str_tmp;
}
CTString & CTString::operator+=(const CTString & refer_str)
{
CTString str_tmp;
str_tmp.ptr_s = new char[mystrlen(ptr_s)+mystrlen(refer_str.ptr_s)+1];
mystrcpy(str_tmp.ptr_s, ptr_s);
mystrcpy(str_tmp.ptr_s+mystrlen(ptr_s), refer_str.ptr_s);
delete [] ptr_s;
ptr_s = new char[mystrlen(str_tmp.ptr_s)+1];
mystrcpy(ptr_s, str_tmp.ptr_s);
return *this;
}
bool operator==(const CTString & refer_str_1, const CTString & refer_str_2)
{
int ret = mystrcmp(refer_str_1.ptr_s, refer_str_2.ptr_s);
if(ret == 0)
return true;
else
return false;
}
bool operator!=(const CTString & refer_str_1, const CTString & refer_str_2)
{
int ret = mystrcmp(refer_str_1.ptr_s, refer_str_2.ptr_s);
if(ret == 0)
return false;
else
return true;
}
CTString CTString::mysubstr(int pos, int posn)
{
CTString str_tmp;
delete [] str_tmp.ptr_s;
str_tmp.ptr_s = new char[posn+1];
for(int i = 0;i < posn;i++)
*(str_tmp.ptr_s+i) = *(ptr_s+pos+i);
*(str_tmp.ptr_s+posn) = '\0';
return str_tmp;
}
void mystrswap(CTString & str1, CTString & str2)
{
char * ptr_s;
ptr_s = str1.ptr_s;
str1.ptr_s = str2.ptr_s;
str2.ptr_s = ptr_s;
}
int CTString::myfind(char ch, int pos)
{
int str_length = mystrlen(ptr_s);
int i;
for(i = 0;i < str_length-pos;i++)
if(*(ptr_s+pos+i) == ch)
return pos+i;
return -1;
}
CTString & CTString::myerase(int pos, int posn)
{
CTString str_tmp;
delete [] str_tmp.ptr_s;
int str_length = mystrlen(ptr_s);
str_tmp.ptr_s = new char[str_length-posn+1];
for(int i = 0;i < pos;i++)
*(str_tmp.ptr_s+i) = *(ptr_s+i);
for(int i = pos+posn;i < str_length;i++)
*(str_tmp.ptr_s+i-posn) = *(ptr_s+i);
*(str_tmp.ptr_s+str_length-posn) = '\0';
char * ptr_s_tmp;
ptr_s_tmp = str_tmp.ptr_s;
str_tmp.ptr_s = ptr_s;
ptr_s = ptr_s_tmp;
return *this;
}
CTString & CTString::myinsert(int pos, const CTString & refer_str)
{
CTString str_tmp;
delete [] str_tmp.ptr_s;
int str_length_1 = mystrlen(ptr_s);
int str_length_2 = mystrlen(refer_str.ptr_s);
str_tmp.ptr_s = new char[str_length_1+str_length_2+1];
int i;
for(i = 0;i < pos;i++)
*(str_tmp.ptr_s+i) = *(ptr_s+i);
mystrcpy(str_tmp.ptr_s+i, refer_str.ptr_s);
while(i < str_length_1)
{
*(str_tmp.ptr_s+str_length_2+i) = *(ptr_s+i);
i++;
}
*(str_tmp.ptr_s+str_length_1+str_length_2) = '\0';
char * ptr_s_tmp;
ptr_s_tmp = str_tmp.ptr_s;
str_tmp.ptr_s = ptr_s;
ptr_s = ptr_s_tmp;
return *this;
}
CTString & CTString::myinsert(int pos, const CTString & refer_str, int posn)
{
CTString str_tmp;
delete [] str_tmp.ptr_s;
int str_length = mystrlen(ptr_s);
str_tmp.ptr_s = new char[str_length+posn+1];
int i;
for(i = 0;i < pos;i++)
*(str_tmp.ptr_s+i) = *(ptr_s+i);
for(int j = 0;j < posn;j++)
*(str_tmp.ptr_s+pos+j) = *(refer_str.ptr_s+j);
while(i < str_length)
{
*(str_tmp.ptr_s+posn+i) = *(ptr_s+i);
i++;
}
*(str_tmp.ptr_s+str_length+posn) = '\0';
char * ptr_s_tmp;
ptr_s_tmp = str_tmp.ptr_s;
str_tmp.ptr_s = ptr_s;
ptr_s = ptr_s_tmp;
return *this;
}
int mystrlen(const char * ptr_s)
{
int length = 0;
while(*(ptr_s++) != '\0')
length++;
return length;
}
void mystrcpy(char * ptr_s_1, const char * ptr_s_2)
{
while(*ptr_s_2 != '\0')
{
*ptr_s_1 = *ptr_s_2;
ptr_s_1++;
ptr_s_2++;
}
*ptr_s_1 = '\0';
}
int mystrcmp(const char * ptr_s_1, const char * ptr_s_2)
{
int length_1 = mystrlen(ptr_s_1);
int length_2 = mystrlen(ptr_s_2);
int length_shorter = length_1 > length_2 ? length_2 : length_1;
int i;
for(i = 0;i < length_shorter;i++)
if(*(ptr_s_1+i) != *(ptr_s_2+i))
break;
if(i == length_shorter)
{
if(length_1 == length_2)
return 0;
else if(length_1 > length_2)
return 1;
else
return -1;
}
else
{
if(*(ptr_s_1+i) > *(ptr_s_2+i))
return 1;
else
return -1;
}
}
void mystrcat(char * ptr_s_1, const char * ptr_s_2)
{
ptr_s_1 += mystrlen(ptr_s_1);
while(*ptr_s_2 != '\0')
{
*ptr_s_1 = *ptr_s_2;
ptr_s_1++;
ptr_s_2++;
}
*ptr_s_1 = '\0';
}
#include<iostream>
#include<string>
using namespace std;
class CEmployee
{
public:
virtual void Show_WI() const = 0; //显示信息
virtual double CalSalaryMonth() const=0;//计算月薪
protected:
int m_Id; //职工编号
string m_Name; //姓名
int m_DeptId; //职工所在部门名称编号
int m_Year; //入职年份
int m_BasicSal; //基础工资
};
class CManager : public CEmployee
{
public:
CManager (int Id, string Name, int dId,int year,int sal);
virtual ~CManager();
virtual void Show_WI() const;
virtual double CalSalaryMonth() const;
};
CManager::CManager(int Id, string Name, int dId,int year,int sal)
{
m_Id = Id; //职工编号
m_Name = Name; //姓名
m_DeptId = dId; //职工所在部门名称编号
m_Year = year; //入职年份
m_BasicSal = sal; //基础工资
}
CManager::~CManager()
{
}
void CManager::Show_WI() const
{
cout<<"职工编号"<<m_Id<<endl;
cout<<"职工姓名"<<m_Name<<endl;
cout<<"部门编号"<<m_DeptId<<endl;
cout<<"入职年份"<<m_Year<<endl;
cout<<"基础工资"<<m_BasicSal<<endl;
}
double CManager::CalSalaryMonth() const
{
return 30*10*15+m_BasicSal;
}
int main()
{
CEmployee* employee = new CManager(1,"张三",100,2021,3000);
employee->Show_WI();
cout<<"月薪"<<CEmployee->CalSalaryMonth()<<endl;
}
第二题要简单一些
第一题考察字符串的各种功能,难度更大
您好,我是有问必答小助手,您的问题已经有小伙伴解答了,您看下是否解决,可以追评进行沟通哦~
如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~
ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632