working with strings
Этот коммит содержится в:
@@ -3,6 +3,9 @@
|
||||
#include <iomanip>
|
||||
#include <bitset>
|
||||
#include <cstddef>
|
||||
#include <fstream>
|
||||
#include <cstring>
|
||||
#include <cctype>
|
||||
|
||||
using namespace std;
|
||||
|
||||
@@ -104,6 +107,29 @@ struct Student {
|
||||
Student* monitor; // 8 áàéò (äëÿ 64-áèòíîé ñèñòåìû)
|
||||
};
|
||||
|
||||
|
||||
bool is_valid_filename(const char* name) {
|
||||
const char* forbidden = "*\"<>?|";
|
||||
for (size_t i = 0; i < strlen(name); i++) {
|
||||
if (strchr(forbidden, name[i])) return false;
|
||||
}
|
||||
|
||||
// Ïðîâåðêà äâîåòî÷èÿ
|
||||
const char* colon = strchr(name, ':');
|
||||
if (colon) {
|
||||
if (colon - name != 1 || !isalpha(name[0]) || *(colon + 1) != '\\')
|
||||
return false;
|
||||
}
|
||||
|
||||
// Ïðîâåðêà ðàñøèðåíèÿ
|
||||
const char* dot = strrchr(name, '.');
|
||||
if (dot) {
|
||||
if (strncasecmp(dot, ".txt", 4) != 0) return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
int main() {
|
||||
// Òåñòèðîâàíèå ÷èñåë èç ëåêöèîííîãî ñëàéäà
|
||||
uint16_t a = 0x4d2; // 1234 â äåñÿòè÷íîé
|
||||
@@ -185,6 +211,52 @@ int main() {
|
||||
|
||||
cout << endl;
|
||||
|
||||
|
||||
char filename[256];
|
||||
cout << "Enter file name: ";
|
||||
cin.getline(filename, 256);
|
||||
|
||||
if (!is_valid_filename(filename)) {
|
||||
cerr << "Incorrect file name!";
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Äîáàâëåíèå .txt ïðè îòñóòñòâèè ðàñøèðåíèÿ
|
||||
if (!strrchr(filename, '.')) {
|
||||
strcat(filename, ".txt");
|
||||
}
|
||||
|
||||
// Çàãðóçêà ôàéëà
|
||||
ifstream file(filename, ios::binary | ios::ate);
|
||||
if (!file) {
|
||||
cerr << "Error open file!";
|
||||
return 1;
|
||||
}
|
||||
|
||||
streamsize size = file.tellg();
|
||||
file.seekg(0, ios::beg);
|
||||
char* buffer = new char[size + 1];
|
||||
file.read(buffer, size);
|
||||
buffer[size] = '\0';
|
||||
|
||||
// Ïîèñê ïîäñòðîêè
|
||||
char query[256];
|
||||
cout << "Enter line for find: ";
|
||||
cin.getline(query, 256);
|
||||
|
||||
int count = 0;
|
||||
const char* ptr = buffer;
|
||||
while ((ptr = strstr(ptr, query)) != nullptr) {
|
||||
count++;
|
||||
ptr += strlen(query);
|
||||
}
|
||||
|
||||
cout << "number of occurrences: " << count << endl;
|
||||
|
||||
delete[] buffer;
|
||||
|
||||
cout << endl;
|
||||
|
||||
char op;
|
||||
|
||||
cout << "Enter first operand (hex): ";
|
||||
@@ -204,6 +276,7 @@ int main() {
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
// Âûâîä â hex
|
||||
cout << "\nResult (hex): ";
|
||||
print_in_hex(a);
|
||||
|
||||
Ссылка в новой задаче
Block a user