This commit is contained in:
Sandipsinh Rathod 2024-10-09 19:39:24 -04:00
commit a79ad53b7a
No known key found for this signature in database
2 changed files with 115 additions and 0 deletions

14
hw2.txt Normal file

@ -0,0 +1,14 @@
3 8
B 1 0
R 0 1
B 3 2
R 4 1
B 1 4
R 2 3
B 4 3
R 3 4
R 0 3
B 1 2
B 2 1
B 3 0
END

101
main.cpp Normal file

@ -0,0 +1,101 @@
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#define pt cout <<
#define nl '\n'
#define in cin >>
using namespace std;
// -------- START RAII HELPERS--------
unsigned long allocated_mem = 0;
void *alloc(size_t size) {
void *ptr = malloc(size);
if (ptr == nullptr) {
cerr << "Memory allocation failed" << endl;
return nullptr;
}
allocated_mem += 1;
return ptr;
}
void dealloc(void *ptr) {
if (ptr) free(ptr);
allocated_mem -= 1;
}
bool assert_alloc() {
return !allocated_mem;
}
// -------- END RAII HELPERS--------
struct Move {
char player;
int moveX, moveY;
};
struct Moves {
int x{}, y{};
vector<Move> moves;
};
Moves serialize(const string &s) {
Moves result;
istringstream iss(s);
iss >> result.x >> result.y;
string player;
int x, y;
while (iss >> player >> x >> y) {
if (player == "END") break;
result.moves.push_back({player[0], x, y});
}
return result;
}
void solve(const string& fileInp) {
Moves moves = serialize(fileInp);
}
int main(int argc, char **argv) {
if (argc != 2) {
cerr << "Usage: " << argv[0] << " <input_file>" << nl;
return 1;
}
char* fileName = argv[1];
FILE *inp = fopen(fileName, "r");
if (inp == nullptr) {
cerr << "File not found" << endl;
return 1;
}
fseek(inp, 0L, SEEK_END);
long sz = ftell(inp);
fseek(inp, 0L, SEEK_SET);
char *fileInpPtr = static_cast<char *>(alloc(sz + 1 * sizeof(char)));
fileInpPtr[sz] = '\0';
for (int i = 0; i < sz; ++i) {
fscanf(inp, "%c", &fileInpPtr[i]);
}
solve(fileInpPtr);
dealloc(fileInpPtr);
fclose(inp);
if (!assert_alloc()) {
cerr << "Memory leak detected" << endl;
return 1;
}
return 0;
}