Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #define ERRMSG "Wrong input!"
- /**
- * Reads input data for our calculation.
- *
- * @param name Name of parameter to read (specified by single char).
- * @param val Variable to store value.
- * @return Returns false if everything ok and true if reading failed.
- */
- bool readVal ( char name, int & val )
- {
- std::cout << "Enter " << name << ": ";
- return ( ! ( std::cin >> val ) || val < 0 );
- }
- /**
- * Counts spacing we need to achieve our goal. The idea is to get gratest comon divisor (gcd).
- * We use standard euclid algorithm to compute it.
- *
- * @param a Dimension of one side of garden.
- * @param b Dimension of other side of garden.
- * @return Returns spacing we need to use.
- */
- int countSpacing ( int a, int b )
- {
- int tmp;
- while ( b != 0 )
- {
- tmp = a;
- a = b;
- b = tmp % b;
- }
- return a;
- }
- /**
- * Counts how many roses we will need.
- *
- * @param a Dimension of one side of garden.
- * @param b Dimension of other side of garden.
- * @return Returns Roses count.
- */
- int countQuantity ( int a, int b )
- {
- return ( 2*a + 2*b ) / countSpacing( a, b );
- }
- /**
- * Program reads size of garden given by dimensions of two sides.
- * Then it calculates what number of roses we will need
- * and what spacing to use to achieve same spacing between each other and rose in every corner.
- *
- * @return Returns 1 if wrong input is detected, 0 if everything ok.
- * @author Michael Hartman [email protected]
- */
- int main ( int argc, char ** argv )
- {
- int a,b;
- if ( readVal ( 'a', a ) || readVal ( 'b', b ) )
- {
- std::cout << ERRMSG << std::endl;
- return 1;
- }
- std::cout << "Vysázím je ve vzdálenosti: " << countSpacing( a, b ) << std::endl;
- std::cout << "Budu jich potřebovat: " << countQuantity( a, b ) << std::endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment