Добрый вечер. Есть код на С++, необходимо реализовать дополнительное задание - пусть строка представляет предложение, в конце которого стоит точка, а слова отделены друг от друга пробелами. Упорядочить слова в предложении по возрастанию длин слов. Вывести на экран дисплея измененное предложение.
Дополнительное задание реализовано в отдельной программе. Возникли проблемы с ее добавлением в основной код.
основной код:
Код:
#include "stdafx.h"
#include <stdio.h>
#include <tchar.h>
#include <iostream>
#include <string>
using namespace std;
class String
{
public:
String();
String(std::string strIn);
String(int len);
String(const String &ob);
~String();
int lenght();
friend std::ostream &operator<<(std::ostream &out, String ob);
friend std::istream &operator >> (std::istream &in, String &ob);
// Конкатериция строк
String operator+(String &ob2);
// Выделение подстроки с позиции pos длинной len
String strstr(int pos, int len);
// Выделение подстроки с позиции pos
String strstr(int pos);
// Нихождение первого вхождения подстроки в строку, начиная с заданной позиции
int find(String str, int pos);
// Инвертирования содержимого строки
void invert();
//
void paste(char c, int pos);
// Замена символа в строке
void replace(int pos, char c);
// Вставка символа в строку
void insert(int pos, char c);
// Склеивание символа в строках
String operator+(char c);
// получить указатель на строку
char *get_str();
// Присваивание
void operator=(String &ob2);
// ПОльзовательская ф-ия
void permutation();
private:
char *_str;
int _len;
};
String::String()
{
this->_len = 0;
}
String::String(std::string strIn)
{
this->_len = strIn.length();
this->_str = new char[this->_len + 1];
for (int i = 0; i <= this->_len; i++)
this->_str[i] = strIn[i];
}
String::String(const String &ob)
{
this->_len = ob._len;
this->_str = new char[this->_len + 1]; //+1 нужен для нулевого символа
for (int i = 0; i <= this->_len; i++)
this->_str[i] = ob._str[i];
}
String::~String()
{
this->_len = 0;
delete this->_str;
}
String::String(int len)
{
this->_len = len;
this->_str = new char[this->_len];
for (int i = 0; i < this->_len; i++)
{
this->_str[i] = '\0';
}
}
int String::lenght()
{
return this->_len;
}
std::ostream &operator<<(std::ostream &out, String ob)
{
return out << ob._str;
}
std::istream &operator >> (std::istream &in, String &ob)
{
return in >> ob._str;
}
// конкатенация строк
String String::operator+(String& ob2)
{
String result;
result._len = this->lenght() + ob2.lenght();
result._str = new char[result._len + 1]; //+1 для того чтобы добавить символ конца строки
for (int i = 0; i < result._len; i++) { //+1 для того чтобы скопировать 0 из последней строки
if (i<this->lenght())
result._str[i] = this->_str[i];
else
result._str[i] = ob2._str[i - this->lenght()];
}
result._str[result._len] = '\0';
return result;
}
// Выделение подстроки с позиции pos длинной len
String String::strstr(int pos, int len)
{
String buf;
if (this->_len < pos + len)
{
return 1;
}
for (int i = pos; i < pos + len; i++)
{
buf = buf + this->_str[i];
}
return buf;
}
String String::strstr(int pos)
{
String buf;
if (this->_len < pos)
{
return 2;
}
for (int i = pos; i < this->_len; i++)
{
buf = buf + this->_str[i];
}
return buf;
}
// Нахождение первого вхождения подстроки в строку, начиная с заданной позиции
int String::find(String str, int pos)
{
int find_pos = -1;
int buf = 0;
char *c = &this->_str[pos];
int i = 0;
while (*c++ != '\0')
{
if (*c == str._str[buf])
{
buf++;
}
else
buf = 0;
if (buf == str._len)
{
find_pos = i - str._len;
return find_pos;
}
i++;
}
return -1;
}
// Инвертирования содержимого строки
void String::invert()
{
String buf;
for (int i = this->_len - 1; i >= 0; i--)
{
buf = buf + this->_str[i];
}
}
// Замена символа в строке
void String::replace(int pos, char c)
{
if (this->_len < pos)
return;
this->_str[pos] = c;
}
// Вставка символа в строку
void String::insert(int pos, char c)
{
String buf;
buf = *this;
this->_str[pos] = c;
for (int i = pos + 1; i < this->_len; i++)
{
this->_str[i] = buf._str[i - 1];
}
*this = *this + buf._str[buf._len];
}
void String::operator=(String& ob2)
{
if (this->lenght() != 0) {
this->_len = 0;
delete[] this->_str;
}
this->_len = ob2.lenght();
this->_str = new char[this->_len + 1]; //+1 для того чтобы добавить 0 в конец строки
for (int i = 0; i < ob2.lenght(); i++)
this->_str[i] = ob2._str[i];
this->_str[this->_len] = '\0';
}
String String::operator+(char c)
{
String temp;
temp._len = this->_len + 1;
temp._str = new char[temp._len + 1];
int i = 0;
for (i = 0; i < this->_len; i++)
temp._str[i] = this->_str[i];
temp._str[i++] = c;
temp._str[i] = '\0';
return temp;
}
char *String::get_str()
{
return this->_str;
}
// перестановка слов строки в порядке возрастания их длины
void String::permutation()
{
// сюда необходимо добавить доп. задание
}
int main()
{
String str("1 werw23 as31.");
str.permutation();
std::cout << str << std::endl;
system("pause");
return 0;
дополнительное задание:
#include <stdio.h>
#include <iostream>
#include <string>
using namespace std;
int main()
{
setlocale(0, "");
string _array[10] = {};
string temp, str = "1 werw23 as31.";
int j = 0;
//разделяем на слова
for (int i = 0; str[i] != '.'; i++)
{
if (str[i] != ' ')
{
_array[j] += str[i];
}
else
{
j++;
}
}
// сортировка
for (int i = 0; i < j+1; i++)
{
for (int k = 0; k < j; k++)
{
if (_array[k].length() > _array[k + 1].length())
{
temp = _array[k];
_array[k] = _array[k + 1];
_array[k + 1] = temp;
}
}
}
//вывод
for (int i = 0; i < j+1; i++)
{
cout << _array[i] << " ";
}
cout << ".";
system("pause");
return 0;
}