有一道不会的问题,想让大家看看能不能给点意见

img

img

这是程序的main函数


int main() {
    char a[999], b[999];
    int index;
    while(cin>>a>>b>>index){
        MyString s1(a), s2(b), s3;
        int a_int = s1;
        cout << "a = " << a_int << endl;
        double b_double = s2;
        cout << "b = " << b_double << endl;

        if (s1 != s2){
            cout << "a is not equal to b" << endl;
        }
        else{
            cout << "a is equal to b" << endl;
        }

        s3 = s1 + s2;
        if(s3 == s1 + s2){
            cout << "a + b = "<< s3 << endl;
        }       

        try {
            cout << "a[" << index << "] = " << s1[index] << endl;
        }
        catch (const char* msg) {
            cout << msg << endl;
        }   
        try {
            cout << "b[" << index << "] = " << s2[index] << endl;
        }
        catch (const char* msg) {
            cout << msg << endl;
        }       
    }

    return 0;
}

没码出来啊,真的不会了,

#include<iostream>
using namespace std;
class MyString
{
private:
    char*arr;
public:
    MyString(char*x)
    {
        arr = x;
    }
    MyString()
    {

    }
    operator int()
    {
        int a = 0, i = 0;
        while (arr[i] != '\0')
        {
            a += arr[i] - 16;
            a *= 10;
            i++;
        }
        return a;
    }
    operator double()
    {
        double a = 0;
        double b = 0;
        int i = 0, j;
        while (arr[i] != '.'&&arr[i] != '\0')
        {
            a += arr[i] - 16;
            a *= 10;
            i++;
        }
        if (arr[i] == '\0')
            return a;
        if (arr[i] == '.')
        {
            i++;
            j = i;
            while (arr[j] != '\0')
            {
                j++;
            }
            j--;
            for (; j >= i; j--)
            {
                b += arr[j] - 16;
                b /= 10;
            }
            return a + b;
        }
    }
    MyString&operator =(MyString x)
    {
        arr = x.arr;
        return *this;

    }
    MyString operator+(MyString x)
    {
        int i, j;
        i = strlen(arr);
        j = strlen(x.arr);
        for (int k = 0; k < j; k++)
        {
            arr[i] = x.arr[k];
            i++;
        }
        return *this;
    }
    friend bool operator==(MyString&x, MyString&y);
    friend bool operator!=(MyString&x, MyString&y);
    char&operator[](int n)
    {
        int i = strlen(arr);
        if (n > i)
        {
            throw "out of range";
        }
        else
            return arr[n];

    
        return arr[i];
        
    }
    friend ostream&operator<<(ostream&output, MyString&x);
};
bool operator==(MyString&x, MyString&y)
{
    int i = strlen(x.arr);
    int j = strlen(y.arr);
    if (i == j)
    {
        for (int k = 0; k < i; k++)
        {
            if (x.arr[k] == y.arr[k])
                continue;
            else
                return false;
        }
        return true;
    }
    else
        return false;
}
bool operator!=(MyString&x, MyString&y)
{
    int i = strlen(x.arr);
    int j = strlen(y.arr);
    if (i != j)
        return true;
    else
    {
        for (int k = 0; k < i; k++)
        {
            if (x.arr[k] != y.arr[k])
                return true;
            else
            {
                continue;
            }
        }
        return false;
    }
}
ostream&operator<<(ostream&output, MyString&x)
{
    output << x.arr;
    return output;
}
int main() {
    char a[999], b[999];
    int index;
    while (cin >> a >> b >> index) {
        MyString s1(a), s2(b), s3;
        int a_int = s1;
        cout << "a = " << a_int << endl;
        double b_double = s2;
        cout << "b = " << b_double << endl;

        if (s1 != s2) {
            cout << "a is not equal to b" << endl;
        }
        else {
            cout << "a is equal to b" << endl;
        }

        s3 = s1 + s2;
        if (s3 == s1 + s2) {
            cout << "a + b = " << s3 << endl;
        }

        try {
            cout << "a[" << index << "] = " << s1[index] << endl;
        }
        catch (const char* msg) {
            cout << msg << endl;
        }
        try {
            cout << "b[" << index << "] = " << s2[index] << endl;
        }
        catch (const char* msg) {
            cout << msg << endl;
        }
    }

    return 0;
}



最后这样报错了连编译都没过

img

img


有意见吗,哭死。

已经修正了且正常输出:

img

#include <iostream>
#include <cstring>
using namespace std;

class MyString {
public:
    MyString(const char* str = "") {
        data = new char[strlen(str) + 1];
        strcpy(data, str);
    }
    ~MyString() {
        delete[] data;
    }
    operator int() const {
        return atoi(data);
    }
    operator double() const {
        return atof(data);
    }
    MyString& operator=(const MyString& other) {
        if (this != &other) {
            char* temp = new char[strlen(other.data) + 1];
            strcpy(temp, other.data);
            delete[] data;
            data = temp;
        }
        return *this;
    }
    MyString operator+(const MyString& other) const {
        MyString result;
        result.data = new char[strlen(data) + strlen(other.data) + 1];
        strcpy(result.data, data);
        strcat(result.data, other.data);
        return result;
    }
    bool operator==(const MyString& other) const {
        return strcmp(data, other.data) == 0;
    }
    bool operator!=(const MyString& other) const {
        return !(*this == other);
    }
    char& operator[](int index) {
        if (index < 0 || index >= strlen(data)) {
            throw "Index out of range";
        }
        return data[index];
    }
    friend ostream& operator<<(ostream& os, const MyString& str) {
        os << str.data;
        return os;
    }
private:
    char* data;
};

int main() {
    char a[100], b[100];
    int index;
    while(cin >> a >> b >> index) {
        MyString str1(a), str2(b), str3;
        int a_int = str1;
        cout << "a = " << a_int << endl;
        double b_double = str2;
        cout << "b = " << b_double << endl;
        if (str1 != str2) {
            cout << "a is not equal to b" << endl;
        } else {
            cout << "a is equal to b" << endl;
        }
        str3 = str1 + str2;
        if (str3 == str1 + str2) {
            cout << "a + b = " << str3 << endl;
        }
        try {
            cout << "a[" << index << "] = " << str1[index] << endl;
        }
        catch (const char* msg) {
            cout << msg << endl;
        }
        try {
            cout << "b[" << index << "] = " << str2[index] << endl;
        }
        catch (const char* msg) {
            cout << msg << endl;
        }
    }
    return 0;
}

救救孩子

修改后:

img

#include<iostream>
#include<cstring>
#include<math.h>
using namespace std;
class MyString
{
private:
    char* arr;
public:
    MyString(char* x = nullptr)
    {
        if (x == nullptr)
        {
            arr = new char[1];
            arr[0] = '\0';
        }
        else
        {
            int len = strlen(x);
            arr = new char[len + 1];
            strcpy(arr, x);
        }
    }
    MyString(const MyString& other)
    {
        int len = strlen(other.arr);
        arr = new char[len + 1];
        strcpy(arr, other.arr);
    }
    ~MyString() { delete[]arr; }
    operator int() const
    {
        int res = 0;
        for (int i = 0; i < strlen(arr); i++)
        {
            res = res * 10 + arr[i] - '0';
        }
        return res;
    }
    operator double() const
    {
        double res = 0.0;
        bool flag = false;
        int i = 0;
        while (i < strlen(arr) && (isdigit(arr[i]) || arr[i] == '.'))
        {
            if (arr[i] == '.')
                flag = true;
            else if (!flag)
                res = res * 10 + arr[i] - '0';
            else
            {
                res = res + (arr[i] - '0') / pow(10, i - strlen(arr) + strlen(arr + i) + 1);
            }
            i++;
        }
        return res;
    }
    MyString& operator=(const MyString& other)
    {
        if (this != &other)
        {
            delete[]arr;
            int len = strlen(other.arr);
            arr = new char[len + 1];
            strcpy(arr, other.arr);
        }
        return *this;
    }
    MyString operator+(const MyString& other)
    {
        int len1 = strlen(arr);
        int len2 = strlen(other.arr);
        char* new_arr = new char[len1 + len2 + 1];
        strcpy(new_arr, arr);
        strcat(new_arr, other.arr);
        return MyString(new_arr);
    }
    bool operator==(const MyString& other) const
    {
        return strcmp(arr, other.arr) == 0;
    }
    bool operator!=(const MyString& other) const
    {
        return strcmp(arr, other.arr) != 0;
    }
    char& operator [](int index)
    {
        if (index < 0 || index >= strlen(arr))
        {
            throw "out of range";
        }
        return arr[index];
    }
    friend ostream& operator<< (ostream& os, const MyString& str)
    {
        os << str.arr;
        return os;
    }
};
int main() {
    char a[999], b[999];
    int index;
    while (cin >> a >> b >> index) {
        MyString s1(a), s2(b), s3;
        int a_int = s1;
        cout << "a=" << a_int << endl;
        double b_double = s2;
        cout << "b=" << b_double << endl;

        if (s1 != s2) {
            cout << "a is not equal to b" << endl;
        }
        else {
            cout << "a is equal to b" << endl;
        }

        s3 = s1 + s2;
        if (s3 == s1 + s2) {
            cout << "a+b=" << s3 << endl;
        }

        try {
            cout << "a[" << index << "]=" << s1[index] << endl;
        }
        catch (const char* msg) {
            cout << msg << endl;
        }
        try {
            cout << "b[" << index << "]=" << s2[index] << endl;
        }
        catch (const char* msg) {
            cout << msg << endl;
        }
    }

    return 0;
}


该回答引用ChatGPT
根据您提供的信息,可以发现程序中存在以下问题:

1. 在 MyString 类中,字符转换为 int 类型的方法中,进行加减操作时,使用了字符的 ASCII 码减去了 16,这其中的原因不太清楚,可能会导致得到不正确的值。

2. 在 MyString 类中,字符转换为 double 类型的方法中,对于整数部分的转换似乎也存在类似的问题。整数部分中的每个字符也是直接转换为数字,然后进行运算。

3. 在 MyString 类中,字符转换为 double 类型的方法中,对于小数部分的转换,使用了除以 10 的方式,这也有可能会得到不正确的值。应该使用乘以 0.1 或除以 1, 10, 100, ... 等方式来实现小数的转换。

4. MyString 类中的加法运算符重载函数,没有考虑在数组 a 后面添加数组 b 的时候可能会越界的情况。也没有考虑如果其中一个字符串为空字符串时会出现什么情况。

5. 在主函数中,读入的数据应该先判断是否合法,再进行操作。

6. 在主函数中,对于 MyString 类的对象的赋值操作,应该采用指针的方式进行赋值,而不是直接使用赋值语句。

7. 在主函数中,对于 MyString 类的对象的加法操作,应该采用创建新的对象的方式,而不是直接在原有的对象上进行操作。否则,后面的输出 s3 的值也不正确。

根据上述问题,可以对程序进行如下调整:

 c++
#include <iostream>
#include <cstring>
using namespace std;

class MyString {
private:
char *arr;

public:
MyString(char *x) {
int len = strlen(x);
arr = new char[len + 1];
strcpy(arr, x);
}

MyString() {
arr = nullptr;
}

operator int() {
int a = 0, i = 0;
while (arr[i] != '\0') {
if (arr[i] >= '0' && arr[i] <= '9') {
a = a * 10 + arr[i] - '0';
}
i++;
}
return a;
}

operator double() {
double a = 0.0;
double b = 0.0;
int i = 0, j;
while (arr[i] != '.' && arr[i] != '\0') {
if (arr[i] >= '0' && arr[i] <= '9') {
a = a * 10 + arr[i] - '0';
}
i++;
}
if (arr[i] == '\0') {
return a;
}
if (arr[i] == '.') {
i++;
j = i;
while (arr[j] != '\0') {
j++;
}
j--;
for (; j >= i; j--) {
if (arr[j] >= '0' && arr[j] <= '9') {
b = b / 10 + (arr[j] - '0') / 10.0;
}
}
return a + b;
}
}

MyString &operator=(MyString &x) {
if (this != &x) {
delete[] arr;
int len = strlen(x.arr);
arr = new char[len + 1];
strcpy(arr, x.arr);
}
return *this;

}

MyString operator+(MyString x) {
MyString res;
if (this->arr != nullptr && x.arr != nullptr) {
int n = strlen(this->arr) + strlen(x.arr);
res.arr = new char[n + 1];
strcpy(res.arr, this->arr);
strcat(res.arr, x.arr);
} else if (this->arr != nullptr) {
res = *this;
} else if (x.arr != nullptr) {
res = x;
}
return res;
}

friend bool operator==(MyString &x, MyString &y);
friend bool operator!=(MyString &x, MyString &y);

char &operator[](int n) {
int i = strlen(arr);
if (n >= i) {
throw "out of range";
} else {
return arr[n];
}
}

friend ostream &operator<<(ostream &output, MyString &x);
};

