Технологии
Решено
class MyString, Длина L 10, то удаляются все цифры; - вопрос №5319408
Нужно через работу с классом MyString написать код. Есть 2 попытки, обе не выдают ошибок, но и ничего не выводят в консоли.
Дополнение автора от ноябрь 11, 2023 г., 11:41:09
Есть 2 попытки.
Это первая:
#include <iostream>
#include <conio.h>
#include <fstream>
using namespace std;
class MyString {
private:
char* str;
int L;
public:
MyString(char*);
MyString();
void set_MyString(char*);
friend void show_MyString(MyString&);
MyString& operator=(MyString&);
void ofile();
};
MyString::MyString(char* s)
{
int size = strlen(s);
str = new char[size + 1];
str[0] = '\0';
L = size;
//strcpy_s(str, strlen(s), s);
}
MyString::MyString()
{
delete[] str;
}
void MyString::set_MyString(char* s)
{
for (unsigned int i = 0; i < strlen(s); i++)
{
str[i] = s[i];
str[i] = '\0';
}
}
void show_MyString(MyString& obj)
{
cout << obj.str << endl;
}
MyString& MyString:: operator=(MyString& obj1)
{
int j;
char* str_return = new char[obj1.L];
int size = strlen(str_return);
str_return = new char[size + 1];
int count = 0;
if (obj1.L > 10)
for (j = 0; j < obj1.L; j++)
if (*(str + j) < '0' || *(str + j) > '9')
{
count++;
//str_return[j] = '\0';
//strcpy_s(str, strlen(str), str_return);
}
if (obj1.L > 10)
{
cout << «Result string: „ << endl;
for (int i = 0; i < count; i++)
{
cout << *(str + i);
}
//str_return[j] = '\0';
//strcpy_s(str, strlen(str), str_return);
}
else
{
cout << “MyString: „ << str << endl;
//strcpy_s(str, strlen(str), str_return);
}
return *this;
}
void MyString::ofile()
{
ofstream file;
file.open(“work4.txt», ios::app);
file << str << endl;
file.close();
}
int main()
{
setlocale(LC_ALL, «Russian»);
char str[256];
cout << «Enter MyString and press Enter: „ << endl;
cin.getline(str, 256);
MyString s1(str);
MyString s2(str);
cout << “Result: „ << endl;
show_MyString(s1);
s1.ofile();
cout << “Result: „ << endl;
s2 = s1;
show_MyString(s2);
s2.ofile();
system(“pause»);
return 0;
}
Не ошибок, не вывода информации.
Не получается исправить
Вопрос задан анонимно ноябрь 10, 2023 г.
-
Всего ответов: 1
-
С++
#include <iostream>
#include <string>
#include <cctype>
class MyString {
public:
MyString(const std::string& str): str_(str) {}
void removeDigitsIfLong() {
if (str_.length() > 10) {
str_.erase(std::remove_if(str_.begin(), str_.end(), ::isdigit), str_.end());
}
}
void print() const {
std::cout << str_ << std::endl;
}
private:
std::string str_;
};
int main() {
MyString myStr(«Привет, мир1234567890»);
myStr.removeDigitsIfLong();
myStr.print();
return 0;
}Лучший ответ по мнению автора