这是程序的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;
}
最后这样报错了连编译都没过
已经修正了且正常输出:
#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;
}
救救孩子
修改后:
#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 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大模型与博主波罗歌共同编写:
在你的代码中有不少问题,主要如下:
正确的实现方式是,给成员变量arr申请一段与传入的char数组等长的内存空间,然后按索引将传入的字符逐一复制到arr上,并在最后一个位置添加'\0'表示字符串结束。
另一方面,当传入的数字字符串非法时,你的实现方式没有做出应有的异常处理,比如"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;
}
在实际操作时,我们可以针对不同的异常类型定义不同的异常类,从而更好地区分和处理它们,有助于代码的组织和维护。
如果我的回答解决了您的问题,请采纳!