m0_74374730 2024-05-11 12:18 采纳率: 0%
浏览 9

String类定义PTA

自定义String类,重载下列运算符:
下标运算符[ ]、赋值运算符=、连接运算符+ 、输出运算符<<、输入运算符>>,能对两个字符串进行相应操作,在主函数中测试之。

函数接口定义:

        char& String::operator[](int i);
        String& String::operator=(const String& a) ;
        String operator+(const String& a,const String& b);    
        ostream& operator<<(ostream& out,const String& a);    
        istream& operator>>(istream& in,String& a);

char& operator[](int i) 实现返回String对象下标为i的字符;
String& operator=(const String& a) 实现对象间的赋值操作,注意防范String对象对自身的赋值;
String operator+(const String& a,const String& b) 实现两个String对象的连接操作,并返回结果;
ostream& operator<<(ostream& out,const String& a) 实现对String对象的输出操作;
istream& operator>>(istream& in,String& a) 实现对String对象的输入操作,字符串长度不超过1000,可包含空格。

裁判测试程序样例:
#include<iostream>
#include<cstring>
using namespace std;
class String {
    private:
        char *s;
        int len;
    public:
        String(char*t = NULL) {
            if(t == NULL) {    
                s = new char('\0');
                len = 1;
            } else {
                s = new char[len = strlen(t) + 1];
                strcpy(s,t);
            }
        }
        ~String() {delete []s;}
        char& operator[](int i);
        String& operator=(const String& a) ;
        friend String operator+(const String& a,const String& b);    
        friend ostream& operator<<(ostream& out,const String& a);    
        friend istream& operator>>(istream& in,String& a);
};
int main() {
    String a,b,c;
    int x; 
    while(cin >> a >> b >> x) {
        getchar();
        c=a;
        cout << "testing =: " << c<<endl;
        c=a+b;
        cout << "testing +: " << c<<endl;
        cout << "testing []: " << c[x]<<endl;
    }
    return 0;
}
/* 请在这里填写答案 */

输入样例:
在这里给出一组输入。例如:

I love C++ programming.
me too.
4
输出样例:
在这里给出相应的输出。例如:

testing =: I love C++ programming.
testing +: I love C++ programming.me too.
testing []: v

  • 写回答

3条回答 默认 最新

  • Kwan的解忧杂货铺 2024-05-11 12:18
    关注

    中午好🌞🌞🌞
    本答案参考ChatGPT-3.5

    问题分析:

    重载运算符是C++中很常见的操作,本题要求我们定义一个自定义的String类,重载下表运算符[ ]、赋值运算符=、连接运算符+ 、输出运算符<<、输入运算符>>。其涵盖的操作范围很广,需要针对每个运算符进行逐个操作实现。

    解决方案:

    • 下标运算符:

    • 声明并定义“& operator[](int i)”这个函数。

    • 将类中下标i的字符引用返回。

    • 赋值运算符:

    • 声明并定义“String& operator=(const String& a) ”这个函数。

    • 进行自身判断防止给自己赋值。

    • 删除旧的s数组,然后直接strcpy被赋值对象的s数组进行赋值操作。

    • 返回自身对象引用。

    • 连接运算符:

    • 声明并定义“String operator+(const String& a,const String& b)”这个函数。

    • 创建tmp对象并分配空间,将临时对象的s数组清空。

    • 复制两个对象s数组内容到tmp对象中, strcat(tmp.s,a.s)和strcat(tmp.s,b.s)。

    • 返回临时对象。

    • 输出运算符:

    • 在类中声明并定义“friend ostream& operator<<(ostream& out,const String& a) ”这个函数。

    • 使用cout输出s数组的内容出来。

    • 输入运算符:

    • 在类中声明并定义“friend istream& operator>>(istream& in,String& a)”这个函数。

    • 用 cin 输入string,由于string已经重载了=运算,所以可以直接a.s = stringname.c_str();完成赋值操作。

    参考代码:

    评论

报告相同问题?

问题事件

  • 创建了问题 5月11日

悬赏问题

  • ¥60 基于香农编码的图像压缩算法实现
  • ¥15 根据上述描述表示泥浆密度沿着管路的长度方向在不断变化,如何来表示泥浆密度随管路的变化,需要一个完整的程序,并且能够跑通。
  • ¥15 实现胶囊神经网络,最好可以远程操作
  • ¥15 matlabGUI绘制一个函数与其导数的图像
  • ¥20 大数据采集用Python爬取猫眼电影数据
  • ¥15 用freertos后NVIC里系统时钟部分报错
  • ¥15 后缀表达式的计算算法问题
  • ¥15 安装mid360驱动后,执行rviz_MID360.launch rviz不显示
  • ¥15 关于#人工智能#的问题:(2)设计一个GUI,允许语音和文本实现谣言的检测
  • ¥50 请教 麒麟系统挂载怎么安装