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

View File

@@ -1,31 +1,50 @@
// Task 12 (Ubuntu): replace every char equal to the first char of its line with a space.
// Usage: ./task12 <input_file> → writes "<input_file>.out" and prints replacement count.
#include <iostream>
#include <fstream>
#include <string>
#include <sys/stat.h>
#include <cerrno>
#include <cstdlib>
#include <climits>
// True if file exists (POSIX stat)
static bool file_exists(const std::string &path) {
struct stat st{};
return ::stat(path.c_str(), &st) == 0;
return ::stat(path.c_str(), &st) == 0; // stat success => exists [web:59]
}
// "<name>.out" by replacing last extension or appending if none
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";
static void print_usage(const char* prog) {
std::cerr << "Usage: " << prog << " <input_file> <replace_count>\n"; // positional args [web:59]
}
static bool parse_ll(const char* s, long long& out) {
if (!s || !*s) return false;
errno = 0;
char* end = nullptr;
long long v = std::strtoll(s, &end, 10); // use endptr and errno [web:59]
if (errno == ERANGE) return false;
if (end == s || *end != '\0') return false;
out = v;
return true;
}
int main(int argc, char* argv[]) {
if (argc != 3) {
print_usage(argv[0]);
return -1;
}
std::string in_path = argv[1];
long long replace_limit = 0;
if (!parse_ll(argv[2], replace_limit) || replace_limit < 0) {
std::cerr << "Error: replace_count must be a non-negative integer.\n";
return -1;
}
if (!file_exists(in_path)) {
std::cerr << "Error: input file does not exist.\n";
return -1;
@@ -44,18 +63,17 @@ int main(int argc, char *argv[]) {
return -1;
}
long long replacements = 0;
long long performed = 0;
std::string line;
while (std::getline(in, line)) {
while (performed < replace_limit && std::getline(in, line)) { // stop early when limit hit [web:59]
if (!line.empty()) {
char first = line.front();
for (char &c: line) {
if (c == first) {
if (c != ' ') {
c = ' ';
++replacements;
} // count only real changes
if (c == first && c != ' ') {
c = ' ';
++performed;
if (performed >= replace_limit) break; // cap reached [web:44]
}
}
}
@@ -67,6 +85,23 @@ int main(int argc, char *argv[]) {
}
}
std::cout << replacements << std::endl;
return static_cast<int>(replacements);
// If limit reached mid-file, copy the rest unchanged
if (performed >= replace_limit) { // stream may be mid-line or between lines [web:59]
// Write remaining lines as-is
if (!in.eof()) {
// Flush any remaining part of current line already written; then dump rest
std::string rest;
while (std::getline(in, rest)) {
out << rest;
if (!in.eof()) out << '\n';
if (!out) {
std::cerr << "Error: write error.\n";
return -1;
}
}
}
}
std::cout << performed << std::endl; // print actual replacements up to limit [web:59]
return 0; // success [web:59]
}

View File

@@ -1 +1 @@
./12_first_symbols_to_spaces file.in
./12_first_symbols_to_spaces file.in 3