6

mail@pastecode.io avatar
unknown
c_cpp
a year ago
950 B
2
Indexable
Never
#include <iostream>
#include <fstream>
#include <regex>
#include <string>
#include <cstdlib>
#include <cmath>

class Complex {
public:
    Complex(short r, unsigned long i) : real(r), imag(i) {}
    short real;
    unsigned long imag;
};

int main() {
    std::ifstream file("text.cpp");
    std::string str((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());

    std::regex pattern("Complex\\((-?\\d+), (\\d+)ul\\)");
    std::smatch match;

    while (std::regex_search(str, match, pattern)) {
        short real = std::stoi(match[1].str());
        unsigned long imag = std::stoul(match[2].str());
        if (real >= SHRT_MIN && real <= SHRT_MAX && imag <= ULONG_MAX) {
            Complex c(real, imag);
            std::cout << "Constructed Complex object with real=" << c.real << " and imag=" << c.imag << std::endl;
        }
        str = match.suffix().str();
    }

    return 0;
}