michael_hartman_cz

PedF UK - OKB1319306 Ukol1

Oct 17th, 2014
586
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.88 KB | None | 0 0
  1. #include <iostream>
  2. #define ERRMSG "Wrong input!"
  3.  
  4.  /**
  5.   * Reads input data for our calculation.
  6.   *
  7.   * @param name Name of parameter to read (specified by single char).
  8.   * @param val Variable to store value.
  9.   * @return Returns false if everything ok and true if reading failed.
  10.   */
  11.  
  12. bool readVal ( char name, int & val )
  13.  {
  14.     std::cout << "Enter " << name << ": ";
  15.     return ( ! ( std::cin >> val ) || val < 0 );
  16.  }
  17.  
  18.  /**
  19.   * Counts spacing we need to achieve our goal. The idea is to get gratest comon divisor (gcd).
  20.   * We use standard euclid algorithm to compute it.
  21.   *
  22.   * @param a Dimension of one side of garden.
  23.   * @param b Dimension of other side of garden.
  24.   * @return Returns spacing we need to use.
  25.   */
  26.  
  27. int countSpacing ( int a, int b )
  28.  {
  29.     int tmp;
  30.     while ( b != 0 )
  31.      {
  32.         tmp = a;
  33.         a = b;
  34.         b = tmp % b;
  35.      }
  36.     return a;
  37.  }
  38.  
  39.  /**
  40.   * Counts how many roses we will need.
  41.   *
  42.   * @param a Dimension of one side of garden.
  43.   * @param b Dimension of other side of garden.
  44.   * @return Returns Roses count.
  45.   */
  46.  
  47. int countQuantity ( int a, int b )
  48.  {
  49.     return ( 2*a + 2*b ) / countSpacing( a, b );
  50.  }
  51.  
  52.  /**
  53.   * Program reads size of garden given by dimensions of two sides.
  54.   * Then it calculates what number of roses we will need
  55.   * and what spacing to use to achieve same spacing between each other and rose in every corner.
  56.   *
  57.   * @return Returns 1 if wrong input is detected, 0 if everything ok.
  58.   * @author Michael Hartman [email protected]
  59.   */
  60.  
  61. int main ( int argc, char ** argv )
  62.  {
  63.     int a,b;
  64.     if ( readVal ( 'a', a ) || readVal ( 'b', b ) )
  65.      {
  66.         std::cout << ERRMSG << std::endl;
  67.         return 1;
  68.      }
  69.     std::cout << "Vysázím je ve vzdálenosti: " << countSpacing( a, b ) << std::endl;
  70.     std::cout << "Budu jich potřebovat: " << countQuantity( a, b ) << std::endl;
  71.     return 0;
  72.  }
Advertisement
Add Comment
Please, Sign In to add comment