vlad works
This commit is contained in:
@@ -1,10 +1,9 @@
|
|||||||
// 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 <iostream>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
|
#include <cerrno>
|
||||||
|
#include <cstdlib>
|
||||||
|
|
||||||
static bool file_exists(const std::string &path) {
|
static bool file_exists(const std::string &path) {
|
||||||
struct stat st{};
|
struct stat st{};
|
||||||
@@ -17,13 +16,30 @@ static std::string make_out_name(const std::string &in) {
|
|||||||
return in.substr(0, pos) + ".out";
|
return in.substr(0, pos) + ".out";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static bool parse_nonneg_ll(const char *s, long long &out) {
|
||||||
|
if (!s || *s == '\0') return false;
|
||||||
|
char *end = nullptr;
|
||||||
|
errno = 0;
|
||||||
|
long long v = std::strtoll(s, &end, 10);
|
||||||
|
if (errno != 0 || end == s || *end != '\0') return false;
|
||||||
|
if (v < 0) return false;
|
||||||
|
out = v;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
int main(int argc, char *argv[]) {
|
int main(int argc, char *argv[]) {
|
||||||
if (argc != 2) {
|
if (argc != 3) {
|
||||||
std::cerr << "Usage: " << argv[0] << " <input_file>\n";
|
std::cerr << "Usage: " << argv[0] << " input_file replace_count\n";
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string in_path = argv[1];
|
std::string in_path = argv[1];
|
||||||
|
long long target = 0;
|
||||||
|
if (!parse_nonneg_ll(argv[2], target)) {
|
||||||
|
std::cerr << "Usage: " << argv[0] << " input_file replace_count\n";
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
if (!file_exists(in_path)) {
|
if (!file_exists(in_path)) {
|
||||||
std::cerr << "Error: input file does not exist.\n";
|
std::cerr << "Error: input file does not exist.\n";
|
||||||
return -1;
|
return -1;
|
||||||
@@ -46,15 +62,13 @@ int main(int argc, char *argv[]) {
|
|||||||
std::string line;
|
std::string line;
|
||||||
|
|
||||||
while (std::getline(in, line)) {
|
while (std::getline(in, line)) {
|
||||||
if (!line.empty()) {
|
if (!line.empty() && replacements < target) {
|
||||||
for (std::size_t i = 1; i < line.size(); ++i) {
|
for (std::size_t i = 1; i < line.size() && replacements < target; ++i) {
|
||||||
if (line[i] == line[i - 1]) {
|
if (line[i] == line[i - 1]) {
|
||||||
if (line[i] != ' ') {
|
if (line[i] != ' ') {
|
||||||
line[i] = ' ';
|
line[i] = ' ';
|
||||||
++replacements;
|
++replacements;
|
||||||
}
|
}
|
||||||
// Move to next position; overlapping pairs like "aaa":
|
|
||||||
// i at 2 will compare line[2] with line[1] (now space) → no double count.
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -66,6 +80,16 @@ int main(int argc, char *argv[]) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!in.eof() && in.fail()) {
|
||||||
|
std::cerr << "Error: read error.\n";
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (replacements != target) {
|
||||||
|
std::cerr << "Error: could not perform exactly requested replacements.\n";
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
std::cout << replacements << std::endl;
|
std::cout << replacements << std::endl;
|
||||||
return static_cast<int>(replacements);
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user