restructured
This commit is contained in:
parent
c495b7c47f
commit
66cefdfd63
204 changed files with 538 additions and 13662 deletions
22
seminar03_initialization/01_letter_case_switch/main.cpp
Normal file
22
seminar03_initialization/01_letter_case_switch/main.cpp
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
#include <iostream>
|
||||
#include <string>
|
||||
#include <cctype>
|
||||
|
||||
using namespace std;
|
||||
|
||||
string letter_case_switch(const string & str)
|
||||
{
|
||||
string result = str;
|
||||
if (str.size() == 0)
|
||||
return result;
|
||||
result[0] =
|
||||
isupper(result[0]) ? tolower(result[0]) : toupper(result[0]);
|
||||
return result;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
string meow;
|
||||
cin >> meow;
|
||||
cout << letter_case_switch(meow) << endl;
|
||||
}
|
||||
19
seminar03_initialization/01_letter_case_switch/main.cpp~
Normal file
19
seminar03_initialization/01_letter_case_switch/main.cpp~
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
#include <iostream>
|
||||
#include <string>
|
||||
#include <cctype>
|
||||
|
||||
using namespace std;
|
||||
|
||||
string letter_case_switch(const string& str) {
|
||||
string result = str;
|
||||
if (str.size() == 0)
|
||||
return result;
|
||||
result[0] = isupper(result[0]) ? tolower(result[0]) : toupper(result[0]);
|
||||
return result;
|
||||
}
|
||||
|
||||
int main() {
|
||||
string meow;
|
||||
cin >> meow;
|
||||
cout << letter_case_switch(meow) << endl;
|
||||
}
|
||||
BIN
seminar03_initialization/02_repeat/a.out~
Normal file
BIN
seminar03_initialization/02_repeat/a.out~
Normal file
Binary file not shown.
48
seminar03_initialization/02_repeat/main.cpp
Normal file
48
seminar03_initialization/02_repeat/main.cpp
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
#include <iostream>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
|
||||
using namespace std;
|
||||
|
||||
string repeat1(string_view s)
|
||||
{
|
||||
return string {
|
||||
s}
|
||||
+string {
|
||||
s};
|
||||
}
|
||||
|
||||
void repeat2(string & s)
|
||||
{
|
||||
s += s;
|
||||
}
|
||||
|
||||
void repeat3(string * s)
|
||||
{
|
||||
*s += *s;
|
||||
}
|
||||
|
||||
string *repeat4(string_view s)
|
||||
{
|
||||
string *result = new string;
|
||||
*result = string {
|
||||
s} +string {
|
||||
s};
|
||||
return result;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
string meow;
|
||||
cin >> meow;
|
||||
|
||||
cout << "test of repeat1:" << endl << repeat1(meow) << endl;
|
||||
|
||||
repeat2(meow);
|
||||
cout << "test of repeat2:" << endl << meow << endl;
|
||||
|
||||
repeat3(&meow);
|
||||
cout << "test of repeat3:" << endl << meow << endl;
|
||||
|
||||
cout << "test of repeat4:" << endl << *repeat4(meow) << endl;
|
||||
}
|
||||
38
seminar03_initialization/02_repeat/main.cpp~
Normal file
38
seminar03_initialization/02_repeat/main.cpp~
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
#include <iostream>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
|
||||
using namespace std;
|
||||
|
||||
string repeat1(string_view s) {
|
||||
return string{s} + string{s};
|
||||
}
|
||||
|
||||
void repeat2(string& s) {
|
||||
s += s;
|
||||
}
|
||||
|
||||
void repeat3(string* s) {
|
||||
*s += *s;
|
||||
}
|
||||
|
||||
string* repeat4(string_view s) {
|
||||
string* result = new string;
|
||||
*result = string{s} + string{s};
|
||||
return result;
|
||||
}
|
||||
|
||||
int main() {
|
||||
string meow;
|
||||
cin >> meow;
|
||||
|
||||
cout << "test of repeat1:" << endl << repeat1(meow) << endl;
|
||||
|
||||
repeat2(meow);
|
||||
cout << "test of repeat2:" << endl << meow << endl;
|
||||
|
||||
repeat3(&meow);
|
||||
cout << "test of repeat3:" << endl << meow << endl;
|
||||
|
||||
cout << "test of repeat4:" << endl << *repeat4(meow) << endl;
|
||||
}
|
||||
24
seminar03_initialization/03_string_multiplication/main.cpp
Normal file
24
seminar03_initialization/03_string_multiplication/main.cpp
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
using namespace std;
|
||||
|
||||
string operator*(const string str, int n) {
|
||||
string result;
|
||||
for (int i = 0; i < n; ++i)
|
||||
result += str;
|
||||
return result;
|
||||
}
|
||||
|
||||
string operator*(int n, const string str) {
|
||||
string result;
|
||||
for (int i = 0; i < n; ++i)
|
||||
result += str;
|
||||
return result;
|
||||
}
|
||||
|
||||
int main() {
|
||||
string meow;
|
||||
cin >> meow;
|
||||
cout << 3 * meow << endl;
|
||||
}
|
||||
22
seminar03_initialization/04_truncate_to_dot/main.cpp
Normal file
22
seminar03_initialization/04_truncate_to_dot/main.cpp
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
using namespace std;
|
||||
|
||||
void truncateToDot(string& s) {
|
||||
s.resize(s.find('.'));
|
||||
s.shrink_to_fit();
|
||||
}
|
||||
|
||||
int main() {
|
||||
std::string a = "cat.dog.mouse.elephant.tiger.lion";
|
||||
std::string b = "wikipedia.org";
|
||||
std::string c = ".com";
|
||||
truncateToDot(a);
|
||||
truncateToDot(b);
|
||||
truncateToDot(c);
|
||||
|
||||
cout << a << endl
|
||||
<< b << endl
|
||||
<< c << endl;
|
||||
}
|
||||
35
seminar03_initialization/05_string_sum/main.cpp
Normal file
35
seminar03_initialization/05_string_sum/main.cpp
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
#include <iostream>
|
||||
#include <cstdio>
|
||||
#include <string>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int string_sum(const string& str) {
|
||||
int res = 0;
|
||||
int x;
|
||||
int n = str.size();
|
||||
int i = 0;
|
||||
while (i < n) {
|
||||
switch (str[i]) {
|
||||
case '[':
|
||||
case ',' :
|
||||
sscanf((str.c_str()) + i + 1, "%d", &x);
|
||||
res += x;
|
||||
break;
|
||||
case ']' :
|
||||
return res;
|
||||
case ' ':
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
++i;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
int main() {
|
||||
string meow;
|
||||
getline(cin, meow);
|
||||
cout << string_sum(meow) << endl;
|
||||
}
|
||||
34
seminar03_initialization/06_new/main.cpp
Normal file
34
seminar03_initialization/06_new/main.cpp
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
#include <iostream>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main() {
|
||||
int *x = new int{123};
|
||||
cout << *x << endl;
|
||||
|
||||
string *str = new string{"Cats and Dogs"};
|
||||
cout << *str << endl;
|
||||
|
||||
int *xs = new int[]{10, 20, 30, 40, 50};
|
||||
for (int i = 0; i < 5; ++i)
|
||||
cout << xs[i] << " ";
|
||||
cout << endl;
|
||||
|
||||
string *strs = new string[]{"Cat", "Dog", "Mouse"};
|
||||
for (int i = 0; i < 3; ++i)
|
||||
cout << strs[i] << " ";
|
||||
cout << endl;
|
||||
|
||||
string_view *str_views = new string_view[]{strs[0], strs[1], strs[2]};
|
||||
for (int i = 0; i < 3; ++i)
|
||||
cout << str_views[i] << " ";
|
||||
cout << endl;
|
||||
|
||||
delete x;
|
||||
delete str;
|
||||
delete[] xs;
|
||||
delete[] strs;
|
||||
delete[] str_views;
|
||||
}
|
||||
16
seminar03_initialization/08_stringview/main.cpp
Normal file
16
seminar03_initialization/08_stringview/main.cpp
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
#include <iostream>
|
||||
#include "miptstring.cpp"
|
||||
#include "miptstringview.cpp"
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main() {
|
||||
mipt::String str = "Meow!!";
|
||||
cout << str << endl;
|
||||
mipt::StringView sv = str;
|
||||
cout << "string view: " << sv << endl;
|
||||
char a[] = "nyaa!";
|
||||
mipt::StringView sv1 = a;
|
||||
cout << "string view: " << sv1 << endl;
|
||||
cout << sv1.at(200) << endl;
|
||||
}
|
||||
221
seminar03_initialization/08_stringview/miptstring.cpp
Normal file
221
seminar03_initialization/08_stringview/miptstring.cpp
Normal file
|
|
@ -0,0 +1,221 @@
|
|||
#pragma once
|
||||
|
||||
#include <iostream>
|
||||
#include <algorithm>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
using std::cout, std::cin, std::endl, std::size_t;
|
||||
|
||||
namespace mipt{
|
||||
|
||||
char* errorCheckedMalloc(size_t newCapacity)
|
||||
{
|
||||
char* result = static_cast<char*>(std::malloc(newCapacity * sizeof(char)));
|
||||
if (result == NULL)
|
||||
{
|
||||
cout << "Error! Out of memory" << endl;
|
||||
std::exit(1);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
class String
|
||||
{
|
||||
private:
|
||||
|
||||
size_t mSize {0};
|
||||
size_t mCapacity {0};
|
||||
char* mpData {nullptr};
|
||||
|
||||
public:
|
||||
|
||||
String(const char* str)
|
||||
{
|
||||
size_t strSize = std::strlen(str);
|
||||
resize(strSize);
|
||||
std::memcpy(mpData, str, mSize);
|
||||
}
|
||||
|
||||
String() : String("") {}
|
||||
String(const String& s) : String(s.cStr()) {}
|
||||
|
||||
String(size_t n, char a)
|
||||
{
|
||||
resize(n);
|
||||
|
||||
for (size_t i = 0; i < mSize; ++i)
|
||||
mpData[i] = a;
|
||||
}
|
||||
|
||||
~String()
|
||||
{
|
||||
std::free(mpData);
|
||||
}
|
||||
|
||||
void reserve(size_t capacity)
|
||||
{
|
||||
if (capacity <= mCapacity)
|
||||
return;
|
||||
|
||||
mCapacity = std::max(2 * mCapacity, capacity);
|
||||
char* newData = errorCheckedMalloc(mCapacity);
|
||||
|
||||
if (mpData)
|
||||
std::memcpy(newData, mpData, mSize + 1);
|
||||
|
||||
std::free(mpData);
|
||||
mpData = newData;
|
||||
}
|
||||
|
||||
|
||||
void resize(size_t size)
|
||||
{
|
||||
reserve(size + 1);
|
||||
mSize = size;
|
||||
mpData[mSize] = '\0';
|
||||
}
|
||||
|
||||
|
||||
String& operator=(const String& right)
|
||||
{
|
||||
if (this == &right)
|
||||
return *this;
|
||||
|
||||
mSize = right.mSize;
|
||||
resize(mSize);
|
||||
|
||||
std::memcpy(mpData, right.mpData, mSize + 1);
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
String operator+(const String& b)
|
||||
{
|
||||
String result;
|
||||
result.resize(mSize + b.mSize);
|
||||
|
||||
std::memcpy(result.mpData, mpData, mSize);
|
||||
std::memcpy(result.mpData + mSize, b.mpData, b.mSize);
|
||||
result.mpData[result.mSize] = '\0';
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
String& operator+=(const String& right)
|
||||
{
|
||||
*this = *this + right;
|
||||
return *this;
|
||||
}
|
||||
|
||||
bool operator==(const String& right) const
|
||||
{
|
||||
if (mSize != right.mSize)
|
||||
return false;
|
||||
|
||||
size_t i = 0;
|
||||
while (i < mSize && mpData[i] == right.mpData[i])
|
||||
i++;
|
||||
|
||||
return i == mSize;
|
||||
}
|
||||
|
||||
bool operator<(const String& right) const
|
||||
{
|
||||
size_t i = 0;
|
||||
while (i < mSize && i < right.mSize && mpData[i] == right.mpData[i])
|
||||
i++;
|
||||
|
||||
return mpData[i] < right.mpData[i];
|
||||
}
|
||||
|
||||
bool operator<=(const String& right) const
|
||||
{
|
||||
size_t i = 0;
|
||||
while (i < mSize && i < right.mSize && mpData[i] == right.mpData[i])
|
||||
i++;
|
||||
|
||||
return mpData[i] <= right.mpData[i];
|
||||
}
|
||||
|
||||
bool operator!=(const String& right) const
|
||||
{
|
||||
return !(*this == right);
|
||||
}
|
||||
|
||||
bool operator>(const String& right) const
|
||||
{
|
||||
return !(*this <= right);
|
||||
}
|
||||
|
||||
bool operator>=(const String& right) const
|
||||
{
|
||||
return !(*this < right);
|
||||
}
|
||||
|
||||
char& operator[](size_t i)
|
||||
{
|
||||
return mpData[i];
|
||||
}
|
||||
|
||||
const char& operator[](size_t i) const
|
||||
{
|
||||
return mpData[i];
|
||||
}
|
||||
|
||||
char& at(size_t i)
|
||||
{
|
||||
if (i >= mSize)
|
||||
{
|
||||
cout << "Error! Index is out of bounds." << endl;
|
||||
}
|
||||
return mpData[i];
|
||||
}
|
||||
|
||||
void clear()
|
||||
{
|
||||
std::free(mpData);
|
||||
|
||||
mSize = 0;
|
||||
mCapacity = 1;
|
||||
mpData = errorCheckedMalloc(mCapacity);
|
||||
mpData[0] = '\0';
|
||||
}
|
||||
|
||||
void addCharacter(char c)
|
||||
{
|
||||
if (mSize + 1 == mCapacity)
|
||||
reserve(2 * mCapacity);
|
||||
|
||||
mpData[mSize] = c;
|
||||
resize(mSize + 1);
|
||||
}
|
||||
|
||||
|
||||
size_t getSize() const {return mSize;}
|
||||
size_t getCapacity() const {return mCapacity;}
|
||||
const char* cStr() const {return mpData;}
|
||||
};
|
||||
|
||||
|
||||
std::ostream& operator<<(std::ostream& out, const String& s)
|
||||
{
|
||||
out << s.cStr();
|
||||
return out;
|
||||
}
|
||||
|
||||
std::istream& operator>>(std::istream& in, String& s)
|
||||
{
|
||||
s.clear();
|
||||
while (true)
|
||||
{
|
||||
char x = in.get();
|
||||
if (x == ' ' || x == '\n' || x == '\t')
|
||||
break;
|
||||
s.addCharacter(x);
|
||||
}
|
||||
return in;
|
||||
}
|
||||
|
||||
}
|
||||
59
seminar03_initialization/08_stringview/miptstringview.cpp
Normal file
59
seminar03_initialization/08_stringview/miptstringview.cpp
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
#pragma once
|
||||
|
||||
#include <iostream>
|
||||
#include <algorithm>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
using std::cout, std::cin, std::endl, std::size_t;
|
||||
|
||||
namespace mipt {
|
||||
|
||||
class StringView
|
||||
{
|
||||
private:
|
||||
const char* mpData;
|
||||
size_t mSize;
|
||||
|
||||
public:
|
||||
StringView() {;
|
||||
mSize = 0;
|
||||
mpData = nullptr;
|
||||
}
|
||||
StringView(const StringView& str) {
|
||||
mSize = str.mSize;
|
||||
mpData = str.mpData;
|
||||
}
|
||||
StringView(const mipt::String& s) {
|
||||
mSize = s.getSize();
|
||||
mpData = s.cStr();
|
||||
}
|
||||
StringView(const char* s) {
|
||||
mpData = s;
|
||||
mSize = strlen(s);
|
||||
}
|
||||
const char& at(size_t i)
|
||||
{
|
||||
if (i >= mSize)
|
||||
{
|
||||
throw std::out_of_range("Error! Index is out of bounds.");
|
||||
std::exit(1);
|
||||
}
|
||||
return mpData[i];
|
||||
}
|
||||
const char& operator[](size_t i)
|
||||
{
|
||||
return mpData[i];
|
||||
}
|
||||
size_t size() {
|
||||
return mSize;
|
||||
}
|
||||
|
||||
};
|
||||
}
|
||||
|
||||
std::ostream& operator<<(std::ostream& out, mipt::StringView sv) {
|
||||
size_t size = sv.size();
|
||||
for (int i = 0; i < size; ++i)
|
||||
out << sv[i];
|
||||
return out;
|
||||
}
|
||||
BIN
seminar03_initialization/homework_initialization.pdf
Normal file
BIN
seminar03_initialization/homework_initialization.pdf
Normal file
Binary file not shown.
Reference in a new issue