Created
March 30, 2020 08:57
-
-
Save gynvael/a113bce75359ff49829acc480814afc7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <cstdio> | |
#include <iostream> | |
#include <string> | |
class Rectangle { | |
private: | |
int a; | |
int b; | |
public: | |
Rectangle () : a(0), b(0) {} | |
float calculateField() const; | |
std::string toString() const; | |
bool fromString(const std::string& s); | |
}; | |
float Rectangle::calculateField() const { | |
return a * b; | |
} | |
bool Rectangle::fromString(const std::string& s) { | |
int tmp_a; | |
int tmp_b; | |
if (sscanf(s.c_str(), "%i %i", &tmp_a, &tmp_b) != 2) { | |
return false; | |
} | |
a = tmp_a; | |
b = tmp_b; | |
return true; | |
} | |
std::string Rectangle::toString() const { | |
return std::to_string(calculateField()); | |
} | |
int main() { | |
const size_t ARRAY_SIZE = 3; | |
Rectangle rectangles[ARRAY_SIZE]; | |
for (size_t i = 0; i < ARRAY_SIZE; i++) { | |
printf("Podaj boki a oraz b prostokata %i: ", (int)(i + 1)); | |
fflush(stdout); | |
std::string line; | |
std::getline(std::cin, line); | |
if (!rectangles[i].fromString(line)) { | |
fprintf(stderr, "Zle podane dane. Good bye.\n"); | |
return 1; | |
} | |
} | |
putchar('\n'); | |
for (size_t i = 0; i < ARRAY_SIZE; i++) { | |
printf("Pole %i prostokata wynosi: %s\n", | |
(int)(i + 1), rectangles[i].toString().c_str()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment