This commit is contained in:
2025-10-01 19:49:29 +07:00
parent 19009082de
commit f00a12b32e
14 changed files with 218 additions and 72 deletions

5
others/ilya`s/1/file.in Normal file
View File

@@ -0,0 +1,5 @@
Hi! This is a test.
Wait: are commas, periods, and exclamations replaced?
Let's check!
@#$%&*)_+

6
others/ilya`s/1/file.out Normal file
View File

@@ -0,0 +1,6 @@
Hi9 This is a test9
Wait9 are commas9 periods9 and exclamations replaced9
Let's check9
@#$%&*)_+

BIN
others/ilya`s/1/signs Executable file

Binary file not shown.

76
others/ilya`s/1/signs.cpp Normal file
View File

@@ -0,0 +1,76 @@
#include <iostream>
#include <fstream>
#include <string>
#include <sys/stat.h>
using namespace std;
// ???????? ????????????? ?????
bool fileExists(const string& filename) {
struct stat buffer;
return (stat(filename.c_str(), &buffer) == 0);
}
// ????????? ????? ????????? ????? ? ??????? ?????????? ?? ".out"
string getOutputFileName(const string& inputFileName) {
size_t pos = inputFileName.find_last_of('.');
if (pos == string::npos) {
// ???? ?????????? ???, ????????? .out
return inputFileName + ".out";
}
return inputFileName.substr(0, pos) + ".out"; // ???????? ??????????
}
// ????????, ???????? ?? ?????? ?????? ??????????, ?????????? ??????
bool isSign(char c) {
return (c == '!' || c == '?' || c == '.' || c == ',' || c == ';' || c == ':');
}
int main(int argc, char* argv[]) {
if (argc != 3) {
cerr << "Error: Missing arguments.\nUsage: " << argv[0] << " <inputfile> <symbol>\n";
return -1;
}
string inputFileName = argv[1];
char replacementSymbol = argv[2][0];
if (!fileExists(inputFileName)) {
cerr << "Error: Input file \"" << inputFileName << "\" does not exist.\n";
return -1;
}
string outputFileName = getOutputFileName(inputFileName);
ifstream inFile(inputFileName);
if (!inFile) {
cerr << "Error: Cannot open input file.\n";
return -1;
}
ofstream outFile(outputFileName);
if (!outFile) {
cerr << "Error: Cannot create output file.\n";
return -1;
}
int replaceCount = 0;
char ch;
while (inFile.get(ch)) {
if (isSign(ch)) {
ch = replacementSymbol;
replaceCount++;
}
outFile.put(ch);
if (!outFile) {
cerr << "Error: Write error occurred.\n";
return -1;
}
}
inFile.close();
outFile.close();
cout << replaceCount << endl; // ??????? ????? ?????
return replaceCount;
}

View File

@@ -0,0 +1 @@
g++ task18.cpp -o task18

View File

@@ -0,0 +1,11 @@
abcdef
abcdefg
abcdefghij
abc def
x
12
123
(three spaces)
abc
ab cs s dds
\tabcde\tf

View File

@@ -0,0 +1,67 @@
#include <iostream>
#include <fstream>
#include <string>
#include <sys/stat.h>
static bool file_exists(const std::string &path) {
struct stat st{};
return ::stat(path.c_str(), &st) == 0;
}
static std::string make_out_name(const std::string &in) {
std::size_t pos = in.find_last_of('.');
if (pos == std::string::npos) return in + ".out";
return in.substr(0, pos) + ".out";
}
int main(int argc, char *argv[]) {
if (argc != 2) {
std::cerr << "Usage: " << argv[0] << " <input_file>\n";
return -1;
}
std::string in_path = argv[1];
if (!file_exists(in_path)) {
std::cerr << "Error: input file does not exist.\n";
return -1;
}
std::ifstream in(in_path);
if (!in) {
std::cerr << "Error: cannot open input file.\n";
return -1;
}
std::string out_path = make_out_name(in_path);
std::ofstream out(out_path);
if (!out) {
std::cerr << "Error: cannot create output file.\n";
return -1;
}
long long replacements = 0;
std::string line;
while (std::getline(in, line)) {
if (!line.empty()) {
for (std::size_t idx = 0; idx < line.size(); ++idx) {
std::size_t pos1 = idx + 1;
if (pos1 % 3 == 0) {
if (line[idx] != ' ') {
line[idx] = ' ';
++replacements;
}
}
}
}
out << line;
if (!in.eof()) out << '\n';
if (!out) {
std::cerr << "Error: write error.\n";
return -1;
}
}
std::cout << replacements << std::endl;
return static_cast<int>(replacements);
}

1
others/vlad`s/lab1/compile.sh Executable file
View File

@@ -0,0 +1 @@
g++ task11.cpp -o task11

View File

@@ -0,0 +1,13 @@
aabbcc
bookkeeper
Mississippi
x
aa
aba
aaa
baaa
aaab
abba
1222333
(four spaces)
--====

View File

@@ -0,0 +1,71 @@
// Task 11: In every pair of identical adjacent characters, replace the second with a space.
// Usage: ./task11 <input_file> -> writes "<name>.out" and prints replacements count.
#include <iostream>
#include <fstream>
#include <string>
#include <sys/stat.h>
static bool file_exists(const std::string &path) {
struct stat st{};
return ::stat(path.c_str(), &st) == 0;
}
static std::string make_out_name(const std::string &in) {
std::size_t pos = in.find_last_of('.');
if (pos == std::string::npos) return in + ".out";
return in.substr(0, pos) + ".out";
}
int main(int argc, char *argv[]) {
if (argc != 2) {
std::cerr << "Usage: " << argv[0] << " <input_file>\n";
return -1;
}
std::string in_path = argv[1];
if (!file_exists(in_path)) {
std::cerr << "Error: input file does not exist.\n";
return -1;
}
std::ifstream in(in_path);
if (!in) {
std::cerr << "Error: cannot open input file.\n";
return -1;
}
std::string out_path = make_out_name(in_path);
std::ofstream out(out_path);
if (!out) {
std::cerr << "Error: cannot create output file.\n";
return -1;
}
long long replacements = 0;
std::string line;
while (std::getline(in, line)) {
if (!line.empty()) {
for (std::size_t i = 1; i < line.size(); ++i) {
if (line[i] == line[i - 1]) {
if (line[i] != ' ') {
line[i] = ' ';
++replacements;
}
// Move to next position; overlapping pairs like "aaa":
// i at 2 will compare line[2] with line[1] (now space) → no double count.
}
}
}
out << line;
if (!in.eof()) out << '\n';
if (!out) {
std::cerr << "Error: write error.\n";
return -1;
}
}
std::cout << replacements << std::endl;
return static_cast<int>(replacements);
}