bool operator==(MyString &x, MyString &y) {
int i = strlen(x.arr);
int j =

以下答案由GPT-3.5大模型与博主波罗歌共同编写:
在你的代码中有不少问题,主要如下:

  1. MyString类的构造函数中,你直接把传入的char数组指针赋值给了成员变量arr,这样做有潜在的风险。因为当传入的char数组失效时,MyString对象就不能保证其变量数据的正确性了。

正确的实现方式是,给成员变量arr申请一段与传入的char数组等长的内存空间,然后按索引将传入的字符逐一复制到arr上,并在最后一个位置添加'\0'表示字符串结束。

  1. int和double类型的转换运算符实现有点问题。事实上,你的转换方式有着严重的弊端,无法正确处理某些情况下的数字字符串。举个例子,在处理"12.345"的double类型转换时,你会得到12.3999999或者12.3500001等不正确的结果。这是因为浮点数的存储方式导致的精度误差(详见IEEE 754浮点数标准),所以我们不能一位一位地将数字字符转换为数字。正常的处理方式是,依据点号将数字字符串分为整数和小数两个部分,然后分别将它们转换为对应类型的数字。

另一方面,当传入的数字字符串非法时,你的实现方式没有做出应有的异常处理,比如"123xyz"。

建议参考下面的代码实现:

class MyString {
private:
    char* arr;
public:
    MyString(char* x) {
        int len = strlen(x);
        arr = new char[len + 1];  // 申请内存空间,+1 表示用于末尾的 \0 字符
        for (int i = 0; i < len; i++) {
            arr[i] = x[i];
        }
        arr[len] = '\0';  // 在最后一个位置赋值 \0
    }

    operator int() {
        int res = 0, i = 0;
        int sign = 1;
        if (arr[i] == '-') {  // 处理负数的情况
            sign = -1;
            i++;
        }
        while (arr[i] != '\0') {
            if (arr[i] < '0' || arr[i] > '9') {
                throw "invalid input for int()";
            }
            res = res * 10 + (arr[i] - '0');  // 将字符转换为数字
            i++;
        }
        return sign * res;
    }

    operator double() {
        double res_int = 0.0, res_frac = 0.0;
        int i = 0, j = 0;  // i 为整数部分读取位置,j 为小数部分读取位置
        int frac_len = 0;  // 小数部分长度
        int sign = 1;

        if (arr[i] == '-') {  // 处理负数的情况
            sign = -1;
            i++;
        }
        while (arr[i] != '.' && arr[i] != '\0') {
            if (arr[i] < '0' || arr[i] > '9') {
                throw "invalid input for double() (integer part)";
            }
            res_int = res_int * 10 + (arr[i] - '0');  // 将字符转换为数字
            i++;
        }

        if (arr[i] == '.') {
            i++;  // 跳过小数点
            while (arr[i] != '\0') {
                if (arr[i] < '0' || arr[i] > '9') {
                    throw "invalid input for double() (fraction part)";
                }
                res_frac = res_frac * 10 + (arr[i] - '0');  // 将字符转换为数字
                i++;
                frac_len++;
            }
        }

        // 计算小数部分的值
        if (frac_len != 0) {
            res_frac /= pow(10, frac_len);  // 先将小数部分除以 10 的 frac_len 次幂
        }

        return sign * (res_int + res_frac);
    }

    MyString& operator=(MyString& x) {
        arr = x.arr;
        return *this;
    }

    MyString operator+(MyString& x) {
        int len1 = strlen(arr), len2 = strlen(x.arr);
        char* new_arr = new char[len1 + len2 + 1];  // +1 表示用于末尾的 \0 字符
        for (int i = 0; i < len1; i++) {
            new_arr[i] = arr[i];
        }
        for (int i = 0; i < len2; i++) {
            new_arr[len1 + i] = x.arr[i];
        }
        new_arr[len1 + len2] = '\0';
        MyString res(new_arr);
        delete[] new_arr;  // 释放内存空间
        return res;
    }

    friend bool operator==(MyString& x, MyString& y);

    friend bool operator!=(MyString& x, MyString& y);

    char& operator[](int n) {
        if (n < 0 || n >= strlen(arr)) {
            throw "out of range";
        }
        return arr[n];
    }

    friend ostream& operator<<(ostream& output, MyString& x);
};

bool operator==(MyString& x, MyString& y) {
    if (strcmp(x.arr, y.arr) == 0) {  // 判断两个字符串是否相同的标准库函数
        return true;
    }
    else {
        return false;
    }
}

bool operator!=(MyString& x, MyString& y) {
    if (strcmp(x.arr, y.arr) != 0) {
        return true;
    }
    else {
        return false;
    }
}

ostream& operator<<(ostream& output, MyString& x) {
    output << x.arr;
    return output;
}

关于异常的处理,我们使用了C++标准库提供的异常机制,当不符合预期的错误发生时,比如说传入了非法的数字字符串或者访问了超过字符串长度的位置,我们可以通过throw语句抛出一个异常,并在调用处使用try-catch语句来处理它。例如:

try {
    cout << "a[" << index << "] = " << s1[index] << endl;
}
catch (const char* msg) {
    cout << msg << endl;
}

在实际操作时,我们可以针对不同的异常类型定义不同的异常类,从而更好地区分和处理它们,有助于代码的组织和维护。
如果我的回答解决了您的问题,请采纳!